Using Expected Shortfall Estimation and Backtesting Code
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Sai Sri Pathuri
2020 年 4 月 29 日
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;
参考
カテゴリ
Help Center および File Exchange で Portfolio Optimization and Asset Allocation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!