Ejemplo n.º 1
0
    def bootstrap_term_structure(self, interpolator=LogLinear):
        tolerance = 1.0e-15
        settings = Settings()
        calendar = JointCalendar(UnitedStates(), UnitedKingdom())
        # must be a business day
        eval_date = self._eval_date
        settings.evaluation_date = eval_date
        settlement_days = self._params.settlement_days
        settlement_date = calendar.advance(eval_date, settlement_days, Days)
        # must be a business day
        settlement_date = calendar.adjust(settlement_date)
        ts = PiecewiseYieldCurve[BootstrapTrait.Discount,
                                 interpolator].from_reference_date(
                                     settlement_date,
                                     self._rate_helpers,
                                     DayCounter.from_name(
                                         self._termstructure_daycount),
                                     accuracy=tolerance)
        self._term_structure = ts
        self._discount_term_structure = YieldTermStructure()
        self._discount_term_structure.link_to(ts)

        self._forecast_term_structure = YieldTermStructure()
        self._forecast_term_structure.link_to(ts)

        return ts
Ejemplo n.º 2
0
    def _set_evaluation_date(self, dt_obs):

        if not isinstance(dt_obs, Date):
            dt_obs = pydate_to_qldate(dt_obs)

        settings = Settings()
        calendar = JointCalendar(UnitedStates(), UnitedKingdom())
        # must be a business day
        eval_date = calendar.adjust(dt_obs)
        settings.evaluation_date = eval_date
        self._eval_date = eval_date
        return eval_date
Ejemplo n.º 3
0
def make_rate_helper(label, rate, dt_obs, currency='USD'):
    """
    Wrapper for deposit and swaps rate helpers makers
    For Swaps: assume USD swap fixed rates vs. 6M Libor
    TODO: make this more general
    """

    if(currency.upper() != 'USD'):
        raise Exception("Only supported currency is USD.")

    rate_type, tenor, period = _parse_rate_label(label)

    if not isinstance(dt_obs, Date):
        dt_obs = pydate_to_qldate(dt_obs)
    settings = Settings()
    calendar = JointCalendar(UnitedStates(), UnitedKingdom())
    # must be a business day
    eval_date = calendar.adjust(dt_obs)
    settings.evaluation_date = eval_date
    settlement_days = 2
    settlement_date = calendar.advance(eval_date, settlement_days, Days)
    # must be a business day
    settlement_date = calendar.adjust(settlement_date)
    end_of_month = True

    if((rate_type == 'SWAP') & (period == 'Y')):
        liborIndex = Libor(
            'USD Libor', Period(6, Months), settlement_days,
            USDCurrency(), calendar, Actual360()
        )
        spread = SimpleQuote(0)
        fwdStart = Period(0, Days)
        helper = SwapRateHelper.from_tenor(rate,
                 Period(tenor, Years),
                 calendar, Annual,
                 Unadjusted, Thirty360(),
                 liborIndex, spread, fwdStart)
    elif((rate_type == 'LIBOR') & (period == 'M')):
        helper = DepositRateHelper(rate, Period(tenor, Months),
                 settlement_days,
                 calendar, ModifiedFollowing,
                 end_of_month,
                 Actual360())
    else:
        raise Exception("Rate type %s not supported" % label)

    return (helper)
Ejemplo n.º 4
0
    def bootstrap_term_structure(self, interpolator='loglinear'):
        tolerance = 1.0e-15
        settings = Settings()
        calendar = JointCalendar(UnitedStates(), UnitedKingdom())
        # must be a business day
        eval_date = self._eval_date
        settings.evaluation_date = eval_date
        settlement_days = self._params.settlement_days
        settlement_date = calendar.advance(eval_date, settlement_days, Days)
        # must be a business day
        settlement_date = calendar.adjust(settlement_date)
        ts = PiecewiseYieldCurve(
            'discount', interpolator, settlement_date, self._rate_helpers,
            DayCounter.from_name(self._termstructure_daycount), tolerance)
        self._term_structure = ts
        self._discount_term_structure = YieldTermStructure(relinkable=True)
        self._discount_term_structure.link_to(ts)

        self._forecast_term_structure = YieldTermStructure(relinkable=True)
        self._forecast_term_structure.link_to(ts)

        return ts
