damian clarke january 31, 2020 mres. in economics · 2020-02-21 · classes 2-3: “entering the...

39
Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods II MRes. in Economics

Upload: others

Post on 28-Feb-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Classes 2-3: “Entering the Matrix Laboratory & Optimization”

Damian Clarke

January 31, 2020

Research Methods IIMRes. in Economics

Page 2: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Today’s Plan

The command line

Functions

Optimisation

Simulating model solutions

Page 3: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Our approach…

“It can scarcely be denied that the supreme goal ofall theory is to make the irreducible basic elementsas simple and as few as possible without having tosurrender the adequate representation of a singledatum of experience.”

Einstein

Page 4: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Outline

1. The command line

2. Functions

3. Optimisation

4. Simulating model solutions

Page 5: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Outline

1. The command line

2. Functions

3. Optimisation

4. Simulating model solutions

Page 6: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

>> 1 + 1ans =

2

Page 7: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

>> x = [1, 2; 3, 4; 5, 6]x =

1 23 45 6

>> 2*xans =

2 46 810 12

Page 8: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

>> who>> whos

Page 9: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

. sysuse auto(1978 Automobile Data). reg mpg price weight, noheader. outsheet mpg price weight using auto.csv, ///

nonames comma

Page 10: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods
Page 11: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

>> DataIn = dlmread('auto.csv');>> X = DataIn(:, 2:3);>> size(X)ans =

74 2

>> X = [X, ones(74,1)];>> y = DataIn(:,1);

Page 12: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

>> XX=X'*X;>> Xy=X'*y;>> beta=inv(XX)*Xy

Page 13: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

We have used Matlab to recreate Stata’s point estimates in a regression function.Can you now generate the same standard errors?

Page 14: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Outline

1. The command line

2. Functions

3. Optimisation

4. Simulating model solutions

Page 15: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Outline

1. The command line

2. Functions

3. Optimisation

4. Simulating model solutions

Page 16: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

function y = doubleit(x)

y = 2 * x;

return

Page 17: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Challenge: Write a function that (i) accepts y and X, and (ii) returns β̂ and itsstandard error.

Page 18: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Challenge: Write a function to calculate:

u(x1, x2) = x1/21 · x1/22

Page 19: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Suppose that, for some reason, we want to find utility for x1 = 5 and x2 ∈ {1, . . . , 10}…

x1 = [1:10]';x2 = 5;

Page 20: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Outline

1. The command line

2. Functions

3. Optimisation

4. Simulating model solutions

Page 21: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Outline

1. The command line

2. Functions

3. Optimisation

4. Simulating model solutions

Page 22: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Log Likelihood Function for the Linear Model with Normal Errors

Suppose that we have:

yi = β · xi + εi

εi | xi ∼ N (0, σ2)

L(β, σ2; y | x)

= −(

N2

)ln 2π −

(N2

)lnσ2 −

(1

2σ2

)(y − βx)′(y − βx)

Write a function to calculate L(β, σ2; y | x).

Page 23: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

help fmincon

Page 24: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

minx∈Rkf(x)

subject to

Page 25: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

minx∈Rkf(x)

subject to

lb ≤ x ≤ ub

Page 26: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

minx∈Rkf(x)

subject to

A · x = b

Page 27: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

minx∈Rkf(x)

subject to

C · x ≤ d

Page 28: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

minx∈Rkf(x)

subject to

g(x) = 0

Page 29: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

minx∈Rkf(x)

subject to

h(x) ≤ 0

Page 30: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

minx∈Rkf(x)

subject to

lb ≤ x ≤ ubA · x = bC · x ≤ dg(x) = 0

h(x) ≤ 0

Page 31: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Adding More Arguments in fmincon…

>> lb = [-1000, -1000, -1000, 0];>> ub = [1000, 1000, 1000, 100];>> theta0 = [0, 0, 0, 1];>> opt = optimset('TolFun',1E-20,'TolX',1E-20,'MaxFunEvals',1000);

>> fmincon(@(theta)normalML(theta,y,X), theta0, [], ...[], [], [], lb, ub, [], opt);

Page 32: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Adding More Arguments in fmincon…

>> opt = optimset('TolFun',1E-20,'TolX',1E-20,'MaxFunEvals',1000, ...'PlotFcns','optimplotfval');

>> fmincon(@(theta)normalML(theta,y,X), theta0, [], ...[], [], [], lb, ub, [], opt);

Page 33: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Outline

1. The command line

2. Functions

3. Optimisation

4. Simulating model solutions

Page 34: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Outline

1. The command line

2. Functions

3. Optimisation

4. Simulating model solutions

Page 35: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Loops and Conditional Statements in MATLABThere are a number of general programming constructs which may be of use for us inMATLAB. Loops let you repeat tasks for a predetermined series of values.

1. For Loops: Repeat a task for each value listed. Can take a number of forms:

1 for i=1:102 2*i3 end

for i=2:2:102*i

end

for i=[1,3,5,9]2*i

end▶ Here for and end are obligatory, the counter variable (i here) can be called

anything▶ Remember that MATLAB is built for vectors/matrices, so where possible, prefer

vectorized code rather than loops▶ MATLAB has an amazingly simple parfor loop which is exactly the same, but

can speed up work considerably where tasks are “embarrassingly parallel”

Page 36: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Loops and Conditional Statements in MATLABThere are a number of general programming constructs which may be of use for us inMATLAB. Loops let you repeat tasks for a predetermined series of values.

2. While Loops: Repeat a task as long as some condition is still met.

1 i = 02 while i<103 i = i+14 end

▶ Here while and end are obligatory, the counter variable (i here) can be calledanything

▶ Must be careful that logic makes sense and avoid infinite loops. If caught in aloop, we can force a break holding down ctrl+c.

Page 37: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Loops and Conditional Statements in MATLABThere are a number of general programming constructs which may be of use for us inMATLAB. Conditional statements can be handled using if/else.

3. if, elseif, else: Differentially execute statements depending on conditions. Forexample:

1 t = 1.96;2 df = 1000;3 if t>04 p = 2*(1-tcdf(t,df))5 elseif t<=06 p = 2*tcdf(t,df)7 else8 p = NaN9 end

Page 38: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Activity 1: Simulating Consumption

maxx1,x2x1/21 · x1/22 subject to I = p1x1 + p2x2p1 ∼ U(100, 150)

Page 39: Damian Clarke January 31, 2020 MRes. in Economics · 2020-02-21 · Classes 2-3: “Entering the Matrix Laboratory & Optimization” Damian Clarke January 31, 2020 Research Methods

Activity 2: Bootstrap!Challenge: Replicate the following in MATLAB:

bootstrap, reps(10000): reg mpg price weight, noheader

Hint:

>> s = [101, 102, 103, 104, 105]s =

101 102 103 104 105

>> s([1, 1, 1, 4])ans =

101 101 101 104