Lecture 7:
Optimization
Computational Finance
1
Functions
2
3
Functions: Concept
𝑓𝑓 𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖
• Execute some code using
the inputs
• Set the output variable(s)
inputs output(s)
• When calling a function, the caller doesn’t need to know what
happens inside the function.
• We just need to know how to call it and what the outputs are.
• Once the function is written, it is just like calling a built-in
MATLAB function: e.g.,
y = abs(x); %abs() is the function
Why are user-defined functions desirable?
• Readability
• Avoid errors
• Save space
• Change a formula everywhere at once
A user-defined function is a file with MATLAB code, created by the
user, that can be used from other code.
• Can be a simple, single mathematical calculation or a complex and involved
series of calculations
4
User-Defined Functions and Function Files
Functions
The variables created in a
function file are local (not
visible outside the function)
Only variables created inside
the function or passed to the
function in as arguments are
visible inside it.
The name for a function file
should be the same as the name
of the function.
Script Files
The variables created in a script
file are recognized in the
Command Window
Script files can use any variable
that has been defined in the
workspace
Name of script file can be
anything (within regular
MATLAB filename rules)
Function and Script File Differences
5
Example: Estimate CAPM Beta and Alpha
• Function file: estimate_capm.m
• First line defines everything the “outside world” needs to know about the
function
function [beta, alpha] = estimate_capm(Ri,exRm,Rf)
o Name (must match file name): estimate_capm
o Input arguments (information the function needs to receive): Ri, exRm, Rf
o Output argument(s) (information the function will return, must be assigned in the
function): beta, alpha
• Comment immediately below the first line can be viewed with doc
estimate_capm
o Good way to tell the “outside world” what the function does and what its
inputs/outputs are
o So that you or other users don’t have to open the function code to use it
6
Creating a Function File: Example 1
Example: Bond Prices
• Function file: get_bond_price.m
• First line defines everything the “outside world” needs to know about the function
function bond_price = get_bond_price(face_value,coupon_rate,times,rates)
o Name (must match file name): get_bond_price
o Input arguments (information the function needs to receive): face_value,
coupon_rate, times, rates
o Output argument(s) (information the function will return, must be assigned in the function):
bond_price
• Comment immediately below the first line can be viewed with doc
get_bond_price
o Good way to tell the “outside world” what the function does and what its inputs/outputs are
o So that you or other users don’t have to open the function code to use it
Let’s write the function together
And use it to assess the effect of rates on price: RunBondPrice.m
7
Creating a Function File: Example 2
Optimization
We can use MATLAB to solve
non-linear systems of
equations and find minima
and maxima of functions. You
will use these techniques in
Non-Linear Econometrics.
8
We know that the price of a bond is given by
𝑃𝑃 = 𝑐𝑐
(1+𝑟𝑟1)𝑡𝑡1
+ 𝑐𝑐
(1+𝑟𝑟2)𝑡𝑡2
+…
𝑐𝑐+𝑃𝑃
(1+𝑟𝑟𝑇𝑇)𝑇𝑇
The yield of a bond is defined as the single rate that discounts the
cash flows to the price:
𝑃𝑃 = 𝑐𝑐
(1+𝑦𝑦)𝑡𝑡1
+ 𝑐𝑐
(1+𝑦𝑦)𝑡𝑡2
+…
𝑐𝑐+𝑃𝑃
(1+𝑦𝑦)𝑇𝑇
Finding the yield requires us to solve a complicated non-linear
equation for y:
𝑐𝑐
(1+𝑟𝑟1)𝑡𝑡1
+ 𝑐𝑐
(1+𝑟𝑟2)𝑡𝑡2
+…
𝑐𝑐+𝑃𝑃
(1+𝑟𝑟𝑇𝑇)𝑇𝑇
= 𝑐𝑐
(1+𝑦𝑦)𝑡𝑡1
+ 𝑐𝑐
(1+𝑦𝑦)𝑡𝑡2
+…
𝑐𝑐+𝑃𝑃
(1+𝑦𝑦)𝑇𝑇
9
Example: Finding the Bond Yield
The function fsolve can numerically solve non-linear systems of equations
• Can be a system of N equations, can be 1 equation in 1 unknown
• Uses a variation on Newton’s Method, one of the first algorithms in applied mathematics!
• Needs a good initial guess i.e. it helps a lot if you know roughly what the solution is
• E.g. for yield, a good initial guess is one of the zero-coupon bond interest rates. A bad initial
guess is 350.
Syntax: sol = fsolve(func,guess);
• Find a value for sol, such that func(sol) == 0, or a vector of zeros is function is vector-
valued
• func is a user-supplied Matlab function, guess is the initial guess (same dimensions as
sol)
• func must take exactly one input, which has to be a scalar or a vector
• func first output must be some residual that will equal 0 at the correct solution
The “exactly one input” part seems restrictive, but there’s a way around it
• RunBondPrice.m
10
Solving equations with fsolve()
Lecture 7: �Optimization
Functions
Functions: Concept
User-Defined Functions and Function Files
Function and Script File Differences
Creating a Function File: Example 1
Creating a Function File: Example 2
Optimization
Example: Finding the Bond Yield
Solving equations with fsolve()