Defined variable erroring as undefined?

Problem:
The variable INDI is defined (checked by which search) but GJR_GARCH function still rejects it as undefined. Why?
Error:
>> GJR_GARCH
Warning: Variable names were modified to make them valid MATLAB identifiers. The original names are saved in the
VariableDescriptions property.
Undefined function or variable 'INDI'.
Error in GJR_GARCH>gloglike (line 68)
h(t) = a0+a1*eps(t-1)^2 + b1*h(t-1) + c0*INDI*eps(t-1)^2;
Error in fmincon (line 536)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in GJR_GARCH (line 28)
gpar = fmincon(@gloglike,initial,sumA ,sumB,[],[],[],[],[],options,XJO_r);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
Code:
function[] = garch_est()
%===================================================================
% Construct GJR_GARCH Model
%===================================================================
% read in data
formatSpec = '%s %f';
XJO = readtable('AXJO.csv','Format',formatSpec,'ReadVariableNames',true);
% calculate returns
XJO_p = XJO.AdjClose;
XJO_r = log(XJO_p(2:end)./XJO_p(1:end-1));
INDI = (XJO_r<0);
% set initial parameter estimates for optimisation
initial = [var(XJO_r)*(1-0.08-0.85-0.03);0.08;0.85;0.03];
% set constraints
sumA = [-eye(4); 0 1 1 0.5];
sumB = [-1e-9;-1e-6;-1e-6;-1e-6;0.9999];
% set options
options = optimset('Display', 'iter', 'largescale', 'off',...
'TolFun', 1e-007,'MaxIter',1000,'MaxFunEvals',1000);
% define objective function
gpar = fmincon(@gloglike,initial,sumA ,sumB,[],[],[],[],[],options,XJO_r);
% generate conditional variances
nobs = length(XJO_r);
cvar = zeros(nobs,1);
cvar(1) = gpar(1)/(1 - gpar(2) - gpar(3) - gpar(4));
for t = 2:nobs
cvar(t) = gpar(1) + gpar(2)*XJO_r(t-1)^2 + gpar(3)*cvar(t-1) + gpar(4)*INDI*XJO_r(t-1)^2;
end
pVaR = [.05];
Zscore = norminv(pVaR);
Sigma = sqrt(cvar);
Normal95 = zeros(nobs,1);
for t = 1:nobs
Normal95(t) = -Zscore*Sigma(t);
end
plot(Normal95)
end
%===================================================================
% Function for GJR_GARCH loglike
%===================================================================
function [loglike] = gloglike(initial,eps)
% estimates
a0 = initial(1);
a1 = initial(2);
b1 = initial(3);
c0 = initial(4);
n = length(eps);
h = zeros(n,1);
h(1) = a0/(1 - a1 - b1 - c0);
for t = 2:n
h(t) = a0+a1*eps(t-1)^2 + b1*h(t-1) + c0*INDI*eps(t-1)^2;
end
% Max-Loglike function and sum of
logl = - .5*log(h) - .5*eps.^2./h -.5*log(2*pi);
loglike = -sum(logl);
end

2 Comments

Stephen23
Stephen23 on 4 Oct 2018
Edited: Stephen23 on 4 Oct 2018
"Why?"
Because in MATLAB variables do not just magically jump from one workspace to another. You have to pass them explicitly:
As Walter Roberson suggested, using a nested function would be a good idea:
Oh, nesting functions - of course. Thank you!

Sign in to comment.

 Accepted Answer

INDI is defined in function garch_est, but the value for it is not passed into gloglike() . gloglike is not a nested function within garch_est so it does not share variables with garch_est .
Simplest solution: take the "end" that is after plot(Normal95) and move that "end" to after the "end" for gloglike -- so the file would have no "end" just before the function declaration for gloglike, and would have two "end" statements at the bottom of it. That would make gloglike() a nested function that would be able to share variables with garch_est()

More Answers (0)

Categories

Find more on Functions in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!