From: Anthony on
Hey,

I am using fminsearch to optimize a log-likelihood function and I want to extract extra parameters from the function when the optimizing is done. But I am having trouble doing so.

The syntax is as follows:
[thetahat,fval] = fminsearch(@likelihood,theta0,Options,Data,p,q);

And likelihood.m:
function [LLF] = likelihood(theta,y,p,q)

So inside the likelihood function are two vectors that I'd like to have as output after the optimization. "res" and "h". Any ideas on how I can extract them?

I put the entire likelihood.m code at the bottom so it would be easier to understand.

Kindly,
Anthony Farinaccio

***********likelihood.m*********************
function [LLF] = likelihood(theta,y,p,q)

%
% Purpose: Calculates the log-likelihood function of an AR(p)-ARCH(q)

% yt = rho0 + rho1*y(t-1) + rho2*y(t-2) + ... + rhop*y(t-p) + ut
% ht = alpha0 + alpha1*h(t-1) + ... + alphaq*h(t-q)

% Inputs: y = dependent variable vector
% p = # of lags of the AR portion (rho)
% q = # of lags of the ARCH portion (alpha)
% theta = vector of coefficient estimates - p+q+3x1 (last
% coefficient is ficticious and controls the optimisation
% constraints
% theta = [rho alpha];
% Outputs: LLF = the negative of the log-likelihood function value

rho = theta(1:p+1);
alpha = theta(p+2:end);

nobs = length(y);
yp = y(p+1:end); %feeding the lags
% Matrix of lagged regressors for AR portion with constant
xlag = ones(nobs-p,1);
for i =1:p
xlag = [xlag y(p+1-i:end-i,1)];
end

res = yp - xlag*rho; %residuals
res2 = res.*res; %residuals squared, used for estimation of ARCH portion

%Build lagged squared residuals matrix for ARCH part plus a constant
lagres2 = ones(nobs-p-q+1,1); %lose p+q+1 data points because using residuals
for i=1:q
lagres2 = [lagres2, res2(q+1-i:end-i+1,1)];
end
h = lagres2*alpha;

Z=res(q+1:end)./sqrt(h(1:end-1));
LLF=-mean(-0.5*log(h(1:end-1)) - 0.5*Z.^2);
%-0.5ln(2pi) excluded as it includes no parameters and does not alter the
%extrema of the function.
end

**************************************************************