Maximum likelihood with Heteroskedasticity
Show older comments
I am trying to use maximum likelihood estimation for a heteroskedesticity problem. This exact code works for everyone in my class except me (I tried on 3 different computers but with the same license):
%simulate Maximum Likelihood using Monte Carlo simulation
clear
global yhet X2 X3
nobs = 1000; %fix the number of observations
ntrials = 1000; %number of trials
X2 = normrnd(2,0.65,nobs,1);
X3 = normrnd(10,4,nobs,1);
C = ones(nobs,1); %create a constant
X=[C X2 X3];
k=size(X,2); %number of parameters
crit_value=tinv(.975,nobs-k); %t-critical for 95% confidence
%heteroskedasticity
%multiplicative: sig_i_sq = exp(alpha1 + alpha2*X_2i)
alphatrue = [.5 .2];
u=normrnd(0,sqrt(exp(alphatrue(1)-alphatrue(2)*X2)),nobs,1);
%model: y = beta_1 + beta_2*X_2 + beta_3*X_3 + u
betatrue = [2; 3; 5];
yhet=X*betatrue + u;
%create the function DOESN'T WORK YET
theta_0 = [.5; 1.2; 3.7; .9; -.3]; %some initial values
options=optimset('Display','off','MaxIter',10000,'TolX', 10e-6, 'MaxFunEvals', 10000, 'TolFun',10e-6);
[theta,fval,exitflag,output, grad, hessian] = fminunc(@het_ml,theta_0,options,yhet,X2,X3)
The function file, named "het_ml.m":
function lnl=het_ml(theta,yhet,X2,X3)
global nobs
theta1=theta(1);
theta2=theta(2);
theta3=theta(3);
theta4=theta(4);
theta5=theta(5);
like=-0.5*nobs*log(2*pi)-0.5*sum(theta4+theta5*X2)...
-0.5*sum((yhet-theta1-theta2*X2-theta3*X3).^2./exp(theta4+theta5*X2));
lnl=-like;
The error message I get is:
"Error using fminunc (line 383)
Supplied objective function must return a scalar value."
My professor cannot figure out. I don't know who to turn to. Thank you very much for your help!
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!