backtesting value at risk independence test codes

1 view (last 30 days)
i have been trying to do backtesting using the following code, but i get an error saying the functtion defination is not permited in this context.the codes are as follows:-
function res=ind_test(V)
T=length(V);
J=zeros(T,4);
for i = 2:T
J(i,1)=V(i-1)==0 & V(i)==0;
J(i,2)=V(i-1)==0 & V(i)==1;
J(i,3)=V(i-1)==1 & V(i)==0;
J(i,4)=V(i-1)==1 & V(i)==1;
end
V_00=sum(J(:,1));
V_01=sum(J(:,2));
V_10=sum(J(:,3));
V_11=sum(J(:,4));
p_00=V_00/(V_00+V_01);
p_01=V_01/(V_00+V_01);
p_10=V_10/(V_10+V_11);
p_11=V_11/(V_10+V_11);
hat_p=(V_01+V_11)/(V_00+V_01+V_10+V_11);
a=(1-hat_p)^(V_00+V_10)*(hat_p)^(V_01+V_11);
b=(p_00)^(V_00)*(p_01)^(V_01)*(p_10)^(V_10)*p_11^(V_11);
res= -2*log(a/b);
end

Answers (1)

Star Strider
Star Strider on 19 Aug 2014
The problem may be that you are using the function in a script .m file. Function definitions (except Aononymous Functions) are not permitted in script .m files.
Save your function (the one you listed in your Question) as: ind_test.m in your MATLAB search path. Then call it from your script as:
res = ind_test(V);
That should work.
  2 Comments
shreya
shreya on 20 Aug 2014
thanks a lot! it worked
but i am facing another problem now, I am completely new to matlab. I got the above codes for backtesting from a website and now i see i am not getting the codes right.
I aim to forecast VaR values and do backtesting. however, i have my VaR values which i got from using the software WINRATS. I could not perform backtesting on that very software so thought of trying matlab since the codes available.
could you please tell me how to apply the codes for backtesting if i already have VaR values?
It would be great help.
Star Strider
Star Strider on 20 Aug 2014
My pleasure!
I’m glad it worked!
Unfortunately, I have no knowledge at all of backtesting, so I can’t help you with that. According to the documentation, the Financial Toolbox and Econometrics Toolbox, specifically the documentation on Vector Autoregressive Models, might be able to help.
That’s the best I can do. I’m not an Economist, so I don’t have the background to help you.

Sign in to comment.

Categories

Find more on Financial Toolbox 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!