代写代考 Lecture 7: Optimization

Lecture 7: Optimization
Computational Finance

Copyright By PowCoder代写 加微信 powcoder

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

User-Defined Functions and Function Files
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

Function and Script File Differences
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)

Creating a Function File: Example 1
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

Creating a Function File: Example 2
Example: Bond Prices
• Functionfile: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
• Commentimmediatelybelowthefirstlinecanbeviewedwithdoc 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

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.

Example: Finding the Bond Yield
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
𝑃𝑃= 𝑐𝑐 + 𝑐𝑐 +…𝑐𝑐+𝑃𝑃 (1+𝑦𝑦)𝑡𝑡1 (1+𝑦𝑦)𝑡𝑡2 (1+𝑦𝑦)𝑇𝑇
cash flows to the price:
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+𝑦𝑦)𝑇𝑇

Solving equations with fsolve()
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.foryield,agoodinitialguessisoneofthezero-couponbondinterestrates.Abadinitial 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-
• func is a user-supplied Matlab function, guess is the initial guess (same dimensions as
• 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

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com