%% Szimulalt piacok, szintetikus idosorok, Cholesky-faktorizacio % %% % Generaljunk N db, egymastol fuggetlen penzugyi eszkoz hozamanak T % lepesbol allo idosorat. Tegyuk fel, hogy a hozamok N(0, 1) normalis eloszlasuak. N = 10; T = 20; randn('state', 1); X = randn([N, T]); plot(X'); cim = sprintf('Modell: Fuggetlen penzugyi eszkozok N(0,1) normalis eloszlasu hozamanak szintetikus idosora'); title(cim); xlabel('T'); axis([1, T, -3, 3]); grid on; %% % Ugyanezek az adatok matrixkent abrazolva abrazol = inline('blkdiag(M,zeros(1))'); pcolor(abrazol(X)); cim = sprintf('Hozam matrix (%dx%d)', N, T); title(cim); axis ij; axis equal; axis off; caxis([-3,3]); colorbar; %% % Kepezzuk az idosorok kovariancia matrixat Cov = X*X'/T; pcolor(abrazol(Cov)); cim = sprintf('X*X^T/T kovariancia matrix (%dx%d), T=%d', N, N, T); title(cim); axis ij; axis equal; axis off; caxis([-2,2]); colorbar; %% % Ha T tart a vegtelenhez, a kovariancia matrix tart az egysegmatrixhoz T = 5000; X = randn([N, T]); Cov = X*X'/T; pcolor(abrazol(Cov)); cim = sprintf('X*X^T/T kovariancia matrix (%dx%d), T=%d', N, N, T); title(cim); axis ij; axis equal; axis off; caxis([-2.5,2.5]); colorbar; %% % Vizsgaljuk meg a matrixelemek idofejlodeset T = 1000; Cov = zeros([T, N, N]); Cov_diag = zeros([T, N]); i = 1; for t = 1:T; Cov(i,:,:) = X(:,1:t)*X(:,1:t)'/t; for n = 1:N; Cov_diag(i,n) = X(n,1:t)*X(n,1:t)'/t; end; i = i + 1; end; plot(Cov_diag(:,1:N)); cim = sprintf('X*X^T/T kovariancia matrix diagonalis elemeinek idofejlodese (N=%d)', N); title(cim); xlabel('T'); axis([1, T, 0.5, 1.5]); grid on; %% % Nemdiagonalis elemek idofejlodese plot(Cov(:,2:end,1)); cim = sprintf('X*X^T/T kovariancia matrix nemdiagonalis elemeinek idofejlodese (N=%d)', N); title(cim); xlabel('T'); axis([1, T, -0.5, 0.5]); grid on; %% % Allitsunk elo blokkdiagonalis matrixot 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); 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; %% % Cholesky-transformacio: C'*C=Cov C = chol(Cov); pcolor(abrazol(C')); title('A blokkdiagonalis matrix C^T Cholesky-transzformaltja: C^T*C=Cov'); axis ij; axis equal; axis off; colorbar; %% % Korrelalt szintetikus idosorok eloallitasa: szorzas a Cholesky-transzformalttal T = 20; X = randn([N, T]); Y = C' * X; plot(Y'); title('Szintetikus idosor (mogottes kovariancia matrix: blokkmatrix)'); xlabel('T'); grid on; %% % Ugyanezek az adatok matrixkent abrazolva pcolor(abrazol(Y)); title('Szintetikus idosor (mogottes kovariancia matrix: blokkmatrix)'); axis ij; axis equal; axis off; colorbar; %% % Kepezzuk a korrelalt idosorok kovariancia matrixat T = 100; X = randn([N, T]); Y = C' * X; Cov = Y*Y'/T; pcolor(abrazol(Cov)); cim = sprintf('Y*Y^T/T kovariancia matrix (%dx%d), T=%d', N, N, T); title(cim); axis ij; axis equal; axis off; colorbar; %% % Kepezzuk a korrelalt idosorok kovariancia matrixat T = 1000; X = randn([N, T]); Y = C' * X; Cov = Y*Y'/T; pcolor(abrazol(Cov)); cim = sprintf('Y*Y^T/T kovariancia matrix (%dx%d), T=%d', N, N, T); title(cim); axis ij; axis equal; axis off; colorbar;