How to create a for loop with a matrice of S&P 500 asset for computing correlations.

1 次查看(过去 30 天)
Hi, I have a dataset composed about 2 years prices of all the S&P 500 assets (523x504 MATRIX), imported as a matrix. What I'm trying to do is to compute all the correlations between a single asset of the index respect to the remaining 503 (we have a total of 504 asset), and then plotting toghether all the computed correlations.
I wanted to ask you for some suggestion about how to proceed with the creation of a for loop, and if in your opinion could be better to import the data in a table format for then develop the for loop.
For the moment, I have just computed the correlation coefficient running this code:
tmp = corrcoef(SP500price);
plot(tmp,'.')
But in that way I obtained just the correlations matrix, while I'm looking for the correlation of a each single asset respect to the remaining 503.
Thanks in advance!

回答(1 个)

Brahmadev
Brahmadev 2023-9-20
Hi Gianluca,
I understand that you would like to get the correlation between each column of “SP500price” matrix. Each element (i,j) of the Correlation matrix given by “corrcoef ” gives the correlation coefficients of each asset "i" (size:523x1) with a different asset "j" (size: 523x1).Using a for loop is also a possible solution, but you can achieve the same result without it.
To plot the correlations of first asset with all the other assets, you can use:
plot(tmp(1, :),'.');
Please take note that the diagonal elements of the correlation matrix "tmp" are 1 as they denote the correlation of an asset with itself.
For more information on the function 'corrcoef', refer to the following documentation:
Hope this helps in resolving the issue you were facing!
Best Regards,
Brahmadev

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by