Why is my matlab code to estimate VaR not working

load VaRExampleData.mat
Returns = tick2ret(sp);
DateReturns = dates(2:end);
SampleSize = length(Returns);
TestWindowStart = find(year(DateReturns)==1996,1);
TestWindow = TestWindowStart : TestWindowStart + SampleSize -1;
EstimationWindowSize = 250;
pVaR = [0.05 0.01];
Zscore = norminv(pVaR);
Normal95 = zeros(length(TestWindow),1);
Normal99 = zeros(length(TestWindow),1);
for t = TestWindow
i = t - TestWindowStart + 1;
EstimationWindow = t-EstimationWindowSize:t-1;
Sigma = std(Returns(EstimationWindow));
Normal95(i) = -Zscore(1)*Sigma;
Normal99(i) = -Zscore(2)*Sigma;
end
Index exceeds the number of array elements. Index must not exceed 2664.
figure;
plot(DateReturns(TestWindow),[Normal95 Normal99])
xlabel('Date')
ylabel('VaR')
legend({'95% Confidence Level','99% Confidence Level'},'Location','Best')
title('VaR Estimation Using the Normal Distribution Method')
Why is my matlab code to estimate VaR not working where it says Normal95(i) and Normal 99(i), the graph is blank and the normal 99(i)and Normal 95(i) is only output zeros
%computing VaR using Normal linear without drift at 99 and 95% CI
Returns = tick2ret(Price);
DateReturns = Date(1:end);
SampleSize = length(Returns);
TestWindowStart = find(year(DateReturns)==2013,1);
TestWindow = TestWindowStart : SampleSize;
EstimationWindowSize = 250;
pVaR = [0.05 0.01];
Zscore = norminv(pVaR);
Normal95 = zeros(length(TestWindow),1);
Normal99 = zeros(length(TestWindow),1);
for t = TestWindow(end)
i = t - TestWindowStart + 1;
EstimationWindow = t-EstimationWindowSize:t-1;
Sigma = std(Returns(EstimationWindow));
Mean = mean(Returns(EstimationWindow));
Normal95(i) = -Zscore(1)*Sigma+Mean;
Normal99(i) = -Zscore(2)*Sigma+Mean;
end
figure;
plot(DateReturns(TestWindow),[Normal95 Normal99])
xlabel('Date')
ylabel('VaR')
legend({'95% Confidence Level','99% Confidence Level'},'Location','Best')
title('VaR Estimation Using the Normal Distribution Method')

Answers (1)

TestWindow           = TestWindowStart : TestWindowStart+ SampleSize-1;

3 Comments

It is still not working
We can see that. Your current code returns the error "Unrecognized function or variable 'Price'."
Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue. The best way to do this is to use the code section in the editor and use the run button. That way you can make sure we will see the same error message as you do.
hey there, I have attached the excel i am using, can you import same on your side and see if you are getting a blank graph.
I am quite new with matlab, not too sure if you can try that for me

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Asked:

on 3 Jul 2023

Edited:

on 4 Jul 2023

Community Treasure Hunt

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

Start Hunting!