Ejemplo n.º 5
0
def get_term_structure(df_libor, dtObs):

    settings = Settings()

    # libor as fixed in London, but cash-flows are determined according to
    # US calendar, hence the need to combine both holidays lists
    calendar = JointCalendar(UnitedStates(), UnitedKingdom())

    # must be a business day
    eval_date = calendar.adjust(dateToDate(dtObs))
    settings.evaluation_date = eval_date

    settlement_days = 2
    settlement_date = calendar.advance(eval_date, settlement_days, Days)
    # must be a business day
    settlement_date = calendar.adjust(settlement_date)

    depositData = [[1, Months, 'Libor1M'], [3, Months, 'Libor3M'],
                   [6, Months, 'Libor6M']]

    swapData = [[1, Years, 'Swap1Y'], [2, Years,
                                       'Swap2Y'], [3, Years, 'Swap3Y'],
                [4, Years, 'Swap4Y'], [5, Years, 'Swap5Y'],
                [7, Years, 'Swap7Y'], [10, Years, 'Swap10Y'],
                [30, Years, 'Swap30Y']]

    rate_helpers = []

    end_of_month = True

    for m, period, label in depositData:
        tenor = Period(m, Months)
        rate = df_libor.get_value(dtObs, label)
        helper = DepositRateHelper(float(rate / 100), tenor, settlement_days,
                                   calendar, ModifiedFollowing, end_of_month,
                                   Actual360())

        rate_helpers.append(helper)

    endOfMonth = True

    liborIndex = Libor('USD Libor', Period(6, Months), settlement_days,
                       USDCurrency(), calendar, Actual360())

    spread = SimpleQuote(0)
    fwdStart = Period(0, Days)

    for m, period, label in swapData:
        rate = df_libor.get_value(dtObs, label)
        helper = SwapRateHelper.from_tenor(rate / 100., Period(m, Years),
                                           calendar, Annual, Unadjusted,
                                           Thirty360(), liborIndex, spread,
                                           fwdStart)

        rate_helpers.append(helper)

    ts_day_counter = ActualActual(ISDA)
    tolerance = 1.0e-15

    ts = term_structure_factory('discount', 'loglinear', settlement_date,
                                rate_helpers, ts_day_counter, tolerance)

    return ts
Ejemplo n.º 6
0
    def setUp(self):
        self.calendar = UnitedKingdom()
        today = Date(25, 11, 2009)
        evaluation_date = self.calendar.adjust(today)
        Settings().evaluation_date = evaluation_date
        day_counter = ActualActual()

        rpi_schedule = Schedule.from_rule(Date(20, 7,
                                               2007), Date(20, 11, 2009),
                                          Period(1, Months), self.calendar,
                                          ModifiedFollowing)
        self.cpi_ts = ZeroInflationTermStructure()
        self.yts = FlatForward(evaluation_date, 0.05, day_counter)
        self.ii = UKRPI(False, self.cpi_ts)
        fix_data = [
            206.1, 207.3, 208.0, 208.9, 209.7, 210.9, 209.8, 211.4, 212.1,
            214.0, 215.1, 216.8, 216.5, 217.2, 218.4, 217.7, 216, 212.9, 210.1,
            211.4, 211.3, 211.5, 212.8, 213.4, 213.4, 213.4, 214.4
        ]

        for date, data in zip(rpi_schedule, fix_data):
            self.ii.add_fixing(date, data)

        dates = [
            Date(25, 11, 2010),
            Date(25, 11, 2011),
            Date(26, 11, 2012),
            Date(25, 11, 2013),
            Date(25, 11, 2014),
            Date(25, 11, 2015),
            Date(25, 11, 2016),
            Date(25, 11, 2017),
            Date(25, 11, 2018),
            Date(25, 11, 2019),
            Date(25, 11, 2021),
            Date(25, 11, 2024),
            Date(26, 11, 2029),
            Date(27, 11, 2034),
            Date(25, 11, 2039),
            Date(25, 11, 2049),
            Date(25, 11, 2059)
        ]

        rates = [
            3.0495, 2.93, 2.9795, 3.029, 3.1425, 3.211, 3.2675, 3.3625, 3.405,
            3.48, 3.576, 3.649, 3.751, 3.77225, 3.77, 3.734, 3.714
        ]

        observation_lag = Period('2M')
        self.helpers = [ZeroCouponInflationSwapHelper(
            SimpleQuote(r / 100),
            observation_lag,
            maturity, self.calendar, ModifiedFollowing, day_counter, self.ii, self.yts) \
                        for maturity, r in zip(dates, rates)]
        base_zero_rate = rates[0] / 100

        self.cpi_ts.link_to(
            PiecewiseZeroInflationCurve(Interpolator.Linear, evaluation_date,
                                        self.calendar, day_counter,
                                        observation_lag, self.ii.frequency,
                                        self.ii.interpolated, base_zero_rate,
                                        self.helpers))
