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 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.
Create a new folder for this exercise and copy the olsdata.mat file that you downloaded from this page into it.
In MATLAB, create a new script and save it into the same folder naming it e.g.
exercise01.m.Start your script with the following commands
clear all; close all; clc;
load olsdata.matThis will clear everything and load the data into your workspace.
Inspect the workspace window. You should see a matrix
Xand a vectory.The goal of this exercise is to estimate
beta_trueby ordinary least squares (OLS) and to compute its standard errors using the formula above.Like in exercise one of unit one, code up the ols estimator using matrix algebra.
beta_hat = ...formula goes here...Then compute the estimate using the formula from the theory section
sigma2_hat = ...formula goes here...Finally, estimate the variance-covariance matrix of the estimator using the formula given above. Note that the expression should involve your estimate
sigma2_hat.
V_beta_hat = ...formula involving sigma2_hat...Finally, extract the estimated variances of from the diagonal of the variance-covariance matrix and take the square root to get standard errors.
se_beta = ...formula involving diagonal of V_beta_hat...Now you have two column vectors,
betawhich is andse_betawhich is . Think about how to concatenate them into a matrix where the first row carriesbetaand the second row carriesse_beta.After successfully testing your script, move all code after loading the data into a new file where you declare a function that accepts
yandXas inputs and returns the concatenated matrix. Save this function asrun_ols.min your main folder.Modify your script so that it looks like this:
clear all; close all; clc;
load olsdata.mat
ols_result = run_ols(y, X);
ols_resultRun your script and verify that your OLS function is being called and that the correct result is delivered.
Theory
Let be a vector of data on the dependent variable and let be a 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.
Under the assumption of spherical errors, and estimate of the variance-covariance matrix of the estimator can be obtained as
where
The standard errors of the elements of are defined as the square root of the diagonal elements of .
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 periods when initial capital per worker is for the following parameter values.
Parameter
Value
4
0.4
0.9
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?
Create a new folder for this exercise and create a new script in MATLAB which you save into the same folder naming it e.g. 'exercise02.m'.
Start your script with the following commands
clear all; close all; clc;This will clear everything.
We would like to store the values of for in a vector. Initialise the vector at the beginning of your code by creating a vector of
NaNorzerovalues.
k = nan(...appropriate dimensions here...)Think about how you can use a loop to calculate the values of iteratively. How does this loop look like? Where does it start and where does it end. How do you save a single value that you computed for in the vector you created in the step before?
If you have implemented the exercise correctly, your plot of the evolution of the capital stock should look like this.

Theory
In the canonical overlapping-generations (OLG) model, the evolution of the capital stock per worker is described by the following law of motion.
where is the capital per worker, is the population growth rate, is the discount factor, is the capital share in the production function and 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 on the interval .
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 .
Theory
In this exercise, you will find the minimum of the following non-differentiable function on the interval .
Here is a plot of this function with the respective minimum

To find the minimum of on the interval, use the following algorithm:
Pick the point in the middle of the interval . This splits the interval into two intervals, the left interval and the right interval
In each half, pick as a candidate for a minimum and as the middle point of the respective interval
Evaluate and
Now two cases are possible
(a) If i.e. the minimizer is in the right interval. If this is true, set the new interval equal to the right interval.
(b) If , the minimizer is in the left interval. If this is true, set the new interval equal to the left interval.
Using the new interval , repeat steps 1-4 until the absolute difference between the new and old candidate is smaller than
1e-5.
Last updated
Was this helpful?