Пример #1
0
 def test_put_default(self):
     """Test value of default for a put."""
     S = np.linspace(S0 - 10, S0 + 10, 21)
     Vd = np.maximum(K - S, 0)
     payoff = PutA(T, K)
     for t in np.linspace(0, 1, N, endpoint=False):
         self.assertTrue((payoff.default(t, S) == Vd).all())
     self.assertRaises(AssertionError, payoff.default, T, S)
Пример #2
0
 def test_put_default(self):
     """Test value of default for a put."""
     S = np.linspace(S0 - 10, S0 + 10, 21)
     Vd = np.maximum(K - S, 0)
     payoff = PutA(T, K)
     for t in np.linspace(0, 1, N, endpoint=False):
         self.assertTrue((payoff.default(t, S) == Vd).all())
     self.assertRaises(AssertionError, payoff.default, T, S)
Пример #3
0
 def test_put_transient(self):
     """Test value of transient for American put."""
     S = np.linspace(S0 - 10, S0 + 10, 21)
     V = np.linspace(S0 + 10, S0 - 10, 21)
     Vm = np.maximum(K - S, V)
     payoff = PutA(T, K)
     for t in np.linspace(0, 1, N, endpoint=False):
         self.assertTrue((payoff.transient(t, V, S) == Vm).all())
     self.assertRaises(AssertionError, payoff.transient, T, V, S)
Пример #4
0
 def test_put_transient(self):
     """Test value of transient for American put."""
     S = np.linspace(S0 - 10, S0 + 10, 21)
     V = np.linspace(S0 + 10, S0 - 10, 21)
     Vm = np.maximum(K - S, V)
     payoff = PutA(T, K)
     for t in np.linspace(0, 1, N, endpoint=False):
         self.assertTrue((payoff.transient(t, V, S) == Vm).all())
     self.assertRaises(AssertionError, payoff.transient, T, V, S)
