Alastair Hall ECG 790: Topics in Advanced Econometrics Fall 2004 MATLAB Handout # 1 Basic linear algebra commands MATLAB is a matrix programming language. Many of the commands are similar to PROC IML in SAS but it provides a much more flexible programming language than SAS. There are also a number of toolboxes in MATLAB which contain certain canned procedures. For example, there is a Numerical Optimization Toolbox which allows us to optimize any specified function; there is also a Statistics Toolbox which contains many useful statistical procedures such as pdf or cdf calculations. We use commands from both these toolboxes in the GMM programs discussed in class. MATLAB programs can either be executed interactively or by running an M file. An M file is a file containing MATLAB code. The name arises because the file must have a.m extension. In this handout, we consider both approaches. We begin by executing commands interactively. All MATLAB commands below are given in bold. (i) Direct entry of matrices: Supposewewishtoenterthematrix: A = [ 3 4 ] This can be done as follows: A=[12;34]; To exectute the statement it is necessary to hit enter key. Notice that there is no output. To see the contents of A. Type: A Alternatively, if we omit the semi colon from a command then its output is printed to the screen at the time of execution. To illustrate use the key to retrieve the command which entered A and then edit out the ; and re-execute the command. (ii) Editing the elements of a matrix: Supposewewishtoreplacethe(1, 2) element of A by 6. This can be done as follows. 1
A(1,2)=6; Sometimes it is desired to replace either and entire row or column of a matrix this can be done as follows. A(1,:)=[2 1]; A(:,1)=[7; 7]; (iii) Dimensions and rank: The dimensions of a matrix can be calculated as follows. [nr,nc]=size(a); This command creates a row vector whose first element nr (you pick the name) is the number of rows of A and whose second element nc is the number of columns of A. Therankofamatrixcanbecalculatedusingthefollowingcommand. r=rank(a); Note that this calculation is based on the singular value decomposition and so involves an implicit decision about rounding off. (iv) Useful functions of a matrix: The transpose of A is calculated by: B=A ; The determinant is calculated by: d=det(a); The inverse is calculated by: C=inv(A); The trace is calculated by: t=trace(a); (v) Eigenvalues and eigenvectors: The eigenvalues of A are calculated using the command: lambda=eig(a); 2
The matrices of eigenvectors, V say, and a diagonal matrix containing the eigenvalues, L say, can be calculated by: [V,L]=eig(A); (v) Particular matrices: MATLAB has certain functions which return matrices of a particular form. Three are worth noting here chosen dimensions are just used for illustration. The 4 4 identity matrix: eye(4) A10 5 matrix of ones can be calculated by: ones(10,4) A6 2 matrix of zeros can be calculated by: zeros(6,2) A 3 2 matrix of normal random variables: randn(3,2) Note that the elements of the last matrix are random draws from a standard normal distribution. It is possible to initialize the seed for these random draws as follows: randn( seed,1); randn(3,2); Note that this initialization only applies if the two commands are executed sequentially. The seed can be initialized as any positive number, in this case we used 1. (vi) Operations: addition: + subtraction: - matrix multiplication: * array multiplication:.* matrix power: ^ 3
array power:.^ array division:./ Kronceker product: kron(a,b) Use these operators to calculate: A+B, A B, A B, A. B, A 2, C = {a 2 }, D = {b ij ij/a ij }, A B, B A for: [ ] [ ] 1 1 A = B = 3 4 1 1 We now consider how to create, edit and execute M files. There are two types of M files: script and function files. A script file contains a set of self contained commands. A function file contains a set of commands which involve some matrix which must be provided. (vii) Creating script M files: An M file can be created using the MATLAB Editor. Todothis,clickonFile at the top of the MATLAB Command window and then select New followed by M file. You will then be in the editor. Enter the commands: C=[5 4; 3 2]; dc=det(c) Now save the program into a file called test1.m on your k drive. (viii) Executing script M files: To execute this program type test1 on the command line in the MATLAB Command window and then hitting enter. At this point, MATLAB searches the PATH to find this file. The PATH contains a list of subdirectories which are recognized by MATLAB as containing M files. So unless we have done so already it is necessary to tell MATLAB to include k :inthepath.thiscanbe done as follows. Click on File at the top of the MATLAB Command window and then select SET PATH.ThenselectPATH followed by ADD PATH. This gives a window in which you can add the desired directory. It can seen that test1.m causes det(c) to be printed to the screen. After the file is executed, we can also print C to the screen by typing C and hitting enter. 4
(ix) Editing M-files: To edit an existing M-file then click on File at the top of the MATLAB Command window and then select Open. Or equivalently, you can click on the folder sign at the top of the MATLAB command window. Follow these steps and include the commands to calculate the commands to calculate the trace of C. Rerun the program. (x) Creating function M files: The basic creation process is the same as script M files but the structure is different. To illustrate, suppose we wish to create a function M file to calculate the determinant and trace of any square matrix. To this end, follow the steps in (vi) to open a new M file. Now type the following commands: function [detc, trc]=dettr(c); detc=det(c); trc=trace(c); Now save these commands in a file called dettr.m. Notice that the file name matches the function name on the first line of the program this is crucial. (xi) Executing function M-files: A function M-file has an argument, a square matrix C in this case. So this must exist before the file can be run. In our case, it already does and so we can execute the function M file by typing [detc,trc]=dettr(c) on the command line in the MATLAB command window and hitting enter. (xii) Executing DOS commands: It is also possible to execute DOS commands in MATLAB. To do this, the command line must be preceded by!. For example, to copy test1.m to test2.m, type the following at the prompt in the MATLAB command window:!copy k:\test1.m k:\ test2.m Then hit enter. Exercises: 1. Replace the first command in (i) by A = [ 1, 2; 3, 4];. What difference does this make? 2. Replace the first command in (i) by A=[12,34];. What difference does this make? 5
3. Replace the command in (iii) by size(a). What difference does it make? 4. Construct the matrix H = I 5 ι 5 (ι 5 ι 5) 1 ι 5 where ι 5 is a 5 1 column of ones. Recall from ECG 751 that this matrix is a projection matrix by construction. Verify this numerically by calculating: (a) H H ;(b)hh H. 5. Given that H is a projection matrix, what are its eigenvalues? Verify your answer numerically. 6. Given that H is a projection matrix what does its trace equal? Verify your answer numerically. 7. Compare the output from the following three sets of commands: (a) C=randn(3,2); D=randn(3,2); (b) randn( seed, 1); C=randn(3,2); D=randn(3,2); (c) randn( seed, 1);C=randn(3,2); randn( seed, 1); D=randn(3,2); 8. Compare the matrices C and D generated by the following commands: randn( seed, 1);C=randn(3,2); randn( seed, 10); D=randn(3,2);. 9. Create a matrix m whose elements are a random draw from a normal distribution with mean 5andvariance4. 10. Execute the command randn(size(a));. What is the output? 11. For A = [ 2 1 calculate the matrix F such that F AF equals a diagonal matrix. Verify that it works. 12. The symbol % at the beginning of a line means that it will be ignored bt MATLAB. Use this feature to put comments into the function M file created above which explains what it does. 13. Use dettr.m to calculate the determinant and trace of I 20. 14. Create a function M file which calculates the least squares solution to a set of linear equations, Ax = b. Run the program using: 1 A = 3 4 b = 2 5 6 7 What is the least squares solution for x? Verify numerically that the least squares error is in the left null space of A. ] 6