File: testAmericanOptionTree.py

package info (click to toggle)
stopt 5.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,772 kB
  • sloc: cpp: 70,373; python: 5,942; makefile: 67; sh: 57
file content (136 lines) | stat: -rw-r--r-- 4,152 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python3

# Copyright (C) 2019 EDF
# All Rights Reserved
# This code is published under the GNU Lesser General Public License (GNU LGPL)
import numpy as np
import math
import StOptGrids
import StOptTree
import StOptGlobal
import StOptGeners
import Utils
import Simulators
import unittest

# test cases
class testAmericanOptionTest(unittest.TestCase):
    
    def test_americanTree(self):
  
        # define a a time grid
        timeGrid = StOptGrids.OneDimRegularSpaceGrid(0., 2., 1)
        # future values
        futureGrid =  Utils.FutureCurve(timeGrid, [50.,50.])

        # degenerate trinomial tri
        mr = 0.00001
        sigma =0.2

        # interest rate
        r = 0.05

        # number of time steps
        nstep =50
        
        # maturity
        T =1.
        
        # step
        dt = T / nstep
        
        # simulation dates
        dates =  dt * np.arange(0,nstep+1)

        # simulation dates
        treeSim = Simulators.TrinomialTreeOUSimulator(mr, sigma, dates)

        indexT = np.arange(0,nstep+1, 5)
        treeSim.dump("Tree",indexT)

        # read archive
        archiveToRead = StOptGeners.BinaryFileArchive("Tree", "r");
        
       # FIRST VALORIZATION
        ####################
        
        # backward simulator
        backSimulator = Simulators.MeanRevertingSimulatorTree(archiveToRead, futureGrid, sigma, mr)

        # strike
        strike  = 50.

        # actu
        actu = np.exp(r*dates[indexT[-1]])
        # spot : add interest rate
        spot = backSimulator.getSpotValues()*actu
        # actualized payoff
        val1= np.where( strike-spot>0,strike-spot,0)/actu
        for istep in np.arange(np.shape(indexT)[0]-1):
            # backward in simulator
            backSimulator.stepBackward()
            # get back probability matrix
            proba = backSimulator.getProba()
            # and connection matrix
            connected = backSimulator.getConnected()
            # creta tree for conditional expectation
            tree=StOptTree.Tree(proba,connected)      
            # interest rates
            actu = np.exp(r*dates[indexT[-2-istep]])
            # spot : add interest rate
            spot = backSimulator.getSpotValues()*actu
            # pay off 
            payOff = np.where( strike-spot>0,strike-spot,0)/actu
            # actualize value
            val1 = tree.expCond(val1)
            # arbitrage
            val1 = np.where( val1> payOff, val1, payOff)

        final = val1[0]
        
        indexT = np.arange(0,nstep+1)
        treeSim.dump("Tree",indexT)

        # read archive
        archiveToRead = StOptGeners.BinaryFileArchive("Tree", "r");
        
        # backward simulator
        backSimulator = Simulators.MeanRevertingSimulatorTree(archiveToRead, futureGrid, sigma, mr)

        # SECOND VALORIZATION
        ####################
        # actu
        actu = np.exp(r*dates[-1])
        # spot : add interest rate
        spot = backSimulator.getSpotValues()*actu
        # actualized payoff
        val2= np.where( strike-spot>0,strike-spot,0)/actu

        for istep in np.arange(nstep):
            # backward in simulator
            backSimulator.stepBackward()
            # get back probability matrix
            proba = backSimulator.getProba()
            # and connection matrix
            connected = backSimulator.getConnected()
            # creta tree for conditional expectation
            tree=StOptTree.Tree(proba,connected)      
            # actualize value
            val2 = tree.expCond(val2)

            if ((istep+1)%5==0):
                # interest rates
                actu = np.exp(r*dates[-2-istep])
                # spot : add interest rate
                spot = backSimulator.getSpotValues()*actu
                # pay off 
                payOff = np.where( strike-spot>0,strike-spot,0)/actu
                # arbitrage
                val2 = np.where( val2> payOff, val2, payOff)

        print("val1 " , val1 , " val2 " , val2)
        self.assertAlmostEqual(val1, val2, None, None, 0.000001)
        
 
if __name__ == '__main__':
    unittest.main()