

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


ECG 790 S09 Phaneuf Matlab Lab 2
Optimization Toolkit
Many of the models we will look at will be based on maximum likelihood, simulated maximum likelihood, or some other estimator that requires non-linear optimization. MATLAB’s Optimization Toolkit will be used to perform the numerical optimization needed for these estimators. In this section we’ll begin to familiarize ourselves with the mechanics of numeric optimization in MATLAB.
We’ll typically be interested in unconstrained nonlinear optimization. The function fminunc is appropriate for this task and has the format:
[x,fval,exitflag,output,grad,hess] = fminunc(fun,x0,options)
The key input and output fields are defined as follows:
x: the parameter values that minimizes the function of interest fval: value of the function at x grad,hess: value of the gradient and Hessian evaluated at the final values x fun: A pointer to a function that contains the expression to be minimized x0: Starting values options: A structure (multi-field variable) containing optimization options
The key to using fminunc is construction of a MATLAB function that calculates and returns: The value of the function to be minimized coded as a function of the parameters of interest The gradient of the function (optional) The Hessian of the function (optional)
Functions in MATLAB are ubiquitous, and are used to perform a variety of tasks. They take zero or more input arguments and return zero or more output arguments – doing something in between according to the task the function is designed to perform. Functions are m -files that differ from script m -files in how they are formatted. Function m -files ALWAYS start with something like the following:
function [like, grad, hess] = poisson_like(d,y);
%Purpose: calculate the log-likelihood, gradient, and Hessian %for a single dimension Poisson random variable.
%Inputs: parameter d and data y
Note the key components. The first line contains the word ‘function’, which instructs the program to register this as a callable function. The function name is ‘poisson_like’ – the function will be called via this name in the main body of a program. The next few lines contain documentation describing what the function does and what its input requirements are. Finally, the file containing the function MUST be named according to the function name. Here the file would be saved to the MATLAB workspace as poisson_like.m
The function fminunc is quite flexible and allows the user to perform minimization with or without analytical gradients and Hessians. We’ll practice using fminunc in the context of maximum likelihood estimation for the mean of a univariate Poisson random variable, denoted Y. Here are a few mathematical preliminaries:
exp( ) ( ) , ( ) , ln ( ) ln ln( !) !
Y pr Y E Y pr Y Y Y Y
For a sample of realizations y 1 ,…, yM the log-likelihood function for the parameter is
1
( ) ln
N i i
LL y
The gradient and Hessian of the log-likelihood function are
2 2 2 1 1
1;
N N i i i i
LL y LL y
Create an m -file named poisson_like.m containing the function poisson_like as shown above. For the data vector y inputted to the function code the log-likelihood function, gradient, and Hessian as a function of a single parameter according to the formulas given. NOTE: fminunc minimizes the given function – to turn it into a maximization routine you need to take the negative of the objective function. Here are some coding hints:
like = -sum(y*log(d) - d); grad = -sum(y/d - 1); hess = -sum(-y/d^2);
Now create a script m -file to generate experimental data and carry out the estimation. Proceed as follows:
nobs = 200; lambda = 4; y = poissrnd(lambda,nobs,1);
lambda0 = 1; [l_ml,fval,exitflag,output,grad,hessian] = ... fminunc(@(d) poisson_like(d,y),lambda0);
Note a few things. First, if you need to continue a line of code across a carriage return you can use the '...' command. Second, because no options are given the routine uses the default optimization options. Importantly, we HAVE NOT told the routine to use the analytical gradient and Hessian provided. Thus, the routine will use numerical differentiation to carry out the minimization.
Second, notice how the likelihood function is referenced using the ‘@’ sign. This is called a function handle. The syntax demonstrated distinguishes the parameter to be optimized ( d ) from the other arguments of the function being passed ( y in this case).
options = optimset('GradObj','on','Hessian','on');
and your call to the minimization routine will now need to include ‘options’:
[l_ml,fval,exitflag,output,grad,hessian] = fminunc(@(d) poisson_like(d,y),lambda0,options);