Ejemplo n.º 1
0
    def test_price_importance_sampling(self):
        strike = 80
        asset_num = 1
        init_price_vec = 50 * np.ones(asset_num)
        vol_vec = 0.2 * np.ones(asset_num)
        ir = 0.03
        dividend_vec = np.zeros(asset_num)
        corr_mat = np.eye(asset_num)
        time_to_maturity = 1
        random_walk = GBM(time_to_maturity, 100, init_price_vec, ir, vol_vec,
                          dividend_vec, corr_mat)
        analytical2 = Analytical_Sol(init_price_vec[0],
                                     strike,
                                     time_to_maturity,
                                     ir,
                                     vol_vec[0],
                                     dividend_yield=0)

        def test_payoff(*l):
            return max(np.sum(l) - strike, 0)

        test_payoff.strike = strike

        opt2 = Euro(test_payoff, random_walk)
        real_call, _ = analytical2.european_option_price()
        np.random.seed(1)
        approx_call = opt2.price_importance_sampling(10000)
        np.random.seed(1)
        weak_approx_call = opt2.priceV2(10000)
        assert abs(approx_call - 0.06082838151186516) < 0.00000000000001
        assert abs(approx_call - real_call) / real_call < 0.00297664353761824
        assert abs(weak_approx_call -
                   real_call) / real_call < 0.1362349660567213
Ejemplo n.º 2
0
    def test_price1d_control_variates(self):
        strike = 45
        asset_num = 1
        init_price_vec = 50 * np.ones(asset_num)
        vol_vec = 0.3 * np.ones(asset_num)
        ir = 0.05
        dividend_vec = np.zeros(asset_num)
        corr_mat = np.eye(asset_num)
        time_to_maturity = 0.25
        random_walk = GBM(time_to_maturity, 100, init_price_vec, ir, vol_vec,
                          dividend_vec, corr_mat)
        analytical2 = Analytical_Sol(init_price_vec[0],
                                     strike,
                                     time_to_maturity,
                                     ir,
                                     vol_vec[0],
                                     dividend_yield=0)

        def test_payoff(*l):
            return max(np.sum(l) - strike, 0)

        opt2 = Euro(test_payoff, random_walk)

        real_call, _ = analytical2.european_option_price()
        np.random.seed(1)
        approx_call = opt2.price1d_control_variates(1000)
        np.random.seed(1)
        approx_call2 = opt2.price(1000)
        assert abs(approx_call - 6.412754547048265) < 0.00000000000001
        assert abs(approx_call - real_call) / real_call < 0.0025422
        assert abs(abs(approx_call2 - real_call) / real_call) < 0.04899
Ejemplo n.º 3
0
    def setUp(self):
        strike = 100

        asset_num = 1
        init_price_vec = 100 * np.ones(asset_num)
        vol_vec = 0.1 * np.ones(asset_num)
        ir = 0.03
        dividend_vec = np.zeros(asset_num)
        corr_mat = np.eye(asset_num)
        random_walk = GBM(1, 400, init_price_vec, ir, vol_vec, dividend_vec,
                          corr_mat)

        def test_payoff(*l):
            return max(strike - np.sum(l), 0)

        self.opt1 = Euro(test_payoff, random_walk)

        spot_price = init_price_vec[0]
        time_to_maturity = 1
        interest_rate = 0.03
        sigma = 0.1
        self.analytical1 = Analytical_Sol(spot_price,
                                          strike,
                                          time_to_maturity,
                                          interest_rate,
                                          sigma,
                                          dividend_yield=0)
Ejemplo n.º 4
0
 def test_Euro1d(self):
     T = 1
     domain = Domain1d(0, 6, T)
     vol, ir, dividend, strike = 0.1, 0.03, 0.01, 1
     solver = Euro1d(domain, vol, ir, dividend, strike, CallPutType.PUT)
     spot = 1
     solver.solve(400, 200)
     approx_put = solver.evaluate(spot, T)
     analytical = Analytical_Sol(spot, strike, T, ir, vol, dividend_yield=dividend)
     _, real_put = analytical.european_option_price()
     assert abs(approx_put-0.030050214069580493) < 0.00000000000001
     assert abs(approx_put-real_put)/real_put < 0.00054
Ejemplo n.º 5
0
 def restore_helper1d(self, t, nS, model_name, a, b, T, vol, ir, dividend,
                      strike, cpType):
     domain = Domain1d(a, b, T)
     solver = Euro1d(domain, vol, ir, dividend, strike, cpType)
     Sanaly, S = np.linspace(domain.a, domain.b,
                             nS), np.linspace(domain.a, domain.b,
                                              nS).reshape(-1, 1)
     fitted = solver.restore(S, t * np.ones_like(S), model_name)
     real_call, real_put = Analytical_Sol(
         Sanaly, strike, T - t, ir, vol,
         dividend_yield=dividend).european_option_price()
     if cpType == CallPutType.PUT:
         real = real_put
     elif cpType == CallPutType.CALL:
         real = real_call
     diff = abs(fitted - real)
     plt.plot(Sanaly, real, alpha=0.7, label="Real")
     plt.plot(Sanaly, fitted[0], alpha=0.7, label="Approx")
     print("error: {}; max error:{}; mean error: {}".format(
         diff, np.max(diff), np.mean(diff)))
     plt.legend()
     plt.show()