Example #1
0
def test_swaption_price():

    kappa = 0.03

    swaption_expiry = 4
    swap_maturity = 5
    swap_freq = 0.5

    initial_curve = LiborCurve.from_constant_rate(0.06)
    swap_pricer = SwapPricer(initial_curve, kappa=kappa)

    sigma_r = LinearLocalVolatility.from_const(swap_maturity, 0.4, 0.1, 0)

    piterbarg_approx = PiterbargExpectationApproximator(sigma_r, swap_pricer)
    swap = Swap(swaption_expiry, swap_maturity, swap_freq)

    displaced_diffusion = DisplacedDiffusionParameterApproximator(sigma_r, swap_pricer, swap, piterbarg_approx)
    coupon = swap_pricer.price(swap, 0, 0, 0)

    swaption = Swaption(swaption_expiry, coupon, swap)

    b_s = displaced_diffusion.calculate_bs
    lambda_s_bar, b_bar = calculate_vola_skew(swaption.expiry, displaced_diffusion.calculate_lambda_square, b_s)

    swaption_value, black_implied_vola = lognormalimpliedvola(swaption, swap_pricer, lambda_s_bar, b_bar)

    print(lambda_s_bar, b_bar)
    print(swaption_value, black_implied_vola)
Example #2
0
def test_approx():
    kappa = 0.03

    initial_curve = LiborCurve.from_constant_rate(0.06)
    swap_pricer = SwapPricer(initial_curve, kappa=kappa)

    sigma_r = LinearLocalVolatility.from_const(15, 0.4, 0.06, 0.2)
    swaption_expiry = 4
    swap = Swap(swaption_expiry, 5, 0.5)

    coupon = swap_pricer.price(swap, 0, 0, 0)

    swaption = Swaption(swaption_expiry, coupon, swap)
    xyapproximator = RungeKuttaApproxXY
    #xyapproximator = PitergargDiscreteXY

    for xyapproximator in [RungeKuttaApproxXY, PitergargDiscreteXY]:
        for k in [16]:
            grid_size = 2**k + 1
            #xy_calculator = PitergargDiscreteXY(grid_size, swap_pricer, sigma_r, swap)
            xy_calculator = xyapproximator(grid_size, swap_pricer, sigma_r, swap)
            integration_approx = DiscreteParameterAveraging(grid_size, swap_pricer, sigma_r, swap, xy_calculator)
            lambda_avg, beta_avg = integration_approx.calculate_average_param()
            swaption_value, black_implied_vola = lognormalimpliedvola(swaption, swap_pricer, lambda_avg, beta_avg)

            print(lambda_avg, beta_avg)
            print(swaption_value, black_implied_vola)
Example #3
0
def test_runge_kutta_approx():

    kappa = 0.3
    t = 10

    initial_curve = LiborCurve.from_constant_rate(0.06)
    swap_pricer = SwapPricer(initial_curve, kappa=kappa)

    sigma_r = LinearLocalVolatility.from_const(t, 0.4, 0.1, 0)
    swap = Swap(4, 5, 0.5)

    swap0 = swap_pricer.price(swap, 0, 0, 0)

    integration_grid_size = 2*10 + 1
    rk_approx = RungeKuttaApproxXY(integration_grid_size, swap_pricer, sigma_r, swap)
    res = rk_approx.calculate_xy()

    piterbarg_approx = PiterbargExpectationApproximator(sigma_r, swap_pricer)

    time_grid = res.t
    xbar = []
    ybar = []
    for t in time_grid:
        ybar.append(piterbarg_approx.ybar_formula(t))
        xbar.append(piterbarg_approx.xbar_formula(t, ybar[-1], swap, swap0, 0))

    print("As")
Example #4
0
def test_swap_pricer():

    swap = Swap(1, 20, 0.25)
    initial_curve = LiborCurve.from_constant_rate(0.04)

    swap_pricer = SwapPricer(initial_curve, kappa=0.2)
    price = swap_pricer.price(swap, 0, 0, 0)

    print("pas")
Example #5
0
def test_forward_rate_pricer_from_constant_curve():

    bond = Bond(10)
    initial_curve = LiborCurve.from_constant_rate(0.04)
    bond_pricer = BondPricer(initial_curve, 0.2)

    expected_price = bond_pricer.price(bond, 0, 0, 0)
    actual_price = bond_pricer.calculate_price_from_forward(bond)

    np.testing.assert_approx_equal(expected_price, actual_price)
Example #6
0
def test_bond_pricer():

    rate = 0.04
    maturity = 5
    bond = Bond(maturity)
    initial_curve = LiborCurve.from_constant_rate(rate)
    bond_pricer = BondPricer(initial_curve, 0.2)
    actual_price = bond_pricer.price(bond, x=0, y=0, t=0)
    expected_price = np.exp(-rate * maturity)

    np.testing.assert_approx_equal(actual_price, expected_price)
