☕
An Introduction to MATLAB
  • An Introduction to MATLAB
  • Where do I start?
  • 1. Crashcourse
    • Intro to Crashcourse
    • Graphical User Interface
    • Essential commands
      • Matrix Input and Access
      • Matrix Algebra
      • Logical Operations
    • Best practices
    • Self-Assessment
    • Applied exercises
  • 2. MATLAB Programming
    • Intro to MATLAB Programming
    • Programming Fundamentals
    • Conditions
    • Loops
    • Custom Functions
    • Debugging
    • Applied Exercises
  • 3. Data, Graphics & Reporting
    • Intro to Data, Graphics & Reporting
    • Working with Datasets
    • Creating Graphs
    • Applied Exercises
  • 4. RNGs & Simulations
    • Intro to RNGs & Simulations
    • Random Number Generation
    • Monte-Carlo Simulations
    • Applied Exercises
  • 5. Numerical Methods
    • Intro to Numerical Methods
    • Numerical Optimization
    • Numerical Solvers
    • Applied Exercises
Powered by GitBook
On this page
  • 1. Computing the OLS estimator
  • 2. Computing the log-likelihood of a logit model
  • 3. Estimating a factor model using Principal Components (Advanced)

Was this helpful?

  1. 1. Crashcourse

Applied exercises

This section provides some exercises that are meant to deepen your knowledge in the topics covered in this section and to gain experience solving real-world problems.

PreviousSelf-AssessmentNextIntro to MATLAB Programming

Last updated 4 years ago

Was this helpful?

1. Computing the OLS estimator

In this exercise you will compute the OLS estimator on a simulated data set using basic MATLAB commands. Please refer to the theory section below for the necessary formulas.

Import the simulated data from olsdata.m and compute the OLS estimator β^\hat{\beta}β^​ using matrix expressions. Create a results matrix which stacks the estimated parameters and the values supplied in the vector beta_true side by side. Are the estimated and the true values close?

Next, read up on MATLABs regress function on the . Estimate the OLS coefficient using this function and compare the results to the ones you computed manually.

  • Create a new folder for this exercise and copy the olsdata.mat file into it

  • In MATLAB, create a new script and save it into the same folder

  • Start your script with the following commands

  clear all; close all; clc;
  load olsdata.mat
  • Check the data has been loaded into your workspace. You should see a matrix X, a vector y as well as a vector beta_true which contains the true values of β\betaβ that were used to generate the data

  • Add a line which computes the OLS estimator and saves it into a new variable beta_hat

beta_hat = ...formula goes here...

