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
|
# Copyright (C) 2016 EDF
# All Rights Reserved
# This code is published under the GNU Lesser General Public License (GNU LGPL)
import StOptGrids
import StOptReg
import StOptGlobal
import StOptGeners
def DynamicProgrammingByRegressionHighLevel(p_grid, p_optimize, p_regressor, p_funcFinalValue, p_pointStock, p_initialRegime, p_fileToDump) :
# from the optimizer get back the simulation
simulator = p_optimize.getSimulator()
# final values
fin = StOptGlobal.FinalStepDP(p_grid, p_optimize.getNbRegime())
valuesNext = fin.set(p_funcFinalValue, simulator.getParticles())
ar = StOptGeners.BinaryFileArchive(p_fileToDump, "w")
nameAr = "Continuation"
nsteps =simulator.getNbStep()
# iterate on time steps
for iStep in range(nsteps) :
asset = simulator.stepBackwardAndGetParticles()
# conditional expectation operator
if iStep == (simulator.getNbStep() - 1):
p_regressor.updateSimulations(True, asset)
else:
p_regressor.updateSimulations(False, asset)
# transition object
transStep = StOptGlobal.TransitionStepRegressionDP(p_grid, p_grid, p_optimize)
valuesAndControl = transStep.oneStep(valuesNext, p_regressor)
transStep.dumpContinuationValues(ar, nameAr, nsteps - 1 -iStep, valuesNext, valuesAndControl[1], p_regressor)
valuesNext = valuesAndControl[0]
# interpolate at the initial stock point and initial regime
return (p_grid.createInterpolator(p_pointStock).applyVec(valuesNext[p_initialRegime])).mean()
|