Example #7
0
def test_forward_rate_pricer_from_file():

    bond = Bond(10)
    tmp_file = r"C:\Users\d80084\Google Drive\01oxford\7 Thesis\code\quasigaussian\data\market_data\libor_curve\usd_libor\sofr_curve.csv"
    initial_curve = LiborCurve.from_file(tmp_file, "2013-05-20")

    bond_pricer = BondPricer(initial_curve, 0.2)
    expected_price = bond_pricer.price(bond, 0, 0, 0)
    actual_price = bond_pricer.calculate_price_from_forward(bond)

    np.testing.assert_approx_equal(expected_price, actual_price)
Example #8
0
def test_piterbarg_y_bar():

    kappa = 0.001
    t = 15

    initial_curve = LiborCurve.from_constant_rate(0.06)
    swap_pricer = SwapPricer(initial_curve, kappa=kappa)

    sigma_r = LinearLocalVolatility.from_const(t, 0.1, 0.1, 0)
    piterbarg_approx = PiterbargExpectationApproximator(sigma_r, swap_pricer)

    y_bar_actual = piterbarg_approx.calculate_ybar(t)
    y_bar_expected = np.exp(-2*kappa*t)*(np.exp(2*kappa*t) - 1)/(2*kappa)*0.0001

    np.testing.assert_approx_equal(y_bar_actual, y_bar_expected)
Example #9
0
def test_linear_local_volatility_approximation():

    kappa = 0.001
    t = 15

    initial_curve = LiborCurve.from_constant_rate(0.06)
    swap_pricer = SwapPricer(initial_curve, kappa=kappa)

    sigma_r = LinearLocalVolatility.from_const(t, 0.1, 0.1, 0)

    piterbarg_approx = PiterbargExpectationApproximator(sigma_r, swap_pricer)
    swap = Swap(4, 5, 0.5)

    displaced_diffusion = DisplacedDiffusionParameterApproximator(sigma_r, swap_pricer, swap, piterbarg_approx)

    pass
Example #10
0
def test_piterbarg_ksi():

    kappa = 0.001
    t = 15

    initial_curve = LiborCurve.from_constant_rate(0.06)
    swap_pricer = SwapPricer(initial_curve, kappa=kappa)

    sigma_r = LinearLocalVolatility.from_const(t, 0.5, 0.5, 0.2)

    piterbarg_approx = PiterbargExpectationApproximator(sigma_r, swap_pricer)

    swap = Swap(4, 5, 0.5)
    swap_value = swap_pricer.price(swap, 0.01717964, 0.0025, 0.04)
    ksi = piterbarg_approx.calculate_ksi(0.04, swap_value, swap)

    np.testing.assert_approx_equal(ksi, 0.01717964, significant=4)
Example #11
0
def test_black_swaption_pricer():
    # see John Hull ex 28.4, page 661

    kappa = 0.3
    swap = Swap(5, 8, 0.5)
    initial_curve = LiborCurve.from_constant_rate(0.06)

    bond_pricer = BondPricer(initial_curve, kappa=kappa)
    swap_pricer = SwapPricer(initial_curve, kappa=kappa)

    coupon = 0.062
    swaption = Swaption(expiry=5, coupon=coupon, swap=swap)

    swaption_pricer = Black76Pricer(lambda_s=0.2,
                                    b_s=1,
                                    swap_pricer=swap_pricer,
                                    bond_pricer=bond_pricer)
    swaption_value = 100 * swaption_pricer.black76_price(swaption)

    np.testing.assert_approx_equal(swaption_value, 2.07, significant=4)
Example #12
0
def test_implied_black_vola():

    kappa = 0.3
    swap = Swap(5, 8, 0.5)
    initial_curve = LiborCurve.from_constant_rate(0.06)

    bond_pricer = BondPricer(initial_curve, kappa=kappa)
    swap_pricer = SwapPricer(initial_curve, kappa=kappa)

    coupon = 0.062
    swaption = Swaption(expiry=5, coupon=coupon, swap=swap)

    swaption_pricer = Black76Pricer(lambda_s=0.2,
                                    b_s=1,
                                    swap_pricer=swap_pricer,
                                    bond_pricer=bond_pricer)
    swaption_value = swaption_pricer.black76_price(swaption)

    implied_vola = find_implied_black_vola(swaption_value, swaption,
                                           swap_pricer, bond_pricer)

    np.testing.assert_approx_equal(implied_vola, 0.2, significant=4)
