Using Expected Shortfall Estimation and Backtesting Code
5 views (last 30 days)
Show older comments
Hi All,
I use the following coding and would like to test it against the portfolio I have created. Unfortunately I get the same error every time:Array indices must be positive integers or logical values.
H = readtable('CAPM2.csv');
symbol = {'PBIT'};
nAsset = numel(symbol);
Convert prices to returns
Returns = (H{:,symbol});
Select assets into portfolio
symbol2 = {'Datum'};
Datum = (H{:,symbol2});
DateReturns = Datum(1:end);
SampleSize = length(Returns);
TestWindowStart = find(year(DateReturns)==2015,1);
TestWindowEnd = find(year(DateReturns)==2019,1,'last');
TestWindow = TestWindowStart:TestWindowEnd;
EstimationWindowSize = 250;
DatesTest = DateReturns(TestWindow);
ReturnsTest = Returns(TestWindow);
VaRLevel = 0.975;
VaR_Hist = zeros(length(TestWindow),1);
ES_Hist = zeros(length(TestWindow),1);
for t = TestWindow
i = t - TestWindowStart + 1;
EstimationWindow = t-EstimationWindowSize:t-1;
[VaR_Hist(i),ES_Hist(i)] = hHistoricalVaRES(Returns(EstimationWindow),VaRLevel);
end
Array indices must be positive integers or logical values.
The problem arises when creating the EstimationWindow. The result is always negative. Unfortunately I have already tried to convert it to positive, unfortunately without success.
2 Comments
Answers (1)
Sai Sri Pathuri
on 29 Apr 2020
I am assuming that you want to declare EstimationWindow as first 250 indices for t = 1 and shift it by 1 for every loop. The window you declared is,
EstimationWindow = t-EstimationWindowSize:t-1;
For t = 1, this EstimationWindow will take the values -249 to 0. But, in MATLAB, the indices start from 1. Hence, the error is being thrown. You may declare the EstimationWindow as
EstimationWindow = t:EstimationWindowSize-t+1;
2 Comments
See Also
Categories
Find more on Market Risk 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!