Computing Standard Error for MLE using Optim package

515 views
Skip to first unread message

Bradley Fay

unread,
Feb 5, 2014, 5:03:50 PM2/5/14
to julia...@googlegroups.com
I'm trying to figure out how to compute/recover the standard error of the estimates for a simple linear regression using MLE and simulated. Here is the code I'm using to generate data, compute the likelihood function, and optimize the function.

using Optim
using Distributions

xmat = [ones(10000,1) rand(Normal(2,1),10000)]; #creating matrix of Independent varibles
u = rand(Normal(0,1),10000); #create vector of normally distributed errors mean 0 std 1
ymat = xmat*[3,2] + u; #generating dependent variables.

start_val = [1.0,1.0,1.0]; #Starting values for MLE estimation. Need to be in float format.

#Beginning of Likelihood function
function ll(param);
        b1=param[1];
        b2=param[2];
        sig=param[3];   
        ee
=  ymat-xmat*[b1,b2];
        loglik = -0.5*log(2*pi*sig^2)-0.5*(ee.^2)/sig^2;
        -sum(loglik);
end;

optimize(ll,start_val)

I'm more familiar with Gauss and MATLAB as I just started playing with Julia yesterday. To compute the standard errors in the identical situation in MATLAB, I can directly compute the square root of the diagonal of the inverse of the numerical hessian because Matlab will return the hessian.
mlese=sqrt(diag(inv(hessian))); #MATLAB Code for computing standard errors

Is there a way to do this in Julia? I'm having trouble figuring this out.  Thanks!

Andreas Noack Jensen

unread,
Feb 5, 2014, 5:16:47 PM2/5/14
to julia...@googlegroups.com
In the Calculus.jl package, there is a hessian function where you can stick in you likelihood and the estimate.


2014-02-05 Bradley Fay <bradl...@gmail.com>:



--
Med venlig hilsen

Andreas Noack Jensen

John Myles White

unread,
Feb 5, 2014, 5:35:45 PM2/5/14
to julia...@googlegroups.com
We should probably document this in the main README for Optim.

 -- John
Message has been deleted

Bradley Fay

unread,
Feb 11, 2014, 6:06:17 PM2/11/14
to julia...@googlegroups.com
Got it, Thanks!

-Bradley 
Reply all
Reply to author
Forward
0 new messages