def test_constant_vol_uses_constants(self): spot = 10 vol = 2 sut = ConstantVolatilityStock(spot, vol) res = sut.find_volatilities(0.01, [0 for _ in xrange(100)]) self.assertEqual(list(res), [vol for _ in xrange(100)])
def test_swaption(self): spot = 100 strike = 110 risk_free = 0.05 expiry = 1 vol = 0.2 s1 = ConstantVolatilityStock(100, 0.2) s2 = ConstantVolatilityStock(85, 0.4) option = EuropeanSwaption([s1, s2], risk_free, expiry, False) interval = 0.1 solver = HeuristicLayeredMCOptionSolver(interval) print solver.solve_option_price(option)
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 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 main(): import multiprocessing from mlmc.stock import ConstantVolatilityStock pool = multiprocessing.Pool(4) stock = ConstantVolatilityStock(10, 0.1) x = pool.map(calculate, [[create_simple_path] + [[stock], 0.01, 1, 100] for _ in xrange(100)]) import pprint pprint.pprint(sorted(xx[0] for xx in x))
def test_put_option(self): spot = 100 strike = 110 risk_free = 0.05 expiry = 1 vol = 0.2 stock = ConstantVolatilityStock(spot, vol) option = EuropeanStockOption([stock], risk_free, expiry, False, strike) solver = AnalyticEuropeanStockOptionSolver() put_value = black_scholes(spot, strike, risk_free, 0, vol, expiry, 'put') self.assertAlmostEqual(put_value, 10.6753248248) self.assertAlmostEqual(solver.solve_option_price(option), 10.6753248248)
def test_option_pricing(self): spot = 100 strike = 110 risk_free = 0.05 expiry = 1 vol = 0.2 stock = ConstantVolatilityStock(spot, vol) # is_call = True option = EuropeanStockOption([stock], risk_free, expiry, True, strike) solver = AnalyticEuropeanStockOptionSolver() call_value = black_scholes(spot, strike, risk_free, 0, vol, expiry, 'call') self.assertAlmostEqual(call_value, 6.04008812972) self.assertAlmostEqual(solver.solve_option_price(option), 6.04008812972)
def test_put_option(self): spot = 100 strike = 110 risk_free = 0.05 expiry = 1 vol = 0.2 stock = ConstantVolatilityStock(spot, vol) option = EuropeanStockOption([stock], risk_free, expiry, False, strike) interval = 0.1 expected = 10.6753248248 lower_bound = expected - interval upper_bound = expected + interval solver = NaiveMCOptionSolver(interval) n_runs = 20 in_bound_count = sum( 1 for _ in xrange(n_runs) if lower_bound <= solver.solve_option_price(option) <= upper_bound ) self.assertGreaterEqual(in_bound_count, 0.95*n_runs)
def test_call_option(self): spot = 100 strike = 110 risk_free = 0.05 expiry = 1 vol = 0.2 stock = ConstantVolatilityStock(spot, vol) option = EuropeanStockOption([stock], risk_free, expiry, True, strike) interval = 0.1 expected = 6.04008812972 lower_bound = expected - interval upper_bound = expected + interval solver = SimpleLayeredMCOptionSolver(interval) n_runs = 20 in_bound_count = sum( 1 for _ in xrange(n_runs) if lower_bound <= solver.solve_option_price(option) <= upper_bound ) self.assertGreaterEqual(in_bound_count, 0.95*n_runs)
def test_valuation_for_put(self): stock = ConstantVolatilityStock(10, 1) sut = EuropeanSwaption([stock, stock], 0.05, 1, False) self.assertEqual(sut.determine_payoff(10, 4), 0) self.assertEqual(sut.determine_payoff(4, 10), 6)
def test_valuation_for_call(self): stock = ConstantVolatilityStock(10, 1) sut = EuropeanStockOption([stock], 0.05, 1, True, 10) self.assertEqual(sut.determine_payoff(6), 0) self.assertEqual(sut.determine_payoff(15), 5)