File: testTree.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 (99 lines) | stat: -rw-r--r-- 3,018 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
#!/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 unittest
import random
import StOptGrids
import StOptReg
import StOptTree
import StOptGlobal
import Simulators
import StOptGeners


# unit test for tree

class testTree(unittest.TestCase):

    def testContinuation(self):

        # Mean Reverting model
        mr = 0.3;
        sig = 0.6;

        # nb grid points
        nbStock=4
        
        # step
        dt = 1. / 100.
        
        # simulation dates
        dates =  dt * np.arange(0,16)

        # simulaton dates
        tree = Simulators.TrinomialTreeOUSimulator(mr, sig, dates)

        iFirst = 10
        iLast = 14

        # nodes at dates 5
        points = tree.getPoints(iFirst)

        # nodes at last date
        pointsNext = tree.getPoints(iLast)

        # probabilities
        proba = tree.getProbability(iFirst, iLast)

        # connection matrix
        connectAndProba =  tree.calConnected(proba);

        # to regress
        toTreeress= np.zeros( (nbStock, np.shape(pointsNext)[1]))
        for i in range(nbStock):
            toTreeress[i,:] = i + pointsNext[0,:]

        # grid for storage
        grid = StOptGrids.RegularSpaceGrid(np.array([0.]), np.array([1.]), np.array([nbStock - 1]))

        # conditional expectation object by trees
        tree= StOptTree.Tree(connectAndProba[1],connectAndProba[0])

        # conditional expectation taken
        valTree = tree.expCondMultiple(toTreeress)

        # continuation object
        continuation = StOptTree.ContinuationValueTree(grid, tree,  toTreeress.transpose())

        # interpolation point
        ptStock = np.array([0.5*nbStock])

        #  conditional expectation using continuation object
        treeByContinuation = continuation.getValueAtNodes(ptStock);

        for i in range(np.shape(points)[1]):
            self.assertAlmostEqual(treeByContinuation[i],valTree[int(nbStock / 2), i], 7,"Difference between function and its condition expectation estimated greater than tolerance")
 
        # into a list  : two times to test 2 regimes
        listToReg = []
        listToReg.append(toTreeress.transpose())
        listToReg.append(toTreeress.transpose())
 
        #default non compression
        archiveToWrite = StOptGeners.BinaryFileArchive("MyArchive", "w");
        # continuation value dump
        archiveToWrite.dumpGridTreeValue("toStore",1,listToReg,tree,grid)

        # read archive
        archiveToRead =  StOptGeners.BinaryFileArchive("MyArchive","r")
        contValues = archiveToRead.readGridTreeValue(1,"toStore")
        # interpolate
        value =contValues[0].getValues(ptStock)
        for i in range(np.shape(points)[1]):
            self.assertAlmostEqual(value[i],valTree[int(nbStock / 2), i], 7,"Difference between function and its condition expectation estimated greater than tolerance")
  
if __name__ == '__main__': 
    unittest.main()