MATLAB Answers

How to do Weighted Historical Simulation?

조회 수: 29(최근 30일)
C.G. Rodriguez
C.G. Rodriguez 9 Jul 2020
Hi,
I was trying to build a code for Weighted Historical Simulation by altering expected shortfall code from:
However, this is what I came up with:
% Log Returns calculated
format short;
Returns =tick2ret(UKX_Index_PX_LAST,"method",'continuous');
DateReturns = Date(2:end);
SampleSize = length(Returns);
%Estimation window as 250 days; starts on 1st day of 2007 to end of sample
TestWindowStart = find(year(DateReturns)==2007,1);
TestWindow = TestWindowStart : SampleSize;
EstimationWindowSize = 250;
DatesTest = DateReturns(TestWindow);
ReturnsTest = Returns(TestWindow);
%WHS
VarLevel = 0.95
VaR_Hist = zeros(length(TestWindow),1);
VaR_WHSHist = zeros(length(TestWindow),1);
tau = num2cell(sort([1:250],"descend"));
for t = TestWindow
i = t - TestWindowStart + 1;
EstimationWindow = t-EstimationWindowSize:t-1;
[VaR_Hist(i), VaR_WHSHist(i)] = WHSVar(Returns(EstimationWindow),VarLevel);
end
using this new function WHSVar, I am creating:
function [VaR,n_tau] = WHSVar(Sample,VaRLevel)
N = length(Sample);
tau = k;
if k <= N
n_tau = ((eda^(tau-1)*(1-eda)/(1-eda^N));
else
c1= ceil(cumsum(n_tau));
y1 =sortrows([n_tau,w1],"descend");
y2 = [y1,c1];
index = find(y2)==VaRLevel;
WHS_VaR = z(3);
end
end
The error message is
Error: File: WHSVar.m Line: 10 Column: 48
Invalid expression. When calling a function or indexing a variable, use parentheses.
Otherwise, check for mismatched delimiters.
I attached the data which I am using. How to fix this error? Please I would really appreciate your help as it is for my project in finance.
Kindest Regards,
Clarissa

답변(1개)

Jakob B. Nielsen
Jakob B. Nielsen 9 Jul 2020
I think you just need to move your parantheses in the function, to contain the entire logic statement.
function [VaR,n_tau] = WHSVar(Sample,VaRLevel)
N = length(Sample);
tau = k;
if k <= N
n_tau = ((eda^(tau-1)*(1-eda)/(1-eda^N));
else
c1= ceil(cumsum(n_tau));
y1 =sortrows([n_tau,w1],"descend");
y2 = [y1,c1];
index = find(y2==VaRLevel); %the logic statement must be inside the parantheses.
WHS_VaR = z(3);
end
end
  댓글 수: 2
Jakob B. Nielsen
Jakob B. Nielsen 10 Jul 2020
If you look at the error message, it says something with parentheses and delimiters, so that is where you should look. It also says line 7, so you know its this line:
n_tau = ((eda^(tau-1)*(1-eda)/(1-eda^N));
Count the opening parentheses: there's 5. Now count the closing parentheses - there are only 4.
n_tau = ((eda^(tau-1)*(1-eda) ) /(1-eda^N)); %<-- extra parentheses
also, you still have the parentheses around your index wrong. The full function should look like this:
function [WHS_VaR,n_tau] = WHSVar(Sample,VaRLevel)
N = length(Sample);
tau = k;
if k <= N
n_tau = ((eda^(tau-1)*(1-eda))/(1-eda^N));
else
c1 = ceil(cumsum(n_tau));
d1 = [n_tau(:),sample(:)];
y1 =sortrows(d1,"descend");
y2 = [y1,c1];
index = find(y2==VaRLevel);
WHS_VaR = z(3);
end
end

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

Community Treasure Hunt

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

Start Hunting!

Translated by