예제 #1
0
def test_one_curve(frequency, interpolator):
    """1 Nodal Curve: 1 discount, used to produce discount and forward rates"""
    # Create the swap strip (target prices)
    market_portfolio = Portfolio.of_trades(
        strip_of_swaps(dt_settle, ccy, frequency, maturities, fixed_rates_annual))

    # Create market model / term structure
    crv_disc = DiscountCurveWithNodes(dt_val, node_dates,
                                      rates_guess,
                                      interpolator=interpolator,
                                      extrapolate=('clamped', 'clamped'))

    curve_map = {ccy: {'discount': crv_disc}}
    rates_market = RatesTermStructure.from_curve_map(dt_val, curve_map)

    # Calibrate market model to prices
    result = calibrate_rates(rates_market, market_portfolio)
    assert result.success

    # Test that it's worked by pricing swaps
    pv_portfolio = present_value(market_portfolio, rates_market, ccy)
    assert np.isclose(pv_portfolio, 0.0), ('Non-Zero PV after calibration is {}'
                                           .format(pv_portfolio))
    pv_trades = [present_value(trade, rates_market, ccy)
                 for trade in market_portfolio.trades]
    assert np.allclose(pv_trades, 0.0), "PVs after calibration are non-zero"
예제 #2
0
    def vector_pv(rates, market, portfolio):
        """Present Value of each asset in target portfolio

        Objective function for root finder.
        """
        update_rates(rates, market)
        return [present_value(x, market, 'USD') for x in portfolio.trades]
예제 #3
0
def test_trade_present_value():
    calculator = TradeCalculator(trade, market)
    pv_calc = calculator.present_value()
    assert isinstance(pv_calc, CurrencyAmount)
    assert pv_calc.currency == 'USD'
    assert np.allclose(pv_calc.amount, expected_contract_pv), \
        "calculated present value is not as expected."

    pv_dispatch = present_value(trade, market, ccy)
    assert np.isclose(pv_dispatch, pv_calc.amount)
예제 #4
0
def test_trade_present_value():
    calculator = TradeCalculator(trade, market)
    pv_calc = calculator.present_value()
    assert isinstance(pv_calc, CurrencyAmount)
    assert pv_calc.currency == 'USD'
    assert np.allclose(pv_calc.amount, expected_contract_pv), \
        "calculated present value is not as expected."

    pv_dispatch = present_value(trade, market, ccy)
    assert np.isclose(pv_dispatch, pv_calc.amount)
예제 #5
0
def test_trade_present_value_with_settlement_on_valuationdate():
    trade_w_settlement = trades.Trade(contract=bullet, settlement_dt=dt_val,
                                      settlement_amt=notional)
    calculator = TradeCalculator(trade_w_settlement, market)
    pv_calc = calculator.present_value()
    assert isinstance(pv_calc, CurrencyAmount)
    assert pv_calc.currency == 'USD'
    assert np.allclose(pv_calc.amount, expected_contract_pv + notional), \
        "calculated present value is not as expected."

    pv_dispatch = present_value(trade_w_settlement, market, ccy)
    assert np.isclose(pv_dispatch, pv_calc.amount)
예제 #6
0
def test_contract_present_value_with_rates_zero():
    interest = 0.00
    crv = ConstantDiscountRateCurve(dt_valuation=dt_val, zero_rate=interest)
    market = RatesTermStructure.of_single_curve(dt_val, crv)
    calculator = BulletPaymentCalculator(bullet, market)
    pv_calc = calculator.present_value()
    assert isinstance(pv_calc, CurrencyAmount)
    assert pv_calc.currency == 'USD'
    assert np.allclose(pv_calc.amount, notional), \
        "calculated present value is not as expected."

    pv_dispatch = present_value(bullet, market, ccy)
    assert np.isclose(pv_dispatch, pv_calc.amount)
예제 #7
0
def test_trade_present_value_with_settlement_on_valuationdate():
    trade_w_settlement = trades.Trade(contract=bullet,
                                      settlement_dt=dt_val,
                                      settlement_amt=notional)
    calculator = TradeCalculator(trade_w_settlement, market)
    pv_calc = calculator.present_value()
    assert isinstance(pv_calc, CurrencyAmount)
    assert pv_calc.currency == 'USD'
    assert np.allclose(pv_calc.amount, expected_contract_pv + notional), \
        "calculated present value is not as expected."

    pv_dispatch = present_value(trade_w_settlement, market, ccy)
    assert np.isclose(pv_dispatch, pv_calc.amount)
