☕
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. Estimate a logit regression using MLE
  • 2. Estimate an AR(1) regression using MLE
  • Theory
  • 3. Solving an optimal consumption problem

Was this helpful?

  1. 5. Numerical Methods

Applied Exercises

PreviousNumerical Solvers

Last updated 4 years ago

Was this helpful?

1. Estimate a logit regression using MLE

In this exercise you will estimate a logit regression model using maximum likelihood.

Recall the log likelihood of the logit model from Section 1:

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​)

Using the dataset provided in Section 1, write a script that estimates the unknown parameter β\betaβ using

  1. fminunc

  2. fminsearch

  3. fminbnd

  • Using the log likelihood, create a target function .m file that accepts the unknown parameter and the dataset as inputs and outputs the log likelihood for those parameter values.

  • In another script, select the options and starting values you want for each estimation function. Then use the optimization functions (1)-(3) to estimate the unknown parameter.

  • Bonus part: Set up your script using switch-case so that when you run the script, it begins by asking the user which method is desired. Hint: Use matlab's input function to ask the user which method to use.

2. Estimate an AR(1) regression using MLE

In this exercise you will estimate an AR(1) regression using Maximum Likelihood.

This task consists of two parts. In the first part you will simulate data from a stationary AR(1). In the second part you will use this data to estimate the AR(1) model by MLE and verify that you correctly estimated ϕ\phiϕ and σ2\sigma^2σ2.

  1. Simulate data from a stationary AR(1) model building on the code example we covered in Part A of unit 4. Set T=500T=500T=500 and use y0=0y_0=0y0​=0 and use 200 burn-in periods. Use the same parameter values that we used in the example. After you have simulated the data, export the vector containing the yyy values into a .mat file using the save command.

  2. Create a new script and load the .mat file you just generated. Write function file for the target function which calculates the likelihood given parameter values for ϕ\phiϕ and σ\sigmaσ. Then, create a new script and use fminunc to estimate ϕ\phiϕ and σ2\sigma^2σ2 from the data on yyy you just imported.

Note: Remember that you have to choose sensible starting values for the optimisation algorithm. Make sure these starting values are different from the true parameter values you used to generate the data.

Hint: Since σ2\sigma^2σ2 is a variance, it should never take negative values. You can enforce this without having to rely on a constrained optimization routine. You can solve this by defining σ2=exp⁡x\sigma^2=\exp{x}σ2=expx and optimize over xxx instead of σ2\sigma^2σ2. Remember that when you compare your estimated parameter values to the ones you used to generate the data that you should compare to exp⁡x\exp{x}expx and not xxx.

Theory

Consider the following stable AR(1) model

\begin{align}\log \mathcal{L}(y; \phi,\sigma^2) = &-(T/2) \log(2\pi) -(T/2) \log(\sigma^2)-(2\sigma^2)^{-1}\sum_{t=2}^{T}(y_t-\phi y_{t-1})^2 \\ &+ (1/2)\log(1-\phi^2)-(2\sigma^2)^{-1}(1-\phi^2)y_1^2 \end{align}

3. Solving an optimal consumption problem

In this exercise you will solve an optimal consumption problem using MATLABs numerical solvers.

  • First solve the one-dimensional problem using the fzero function.

  • Then solve the one-dimensional problem using the fminunc function.

yt=ϕ  yt−1+ϵtϵt∼iid  N(0,σ2),∣ϕ∣<1y_t = \phi \; y_{t-1} + \epsilon_t \hspace{20pt} \epsilon_t \sim iid \; N(0,\sigma^2), \hspace{20pt} |\phi|<1yt​=ϕyt−1​+ϵt​ϵt​∼iidN(0,σ2),∣ϕ∣<1

We would like to estimate the unknown parameters ϕ,σ2\phi, \sigma^2ϕ,σ2 by maximum likelihood. Therefore we would like to find the values of ϕ\phiϕ and σ2\sigma^2σ2 which maximize the following log-likelihood.

Consider a consumer who solves the following consumption (CCC) vs. leisure (LLL) problem

maxC,L  ACα(1−L)βmax_{C,L} \; AC^{\alpha}(1-L)^{\beta}maxC,L​ACα(1−L)β

subject to the budget constraint C=wL C=wLC=wL where www is the wage.

Write a Matlab program that solves this problem for given parameter values of AAA, α\alphaα, β\betaβ, and www. In particular

Start by reducing the problem to a one-dimensional problem in either CCC or LLL.