



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 1
Basic Interactions
Components of the software: Command Window, Editor, Workspace Working directory, adding a path Scripts and functions loading and saving data
D = load xlsread('data','indepvar'); c = load xlsread('data','constant'); y = load xlsread('data','depvar'); save mydata; clear; load mydata;
Matlab is a matrix-based programming language, which is to say that the basic unit of analysis is a 2- dimensional matrix (or in more advanced applications, a multiple dimension array). Much of programming in Matlab is learning to think in matrices, vectors, and matrix elements. The following are examples of useful matrix manipulations:
% horizontal concatenation X = [c D]; XX = [c c D];
% vertical concatenation cc = [c; c];
Often we will need to refer to components of matrices - individual columns or rows or subsets thereof. The following illustrates some of the syntax for this:
% all rows and a single column s = X(:,3);
% all rows and multiple columns t = X(:,2:3); u = X(:,[1 3]);
% single element v = X(4,3);
% subset of rows w = X(1:3,:); z = X([7 8:15],:);
Matrix operations are frequently used in programming, as are array (element by element) calculations:
% OLS estimator bols = inv(X'X)X'*y;
% matrix of elements X(i,j)^
Xsq = X.*X;
Printing and Formatting Output
Two intrinsic Matlab functions that are useful for displaying formatted output include:
disp() fprintf()
The command disp displays the term included as an argument. If the argument is a string it prints the text. If
it is a numeric variable it prints the value of the variable directly. Define a script M -file and try the following commands:
mean=12; disp(‘mean of the random variable is’); disp(mean);
junk = ones(5,1); disp(‘a vector of ones’); disp(junk);
The command fprintf is more powerful. The syntax is
fprintf( format , arguement-list )
where format is a string argument specifying the output format for each expression in the list. Here is a snapshot from the MATLAB documentation on format:
This snapshot came from the documentation available through the help option – look there for more details.
coef = [2.23; 4.11]; tstat = [1.89; 2.45]; x = [coef tstat]; info.cnames = strvcat(‘estimate’, ‘t-stat’); info.rnames = strvcat(‘parameters’, ‘inter’, ‘slope’);
fprintf(‘RESULTS OF OLS SIMULATION EXERCISE \n’); fprintf(‘********************************** \n’); mprint(x,info);
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:
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);