File: testAmericanOption.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 (90 lines) | stat: -rw-r--r-- 2,669 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python3

# 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
import utils.BasketOptions as bo
import simulators.BlackScholesSimulator as bs
import utils.americanOption as amer
import StOptGrids
import StOptReg as reg
import unittest



def american(p_nDim, p_nbSimul, p_nMesh, p_bRotate) :
    
    initialValues = np.zeros(p_nDim) + 1.0
    sigma = np.zeros(p_nDim) + 0.2
    mu = np.zeros(p_nDim) + 0.05
    corr = np.zeros((p_nDim,p_nDim))
    T = 1.
    nDate = 10
    np.fill_diagonal(corr, 1.)
    strike = 1.

    # simulator
    simulator = bs.BlackScholesSimulator(initialValues, sigma, mu, corr, T, nDate, p_nbSimul, False)
    # payoff
    payoff = bo.BasketPut(strike)
    # mesh
    nbMesh = np.zeros(p_nDim, dtype=np.int32) + p_nMesh
    # regressor
    regressor = reg.LocalLinearRegression(nbMesh,p_bRotate)
    # bermudean value
    value = amer.resolution(simulator, payoff, regressor)
    return value

# test cases
class testAmericanOptionTest(unittest.TestCase):
    
    def test_americanBasket1D(self):
  
        # dimension
        nDim = 1
        nbSimul = 500000
        nbMesh = 16
        referenceValue = 0.06031
        accuracyEqual = 0.13
               
        self.assertAlmostEqual(american(nDim, nbSimul, nbMesh, False), referenceValue, None, None, accuracyEqual)
        
    def test_americanBasket2D(self):    
        
        # dimension
        nDim = 2
        nbSimul = 1000000
        nbMesh = 16
        referenceValue = 0.03882
        accuracyEqual = 0.4
            
        self.assertAlmostEqual(american(nDim, nbSimul, nbMesh, False), referenceValue, None, None, accuracyEqual)
        self.assertAlmostEqual(american(nDim, nbSimul, nbMesh, True), referenceValue, None, None, accuracyEqual)
     
    def test_americanBasket3D(self):
    
        # dimension
        nDim = 3
        nbSimul = 2000000
        nbMesh = 10
        referenceValue = 0.02947
        accuracyEqual = 0.5
           
        self.assertAlmostEqual(american(nDim, nbSimul, nbMesh,False), referenceValue, None, None, accuracyEqual)
        self.assertAlmostEqual(american(nDim, nbSimul, nbMesh,True), referenceValue, None, None, accuracyEqual)
      
    def test_americanBasket4D(self):
        
        # dimension
        nDim = 4
        nbSimul = 2000000
        nbMesh = 6
        referenceValue = 0.02404
        accuracyEqual = 1.
           
        self.assertAlmostEqual(american(nDim, nbSimul, nbMesh,False), referenceValue, None, None, accuracyEqual)

if __name__ == '__main__':
    unittest.main()