File: americanOption.py

package info (click to toggle)
stopt 5.12%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,860 kB
  • sloc: cpp: 70,456; python: 5,950; makefile: 72; sh: 57
file content (33 lines) | stat: -rw-r--r-- 1,246 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Copyright (C) 2016 EDF
# All Rights Reserved
# This code is published under the GNU Lesser General Public License (GNU LGPL)
import numpy as np
import math as maths

# american option by Longstaff-Schwartz
# p_sim        Monte Carlo simulator
# p_payOff     Option pay off
# p_regressor  regressor object
def resolution(p_simulator, p_payOff, p_regressor) :
    
    step = p_simulator.getStep()
    # asset simulated under the neutral risk probability : get the trend of first asset to get interest rate
    expRate = np.exp(-step * p_simulator.getMu()[0])
    # Terminal
    particle = p_simulator.getParticles()
    Cash = p_payOff.operator(particle)
    
    for iStep in range(0, p_simulator.getNbStep()):
        asset = p_simulator.stepBackwardAndGetParticles()
        payOffLoc = p_payOff.operator(asset)
        isLastStep = False        
        if iStep == p_simulator.getNbStep() - 1 :
            isLastStep = True
    
        p_regressor.updateSimulations(isLastStep, asset)
        # conditional expectation
        condEspec = p_regressor.getAllSimulations(Cash).squeeze() * expRate
        # arbitrage
        Cash = np.where(condEspec < payOffLoc, payOffLoc, Cash * expRate) 
       
    return maths.fsum(Cash) / len(Cash)