Ejemplo n.º 1
0
 def valuation(self, market_assumptions, nb_simulations=1000000):
     simulator = ForwardSimulator(
                     market_assumptions.uncertainty_system,
                     [0., self.T],
                     nb_simulations,
                     antithetic=True)
     simulator.go_end()
     discount_factor = market_assumptions.discount_factor(self.T)
     return MonteCarloEstimation(self.get_payoff(simulator) * discount_factor)
Ejemplo n.º 2
0
    def simulate(self, market_assumptions, nb_simulations, antithetic=False):
        """
       Returns a generator yielding for each simulation timestep
       a map of the type :
           (asset, state) -> command -> simulation_ids
       """
        exercise_times = list(self.optional_asset.exercise_times)
        simulation_timeline = sorted(set([0.0] + exercise_times))
        asset_states = {self.optional_asset.initial_state: np.arange(0, nb_simulations, dtype=np.int32)}
        states_and_command = {}
        simulator = ForwardSimulator(
            market_assumptions.uncertainty_system,
            simulation_timeline,
            nb_simulations=nb_simulations,
            antithetic=antithetic,
        )
        # simulator.go_to(exercise_times[0]) # todo handle stuff not beginning at t0
        for t in exercise_times:
            tid = simulator.tid
            new_asset_states = {}
            for (asset_state, sim_ids) in asset_states.items():
                if sim_ids.shape != (0,):
                    asset_stock_optimal_command_ids = {}
                    cur_masked_state = simulator.state.mask(sim_ids)
                    optimal_command_ids = compute_optimal_command(
                        asset_state,
                        cur_masked_state,
                        self.conditional_expectation_store,
                        market_assumptions.discount_factor,
                    )

                    commands = asset_state.get_commands(t)
                    for (command_id, command) in enumerate(commands):
                        # simulation indexes on which the command is optimal
                        sim_indexes = np.where(optimal_command_ids == command_id)[0]
                        optimal_command_sim_ids = sim_ids[sim_indexes]
                        asset_stock_optimal_command_ids[command] = optimal_command_sim_ids
                        cur_masked_state_for_command = simulator.state.mask(optimal_command_sim_ids)
                        former_simulation_ids = new_asset_states.get(command.dest_state, np.array([], dtype=np.int32))
                        new_asset_states[command.dest_state] = np.union1d(
                            former_simulation_ids, optimal_command_sim_ids
                        )
                        asset_states = new_asset_states
                    states_and_command[asset_state] = asset_stock_optimal_command_ids
            yield (simulator.state, states_and_command)
            simulator.go_next()
Ejemplo n.º 3
0
 def test_forward(self):
     simulator = ForwardSimulator(UncertaintySystem([self.model]), self.timeline, 2)
     model_state = simulator.state[self.model]
     self.assertTrue((model_state == np.array([[1., 2.]])).all())
     simulator.go_next()
     model_state = simulator.state[self.model]
     self.assertTrue((model_state == np.array([[1., 4.]])).all())
     simulator.go_next()
     model_state = simulator.state[self.model]
     self.assertTrue((model_state == np.array([[1., 8.]])).all())
     simulator.go_next()
     model_state = simulator.state[self.model]
Ejemplo n.º 4
0
 def test_spot(self):
     simulator = ForwardSimulator(UncertaintySystem([self.market]), self.timeline, 10000)
     while (simulator.go_next()):
         forward_curves = simulator.state.cursor(self.market) 
         error = np.abs( forward_curves.spot().mean() - self.F0(simulator.t) )
         self.assertTrue( np.all(error < .05))
Ejemplo n.º 5
0
 def test_antithetic(self):
     simulator = ForwardSimulator(UncertaintySystem([self.model]), self.timeline, 10000, True)
     model_state = simulator.state[self.model]
     while (simulator.go_next()):
         pass
Ejemplo n.º 6
0
from sys import path
from os import path as osp

CURDIR = osp.abspath(osp.dirname(__file__))
path.append(CURDIR + "/../../../src")

from strongchicken.uncertainties import BlackScholes
from strongchicken.uncertainties import ForwardSimulator
from strongchicken.uncertainties import UncertaintySystem
from strongchicken.utils import *

# this is the timeline on which we want to perform the simulation
timeline = np.arange(0., UNIT.YEAR, UNIT.DAY)

# ... a BS market with 20% / sqrt(year) of volatilty
market = BlackScholes(36., .06*INV.YEAR, .2*INV.SQRT.YEAR)

uncertainty_system = UncertaintySystem([market])

# and the simulator, we're all set to begin the simulation!
nb_paths = 1000
simulator = ForwardSimulator(uncertainty_system, timeline, nb_paths)

paths = np.zeros((len(timeline), nb_paths))
for (t_id,t) in enumerate(timeline):
	paths[t_id,:] = simulator.state[market]
	simulator.go_next()
print paths.mean(axis=1)

#plt.plot(paths)
#plt.savefig("tutorial1_1factor.png")