예제 #8
0
def test_contract_present_value_with_rates_zero():
    interest = 0.00
    crv = ConstantDiscountRateCurve(dt_valuation=dt_val, zero_rate=interest)
    market = RatesTermStructure.of_single_curve(dt_val, crv)
    calculator = BulletPaymentCalculator(bullet, market)
    pv_calc = calculator.present_value()
    assert isinstance(pv_calc, CurrencyAmount)
    assert pv_calc.currency == 'USD'
    assert np.allclose(pv_calc.amount, notional), \
        "calculated present value is not as expected."

    pv_dispatch = present_value(bullet, market, ccy)
    assert np.isclose(pv_dispatch, pv_calc.amount)
예제 #9
0
def test_two_curve(interpolator):
    """Calibrate Term Structure to strips of swaps of 2 frequencies

    The primary curve is used to produce forward rates for 1st frequency
    AND discount rates. This is typically chosen to be the smaller frequency.
    The secondary curve is used to produce forward rates for the 2nd frequency.
    """

    # Targets are 2 strips, Swaps paying quarterly, and ones paying annually
    market_portfolio_3s12s = Portfolio.of_trades(
        strip_of_swaps(dt_settle, ccy, freq3, maturities, fixed_rates_quarterly) +
        strip_of_swaps(dt_settle, ccy, freq12, maturities, fixed_rates_annual))

    # Create TermStructure model
    crv1 = DiscountCurveWithNodes(dt_val, node_dates, rates_guess,
                                  interpolator=interpolator,
                                  extrapolate=('clamped', 'clamped'))

    crv2 = DiscountCurveWithNodes(dt_val, node_dates, rates_guess,
                                  interpolator=interpolator,
                                  extrapolate=('clamped', 'natural'))

    curve_map = {ccy: {'discount': crv1, freq12: crv2}}

    rates_market = RatesTermStructure.from_curve_map(dt_val, curve_map)

    # Calibrate market model to prices
    result = calibrate_rates(rates_market, market_portfolio_3s12s)
    assert result.success

    # Test that it's worked by pricing swaps
    pv_portfolio = present_value(market_portfolio_3s12s, rates_market, ccy)
    assert np.isclose(pv_portfolio, 0.0), ('PV of market portfolio after '
                                           'calibration is non-zero')
    pv_trades = [present_value(trade, rates_market, ccy)
                 for trade in market_portfolio_3s12s.trades]
    assert np.allclose(pv_trades, 0.0), ("PVs of market portfolio's trades "
                                         "after calibration are non-zero")
예제 #10
0
    sens_fixed = sens_to_market_rates(fixed_legs[i], rates_market, rep_ccy)
    sens_swap = sens_to_market_rates(swaps[i], rates_market, rep_ccy)

    print('sens_fixed: {}'.format(sens_fixed))
    print('sens_float: {}'.format(sens_float))
    print('sens fixed+float: {}'.format(sens_float + sens_fixed))
    print('sens_swap: {}'.format(sens_swap))
    assert (np.allclose(sens_swap, sens_fixed + sens_float))

    # --------- Test Calibration -------------------------
    from time import time
    t = time()
    # Show the price of the target portfolio given the initial rate guess of 5%
    print('pv of portfolio before calibration: {}'.format(
        present_value(market_portfolio, rates_market, rep_ccy)))
    print('pv of each trade BEFORE: {}'.format([
        present_value(trade, rates_market, rep_ccy)
        for trade in market_portfolio.trades
    ]))

    # Calibrate market model to prices
    method = 'hybr'  # hybr, lm, broyden1, anderson
    options = None
    result = calibrate_rates(rates_market,
                             market_portfolio,
                             method=method,
                             options=options)

    print(result)
    print('discount rates: {}'.format(
예제 #11
0
def test_dispatch_present_value():
    pv_dispatch = present_value(bullet, market, ccy)
    calculator = BulletPaymentCalculator(bullet, market)
    pv_calc = calculator.present_value()
    assert np.isclose(pv_dispatch, pv_calc.amount)
예제 #12
0
def test_dispatch_present_value():
    pv_dispatch = present_value(bullet, market, ccy)
    calculator = BulletPaymentCalculator(bullet, market)
    pv_calc = calculator.present_value()
    assert np.isclose(pv_dispatch, pv_calc.amount)