Using Expected Shortfall Estimation and Backtesting Code

5 Ansichten (letzte 30 Tage)
jens pauwels
jens pauwels am 26 Apr. 2020
Kommentiert: jens pauwels am 30 Apr. 2020
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.

Antworten (1)

Sai Sri Pathuri
Sai Sri Pathuri am 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 Kommentare
jens pauwels
jens pauwels am 29 Apr. 2020
thanks that took me a step further, unfortunately he gives me the next error in the next line
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
Operator '.*' is not supported for operands of type 'cell'.
Error in ExpectedShortfallBacktestingExample2>hHistoricalVaRES (line 249)
ES = ((k - N*VaRLevel).*z(k) + sum(z(k+1:N)))/(N.*(1 - VaRLevel));
Local Functions
function [VaR,ES] = hHistoricalVaRES(Sample,VaRLevel)
% Compute historical VaR and ES
% See [5] for technical details
% Convert to losses
Sample = Sample;
N = length(Sample);
k = ceil(N*VaRLevel);
z = sort(Sample);
VaR = z(k);
if k < N
ES = ((k - N*VaRLevel)*z(k) + sum(z(k+1:N)))/(N*(1 - VaRLevel));
else
ES = z(k);
end
end
function [VaR,ES] = hNormalVaRES(Mu,Sigma,VaRLevel)
% Compute VaR and ES for normal distribution
% See [4] for technical details
VaR = -1*(Mu-Sigma*norminv(VaRLevel));
ES = -1*(Mu-Sigma*normpdf(norminv(VaRLevel))./(1-VaRLevel));
end
function [VaR,ES] = hTVaRES(DoF,Mu,Sigma,VaRLevel)
% Compute VaR and ES for t location-scale distribution
% See [4] for technical details
VaR = -1*(Mu-Sigma*tinv(VaRLevel,DoF));
ES_StandardT = (tpdf(tinv(VaRLevel,DoF),DoF).*(DoF+tinv(VaRLevel,DoF).^2)./((1-VaRLevel).*(DoF-1)));
ES = -1*(Mu-Sigma*ES_StandardT);
end
jens pauwels
jens pauwels am 30 Apr. 2020
I was able to solve this problem but instead i recieve the next error
Array indices must be positive integers or logical values.
Error in ExpectedShortfallBacktestingExample2>hHistoricalVaRES (line 246)VaR = z(k);
but k i positive
function [VaR,ES] = hHistoricalVaRES(Sample,VaRLevel)
% Compute historical VaR and ES
% See [5] for technical details
% Convert to losses
Sample = -Sample;
N = length(Sample);
k = ceil(N*VaRLevel);
z = sort(Sample);
VaR = z(k);
if k < N
ES = ((k - N*VaRLevel)*z(k) + sum(z(k+1:N)))/(N*(1 - VaRLevel));
else
ES = z(k);
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Market Risk finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by