def test_coupon(self): """Test coupon value of annuity.""" payoff = Annuity(T, np.linspace(0, 1, N // 2 + 1), 1, 10) payoffI = AnnuityI(T, np.linspace(0, 1, N // 2 + 1), 1, 10) pay = True for t in np.linspace(0, 1, N + 1): if pay: V = 1 else: V = 0 pay = not pay self.assertTrue(payoff.coupon(t) == V) self.assertTrue(payoffI.coupon(t) == 0)
def setUp(self): T = 9 B = Annuity(T, [8], 4, 100, 0.5) P = Time(PutV(T, 105), [2, 4, 5, 6]) C = Time(CallVR(T, 110), [3, 4, 6, 7]) S = Time(CallA(T, 0), [1, 2, 3, 4, 9]) self.payoff = Stack([B, P, C, S]) Ssect = [ np.linspace(0, 0.25, 10), np.linspace(0.25, -.25, 10), np.linspace(-.25, 0.75, 10), np.linspace(0.75, 1, 9, endpoint=False) ] Vsect = [ np.linspace(0, -.25, 10), np.linspace(-.25, 0.25, 10), np.linspace(0.25, 0.50, 10), np.linspace(0.50, 1, 9, endpoint=False) ] Ssect = np.array(sum((list(i) for i in Ssect), [])) Vsect = np.array(sum((list(i) for i in Vsect), [])) critical = [10, 50, 100, 105, 110, 200] S = [] V = [] for i in range(len(critical) - 1): l, u = critical[i:i + 2] lu = u - l S.extend(Ssect * lu + l) V.extend(Vsect * lu + l) self.S = np.array(S) self.V = np.array(V)
def test_transcient(self): S = np.linspace(S0 - 10, S0 + 10, 21) V = np.linspace(S0 + 10, S0 - 10, 21) payoff = Annuity(T, np.linspace(0, 1, N // 2 + 1), 1, 10) payoffI = AnnuityI(T, np.linspace(0, 1, N // 2 + 1), 1, 10) pay = True for t in np.linspace(0, 1, N, endpoint=False): if pay: C = 1 else: C = 0 pay = not pay self.assertTrue((payoff.transient(t, V, S) == V).all()) self.assertTrue((payoffI.transient(t, V, S) == V + C).all()) self.assertRaises(AssertionError, payoff.transient, T, V, S) self.assertRaises(AssertionError, payoffI.transient, T, V, S)
def test_terminal(self): """Test terminal value of annuity.""" S = np.linspace(S0 - 10, S0 + 10, 21) Vo = np.ones(S.shape) * 10 self.assertTrue((Annuity(T, (), 1, 10).terminal(S) == Vo).all()) self.assertTrue((AnnuityI(T, (), 1, 10).terminal(S) == Vo).all()) self.assertTrue((AnnuityI(T, (T, ), 1, 10).terminal(S) == Vo + 1).all())
def test_annuity(self): """Test the pricing of a series of payments.""" def price(r): erdt = np.exp(-r) i = 1 / erdt - 1 return (1 - erdt**(T * 2)) / i + 10 * erdt**(T * 2) dS = WienerJumpProcess(0.1, 0.1) dSdq = WienerJumpProcess(0.1, 0.1, 0.02) # Semi-annual payments of 1 T = 10 V = Annuity(T, np.arange(0.5, T + 0.5, 0.5), 1, 10) model = BinomialModel(T * 2, dS, V) self.assertAlmostEqual(float(model.price(100)), price(0.05)) model = BinomialModel(T * 2, dSdq, V) self.assertAlmostEqual(float(model.price(100)), price(0.06))
def test_annuity(self): """Test the pricing of a series of payments.""" def price(r): erdt = np.exp(-r) i = 1 / erdt - 1 return (1 - erdt**(T * 2)) / i + 10 * erdt**(T * 2) dS = WienerJumpProcess(0.1, 0.1) dSdq = WienerJumpProcess(0.1, 0.1, 0.02) # Semi-annual payments of 1 T = 10 V = Annuity(T, np.arange(0.5, T + 0.5, 0.5), 1, 10) model = FDEModel(T * 8, dS, V) model2 = FDEModel(T * 8, dSdq, V) for scheme in SCHEMES: P = model.price(0, 200, 25, scheme=scheme) self.assertTrue((np.abs(P.V[0] - price(0.05)) < 1e-1).all()) P = model2.price(0, 200, 25, scheme=scheme) self.assertTrue((np.abs(P.V[0] - price(0.06)) < 1e-1).all())
def test_value(self): """Test the value object for the finite difference model.""" dS = WienerJumpProcess(0.1, 0.1, 0.02, 0) N = 16 T = 1 ct = np.linspace(0, T, N // 2 + 1) c = 1 Sl = 0 Su = 200 K = 40 model = FDEModel(N, dS, Stack([CallA(T, 100), Annuity(T, ct, c)])) P = model.price(Sl, Su, K) S = P.S # Test N self.assertEqual(P.N, N) # Test time series self.assertEqual(P.t[0], 0) self.assertEqual(P.t[-1], T) self.assertEqual(len(P.t), N + 1) # Test Coupon sequence self.assertTrue((P.C[::2] == c).all()) self.assertTrue((P.C[1::2] == 0).all()) # Test K self.assertEqual(P.K, K) # Test Stock series self.assertEqual(P.S[0], Sl) self.assertEqual(P.S[-1], Su) self.assertEqual(len(P.S), K + 1) # Test default sequence self.assertEqual(len(P.V), N + 1) for i in range(1, N + 1): self.assertLessEqual(P.V[i][0] - P.C[i], P.V[i - 1][0]) self.assertLessEqual(P.V[i][-1] - P.C[i], P.V[i - 1][-1]) self.assertTrue((P.V[i][:-1] - 1e14 <= P.V[i][1:]).all()) self.assertEqual(len(P.X), N) for i in range(1, N): self.assertGreaterEqual(P.X[i][0], P.X[i - 1][0]) self.assertLessEqual(P.X[i][-1], P.X[i - 1][-1]) self.assertTrue((P.X[i][:-1] <= P.X[i][1:]).all())
def test_value(self): """Test the value object for the binomial model.""" dS = WienerJumpProcess(0.1, 0.1, 0.02, 0) N = 16 T = 1 ct = np.linspace(0, T, N // 2 + 1) c = 1 S = 100 model = BinomialModel(N, dS, Stack([CallA(T, S), Annuity(T, ct, c)])) P = model.price(S) # Test N self.assertEqual(P.N, N) # Test time series self.assertEqual(P.t[0], 0) self.assertEqual(P.t[-1], T) self.assertEqual(len(P.t), N + 1) # Test Coupon sequence self.assertTrue((P.C[::2] == c).all()) self.assertTrue((P.C[1::2] == 0).all()) # Test price sequence self.assertEqual(P.S[0][0], S) self.assertEqual(len(P.S), N + 1) for i in range(1, N + 1): self.assertGreaterEqual(P.S[i][0], P.S[i - 1][0]) self.assertLessEqual(P.S[i][-1], P.S[i - 1][-1]) self.assertTrue((P.S[i][:-1] > P.S[i][1:]).all()) # Test default sequence self.assertEqual(len(P.V), N + 1) for i in range(1, N + 1): self.assertGreaterEqual(P.V[i][0] - P.C[i], P.V[i - 1][0]) self.assertLessEqual(P.V[i][-1] - P.C[i], P.V[i - 1][-1]) self.assertTrue((P.V[i][:-1] >= P.V[i][1:]).all()) self.assertEqual(len(P.X), N) for i in range(1, N): self.assertGreaterEqual(P.X[i][0], P.X[i - 1][0]) self.assertLessEqual(P.X[i][-1], P.X[i - 1][-1]) self.assertTrue((P.X[i][:-1] >= P.X[i][1:]).all())