def process_bond_fd(input_file):

    output_fd = pd.read_hdf(input_file, key="data")

    meta_data = pd.read_hdf(input_file, key="metadata").iloc[0]
    xmesh = pd.read_hdf(input_file, key="xmesh")
    ymesh = pd.read_hdf(input_file, key="ymesh")

    xgrid = pd.read_hdf(input_file, key="xgrid")
    ugrid = pd.read_hdf(input_file, key="ygrid")

    bond = Bond(meta_data["maturity"])
    curve = LiborCurve.from_constant_rate(meta_data["curve_rate"])
    bond_pricer = BondPricer(curve, meta_data["kappa"])
    bond_value = bond_pricer.price(bond, xmesh, ymesh, 0)

    #relative_diff = (output_fd - bond_value)/bond_value
    analytics_bond = extract_x0_result(bond_value.values,
                                       np.array(xgrid[0].values),
                                       ugrid[0].values)
    fd_bond = extract_x0_result(output_fd.values, np.array(xgrid[0].values),
                                ugrid[0].values)

    return fd_bond, analytics_bond, output_fd, bond_value, meta_data, xgrid, ugrid
Example #14
0
def get_mock_yield_curve_const(rate=0.04):
    return LiborCurve.from_constant_rate(rate)
Example #15
0
def get_mock_yield_curve_from_file():
    tmp_file = r"C:\Users\d80084\Google Drive\01oxford\7 Thesis\code\quasigaussian\data\market_data\libor_curve\usd_libor\sofr_curve.csv"
    initial_curve = LiborCurve.from_file(tmp_file, "2013-05-20")
    return initial_curve
def calculate_swaption_prices():

    output_path = os.path.join(output_data_raw_approx, date_timestamp,
                               "result")
    output_file = os.path.join(output_path, "swaption_approximation.hdf")
    file_path = get_nonexistant_path(output_file)

    grid_size = 2**12 + 1
    swap_freq = 0.5
    curve_rate = 0.06

    initial_curve = LiborCurve.from_constant_rate(curve_rate)

    kappa_grid = [0.03]

    vola_parameters = [(i, curve_rate, j) for i in [0.6, 0.8]
                       for j in [0.05, 0.2]]
    vola_grid_df = pd.DataFrame(vola_parameters,
                                columns=["lambda", "alpha", "beta"])
    vola_grid_df = vola_grid_df.iloc[[0, 3]]

    #coupon_grid = [0, +0.0025, -0.0025, +0.005, -0.005, +0.01, -0.01, 0.015, -0.015, 0.02, -0.02, 0.025, -0.025]

    XYApproximator = PitergargDiscreteXY
    XYApproximator = RungeKuttaApproxXY

    swap_ls = [(20, 21)]
    coupon_grid = [0, +0.005, -0.005, +0.01, -0.01, 0.015, -0.015]
    #vola_grid_df = vola_grid_df.iloc[9:10]

    for swap_T0_TN in swap_ls:
        print(swap_T0_TN)
        T0, TN = swap_T0_TN
        for kappa in kappa_grid:
            swap_pricer = SwapPricer(initial_curve, kappa)
            swap = Swap(T0, TN, 1 / 2)
            for index, vola_grid_row in vola_grid_df.iterrows():
                sigma_r = LinearLocalVolatility.from_const(
                    swap.TN, vola_grid_row["lambda"], vola_grid_row["alpha"],
                    vola_grid_row["beta"])

                swap = Swap(T0, TN, swap_freq)

                atm_swap_price = swap_pricer.price(swap, 0, 0, 0)
                strike_grid = [
                    atm_swap_price + coupon for coupon in coupon_grid
                ]
                #strike_grid = [0.01, 0.015, 0.02, 0.025, 0.03]

                xy_calculator = XYApproximator(grid_size, swap_pricer, sigma_r,
                                               swap)
                integration_approx = DiscreteParameterAveraging(
                    grid_size, swap_pricer, sigma_r, swap, xy_calculator)
                lambda_avg, beta_avg = integration_approx.calculate_average_param(
                )

                for strike in strike_grid:
                    swaption = Swaption(T0, strike, swap)
                    swaption_value, black_implied_vola = lognormalimpliedvola(
                        swaption, swap_pricer, lambda_avg, beta_avg)

                    output_data = pd.DataFrame({
                        'expiry': [T0],
                        "maturity": [TN],
                        "atm strike":
                        atm_swap_price,
                        "swaption_value": [swaption_value],
                        "kappa": [kappa],
                        "vola_lambda": [vola_grid_row["lambda"]],
                        "vola_alpha": [vola_grid_row["alpha"]],
                        "vola_beta": [vola_grid_row['beta']],
                        "strike": [strike],
                        'moneyness': [strike - atm_swap_price],
                        "curve_rate": [curve_rate],
                        "implied_black_vola": [black_implied_vola],
                        'integration_grid': [grid_size],
                        "xy_approximation": [str(xy_calculator)]
                    })
                    try:
                        all_output_data = pd.read_hdf(file_path, key="data")
                    except:
                        all_output_data = pd.DataFrame()
                    all_output_data = pd.concat([all_output_data, output_data])
                    all_output_data.to_hdf(file_path, key="data", complevel=9)