Impact analysis - financial correlation matrices

Hello everyone,
I am quite new with Matlab so please bare with me. I am doind an impact analysis for financial correlation matrices and I cannot ger the code right. Could you please take a look and advise?
The basic idea is that the accuracy of the financial matrices will vary with the training period used in the estimation. Therefore, I will work with different lengts of the training period in order to minimise the error to the correlation matrix of the investment perios (test period). The data I am using is the S&P 500 stock prices. Here is the basic code:
>> P = COPY;
R = price2ret(P);
lengthInvestmentPeriod = 5*size(R,2);
InvestmentPeriodReturns = R(end - lengthInvestmentPeriod:end,:);
OracleInvestmentPeriodCorrelationMatrix = corr(InvestmentPeriodReturns);
TrainingPeriodReturns = R(1:end - lengthInvestmentPeriod - 1,:);
>> for i = size(TrainingPeriodReturns,2):size(TrainingPeriodReturns,1)
tempReturns = TrainingPeriodReturns(1:I,:); % I chose I=1.5 but I am not sure what the range can be
tempCorrelation(:,:,i) = corr(tempReturns);
end
>>
>> for i = 1:size(tempCorrelation,3)
Error(i) = sum(sum(abs(tempCorrelation(:,:,i)-OracleInvestmentPeriodCorrelationMatrix)));
end
>> plot(Error);
Undefined function or variable 'tempCorrelation'.
So in the end I get the above error message and I am not sure why the variable tempCorrelation is not identified.
Could you please advise?
Thank you in advance!
Nadya

4 commentaires

Hi Nadya,
I'm guessing that
size(TrainingPeriodReturns,2) > size(TrainingPeriodReturns,1)
so the loop that defines tempCorrelation(:,:,i) never gets run at all. Just a guess.
The best thing you can do is get familar with the debug option which is a highly valuable tool.
Hi David,
I made it work, thanks for the reply.
I have another question: for the investment (test) period or the crossvalidation I used the first 20 assets but I would like to write another function that randomly samples some stocks, not just the first X assets. Do you have an idea what function could that be?
Currently, my approach is as follows:
FirstXAssets = 20;
R=R(:,1:FirstXAssets);
MinimumConcentrationRatio = 5;
lengthInvestmentPeriod = MinimumConcentrationRatio*size(R,2);
InvestmentPeriodReturns = R(end - lengthInvestmentPeriod+1:end,:);
OracleInvestmentPeriodCorrelationMatrix = corr(InvestmentPeriodReturns);
Thanks a lot!
Hi Nadezhda
randperm(n) supplies a random permutation of the integers 1:n. So, e.g.
a = randperm(100);
b = a(1:20);
gives 20 items randomly chosen from 100 items.
Hi David,
Perfect, thank you very much! It is a great idea and Ive used it in my algorithm :)
I have another problem with the code now but will post another question.
Many thanks again!
Nadya

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by