%% A portfolio optimalizalas instabilitasa % %% % Hasznaljunk blokkdiagonalis matrixot mogottes vezerlokent N = 12; M = 4; a = 1; b = 0.75; B = (a-b) * eye(M) + b * ones(M); Cov = blkdiag(0.3 * B, 0.6 * B, B); abrazol = inline('blkdiag(M,zeros(1))'); pcolor(abrazol(Cov)); cim = sprintf('%dx%d-os blokkdiagonalis matrix, blokkmeret: %dx%d', N, N, M, M); title(cim); axis ij; axis equal; axis off; colorbar; %% % Porfolio optimalizalasi modell: a variancia globalis minimuma. Ehhez kell % a kovariancia matrix inverze. CovInv = inv(Cov); pcolor(abrazol(CovInv)); title('A blokkdiagonalis matrix inverze'); axis ij; axis equal; axis off; colorbar; %% % Optimalis sulyok, legkisebb kockazat norm = sum(sum(CovInv)); w = sum(CovInv) / norm plot(w, 's'); title('Optimalis sulyok'); xlabel('i'); ylabel('w_i'); axis([0, N+1, 0, 0.16]); grid on; R = w * (Cov * w') %% % Becsult optimalis portfolio keresese szintetikus idosorok felhasznalasaval T = 10000; C = chol(Cov); X = randn(N, T); Y = C' * X; w2 = zeros(T, N); R2 = [0]; T1 = N; T2 = 30; for t = T1:T2; Cov2 = Y(:,1:t) * (Y(:,1:t))' / t; Cov2Inv = inv(Cov2); norm = sum(sum(Cov2Inv)); w2(t,:) = sum(Cov2Inv) / norm; R2(t) = w2(t,:) * (Cov * w2(t,:)'); end; plot(w2); title('Veges idosorok alapjan becsult optimalis sulyok'); xlabel('T'); ylabel('w_i'); axis([T1, T2, -2, 2]); grid on; %% % Becsult optimalis portfolio keresese szintetikus idosorok felhasznalasaval T1 = 50; T2 = 100; for t = T1:T2; Cov2 = Y(:,1:t) * (Y(:,1:t))' / t; Cov2Inv = inv(Cov2); norm = sum(sum(Cov2Inv)); w2(t,:) = sum(Cov2Inv) / norm; R2(t) = w2(t,:) * (Cov * w2(t,:)'); end; plot(w2); title('Veges idosorok alapjan becsult optimalis sulyok'); xlabel('T'); ylabel('w_i'); axis([T1, T2, -0.2, 0.5]); grid on; %% % Becsult optimalis portfolio keresese szintetikus idosorok felhasznalasaval T1 = 950; T2 = 1000; for t = T1:T2; Cov2 = Y(:,1:t) * (Y(:,1:t))' / t; Cov2Inv = inv(Cov2); norm = sum(sum(Cov2Inv)); w2(t,:) = sum(Cov2Inv) / norm; R2(t) = w2(t,:) * (Cov * w2(t,:)'); end; plot(w2); title('Veges idosorok alapjan becsult optimalis sulyok'); xlabel('T'); ylabel('w_i'); axis([T1, T2, 0, 0.25]); grid on; %% % Becsult optimalis portfolio keresese szintetikus idosorok felhasznalasaval T1 = 9950; T2 = 10000; for t = T1:T2; Cov2 = Y(:,1:t) * (Y(:,1:t))' / t; Cov2Inv = inv(Cov2); norm = sum(sum(Cov2Inv)); w2(t,:) = sum(Cov2Inv) / norm; R2(t) = w2(t,:) * (Cov * w2(t,:)'); end; plot(w2); title('Veges idosorok alapjan becsult optimalis sulyok'); xlabel('T'); ylabel('w_i'); axis([T1, T2, 0, 0.25]); grid on; %% % Becsult kockazat fuggese az idosorok hosszatol T1 = N; T2 = 20; for t = T1:T2; Cov2 = Y(:,1:t) * (Y(:,1:t))' / t; Cov2Inv = inv(Cov2); norm = sum(sum(Cov2Inv)); w2(t,:) = sum(Cov2Inv) / norm; R2(t) = w2(t,:) * (Cov * w2(t,:)'); end; plot(R2/R); title('Veges idosorok alapjan becsult normalt kockazat, q_0'); xlabel('T'); ylabel('q_0'); axis([T1, T2, 0, 35]); grid on; %% % Becsult kockazat fuggese az idosorok hosszatol T1 = 20; T2 = 501; for t = T1:T2; Cov2 = Y(:,1:t) * (Y(:,1:t))' / t; Cov2Inv = inv(Cov2); norm = sum(sum(Cov2Inv)); w2(t,:) = sum(Cov2Inv) / norm; R2(t) = w2(t,:) * (Cov * w2(t,:)'); end; plot(R2/R); title('Veges idosorok alapjan becsult normalt kockazat, q_0'); xlabel('T'); ylabel('q_0'); axis([T1, T2-1, 0, 2]); grid on;