Note that you can code up the formula in two ways. Either by computing (X′X)−1(X'X)^{-1}(X′X)−1 separately from X′yX'yX′y and then multiplying them or by directly writing the formula into one line.

% Compute OLS estimator
clear all; close all; clc;

load olsdata

% Compute OLS estimator
beta_hat = (X'*X)\(X'*y);

% Compare to true value
[beta_hat, beta_true]

% Extension: Compare to MATLABs regress function
beta_regress = regress(y,X);
[beta_hat, beta_regress]

Theoretical Background

Let yyy be a N×1N \times 1N×1 vector of data on the dependent variable and let XXX be a N×KN \times KN×K matrix with data on the regressors where the first column is a vector of ones.

The OLS estimator of the regression coefficients is defined as β^=(X′X)−1(X′y)\hat{\beta} = (X'X)^{-1} (X'y)β^​=(X′X)−1(X′y).

2. Computing the log-likelihood of a logit model

In this exercise you will compute the log-likelihood of a logit model on a simulated data set using basic MATLAB commands. Please refer to the theory section below for the necessary formulas.

Import the simulated data from logitdata.m and calculate the value of the log-likelihood for different values of the parameter vector β\betaβ using matrix expressions.

Approximately, for which value of β\betaβ is the log-likelihood maximal?

  • Create a new folder for this exercise and copy the logitdata.mat file into it

  • In MATLAB, create a new script and save it into the same folder

  • Start your script with the following commands

  clear all; close all; clc;
  load logitdata.m
  • Check the data has been loaded into your workspace. You should see a matrix X, and a vector y

  • Fix a value for β\betaβ by setting it to e.g. 0.5

beta = 0.5
  • Create a variable Land assign it the value of the formula for the likelihood from the theory section

L = ...formula goes here...

Hint: When coding up the formula, write it as a function of a vector. Start with the inner parts of the formula i.e. first think about how xiβx_i \betaxi​β looks like for different iii and how you can write it as a vector. Then think about what applying functions like exp() and ln (which in MATLAB is the log function) does to this vector. Finally think about how to evaluate the sum. It might be easiest to split up the formula into two separate parts (the one starting with yiy_iyi​ and the one starting with (1−yi)(1-y_i)(1−yi​) that you save in different variables, then evaluate the sum operator over the sum of these variables.

To check your log-likelihood implementation is working correctly, try out different values for β\betaβ and compare the resulting log-likelihood values. The data was generated with β0=1.5\beta_0=1.5β0​=1.5 and so your code should give you the maximal log-likelihood value close to this point.

Here is a plot of some reference values for the function.

Theoretical Background

Consider the following discrete choice logit model with no constant and one regressor

yi=xiβ+εiy_i = x_i \beta + \varepsilon_iyi​=xi​β+εi​

where all variables are scalars and yiy_iyi​ is a binary variable (i.e. it has 0/1 values).

The log-likelihood of the data given a value for the parameter vector β\betaβ is defined as

L(β)=∑i=1N  yiln⁡(exp(xiβ)1+exp(xiβ))+(1−yi)ln⁡(11+exp(xiβ))\mathcal{L}(\beta) = \sum^N_{i=1} \; y_i \ln\left( \frac{exp(x_i \beta)}{1+exp(x_i \beta)} \right) + (1-y_i) \ln\left( \frac{1}{1+exp(x_i \beta)} \right)L(β)=∑i=1N​yi​ln(1+exp(xi​β)exp(xi​β)​)+(1−yi​)ln(1+exp(xi​β)1​)

3. Estimating a factor model using Principal Components (Advanced)

In this exercise you will estimate a factor model on a simulated data set using basic MATLAB commands. Please refer to the theory section below for the necessary formulas.

Caution: By default, eig sorts the eigenvalues and corresponding vectors in ascending order of magnitude of the eigenvalues. Make sure you extract the rrr eigenvectors corresponding to the rrrlargest eigenvalues.

The estimation above is valid only under the assumption that the factors are orthogonal i.e. F′F/T=IF'F/T = IF′F/T=I. Use MATLABs scatter(x,y) command and verify graphically that the normalization holds for the estimated factors. In the scatter command, x should be the first factor (i.e. the first column of F^\hat{F}F^) and y should be the second factor.

Theoretical Background

We will use the following factor model

Xt=Λ  Ft+utX_t = \Lambda \; F_t + u_tXt​=ΛFt​+ut​

where XtX_tXt​ is large N×1N \times 1N×1 vector of series which we would like to explain by a lower number of factors. FtF_tFt​ is a r×1r \times 1r×1 vector of factors and utu_tut​ an N×1N \times 1N×1 vector of idiosyncratic shocks. Λ\LambdaΛ is a matrix of factor loadings of dimension N×rN \times rN×r. TTT is the number of observations.

Under some normalizations, the rrr factors and their factor loadings Λ\LambdaΛ can be estimated by principal components using the following formulae.

F^=T  EV(XX′)1:rΛ^=F^′X/T\hat{F} = \sqrt{T} \; EV(XX')_{1:r}\hspace{1.5cm} \hat{\Lambda} = \hat{F}' X / TF^=T​EV(XX′)1:r​Λ^=F^′X/T

where FFF is a T×rT \times rT×r matrix and XXX is a T×NT \times NT×N matrix. EV(A)1:rEV(A)_{1:r}EV(A)1:r​ denotes the first rrr eigenvectors of the matrix AAA which correspond to the rrr largest eigenvalues.

Read up on MATLABs eig function on the . Use the eig function to estimate the matrix of factors FFF and loadings Λ\LambdaΛ for the following dataset for r=2r=2r=2.

MATLAB documentation page on eig
MATLAB documentation page on regress
243KB
datasets-getting-started.zip
archive
Datasets for this section
243KB
datasets-getting-started.zip
archive
Datasets for this section
243KB
datasets-getting-started.zip
archive
Datasets for this section
Reference values for the log-likelihood function