Matlab Session 5
Matlab Session 5
Matlab Session 5
Danila Smirnov
September 2022
Introduction
We want to find the solution of a system of equations or the max/min of
a function
Find the roots or zeroes of a function: f (x ∗ ) = 0
Find the maximize/minimize a function: f 0 (x ∗ ) = 0
Examples
Find the equilibrium price of an economy
Maximize a log-likelihood function
Key
Because we are numerically solving a problem, we have to accept
some tolerance for the solution
We will rely on iterative algorithms: the starting point matters
Local solutions
2/23
Example
Where are the zeroes? Where is the function maximized/minimized?
f (x) = x cos(x 2 )
-1
-2
-3
-1 -0.5 0 0.5 1 1.5 2 2.5 3
3/23
Rootfinding problems
Bisection
4/23
Rootfinding problems
Bisection: Graphical example
Source: https://orionquest.github.io/Numacom/bisection.html
5/23
Rootfinding problems I
Bisection
6/23
Rootfinding problems
Newton’s Methods
Basic algorithm
1. Guess x (0) for the root of f . The super-script 0 denotes the iteration
number
2. Using (1), update the guess with
7/23
Rootfinding problems
Quasi-Newton’s Methods
f (x (k) ) − f (x (k−1) )
f 0 (x (k) ) ≈
x (k) − x (k−1)
x (k) − x (k−1)
x (k+1) = x (k) − f (x (k) ) (3)
f (x (k) ) − f (x (k−1) )
8/23
Rootfinding problems I
MATLAB functions
Input
‘fun’: function we want to find a zero
x0: initial guess for the solution.
Depending on the problem, you can set different options
Output
x: zero of equation ‘fun’
fval: value of the function at the optimal point
exitflag: integer encoding the exit condition
output: information about the root-finding process
9/23
Rootfinding problems II
MATLAB functions
Important!
When using fzero, you a single equation and must provide a single
initial starting point. When using fsolve, you have a system of N
equations and have to provide a N-dimensional vector as starting point.
10/23
Exercise 1
11/23
Rootfinding problems
Setting options
Note! you can set more options. See help fzero and help fsolve for
details
12/23
Rootfinding problems
MATLAB functions: Passing arguments
13/23
Numerical optimization
Two commonly used methods
Grid search
Generate a fine grid of points and evaluate the function at each one.
Choose x ∗ as the value that generates the highest/lowest f (x ∗ )
Derivative based
At the optimum, the first derivative of the function is zero. As in
root-finding methods, this is an iterative procedure that uses
numerical derivatives
14/23
Numerical optimization
Grid search
Basic steps
1. Generate a first rough grid with n points and evaluate the function
2. Select the point that maximizes/minimizes the function on this grid,
x (k)
3. Refine the initial grid around the optimal point x (k)
4. Select the new point that optimizes the function
5. Repeat steps 3-4 until the optimized values of consecutive iterations
are “close”
15/23
Numerical optimization
Newton-Raphson
g (x) ≈ g (x k ) + g 0 (x k )(x − x k )
f 0 (x k ) + f 00 (x k )(x − x k ) = 0
Updating rule
x k+1 = x k − [f 00 (x k )]−1 f 0 (x k )
16/23
Numerical optimization
MATLAB functions
Note! for fminsearch you can set similar options as with fzero (using
optimset). See help fminsearch.
Important!
When using fminsearch or fminunc you can have a N-dimensional
vector as starting point, but the output of function fun must be a scalar
17/23
Exercise 2
18/23
Numerical optimization
Tips
Variance θ>0 θ = ψ2
θ = exp(ψ)
1
Probability θ ∈ (0, 1) θ= 1+exp(ψ −1 )
ψ
Stationary autoregressive parameter θ ∈ (−1, 1) θ= 1+|ψ|
1
Shares θ1 , θ2 ≥ 0 θ1 = 1+exp(ψ)
exp(ψ)
θ1 + θ2 = 1 θ2 = 1+exp(ψ)
19/23
Exercise 3
Consider a Cournot duopoly model, in which the inverse demand is:
P(q) = q 1/η
∂πi
= P(q1 + q2 ) + P 0 (q1 + q2 )qi − Ci0 (qi ) = 0
∂qi
Thus the market equilibrium outputs, q1 and q2 , are the roots of the two
nonlinear equations
20/23
Exercise 3
Assume η = 1.6, c1 = 0.6 and c2 = 0.8 and find the equilibrium of this
economy
Create a function called cournot_res that receives as input
1. The initial guess for the vector of equilibrium output
2. The values of parameters (η, c1 , c2 )
and takes as output the “residual” of the equilibrium conditions
Use one the solvers covered previously (which one? why?)
How the equilibrium output of each firm changes with the elasticity η?
Set a linear space of 100 values between 0.6 and 5 for η
Solve the model for each value on the grid
Plot the equilibrium output of each firm against the elasticity
21/23
Exercise 4
Suppose you want to estimate the following model
yi = α + βxi + ui
xi : regressor of interest
yi : dependent variable
ui ∼ N(0, σ 2 ): error term, independent of xi and independent across
observations
n
n 1 X
` = − log(2π) − n log(s) − 2 (yi − α − βxi )2 (4)
2 2s
i=1
22/23
Exercise 4
23/23