Error estimating VaR, riskmetrics function?

*******I'm trying to estimate VaR with riskmetrics function. Could anyone please help. My code:
load SP500;
SPX2r_star=SPX2r(SPX2r~=0);
SPX2rv=SPX2rv(SPX2r~=0);
DateID=DateID(SPX2r~=0);
R=1000;
load GJR_estimates_rolling_SP500
RV=reshape(SPX2rv,1,1,rows(SPX2rv));
RM_t = squeeze(riskmetrics(RV,0.94));
******I get the following error:
Undefined function or variable "T".
Error in riskmetrics (line 59)
endPoint = max(min(floor(log(.01)/log(lambda)),T),k);
Error in VaR_estimation_TV (line 48)
RM_t = squeeze(riskmetrics(RV,0.94));
*** *****riskmetrics function:
function Ht = riskmetrics(data,lambda,backCast)
switch nargin
case 2
backCast = [];
case 3
% nothing
otherwise
error('2 or 3 inputs required.')
end
if ndims(data)==2
[T,k] = size(data);
temp = zeros(k,k,T);
for t=1:T
temp(:,:,t) = data(t,:)'*data(t,:);
end
data = temp;
end
if lambda<=0 || lambda>=1
error('LAMBDA must be between 0 and 1.')
end
if isempty(backCast)
endPoint = max(min(floor(log(.01)/log(lambda)),T),k);
weights = (1-lambda).*lambda.^(0:endPoint-1);
weights = weights/sum(weights);
backCast = zeros(k);
for i=1:endPoint
backCast = backCast + weights(i)*data(:,:,i);
end
end
backCast = (backCast+backCast)/2;
if min(eig(backCast))<0
error('BACKCAST must be positive semidefinite if provided.')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Ht = zeros(k,k,T);
Ht(:,:,1) = backCast;
for i=2:T
Ht(:,:,i) = (1-lambda)*data(:,:,i-1) + lambda * Ht(:,:,i-1);
end

 Accepted Answer

More Answers (1)

You try and use a variable T inside of this function, but you define T inside of an if statement. Therefore T only exists if
ndims(data)==2
but in the line:
RV=reshape(SPX2rv,1,1,rows(SPX2rv));
RV is clearly 3 dimensional and this is what you pass as the input to the data variable in your call to the riskmetrics function.

Categories

Find more on Live Scripts and Functions in Help Center and File Exchange

Asked:

on 6 Aug 2015

Answered:

on 6 Aug 2015

Community Treasure Hunt

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

Start Hunting!