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')

답변 (1개)

Walter Roberson
Walter Roberson 2023년 7월 3일

0 개 추천

TestWindow           = TestWindowStart : TestWindowStart+ SampleSize-1;

댓글 수: 3

It is still not working
Rik
Rik 2023년 7월 4일
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.
Shagoofa
Shagoofa 2023년 7월 4일
편집: Shagoofa 2023년 7월 4일
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2023년 7월 3일

편집:

2023년 7월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by