Пример #5
0
class TestHullCh20(unittest.TestCase):
    """Test H12 Chapter 12: Basic Numerical Procedure."""

    dS = HullProcess(0.1, 0.4)
    V = PutA(5. / 12, 50)

    table20_4 = np.array([
        #  5.0,   4.5,   4.0,   3.5,   3.0,   2.5,   2.0,   1.5,   1.0,   0.5,   0.0
        [0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
         0.00],  # 100
        [0.02, 0.02, 0.01, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
         0.00],  #  95
        [0.05, 0.04, 0.03, 0.02, 0.01, 0.01, 0.00, 0.00, 0.00, 0.00,
         0.00],  #  90
        [0.09, 0.07, 0.05, 0.03, 0.02, 0.01, 0.01, 0.00, 0.00, 0.00,
         0.00],  #  85
        [0.16, 0.12, 0.09, 0.07, 0.04, 0.03, 0.02, 0.01, 0.00, 0.00,
         0.00],  #  80
        [0.27, 0.22, 0.17, 0.13, 0.09, 0.06, 0.03, 0.02, 0.01, 0.00,
         0.00],  #  75
        [0.47, 0.39, 0.32, 0.25, 0.18, 0.13, 0.08, 0.04, 0.02, 0.00,
         0.00],  #  70
        [0.82, 0.71, 0.60, 0.49, 0.38, 0.28, 0.19, 0.11, 0.05, 0.02,
         0.00],  #  65
        [1.42, 1.27, 1.11, 0.95, 0.78, 0.62, 0.45, 0.30, 0.16, 0.05,
         0.00],  #  60
        [2.43, 2.24, 2.05, 1.83, 1.61, 1.36, 1.09, 0.81, 0.51, 0.22,
         0.00],  #  55
        [4.07, 3.88, 3.67, 3.45, 3.19, 2.91, 2.57, 2.17, 1.66, 0.99,
         0.00],  #  50
        [6.58, 6.44, 6.29, 6.13, 5.96, 5.77, 5.57, 5.36, 5.17, 5.02,
         5.00],  #  45
        [
            10.15, 10.10, 10.05, 10.01, 10.00, 10.00, 10.00, 10.00, 10.00,
            10.00, 10.00
        ],  #  40
        [
            15.00, 15.00, 15.00, 15.00, 15.00, 15.00, 15.00, 15.00, 15.00,
            15.00, 15.00
        ],  #  35
        [
            20.00, 20.00, 20.00, 20.00, 20.00, 20.00, 20.00, 20.00, 20.00,
            20.00, 20.00
        ],  #  30
        [
            25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00,
            25.00, 25.00
        ],  #  25
        [
            30.00, 30.00, 30.00, 30.00, 30.00, 30.00, 30.00, 30.00, 30.00,
            30.00, 30.00
        ],  #  20
        [
            35.00, 35.00, 35.00, 35.00, 35.00, 35.00, 35.00, 35.00, 35.00,
            35.00, 35.00
        ],  #  15
        [
            40.00, 40.00, 40.00, 40.00, 40.00, 40.00, 40.00, 40.00, 40.00,
            40.00, 40.00
        ],  #  10
        [
            45.00, 45.00, 45.00, 45.00, 45.00, 45.00, 45.00, 45.00, 45.00,
            45.00, 45.00
        ],  #   5
        [
            50.00, 50.00, 50.00, 50.00, 50.00, 50.00, 50.00, 50.00, 50.00,
            50.00, 50.00
        ],  #   0
    ])

    table20_5 = np.array([
        #  5.0,   4.5,   4.0,   3.5,   3.0,   2.5,   2.0,   1.5,   1.0,   0.5,   0.0
        [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
         0.00],  # 100
        [0.06, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
         0.00],  #  95
        [-0.11, 0.05, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
         0.00],  #  90
        [0.28, -0.05, 0.05, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
         0.00],  #  85
        [-0.13, 0.20, 0.00, 0.05, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
         0.00],  #  80
        [0.46, 0.06, 0.20, 0.04, 0.06, 0.00, 0.00, 0.00, 0.00, 0.00,
         0.00],  #  75
        [0.32, 0.46, 0.23, 0.25, 0.10, 0.09, 0.00, 0.00, 0.00, 0.00,
         0.00],  #  70
        [0.91, 0.68, 0.63, 0.44, 0.37, 0.21, 0.14, 0.00, 0.00, 0.00,
         0.00],  #  65
        [1.48, 1.37, 1.17, 1.02, 0.81, 0.65, 0.42, 0.27, 0.00, 0.00,
         0.00],  #  60
        [2.59, 2.39, 2.21, 1.99, 1.77, 1.50, 1.24, 0.90, 0.59, 0.00,
         0.00],  #  55
        [4.26, 4.08, 3.89, 3.68, 3.44, 3.18, 2.87, 2.53, 2.07, 1.56,
         0.00],  #  50
        [6.76, 6.61, 6.47, 6.31, 6.15, 5.96, 5.75, 5.50, 5.24, 5.00,
         5.00],  #  45
        [
            10.28, 10.20, 10.13, 10.06, 10.01, 10.00, 10.00, 10.00, 10.00,
            10.00, 10.00
        ],  #  40
        [
            15.00, 15.00, 15.00, 15.00, 15.00, 15.00, 15.00, 15.00, 15.00,
            15.00, 15.00
        ],  #  35
        [
            20.00, 20.00, 20.00, 20.00, 20.00, 20.00, 20.00, 20.00, 20.00,
            20.00, 20.00
        ],  #  30
        [
            25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00,
            25.00, 25.00
        ],  #  25
        [
            30.00, 30.00, 30.00, 30.00, 30.00, 30.00, 30.00, 30.00, 30.00,
            30.00, 30.00
        ],  #  20
        [
            35.00, 35.00, 35.00, 35.00, 35.00, 35.00, 35.00, 35.00, 35.00,
            35.00, 35.00
        ],  #  15
        [
            40.00, 40.00, 40.00, 40.00, 40.00, 40.00, 40.00, 40.00, 40.00,
            40.00, 40.00
        ],  #  10
        [
            45.00, 45.00, 45.00, 45.00, 45.00, 45.00, 45.00, 45.00, 45.00,
            45.00, 45.00
        ],  #   5
        [
            50.00, 50.00, 50.00, 50.00, 50.00, 50.00, 50.00, 50.00, 50.00,
            50.00, 50.00
        ],  #   0
    ])

    def test_table20_4(self):
        """Test Example 20.1 using implicit finite difference method."""
        model = FDEModel(10, self.dS, self.V)
        P = model.price(0, 100, 20, scheme=ImplicitScheme, boundary="ignore")
        for i in reversed(range(len(P.V))):
            self.assertTrue(
                (np.abs(self.table20_4[::-1, i] - P.V[i]) < 0.01).all())

    def test_table20_5(self):
        """Test Example 20.1 using explicit finite difference method."""
        model = FDEModel(10, self.dS, self.V)
        P = model.price(0, 100, 20, scheme=ExplicitScheme, boundary="ignore")
        for i in reversed(range(len(P.V))):
            self.assertTrue(
                (np.abs(self.table20_5[::-1, i] - P.V[i]) < 0.01).all())