Dauphine-M2 VBA+Python Tree PDF 2024-25
Document Details
Uploaded by RecommendedBernoulli7676
Dauphine
2024
Tags
Summary
This PDF document details M2 course content related to financial engineering, option pricing, and lattice methods. It includes information on VBA and Python programming used in the course.
Full Transcript
16-Oct-24 M2 Course content Introduction Language reminders: ◦ VBA types & instructions ◦ VBA Code samples comments ◦ Python Classes in VBA Classes in Python...
16-Oct-24 M2 Course content Introduction Language reminders: ◦ VBA types & instructions ◦ VBA Code samples comments ◦ Python Classes in VBA Classes in Python Case #2: Option pricing with a lattice 16-Oct-24 21:50:02 Pricing model landscape Here are the three types of pricing models: Closed-form Trees Monte-Carlo formulas Black-Scholes, Examples Hull & White Longstaff-Schwartz Heston, SABR European ex. Yes Yes Yes American ex. No Yes Hard Path No Hard Yes dependent Speed Instantaneous Fast Slow Precision Perfect Error 1/Nb Steps Error 1/√Nb Draws Memory Light Depends Depends 1 16-Oct-24 Lattice node VBA class Goal #1, practice PDE and trees: ◦ Simulate a stock price in a recombining lattice ◦ Price European and American calls and puts ◦ Assess convergence with Black-Scholes Goal #2, code and use classes: ◦ Define classes for the lattice node and tree ◦ Build a tree with reconnecting nodes ◦ Price options on this lattice 16-Oct-24 21:50:02 Stock price behavior In the « Derivatives Pricing and Stochastic Calculus » course by Elie & Kharroubi we can see for Black & Scholes p. 61: We can use those assumptions for the interest rate and the volatility But the dividend is discrete and it is simplistic to define μ as: μ = rate - dividend 16-Oct-24 21:50:02 2 16-Oct-24 Discrete dividends Dividends in a near future are known Dividends in several years can be approximated as a % of the stock price Example with a stock price S0 = 100 € ◦ First annual dividend = 3 € ◦ Second annual dividend = 2 € + 1% of X1Y ◦ Third annual dividend = 1.4 € + 1.6% of X2Y ◦ Fourth annual dividend = 1 € + 2% of X3Y ◦ … ◦ Tenth annual dividend = 3% of X10Y 16-Oct-24 21:50:02 Dividend model The dividend is discrete and moves from constant to proportional to the stock price Example of possible functional form: ◦ Dt = ρ (S0 e-λ(t-t0) + St (1- e-λ(t-t0))) on ex-div. dates ◦ Dt = 0 on all other dates dSt = St r dt + St σ dWt – Dt 16-Oct-24 21:50:02 3 16-Oct-24 Trinomial lattice Example of trinomial lattice: Degrees of freedom and constraints: Degrees of freedom Constraints Time steps Match expected value Nodes values Match variance Choice of next middle node Positive probabilities Transition probabilities Sum of probabilities = 1 16-Oct-24 21:50:02 Trinomial tree assumptions We use the following assumptions: ◦ Time steps are all equal: Δt ◦ The middle node is equal to the forward price ◦ Nodes values are geometric series: α = Si,j+1/Si,j ◦ The next middle node is the closest to the forward price Example with a dividend on the fifth date: 16-Oct-24 21:50:02 4 16-Oct-24 Lattice building Start from the root: N0,0 with S0,0 = s0 Branch to the next central node: N1,0 S1,0 = S0,0 exp(r Δt) – D1 Build other nodes on date d1: S1,j = S1,0 αj α is defined by a multiple of the standard deviation over one time step ≈ St σ sqr(Δt): Si,j+1 - Si,j ≈ sqr(3) StdDev Divide by Si,j: α ≈ 1+ sqr(3) StdDev / Si,j The actual formula is: 𝛼 = 𝑒 𝜎 3∆𝑡 ◦ Please note: 3 is indicative. 2 as well as 2 work too 16-Oct-24 21:50:02 Probabilities (1) For each node Ni,j we calculate the probabilities: ◦ Middle node on ti+1 is the closer to the forward: Ni+1,j’ ◦ The sum of the three probabilities is equal to 1: 𝑝𝑢𝑝 + 𝑝𝑚𝑖𝑑 + 𝑝𝑑𝑜𝑤𝑛 = 1 ◦ Matching the expected value means: 𝐸𝑖+1,𝑗 = 𝑝𝑢𝑝 𝑆𝑖+1,𝑗 ′ +1 + 𝑝𝑚𝑖𝑑 𝑆𝑖+1,𝑗 ′ + 𝑝𝑑𝑜𝑤𝑛 𝑆𝑖+1,𝑗 ′ −1 = 𝐸 𝑆𝑡𝑖+1 ቚ𝑆𝑡𝑖 1 𝑟− 𝜎 2 ∆𝑡+𝜎𝑊∆𝑡 ◦ With 𝑆𝑡𝑖+1 = 𝑆𝑡𝑖 𝑒 2 − 𝐷𝑡𝑖+1 𝐸 𝑆𝑡𝑖+1 ቚ𝑆𝑡𝑖 = 𝑆𝑡𝑖 𝑒 𝑟∆𝑡 − 𝐷𝑡𝑖+1 16-Oct-24 21:50:02 5 16-Oct-24 Probabilities (2) Matching the variance on the step after Ni,j means: 2 2 2 2 𝑉𝑖+1,𝑗 + 𝐸𝑖+1,𝑗 = 𝑝𝑢𝑝 𝑆𝑖+1,𝑗 ′ +1 + 𝑝𝑚𝑖𝑑 𝑆𝑖+1,𝑗 ′ + 𝑝𝑑𝑜𝑤𝑛 𝑆𝑖+1,𝑗 ′ −1 2 𝑉𝑖+1,𝑗 = 𝐸 𝑆𝑡𝑖+1 − 𝐸 𝑆𝑡𝑖+1 ቚ𝑆𝑡𝑖 ቚ𝑆𝑡𝑖 1 2 𝑟− 𝜎 2 ∆𝑡+𝜎𝑊∆𝑡 ◦ 𝑉𝑖+1,𝑗 = 𝐸 𝑆𝑡2𝑖 𝑒 2 −𝑒 𝑟∆𝑡 ቚ𝑆𝑡𝑖 2 ∆𝑡+2𝜎𝑊 1 2 ◦ 𝑉𝑖+1,𝑗 = 𝑆𝑡2𝑖 𝑒 2𝑟∆𝑡 𝐸 𝑒 −𝜎 ∆𝑡 − 2𝑒 −2𝜎 ∆𝑡+𝜎𝑊∆𝑡 +1 𝜎 2 ∆𝑡 ◦ 𝑉𝑖+1,𝑗 = 𝑆𝑡2𝑖 𝑒 2𝑟∆𝑡 𝑒 −1 16-Oct-24 21:50:02 Probabilities (3) The three constraints make a linear system: ◦ 1 = 𝑝𝑢𝑝 + 𝑝𝑚𝑖𝑑 + 𝑝𝑑𝑜𝑤𝑛 ◦ 𝐸𝑖+1,𝑗 = 𝑝𝑢𝑝 𝑆𝑖+1,𝑗 ′ +1 + 𝑝𝑚𝑖𝑑 𝑆𝑖+1,𝑗 ′ + 𝑝𝑑𝑜𝑤𝑛 𝑆𝑖+1,𝑗 ′ −1 2 2 2 2 ◦ 𝑉𝑖+1,𝑗 + 𝐸𝑖+1,𝑗 = 𝑝𝑢𝑝 𝑆𝑖+1,𝑗 ′ +1 + 𝑝𝑚𝑖𝑑 𝑆𝑖+1,𝑗 ′ + 𝑝𝑑𝑜𝑤𝑛 𝑆𝑖+1,𝑗 ′ −1 That can be written as a matrix product: 1 1 1 𝑝𝑢𝑝 1 𝑆𝑖+1,𝑗 ′ +1 𝑆𝑖+1,𝑗 ′ 𝑆𝑖+1,𝑗 ′ −1 𝑝𝑚𝑖𝑑 = 𝐸𝑖+1,𝑗 2 2 2 𝑝𝑑𝑜𝑤𝑛 2 𝑆𝑖+1,𝑗 ′ +1 𝑆𝑖+1,𝑗 ′ 𝑆𝑖+1,𝑗 ′ −1 𝑉𝑖+1,𝑗 + 𝐸𝑖+1,𝑗 1 0 0 1 1 1 𝑝𝑢𝑝 1 0 𝑆𝑖+1,𝑗 ′ 0 𝛼 1 𝛼 −1 𝑝𝑚𝑖𝑑 = 𝐸𝑖+1,𝑗 2 0 0 𝑆𝑖+1,𝑗 ′ 𝛼 2 1 𝛼 −2 𝑝𝑑𝑜𝑤𝑛 2 𝑉𝑖+1,𝑗 + 𝐸𝑖+1,𝑗 16-Oct-24 21:50:02 6 16-Oct-24 Probabilities (4) The system has a unique solution with α>1 and Si+1,j’>0: 𝑝𝑢𝑝 1 1 1 1 −1 𝛼 1 𝛼 −1 𝑝𝑚𝑖𝑑 = 𝑆𝑖+1,𝑗 ′ 𝐸𝑖+1,𝑗 𝛼2 1 𝛼 −2 𝑝𝑑𝑜𝑤𝑛 −2 𝑆𝑖+1,𝑗 2 ′ 𝑉𝑖+1,𝑗 + 𝐸𝑖+1,𝑗 Solve by substitutions: 𝑝𝑢𝑝 1 1 1 1 −1 𝛼−1 0 𝛼 −1 − 1 𝑝𝑚𝑖𝑑 = 𝑆𝑖+1,𝑗 ′ 𝑖+1,𝑗 − 1 𝐸 𝛼2 − 1 0 𝛼 −2 − 1 𝑝𝑑𝑜𝑤𝑛 −2 𝑆𝑖+1,𝑗 2 ′ 𝑉𝑖+1,𝑗 + 𝐸𝑖+1,𝑗 − 1 Subtract (α+1) times the second line to the third to get: 1 − 𝛼 𝛼 −2 − 1 𝑝𝑑𝑜𝑤𝑛 −2 2 −1 = 𝑆𝑖+1,𝑗 ′ 𝑉𝑖+1,𝑗 + 𝐸𝑖+1,𝑗 − 1 − 𝛼 + 1 𝑆𝑖+1,𝑗 ′ 𝐸𝑖+1,𝑗 − 1 pdown has no dimension (good!), but is not guaranteed to be in [0, 1] 16-Oct-24 21:50:02 Probabilities (5) −2 2 −1 𝑆𝑖+1,𝑗 ′ 𝑉𝑖+1,𝑗 + 𝐸𝑖+1,𝑗 − 1 − 𝛼 + 1 𝑆𝑖+1,𝑗 ′ 𝐸𝑖+1,𝑗 − 1 𝑝𝑑𝑜𝑤𝑛 = 1 − 𝛼 𝛼 −2 − 1 When there is no dividend, 𝑆𝑖+1,𝑗′ = 𝐸𝑖+1,𝑗 and we have: −2 2 𝑆𝑖+1,𝑗 ′ 𝑉𝑖+1,𝑗 𝑒 𝜎 ∆𝑡 − 1 𝑝𝑑𝑜𝑤𝑛 = = 1 − 𝛼 𝛼 −2 − 1 1 − 𝛼 𝛼 −2 − 1 Use the second row to solve pup: −1 𝛼 − 1 𝑝𝑢𝑝 + 𝛼 −1 − 1 𝑝𝑑𝑜𝑤𝑛 = 𝑆𝑖+1,𝑗 ′ 𝐸𝑖+1,𝑗 − 1 𝑝𝑑𝑜𝑤𝑛 Without div: 𝑝𝑢𝑝 = 𝛼 Use the sum to solve pmid 7 16-Oct-24 European option pricing Build a lattice with N steps and tN = T, the option maturity Then value the payoff on each node on T ◦ On ITM nodes the payoff is for a call: SN,j – K ◦ On OTM nodes the payoff is equal to 0 Propagate the net future value on tN-1 nodes: ◦ NFVN-1,j = sum of probability x next node value x DF ◦ DF is the discount factor = exp(-r Δt) NFV0,0 = NPV is the net present value of the option 16-Oct-24 21:50:02 American option pricing Same as European options except for the early exercise At any time before the maturity the option holder can exercise the option On each interim node the value is the maximum of two quantities: ◦ Hold: value = discounted value like for European case ◦ Exercise: value = Si,j – K for a call on node (i,j) 16-Oct-24 21:50:02 8 16-Oct-24 Assignment (1) Build a trinomial lattice and an option pricer ◦ Start with few steps, no dividend, European exercise ◦ Try to have as much code as possible in the classes ◦ Check that the price converges to Black formula ◦ Describe the gap as a function of the number of steps ◦ Describe the gap as a function of the strike Add the American vs. European exercise ◦ Study the difference of price American – European ◦ Observe the effect of the interest rate (>0 or Gap = f(S0, σ, K, T, r) / NbSteps f(S0, σ, K, T, r) is a function of other parameters: vol, strike, maturity, etc. 16-Oct-24 21:50:02 16 16-Oct-24 Calculate number of steps (2) Tree vs. Black Price function of K (10 steps): Tree and Black-Scholes prices as functions of the strike 18 0.2 16 0.0 14 -0.2 12 -0.4 10 8 -0.6 6 -0.8 89 94 99 104 109 BS Tree Tree - BS -> Slope BS -> Slope Tree -> 16-Oct-24 21:50:02 Calculate number of steps (3) Idea: consider the ATM forward option Gap ≈ s va u f d wh r = K Value of that node with BS: Δ 1 ∆𝑆 2 1 Red triangle x density = 2 2 2𝜋 Σ ∆𝑆 = 𝐹𝑤𝑑 𝑇 𝛼 − 1 ∆𝑆 = 𝑆0 𝑒 𝑟𝑇 − 𝐷 𝑒 𝑟∆𝑡+𝜎 3∆𝑡 − 1 Σ s h s a dard d v a f h s distribution on the maturity date T 2𝑇 Σ = 𝑆0 𝑒 𝑟𝑇 𝑒 𝜎 −1 16-Oct-24 21:50:02 17 16-Oct-24 Calculate number of steps (4) Ga fu f ∆𝑡 is: 2 3 𝑆0 𝑒 𝜎 ∆𝑡 − 1 𝑒 2𝑟∆𝑡 𝐺𝑎𝑝 ≈ 8 2𝜋 2 𝑒𝜎 𝑇 − 1 Δ 2 h a d d y ∆𝑡 is in 𝑒 𝜎 ∆𝑡 − 1 2 1 8 2𝜋 𝐺𝑎𝑝 𝑒 𝜎 𝑇 − 1 ∆𝑡 ≈ 2 𝑙𝑛 1 + 𝜎 3 𝑆0 𝑇 𝜎2 𝑇 𝑁𝑏𝑆𝑡𝑒𝑝𝑠 = ≈ ∆𝑡 2 8 2𝜋 𝐺𝑎𝑝 𝑒 𝜎 𝑇 − 1 𝑙𝑛 1 + 3 𝑆0 16-Oct-24 21:50:02 Calculate number of steps (5) Remarks: 𝐺𝑎𝑝 is a relative precision on the stock price 𝑆0 F r s a ga s Nb s = / ∆𝑡 is inversely proportional to the gap (and the gap to NbSteps) Simplified formulas: 2 3 𝑆0 𝜎 2 ∆𝑡 8 2𝜋 𝐺𝑎𝑝 𝑒 𝜎 𝑇 −1 𝐺𝑎𝑝 ≈ ∆𝑡 ≈ 8 2𝜋 2 𝑒 𝜎 𝑇 −1 3 𝑆0 𝜎2 𝑇 3 𝑆0 𝜎2𝑇 𝑁𝑏𝑆𝑡𝑒𝑝𝑠 = ≈ 16-Oct-24 21:50:02 ∆𝑡 8 2𝜋 𝐺𝑎𝑝 𝑒 𝜎2 𝑇 − 1 18 16-Oct-24 Calculate number of steps (6) Remarks: W h = 1 y ar σ = 3 % w hav : 𝐺𝑎𝑝 1 3 0.32 ◦ ≈ 𝑁8 𝑆0 2𝜋 𝑒 0.32 −1 𝐺𝑎𝑝 ◦ N=1: = 4% 𝑆0 𝐺𝑎𝑝 ◦ N=10: = 0.4% 𝑆0 𝐺𝑎𝑝 ◦ N=100: = 0.04% 𝑆0 𝐺𝑎𝑝 ◦ N=1000: = 0.004% 𝑆0 16-Oct-24 21:50:02 How to check prices? You can check several results: ◦ European options without dividends compared to Black-Scholes prices ◦ European calls with strike = 0, compared with the forward price on maturity, including with dividends ◦ American options are harder to test, but the risk of error is lower (very little code is involved) and you can check the exercise frontier 27-Nov-19 21:50:02 19 16-Oct-24 Performance: VBA vs. Python VBA performance has been poor in 365 version: Run time function of number of steps Time in s 100 10 1 0.1 0.01 0.001 1 10 100 Nb steps 1000 VBA Office 365 VBA Office 2010 Python Gap analysis: VBA vs. Python Test on random parameters: ◦ Draw a set of random parameters ◦ Price with VBA and Python ◦ Store set of parameters if: The gap is larger than 1.10-13 Or there is a negative probability Run test Start date Start priceInterest rate Volatility Dividend Div ex-date Maturity Type Exercise Strike Nb steps VBA Py Rec Py Backward Gap 1 Gap 2 23-Aug-22 88.71 1.46% 30.95% 1.36 08-Dec-22 07-Apr-23 Put European 70.40 46 1.882848 1.882848 1.882848 -3.11E-15 0 On 06-Aug-22 117.04 3.80% 44.15% 10.04 29-Apr-23 08-May-23 Put American 122.97 81 25.482674 AssertionError: Error: NbStep=81 proba mid =-11.39%! #VALUE! #VALUE! assert(0