def test_european_call_price_decrease_with_strike(self):
     mdl = create_binomial_model(0.1, 0.02, 100, 1, n=100)
     last = None
     for s in range(90, 110):
         current = european_call_price(mdl, s)
         if last is not None:
             self.assertLess(current, last)
         last = current
 def test_american_european_call_equality(self):
     european = []
     american = []
     for sigma in [0.05, 0.1, 0.2]:
         for r in [-0.02, 0.0, 0.01, 0.1]:
             for S0 in [50, 100, 5000]:
                 mdl = create_binomial_model(0.1, 0.02, 100, 1, n=100)
                 for strike in [80, 90, 100, 110, 120]:
                     european.append(european_call_price(mdl, strike))
                     american.append(american_call_price(mdl, strike))
     self.assertTrue(np.allclose(european, american))
 def test_deep_itm_american_out(self):
     '''Test a deep in the money american out option.'''
     S0 = 100
     n = 5
     T = 5
     strike = 500
     sigma = 0.4
     r = 0.1
     mdl = create_binomial_model(sigma, r, S0, T, n)
     npv = american_put_price(mdl, strike)
     self.assertAlmostEqual(strike - S0, npv)
 def test_ucsd_example(self):
     '''Test American put example data found in
        https://www.math.ucsd.edu/~p1tong/ProgramForFun/
        Binomial%20Tree%20model%20for%20an%20American%20Put%20option.xls
     '''
     S0 = 50
     n = 5
     T = 0.4167
     strike = 50
     sigma = 0.4
     r = 0.1
     mdl = create_binomial_model(sigma, r, S0, T, n)
     npv = american_put_price(mdl, strike)
     self.assertAlmostEqual(4.48859919382338, npv)
 def test_put_call_parity(self):
     expected = []
     actual = []
     for sigma in [0.05, 0.1, 0.2]:
         for r in [-0.02, 0.0, 0.01, 0.1]:
             for S0 in [50, 100, 5000]:
                 mdl = create_binomial_model(0.1, 0.02, 100, 1, n=100)
                 for strike in [80, 90, 100, 110, 120]:
                     expected.append(mdl.S0 -
                                     strike * np.exp(-mdl.r * mdl.T))
                     actual.append(
                         european_call_price(mdl, strike) -
                         european_put_price(mdl, strike))
     self.assertTrue(np.allclose(expected, actual))
 def test_american_exercise_barrier_fitted(self):
     '''Test fitted exercise barrier of american out option.'''
     S0 = 100
     n = 100
     T = 5
     strike = 100
     sigma = 0.4
     r = 0.1
     mdl = create_binomial_model(sigma, r, S0, T, n)
     barrier = american_put_exercise_barrier_fitted(mdl, strike, 2)
     barrier = barrier.convert(domain=[-1, 1])
     self.assertGreater(barrier.coef[0], 0)
     self.assertLessEqual(barrier.coef[1], 0)
     self.assertGreater(barrier.coef[2], 0)
 def test_american_european_put_difference(self):
     european = []
     american = []
     for sigma in [0.05, 0.1, 0.2]:
         for r in [-0.02, 0.0, 0.01, 0.1]:
             for S0 in [50, 100, 5000]:
                 mdl = create_binomial_model(0.1, 0.02, 100, 1, n=100)
                 for strike in [80, 90, 100, 110, 120]:
                     european.append(european_put_price(mdl, strike))
                     american.append(american_put_price(mdl, strike))
     aamerican = np.array(american)
     aeuropean = np.array(european)
     self.assertTrue((aamerican > aeuropean).all())
     # test for some "real" difference, threshold 1.23 is rather arbitrary
     self.assertGreater((aamerican - aeuropean).max(), 1.23)
 def test_american_exercise_barrier(self):
     '''Test exercise barrier of american out option.'''
     S0 = 100
     n = 100
     T = 5
     strike = 100
     sigma = 0.4
     r = 0.1
     mdl = create_binomial_model(sigma, r, S0, T, n)
     exercise_barrier = american_put_exercise_barrier(mdl, strike)
     last_even = np.nan
     last_odd = np.nan
     for i, s in enumerate(exercise_barrier):
         if i % 2 == 0:
             if not np.isnan(last_even):
                 self.assertGreaterEqual(s, last_even)
             last_even = s
         elif i % 2 == 1:
             if not np.isnan(last_odd):
                 self.assertGreaterEqual(s, last_odd)
             last_odd = s
Exemple #9
0
plt.legend([handle_path, handle_stopped_path, handle_first_ex], [
    'Path before exercise', 'Path after exercise', 'First favorable exercise'
])
plt.xlabel('Time t')
plt.ylabel('Stock value')
plt.savefig(
    "/home/ppl/Documents/Universitet/KUKandidat/Speciale/DeepPricing/latex/Figures/LSMFit2.pdf"
)
plt.show()

####################
## Fit exercise boundary
################
from longstaff_schwartz.binomial import create_binomial_model, american_put_price, american_put_exercise_barrier_fitted

mdl = create_binomial_model(sigma=sigma, r=1e-14, S0=S0, T=5, n=100)
exercise_barrier = american_put_exercise_barrier_fitted(mdl, strike, 3)

plt.figure(figsize=figsize)
plt.plot(exercise_times, exercises, 'rx', zorder=2)
plt.plot(*exercise_barrier.linspace(), 'g', zorder=3)
plt.plot(t, X, color='#dddddd', zorder=1)
plt.legend([
    'Exercise Favourable (Simulated)',
    'Fitted Exercise Boundary (Binomial Model)', 'Simulated Paths'
])
plt.xlabel('Time t')
plt.ylabel('Stock Price')
plt.show()

european_cashflow = np.maximum(strike - X[-1, :], 0)