Maximum likelihood with Heteroskedasticity

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

采纳的回答

Adam Danz
Adam Danz 2020-9-25
编辑:Adam Danz 2020-9-25
You are suffering from the side effects of using global variables (incorrectly).
Don't use global variables in Matlab.
Instead, pass the nobs variable in to your objective function as a 5th input variable. Then remove both lines in your code that declare variables as global.
[theta,fval,exitflag,output, grad, hessian] = fminunc(@het_ml,theta_0,options,yhet,X2,X3, nobs)
function lnl=het_ml(theta,yhet,X2,X3,nobs)
Here's why your version was failing.
You were declaring nobs as global in the objective function but not in the script. Even though nobs was defined in the script, it remained empty in the objective function because undefined global variables are empty. Since nobs was empty, the output of the objective function was empty and Matlab threw an error because the objective function must return a scalr value. If nobs was declared as global in the script as well as the objective function, then you would have avoided the error. But you'd still have global variable which frequently cause problems that could have been avoided by never using them.
  3 个评论
azhar albaaj
azhar albaaj 2021-10-29
Sorry, I need the code after debugging
Can you help me and send it to me here or to my email?
azhar.albaaj1987@gmail.com

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MuPAD 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by