Ejemplo n.º 7
0
class TestCPIBond(unittest.TestCase):
    def setUp(self):
        self.calendar = UnitedKingdom()
        today = Date(25, 11, 2009)
        evaluation_date = self.calendar.adjust(today)
        Settings().evaluation_date = evaluation_date
        day_counter = ActualActual()

        rpi_schedule = Schedule.from_rule(Date(20, 7,
                                               2007), Date(20, 11, 2009),
                                          Period(1, Months), self.calendar,
                                          ModifiedFollowing)
        self.cpi_ts = ZeroInflationTermStructure()
        self.yts = FlatForward(evaluation_date, 0.05, day_counter)
        self.ii = UKRPI(False, self.cpi_ts)
        fix_data = [
            206.1, 207.3, 208.0, 208.9, 209.7, 210.9, 209.8, 211.4, 212.1,
            214.0, 215.1, 216.8, 216.5, 217.2, 218.4, 217.7, 216, 212.9, 210.1,
            211.4, 211.3, 211.5, 212.8, 213.4, 213.4, 213.4, 214.4
        ]

        for date, data in zip(rpi_schedule, fix_data):
            self.ii.add_fixing(date, data)

        dates = [
            Date(25, 11, 2010),
            Date(25, 11, 2011),
            Date(26, 11, 2012),
            Date(25, 11, 2013),
            Date(25, 11, 2014),
            Date(25, 11, 2015),
            Date(25, 11, 2016),
            Date(25, 11, 2017),
            Date(25, 11, 2018),
            Date(25, 11, 2019),
            Date(25, 11, 2021),
            Date(25, 11, 2024),
            Date(26, 11, 2029),
            Date(27, 11, 2034),
            Date(25, 11, 2039),
            Date(25, 11, 2049),
            Date(25, 11, 2059)
        ]

        rates = [
            3.0495, 2.93, 2.9795, 3.029, 3.1425, 3.211, 3.2675, 3.3625, 3.405,
            3.48, 3.576, 3.649, 3.751, 3.77225, 3.77, 3.734, 3.714
        ]

        observation_lag = Period('2M')
        self.helpers = [ZeroCouponInflationSwapHelper(
            SimpleQuote(r / 100),
            observation_lag,
            maturity, self.calendar, ModifiedFollowing, day_counter, self.ii, self.yts) \
                        for maturity, r in zip(dates, rates)]
        base_zero_rate = rates[0] / 100

        self.cpi_ts.link_to(
            PiecewiseZeroInflationCurve(Interpolator.Linear, evaluation_date,
                                        self.calendar, day_counter,
                                        observation_lag, self.ii.frequency,
                                        self.ii.interpolated, base_zero_rate,
                                        self.helpers))

    def test_clean_price(self):
        notional = 1000000.0
        fixed_rates = [0.1]
        fixed_day_count = Actual365Fixed()
        fixed_calendar = self.calendar
        fixed_index = self.ii
        contractObservationLag = Period(3, Months)
        observationInterpolation = InterpolationType.Flat
        settlement_days = 3
        growth_only = True

        baseCPI = 206.1

        fixed_schedule = Schedule.from_rule(Date(2, 10,
                                                 2007), Date(2, 10, 2052),
                                            Period(6, Months), fixed_calendar,
                                            Unadjusted, Rule.Backward)

        cpi_bond = CPIBond(settlement_days, notional, growth_only, baseCPI,
                           contractObservationLag, fixed_index,
                           observationInterpolation, fixed_schedule,
                           fixed_rates, fixed_day_count, ModifiedFollowing)

        engine = DiscountingBondEngine(self.yts)
        cpi_bond.set_pricing_engine(engine)
        set_coupon_pricer(cpi_bond.cashflows, CPICouponPricer(self.yts))
        storedPrice = 383.01816406
        calculated = cpi_bond.clean_price
        self.assertAlmostEqual(storedPrice, calculated)
