def test_stocks_input_unchanged(self): ''' The stock objects used as inputs should not themselves be walked ''' stocks = [ConstantVolatilityStock(i, 1) for i in xrange(1, 5)] create_simple_path(stocks, risk_free=0.01, T=10, n_steps=5) for spot, stock in enumerate(stocks, start=1): self.assertEqual(stock.post_walk_price, spot)
def test_all_stocks_walked(self): ''' The stock objects used as inputs should not themselves be walked ''' stocks = [ConstantVolatilityStock(i, 1) for i in xrange(1, 5)] paths = create_simple_path(stocks, risk_free=0.01, T=10, n_steps=5) self.assertEqual(len(paths), len(stocks))
def run_bottom_level(self, option, steps): ''' Run the bottom level of E-M paths. Each of the E-M paths will have only one step option: Option. What we're looking to price steps: int. Totally irrelevant, used only because run_upper_levels requires it as part of the signature. ''' result = path.create_simple_path(option.assets, option.risk_free, option.expiry, 1, self.rng_creator) return option.determine_payoff(*result),
def _simulate_paths(self, option, n_steps, discount): stat_tracker = StatTracker(discount) cnt = itertools.count() while next(cnt) < 10 or stat_tracker.get_interval_length( self.z_score) > self.max_interval_length: result = path.create_simple_path(option.assets, option.risk_free, option.expiry, n_steps, self.rng_creator) payoff = option.determine_payoff(*result) stat_tracker.add_sample(payoff) return stat_tracker
def test_path_creation_will_walk_all_steps(self): spots = map(float, xrange(1, 3)) r = 0.01 T = 5 n_steps = 2 stocks = [MockStock(s) for s in spots] samples = np.array([ np.array([5, 6, 7]), np.array([1, 2, 3]), np.array([9, 8, 7]), np.array([4, 6, 2]), ]) rng_creator = functools.partial(MockSampleCreator, samples) paths = create_simple_path(stocks, r, T, n_steps, rng_creator) expecteds = [376.063125, 21610.50125] for observed, expected in zip(paths, expecteds): self.assertAlmostEqual(observed, expected)
def run_bottom_level(self, option, steps): result = path.create_simple_path(option.assets, option.risk_free, option.expiry, 1, self.rng_creator) return option.determine_payoff(*result),