FMINUNC cannot continue...variable missing , although variable was included to be estimated in sub-function

1 view (last 30 days)
I am trying to execute the following command.
load('F:\OneDrive - University of Tasmania\Mardi Meetings\Meeting 22\Testing\MyTesting.mat');
CP1=MyTesting;
CP1(:,14)=[];
CP1(:,32:36)=[];
SAU = CP1(:,16);
ISR = CP1(:,18);
IRQ = CP1(:,15);
SLK = CP1(:,10);
NIG = CP1(:,12);
VEN = CP1(:,13);
CP1=[SAU ISR IRQ SLK NIG VEN];
CPNew = log(CP1);
CPNew= 100*(trimr(CPNew,1,0)-trimr(CPNew,0,1));
window_Size = 2;
delta = (1/window_Size)*ones(1,window_Size);
gama=1;
filt=filter(delta,gama,CPNew);
[nRow, nCol] = size(filt);
windowSize=100;
y = filt;
z = bsxfun(@rdivide,bsxfun(@minus,y,mean(y)),std(y));
%disp(' Mean Median Max Min Std.dev. Skew Kurt')
%disp( [ mean(y)' median(y)' max(y)' min(y)' std(y)' mean(z.^3)' mean(z.^4)'] )
x = [ones(length(y)-2,1) trimr(y,1,1) trimr(y,0,2) ];
b = x\trimr(y,2,0);
[rcnt, ccnt] = size(b);
mu = trimr(b,0,rcnt-1)'; % Vector of intercepts
phi1 = trimr(b,1,ccnt)'; % Lag 1 parameter estimates
phi2 = trimr(b,ccnt+1,0)'; % Lag 2 parameter estimates
% Invert A(1) matrix needed for the MLE
a1inv = inv(phi2);
bstart = [
0.001855486206481
0.068693371469487
0.023880592273368
0.001989809184491
0.013648672676092
0.000281803828058
-0.121999669995451
-0.002888991832387
0.002650363820455
0.217906436715095
0.001195294344330
0.008613191798914
0.175400639477836
1.103000246244063
0.440791629588056
0.495605700916872
0.365097267442524
-0.011572135693424
0.650892298805934
-1.176100342250688
-0.342100219212525
-1.059396487450620
-0.545902063123713
-0.729900334997059
-0.402000678739595
1.013901399027622
-1.601300249119193
1.018606365787122
0.791195906556810
0.818998254746848
0.338395871264844
0.465287945621782
-1.145684521466255
];
% bstart = rand(1,33);
options = optimset( 'Display', 'iter', ...
'MaxIter', 20000, ...
'MaxFunEvals', 20000, ...
'TolX', 1e-8, ...
'TolFun', 1e-4 );
[ b,fval ] = fminunc(@(b) neglogl(b,v),bstart,options );
disp(' Log-likelihood function ');
disp( fval );
disp( b );
%
%--------------------------- Functions ----------------------------------
%
%--------------------------------------------------------------------------
% Wrapper function for log-liklihood
%--------------------------------------------------------------------------
function lf = neglogl( b,v )
lf = - mean( loglt( b,v ) );
end
%--------------------------------------------------------------------------
% Log-likelihood function for SVAR
%--------------------------------------------------------------------------
function lf = loglt( b,v )
% Unpack parameter vector
delta = b(16:21).^2;
alpha = normcdf( b(22:27) );
beta = normcdf( b(28:33) );
[ t,n ] = size( v );
lf = zeros( t,1 );
binv = [ 1 0 0 0 0 0
b(1) 1 0 0 0 0
b(2) b(6) 1 0 0 0
b(3) b(7) b(10) 1 0 0
b(4) b(8) b(11) b(13) 1 0
b(5) b(9) b(12) b(14) b(15) 1 ];
u = ( inv(binv)*v' )';
for i = 1:t;
if i == 1;
d = delta + alpha.*(std(u).^2)' + beta.*(std(u).^2)';
else
d = delta + alpha.*(u(i-1,:).^2)' + beta.*d;
end
omega = binv*diag(d)*binv';
lf(i) = -0.5*n*log(2*pi) - 0.5*log(det(omega)) - 0.5*v(i,:)*inv(omega)*v(i,:)';
end
end
%--------------------------------------------------------------------------
% Return matrix to compute time-varying covariances
%--------------------------------------------------------------------------
function S = getSmatrix( b,v )
% Unpack parameter vector
delta = b(16:21).^2;
alpha = normcdf( b(22:27) );
beta = normcdf( b(28:33) );
[ t,n ] = size( v );
lf = zeros( t,1 );
binv = [ 1 0 0 0 0 0
b(1) 1 0 0 0 0
b(2) b(6) 1 0 0 0
b(3) b(7) b(10) 1 0 0
b(4) b(8) b(11) b(13) 1 0
b(5) b(9) b(12) b(14) b(15) 1 ];
u = (inv(binv)*v')';
for i = 1:t;
if i == 1;
d = delta + alpha.*(std(u).^2)' + beta.*(std(u).^2)';
else
d = delta + alpha.*(u(i-1,:).^2)' + beta.*d;
end
tmp = binv*diag( sqrt(d) );
S(i,:) = vec( tmp )';
end
end
%--------------------------------------------------------------------------
% Reshape a matrix to agree with GAUSS reshape command
%--------------------------------------------------------------------------
function X = reshapeg(Y,r,c)
tmp = reshape(Y',c,r);
X = tmp';
end
Getting error"
Undefined function or variable 'v'.
Error in GHVD2C>@(b)neglogl(b,v)
Error in fminunc (line 292)
f = feval(funfcn{3},x,varargin{:});
Error in GHVD2C (line 82)
[ b,fval ] = fminunc(@(b) neglogl(b,v),bstart,options );
Caused by:
Failure in initial objective function evaluation. FMINUNC cannot continue."
How to fix this?

Answers (1)

Stephan
Stephan on 5 Aug 2018
Hi,
you use v as an input to your neglogl function. There should be at least one line in your code that associates v with a value, a vector, a matrix... (Since v' is multiplied to a matrix, i think it is a vector.)
This is what the error message is telling you.
Since i have no insight in the details of your problem, i can not say how to define v in detail.
I recommend to find out the meaning of v, and define it or get it from another function or .mat-file.
Best regards
Stephan

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!