Ejemplo n.º 8
0
    def setUp(self):
        self.calendar = UnitedKingdom()
        today = Date(25, 11, 2009)
        evaluation_date = self.calendar.adjust(today)
        Settings().evaluation_date = evaluation_date
        day_counter = ActualActual()

        rpi_schedule = Schedule.from_rule(Date(20, 7, 2007),
                                          Date(20, 11, 2009),
                                          Period(1, Months),
                                          self.calendar,
                                          ModifiedFollowing)
        self.cpi_ts = ZeroInflationTermStructure()
        self.yts = FlatForward(evaluation_date, 0.05, day_counter)
        self.ii = UKRPI(False, self.cpi_ts)
        fix_data = [206.1, 207.3, 208.0, 208.9, 209.7, 210.9,
                    209.8, 211.4, 212.1, 214.0, 215.1, 216.8,
                    216.5, 217.2, 218.4, 217.7, 216,
                    212.9, 210.1, 211.4, 211.3, 211.5,
                    212.8, 213.4, 213.4, 213.4, 214.4]

        for date, data in zip(rpi_schedule, fix_data):
            self.ii.add_fixing(date, data)

        dates = [Date(25, 11, 2010),
                 Date(25, 11, 2011),
                 Date(26, 11, 2012),
                 Date(25, 11, 2013),
                 Date(25, 11, 2014),
                 Date(25, 11, 2015),
                 Date(25, 11, 2016),
                 Date(25, 11, 2017),
                 Date(25, 11, 2018),
                 Date(25, 11, 2019),
                 Date(25, 11, 2021),
                 Date(25, 11, 2024),
                 Date(26, 11, 2029),
                 Date(27, 11, 2034),
                 Date(25, 11, 2039),
                 Date(25, 11, 2049),
                 Date(25, 11, 2059)]

        rates = [3.0495,
                 2.93,
                 2.9795,
                 3.029,
                 3.1425,
                 3.211,
                 3.2675,
                 3.3625,
                 3.405,
                 3.48,
                 3.576,
                 3.649,
                 3.751,
                 3.77225,
                 3.77,
                 3.734,
                 3.714]

        observation_lag = Period('2M')
        self.helpers = [ZeroCouponInflationSwapHelper(
            SimpleQuote(r / 100),
            observation_lag,
            maturity, self.calendar, ModifiedFollowing, day_counter, self.ii) \
                        for maturity, r in zip(dates, rates)]
        base_zero_rate = rates[0] / 100

        self.cpi_ts.link_to(
            PiecewiseZeroInflationCurve(Interpolator.Linear,
                                        evaluation_date, self.calendar, day_counter,
                                        observation_lag, self.ii.frequency, self.ii.interpolated,
                                        base_zero_rate, self.yts, self.helpers))
