Error estimating VaR, riskmetrics function?

4 次查看(过去 30 天)
*******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

采纳的回答

Mira
Mira 2015-8-6
I see, thank you.

更多回答(1 个)

Brendan Hamm
Brendan Hamm 2015-8-6
编辑:Brendan Hamm 2015-8-6
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.

类别

Help CenterFile Exchange 中查找有关 Probability Distributions and Hypothesis Tests 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by