File: SimulateRegressionControl.py

package info (click to toggle)
stopt 5.12%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,860 kB
  • sloc: cpp: 70,456; python: 5,950; makefile: 72; sh: 57
file content (65 lines) | stat: -rw-r--r-- 2,330 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
# 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 StOptReg as reg
import StOptGrids
import StOptGeners
from StOptGlobal import StateWithStocks


def SimulateRegressionControl(p_grid, p_optimize, p_funcFinalValue, p_pointStock,
                              p_initialRegime, p_fileToDump, key="Continuation"):
    """
    Simulate the optimal strategy.

    Parameters:
    -----------
    p_grid
        Grid used for  deterministic state (stocks for example).
    p_optimize
        Optimizer defining the optimization between two time steps.
    p_funcFinalValue
        Function defining the final value.
    p_pointStock
        Initial point stock.
    p_initialRegime
        Regime at initial date.
    p_fileToDump
        Name of the file used to dump continuation values in optimization.
    """
    simulator = p_optimize.getSimulator()
    nsteps = simulator.getNbStep()
    nsims = simulator.getNbSimul()
    dim = simulator.getDimension()
    particle0 =  simulator.getParticles()[:,0]
    states = [StateWithStocks(p_initialRegime, p_pointStock, particle0)
              for _ in range(nsims)]
    # Retrieve the file containing the continuation values:
    ar = StOptGeners.BinaryFileArchive(p_fileToDump, "r")
    # Cost function
    costFunction = np.zeros((p_optimize.getSimuFuncSize(), nsims))
    # Iterate on time steps.
    for istep in range(nsteps) :
        continuation = ar.readGridAndRegressedValue(istep, key)
        grid = continuation[0].getGrid()
        for i in range(nsims):
            state = states[i]
            p_optimize.stepSimulate(grid, continuation, state,
                                    costFunction[:, i])
        particles = simulator.stepForwardAndGetParticles()
        for i in range(nsims) :
            states[i].setStochasticRealization(particles[:, i])

    # Final step: accept to exercise if not already done entirely.
    for i in range(simulator.getNbSimul()):
        costFunction[0, i] += p_funcFinalValue.set(states[i].getRegime(),
                                                   states[i].getPtStock(),
                                                   states[i].getStochasticRealization())

    # Average gain/cost.
    return costFunction.mean()