Ejemplo n.º 9
0
class TestCPIBond(unittest.TestCase):
    def setUp(self):
        self.calendar = UnitedKingdom()
        today = Date(25, 11, 2009)
        evaluation_date = self.calendar.adjust(today)
        Settings().evaluation_date = evaluation_date
        day_counter = ActualActual()

        rpi_schedule = Schedule.from_rule(Date(20, 7, 2007),
                                          Date(20, 11, 2009),
                                          Period(1, Months),
                                          self.calendar,
                                          ModifiedFollowing)
        self.cpi_ts = ZeroInflationTermStructure()
        self.yts = FlatForward(evaluation_date, 0.05, day_counter)
        self.ii = UKRPI(False, self.cpi_ts)
        fix_data = [206.1, 207.3, 208.0, 208.9, 209.7, 210.9,
                    209.8, 211.4, 212.1, 214.0, 215.1, 216.8,
                    216.5, 217.2, 218.4, 217.7, 216,
                    212.9, 210.1, 211.4, 211.3, 211.5,
                    212.8, 213.4, 213.4, 213.4, 214.4]

        for date, data in zip(rpi_schedule, fix_data):
            self.ii.add_fixing(date, data)

        dates = [Date(25, 11, 2010),
                 Date(25, 11, 2011),
                 Date(26, 11, 2012),
                 Date(25, 11, 2013),
                 Date(25, 11, 2014),
                 Date(25, 11, 2015),
                 Date(25, 11, 2016),
                 Date(25, 11, 2017),
                 Date(25, 11, 2018),
                 Date(25, 11, 2019),
                 Date(25, 11, 2021),
                 Date(25, 11, 2024),
                 Date(26, 11, 2029),
                 Date(27, 11, 2034),
                 Date(25, 11, 2039),
                 Date(25, 11, 2049),
                 Date(25, 11, 2059)]

        rates = [3.0495,
                 2.93,
                 2.9795,
                 3.029,
                 3.1425,
                 3.211,
                 3.2675,
                 3.3625,
                 3.405,
                 3.48,
                 3.576,
                 3.649,
                 3.751,
                 3.77225,
                 3.77,
                 3.734,
                 3.714]

        observation_lag = Period('2M')
        self.helpers = [ZeroCouponInflationSwapHelper(
            SimpleQuote(r / 100),
            observation_lag,
            maturity, self.calendar, ModifiedFollowing, day_counter, self.ii) \
                        for maturity, r in zip(dates, rates)]
        base_zero_rate = rates[0] / 100

        self.cpi_ts.link_to(
            PiecewiseZeroInflationCurve(Interpolator.Linear,
                                        evaluation_date, self.calendar, day_counter,
                                        observation_lag, self.ii.frequency, self.ii.interpolated,
                                        base_zero_rate, self.yts, self.helpers))

    def test_clean_price(self):
        notional = 1000000.0;
        fixed_rates = [0.1]
        fixed_day_count = Actual365Fixed()
        fixed_calendar = self.calendar
        fixed_index = self.ii
        contractObservationLag = Period(3, Months);
        observationInterpolation = InterpolationType.Flat;
        settlement_days = 3
        growth_only = True

        baseCPI = 206.1

        fixed_schedule = Schedule.from_rule(Date(2, 10, 2007),
                                            Date(2, 10, 2052),
                                            Period(6, Months),
                                            fixed_calendar,
                                            Unadjusted,
                                            Rule.Backward)

        cpi_bond = CPIBond(settlement_days, notional, growth_only,
                           baseCPI, contractObservationLag, fixed_index,
                           observationInterpolation, fixed_schedule,
                           fixed_rates, fixed_day_count, ModifiedFollowing)

        engine = DiscountingBondEngine(self.yts)
        cpi_bond.set_pricing_engine(engine)

        storedPrice = 383.01816406
        calculated = cpi_bond.clean_price
        self.assertAlmostEqual(storedPrice, calculated)