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 137 138
|
#!/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 unittest
import random
import StOptGrids
import StOptReg
import StOptGeners
# unit test for dumping binary archive of regressed value and Read then
######################################################################
class testBinaryArchiveStOpt(unittest.TestCase):
def testSimpleStorageAndLectureRecGrid(self):
# low value for the mesh
lowValues =np.array([1.,2.,3.],dtype=np.float64)
# size of the mesh
step = np.array([0.7,2.3,1.9],dtype=np.float64)
# number of step
nbStep = np.array([4,5,6], dtype=np.int32)
# degree of the polynomial in each direction
degree = np.array([2,1,3], dtype=np.int32)
# create the Legendre grid
grid = StOptGrids.RegularLegendreGrid(lowValues,step,nbStep,degree )
# simulate the perburbed values
################################
nbSimul =40000
np.random.seed(1000)
x = np.random.uniform(-1.,1.,size=(1,nbSimul));
# mesh
nbMesh = np.array([16],dtype=np.int32)
# Create the regressor
#####################
regressor = StOptReg.LocalLinearRegression(False,x,nbMesh)
# regressed values same values for each point of the grid
#########################################################
toReal = (2+x[0,:]+(1+x[0,:])*(1+x[0,:]))
# function to regress
toRegress = toReal + 4*np.random.normal(0.,1,nbSimul)
# create a matrix (number of stock points by number of simulations)
toRegressMult = np.zeros(shape=(len(toRegress),grid.getNbPoints()))
for i in range(toRegressMult.shape[1]):
toRegressMult[:,i] = toRegress
# into a list : two times to test 2 regimes
listToReg = []
listToReg.append(toRegressMult)
listToReg.append(toRegressMult)
# Create the binary archive to dump
###################################
archiveToWrite = StOptGeners.BinaryFileArchive("MyArchive","w")
# step 1
archiveToWrite.dumpGridAndRegressedValue("toStore", 1,listToReg, regressor,grid)
# step 3
archiveToWrite.dumpGridAndRegressedValue("toStore", 3,listToReg, regressor,grid)
# Read the regressed values
###########################
archiveToRead = StOptGeners.BinaryFileArchive("MyArchive","r")
contValues = archiveToRead.readGridAndRegressedValue(3,"toStore")
# list of 2 continuation values
##############################
stockPoint = np.array([1.5,3.,5.])
uncertainty = np.array([0.])
value =contValues[0].getValue(stockPoint,uncertainty)
# non regular grid
def testSimpleStorageAndLectureNonRegular(self):
# create the Legendre grid
grid = StOptGrids.GeneralSpaceGrid([[ 1., 1.7, 2.4, 3.1, 3.8 ],
[2., 4.3, 6.6, 8.9, 11.2, 15.],
[3., 4.9, 5.8, 7.7, 10.,20.]])
# simulate the perburbed values
################################
nbSimul =40000
np.random.seed(1000)
x = np.random.uniform(-1.,1.,size=(1,nbSimul));
# mesh
nbMesh = np.array([16],dtype=np.int32)
# Create the regressor
#####################
regressor = StOptReg.LocalLinearRegression(False,x,nbMesh)
# regressed values same values for each point of the grid
#########################################################
toReal = (2+x[0,:]+(1+x[0,:])*(1+x[0,:]))
# function to regress
toRegress = toReal + 4*np.random.normal(0.,1,nbSimul)
# create a matrix (number of stock points by number of simulations)
toRegressMult = np.zeros(shape=(len(toRegress),grid.getNbPoints()))
for i in range(toRegressMult.shape[1]):
toRegressMult[:,i] = toRegress
# into a list : two times to test 2 regimes
listToReg = []
listToReg.append(toRegressMult)
listToReg.append(toRegressMult)
# Create the binary archive to dump
###################################
archiveToWrite = StOptGeners.BinaryFileArchive("MyArchive","w")
# step 1
archiveToWrite.dumpGridAndRegressedValue("toStore", 1,listToReg, regressor,grid)
# step 3
archiveToWrite.dumpGridAndRegressedValue("toStore", 3,listToReg, regressor,grid)
# Read the regressed values
###########################
archiveToRead = StOptGeners.BinaryFileArchive("MyArchive","r")
contValues = archiveToRead.readGridAndRegressedValue(3,"toStore")
# list of 2 continuation values
##############################
stockPoint = np.array([1.5,3.,5.])
uncertainty = np.array([0.])
value =contValues[0].getValue(stockPoint,uncertainty)
if __name__ == '__main__':
unittest.main()
|