Moving window to create rolling covariance matrix
5 views (last 30 days)
Show older comments
Hey guys,
I'am quite new to Matlab and am currently trying to create a rolling covariance matrix.
I got 260 return series and want to create covariance matrices for 10 return series at a time. In the end I want so create one variable with the cov.matrices plotted below each other.
I could to this by hand by writing:
kov_test=cov(returns_sec(1:10,:));
kov_test2=cov(returns_sec(11:21,:));
And copy all kov_test variables in one file.
But since I'am trying to learn to program I would like to know how to apply a for-loop for this problem. The number of return series (260) is stored under the name N_ret.
Thank you!
1 Comment
Mudambi Srivatsa
on 27 Sep 2016
Edited: Mudambi Srivatsa
on 27 Sep 2016
I understand that you want to compute the covariance matrices for the 10 return series at a time in a total of 260. You can do so using the following code snippet
N_ret = 260;
cols = size(return_secs, 2);
sliceLen = 10;
numSlices = N_ret / sliceLen;
startArr = 1:sliceLen:N_ret;
% preallocate multidimensional output
kov_tests = zeros(sliceLen,cols,numSlices);
counter = 1;
for i = startArr
kov_tests(:,:,counter) = cov(return_secs(i: i+9, :));
counter = counter + 1;
end
You can use "plot" function to plot each of the covariance matrices as follows:
plot(kov_tests(:,:,1) % first covariance matrix
For more information on the programming constructs used, refer to the following documentation links:
To save the variables to a file, refer to the data import and export section link given below.
https://www.mathworks.com/help/matlab/functionlist.html#data-import-and-export
To save variables to a mat file, refer to the following link:
https://www.mathworks.com/help/matlab/ref/save.html
Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!