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.‌

1. Writing a custom "regress" function

In this exercise you will write your custom function which accepts a vector of dependent variables and a matrix of regressors and computes the OLS estimator as well as its standard error.

Write a function which accepts a vector y and a matrix X (without the column of ones) as inputs and which returns a 2×N2 \times N matrix where the first row carries the estimated regression coefficients and the second row carries their standard errors.

Check your function works appropriately by comparing to MATLAB's regressfunction.

Extension 1 - Constant in the regression: Extend the function to accept a boolean argument constantwhich determines whether a constant should be included in the regression.

Extension 2 - Using structure array objects to return estimated values): Read the MATLAB reference page on structure arrays to learn about struct objects. Modify your OLS function such that it returns a struct object estimates with sub-objects estimates.params and estimates.se which contain the parameter estimates and standard errors, respectively.

Datasets from 'Crashcourse' section

Theory

Let yy be a T×1T \times 1 vector of data on the dependent variable and let XX be a T×NT \times N 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.

β^=(XX)1(Xy)\hat{\beta} = (X'X)^{-1} (X'y)

Under the assumption of spherical errors, and estimate of the variance-covariance matrix of the estimator can be obtained as

Var^(β^X)=σ^2  (XX)1\hat{Var}(\hat{\beta} \vert X) = \hat{\sigma}^2 \;(X'X)^{-1}

where

σ^2=(yXβ^)(yXβ^)TN\hat{\sigma}^2 = \frac{(y-X\hat{\beta})'(y-X\hat{\beta})}{T-N}

The standard errors of the elements of β^\hat{\beta} are defined as the square root of the diagonal elements of Var^(β^X)\hat{Var}(\hat{\beta}\vert X).

2. Simulating capital evolution in the canonical OLG model

In this exercise you will simulate a capital evolution in a simple canonical OLG model.

Write a script that calculates a vector that contains the evolution of capital for T=30T=30 periods when initial capital per worker is k0=0.1k_0=0.1 for the following parameter values.

Parameter

Value

AA

4

α\alpha

0.4

β\beta

0.9

nn

0.02

Tip: Create a vector of zeros of appropriate size, then loop through the elements of the vector and fill in the values of capital per worker.

When you have finished you can plot the resulting vector with the command plot(k) where k is the vector holding the evolution of capital. How many periods does it take the economy to reach its steady state (i.e. capital per worker does not change anymore)? Now try out to simulate the economy for different parameter values. Does a steady state exist and how long does it take to get there?

Theory

In the canonical overlapping-generations (OLG) model, the evolution of the capital stock per worker is described by the following law of motion.

kt+1=11+nβ1+β  (1α)  A  ktαk_{t+1} = \frac{1}{1+n} \frac{\beta}{1+\beta} \; (1-\alpha) \; A \; k_t^\alpha

where kk is the capital per worker, nn is the population growth rate, β\beta is the discount factor, α\alpha is the capital share in the production function and AA is the TFP parameter from the production function.

3. Minimization of a non-differentiable function by bisection

In this exercise you will implement a simple algorithm to find the minimum of a non-differentiable function by bisection minimization.

Code up the algorithm that is described in the theory section below to find the minimum of f(x)f(x) on the interval [0,5][0, 5].

Tips: Use a while loop which iterates on the algorithm steps 1-4 until convergence is achieved. Use variables to keep track of the old and new candidate values for xminx^{min}.

Theory

In this exercise, you will find the minimum of the following non-differentiable function on the interval [a,b][a, b].

f(x)=x1+(x2)2f(x) = \lvert x - 1 \rvert + (x-2)^2

Here is a plot of this function with the respective minimum

Function to be minimized

To find the minimum of f(x)f(x) on the interval, use the following algorithm:

  1. Pick the point in the middle of the interval xMx_M. This splits the interval into two intervals, the left interval [a,xM][a, x_M] and the right interval (xM,b](x_M,b]

  2. In each half, pick as a candidate for a minimum xLx_L and xRx_R as the middle point of the respective interval

  3. Evaluate f(xL)f(x_L) and f(xR)f(x_R)

  4. Now two cases are possible

    • (a) If f(xL)>f(xR)f(x_L)>f(x_R) i.e. the minimizer is in the right interval. If this is true, set the new interval equal to the right interval.

    • (b) If f(xL)<=f(xR)f(x_L)<=f(x_R), the minimizer is in the left interval. If this is true, set the new interval equal to the left interval.

  5. Using the new interval [a,b][a^*, b^*], repeat steps 1-4 until the absolute difference between the new and old candidate is smaller than 1e-5.

Last updated

Was this helpful?