示例#1
0
    def _rebuild_object(self):
        if self._is_modified_property(
                '_calendar_') and not self._is_modified_property('_spot_'):
            self._spot_ = BusinessPeriod(businessdays=self._spot_.businessdays,
                                         holiday=self._calendar_)

        x_list = list()
        # carefully cast x_list/curve dates
        for x in self._curve_.row_keys():
            if BusinessDate.is_businessdate(x):
                x = BusinessDate(x)
            elif BusinessPeriod.is_businessperiod(x):
                x = BusinessPeriod(x).to_businessdate(self.origin)
            else:
                s = repr(x), type(
                    x), BusinessDate.__name__, self.__class__.__name__
                raise ValueError(
                    'Cannot cast value %s of type %s to %s in %s construction.'
                    % s)
            x_list.append(x)

        y_list = list(map(float, self._curve_.col('Rate')))
        y_inter = Constant(), self._interpolation_, self._extrapolation_
        if self._rate_type_ == "Zero":
            self._inner_curve = ZeroRateCurve(x_list,
                                              y_list,
                                              y_inter,
                                              self.origin,
                                              day_count=self._day_count_)
        else:
            msg = "_curve_ " + self.object_name + ": RateType " + self._rate_type_ + " not implemented, yet."
            raise NotImplementedError(msg)

        self._curve_df_dict = dict()
        return self
示例#2
0
    def test_diff(self):
        d1 = BusinessDate.from_ymd(2016, 1, 31)
        d2 = BusinessDate.from_ymd(2017, 11, 1)

        y, m, d = BusinessDate.diff_in_ymd(d1, d2)
        diff = BusinessPeriod(years=y, months=m, days=d)
        self.assertEqual('1Y9M1D', str(diff))

        d1 = BusinessDate.from_ymd(2016, 2, 29)
        d2 = BusinessDate.from_ymd(2017, 3, 1)

        y, m, d = BusinessDate.diff_in_ymd(d1, d2)
        diff = BusinessPeriod(years=y, months=m, days=d)
        self.assertEqual('1Y1D', str(diff))

        d2 = BusinessDate.from_ymd(2017, 2, 28)

        y, m, d = BusinessDate.diff_in_ymd(d1, d2)
        diff = BusinessPeriod(years=y, months=m, days=d)
        self.assertEqual('11M30D', str(diff))

        d1 = BusinessDate.from_ymd(2016, 11, 15)
        d2 = BusinessDate.from_ymd(2017, 1, 15)

        y, m, d = BusinessDate.diff_in_ymd(d1, d2)
        diff = BusinessPeriod(years=y, months=m, days=d)
        self.assertEqual('2M', str(diff))

        d1 = BusinessDate.from_ymd(2015, 7, 31)
        d2 = BusinessDate.from_ymd(2017, 2, 20)

        y, m, d = BusinessDate.diff_in_ymd(d1, d2)
        diff = BusinessPeriod(years=y, months=m, days=d)
        self.assertEqual('1Y6M20D', str(diff))
示例#3
0
    def test_more_calculations(self):

        periods = list()
        for y in range(5):
            for m in range(13):
                for d in list(range(5)) + list(range(25, 33)) + list(
                        range(58, 66)):
                    periods.append(
                        BusinessPeriod(
                            str(y) + 'y' + str(m) + 'm' + str(d) + 'd'))

        for d in self.dates:
            for p in periods:
                dp = d + p
                q = dp - d
                dq = d + q
                if d.day < 28 and p.days < 28:
                    self.assertEqual(q, p, (q, d, p, dp))

                # only idempotent pairs work always (e.g. above)
                self.assertEqual(dq, dp, (dq, d, p, dp, q))
                self.assertEqual((dq - d), q, (dq - d, d, q, dq))

        a = BusinessDate('20150228')
        for y in range(3):
            for m in range(0, 7, 5):
                for d in range(0, 13, 5):
                    b = a + BusinessPeriod(years=y, months=m, days=d)
                    self.assertEqual(-b._diff_in_days(a), a._diff_in_days(b))
示例#4
0
 def test_wrapper_methods(self):
     p = BusinessPeriod(years=1, months=1, days=1)
     self.assertEqual(p.add_years(p.years).add_months(p.months).add_days(p.days),
                      BusinessPeriod(years=2, months=2, days=2))
     self.assertEqual(p * 2, BusinessPeriod(years=4, months=4, days=4))
     self.assertEqual(BusinessDate.add_period(BusinessDate('20160110'), BusinessPeriod(days=-1)),
                      BusinessDate('20160109'))
示例#5
0
    def test__diff(self):
        d1 = BusinessDate.from_ymd(2016, 1, 31)
        d2 = BusinessDate.from_ymd(2017, 11, 1)

        diff = BusinessDate.diff(d1, d2)
        diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2])
        self.assertEqual('1Y9M1D', diff.to_string())

        d1 = BusinessDate.from_ymd(2016, 2, 29)
        d2 = BusinessDate.from_ymd(2017, 3, 1)

        diff = BusinessDate.diff(d1, d2)
        diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2])
        self.assertEqual('1Y1D', diff.to_string())

        d2 = BusinessDate.from_ymd(2017, 2, 28)

        diff = BusinessDate.diff(d1, d2)
        diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2])
        self.assertEqual('1Y', diff.to_string())

        d1 = BusinessDate.from_ymd(2016, 11, 15)
        d2 = BusinessDate.from_ymd(2017, 1, 15)

        diff = BusinessDate.diff(d1, d2)
        diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2])
        self.assertEqual('2M', diff.to_string())

        d1 = BusinessDate.from_ymd(2015, 7, 31)
        d2 = BusinessDate.from_ymd(2017, 2, 20)

        diff = BusinessDate.diff(d1, d2)
        diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2])
        self.assertEqual('1Y6M20D', diff.to_string())
示例#6
0
 def try_get_business_period(val):
     if isinstance(val, BusinessPeriod):
         return val
     if isinstance(val, str):
         y, m, d = BusinessPeriod.parse(val)
         if y > 0 or m > 0 or d > 0:
             return BusinessPeriod(years=y, months=m, days=d)
         else:
             return None
示例#7
0
 def test_parse(self):
     f = lambda x: '%sb%sy%sq%sm%sw%sd%sb' % tuple(x)
     d = lambda x: tuple(map(int, x))
     for p in ('1000000', '0100000', '0010000', '0001000', '0000100',
               '0000010', '0000001'):
         self.assertEqual(d(p), BusinessPeriod._parse_ymd(f(p)))
     for p in ('1000001', '0100001', '1010001', '1101011', '0110100',
               '0010010', '1111111'):
         self.assertEqual(d(p), BusinessPeriod._parse_ymd(f(p)))
示例#8
0
 def setUp(self):
     self._1y = BusinessPeriod('1y')
     self._3m = BusinessPeriod('1m')
     self._1y6m = BusinessPeriod('1y6m')
     self._1b = BusinessPeriod('1b')
     self._2y = BusinessPeriod('2y')
     self._3y = BusinessPeriod('3y')
     self._5y = BusinessPeriod('5y')
     self._2q = BusinessPeriod('2q')
     self._2w = BusinessPeriod('2w')
示例#9
0
    def __init__(self, object_name_str=''):
        super(RateIndex, self).__init__(object_name_str)
        self._index_model_ = InterestRateIndexModel()

        self._calendar_ = TAR()
        self._spot_ = BusinessPeriod(businessdays=2, holiday=self._calendar_)
        self._tenor_ = BusinessPeriod(months=6, holiday=self._calendar_)
        self._day_count_ = Act360()
        self._compounding_ = Simple()
        self._rolling_method_ = ModFollow()
        self._rolling_method_.holidays = self._calendar_
示例#10
0
 def setUp(self):
     self._1b = BusinessPeriod('1b')
     self._1y = BusinessPeriod('1y')
     self._3m = BusinessPeriod('3m')
     self._1y6m = BusinessPeriod('1y6m')
     self._1b = BusinessPeriod('1b')
     self._2y = BusinessPeriod('2y')
     self._3y = BusinessPeriod('3y')
     self._5y = BusinessPeriod('5y')
     self._2q = BusinessPeriod('2q')
     self._2w = BusinessPeriod('2w')
示例#11
0
 def test_operators(self):
     self.assertEqual(self.jan01 + '2D', self.jan02 + '1D')
     self.assertEqual(self.jan01 - '1D', self.jan02 - '2D')
     self.assertEqual(self.jan02 - '1D' + '1M', self.feb01)
     self.assertRaises(TypeError, lambda: '1D' + self.jan02)
     self.assertEqual(self.jan01 - BusinessPeriod('1D'), self.jan02 - '2D')
     self.assertRaises(TypeError, lambda: BusinessPeriod('1D') + self.jan02)
     self.assertRaises(TypeError, lambda: BusinessPeriod('1D') - self.jan01)
     self.assertEqual(
         self.dec31.add_period(BusinessPeriod('2B', BusinessHolidays())),
         self.dec31.add_period(BusinessPeriod('2B'),
                               BusinessHolidays([self.jan02])))
示例#12
0
 def test_wrapper_methods(self):
     p = BusinessPeriod(years=1, months=1, days=1)
     self.assertEqual(
         p + '%sY' % (p.years) + '%sM' % (p.months) + '%sD' % (p.days),
         BusinessPeriod(years=2, months=2, days=2))
     self.assertEqual(p * 4, BusinessPeriod(years=4, months=4, days=4))
     self.assertEqual(
         BusinessDate('20160110').add_period(BusinessPeriod(days=-1)),
         BusinessDate('20160109'))
     self.assertEqual(
         BusinessDate('20160110') + BusinessPeriod(days=-1),
         BusinessDate('20160109'))
示例#13
0
class BusinessPeriodUnitTests(unittest.TestCase):
    def setUp(self):
        self._1y = BusinessPeriod('1y')
        self._3m = BusinessPeriod('1m')
        self._1y6m = BusinessPeriod('1y6m')
        self._1b = BusinessPeriod('1b')
        self._2y = BusinessPeriod('2y')
        self._3y = BusinessPeriod('3y')
        self._5y = BusinessPeriod('5y')
        self._2q = BusinessPeriod('2q')
        self._2w = BusinessPeriod('2w')

    def test_constructors(self):
        self.assertEqual(self._1y, BusinessPeriod(years=1))
        self.assertEqual(self._1y6m, BusinessPeriod(years=1, months=6))
        self.assertEqual(self._1y6m, BusinessPeriod('6m', years=1))
        self.assertEqual(-1 * self._1y6m, BusinessPeriod('-6m', years=-1))
        self.assertEqual(self._2q, BusinessPeriod(months=6))

    def test_properties(self):
        self.assertEqual(self._1y.years, 1)
        self.assertEqual(self._1y.months, 0)
        self.assertEqual(self._1y.days, 0)
        self.assertEqual(-1 * self._1y.years, -1)
        self.assertEqual(-1 * self._1y.days, 0)
        self.assertEqual(self._1b.businessdays, 1)

    def test_operators(self):
        self.assertTrue(self._2y < BusinessPeriod('10Y'))
        self.assertFalse(self._3y < BusinessPeriod('1Y'))
        self.assertEqual(self._2y.__cmp__(BusinessPeriod('10Y')), -2922.0)
        self.assertNotEqual(self._2y, self._5y)
        self.assertEqual(BusinessPeriod('5y'), self._5y)

    def test_validations(self):
        self.assertTrue(BusinessPeriod.is_businessperiod('1y'))
        self.assertTrue(BusinessPeriod.is_businessperiod('-1y'))
        self.assertFalse(BusinessPeriod.is_businessperiod('1y-6m'))

    def test_calculations(self):
        self.assertEqual(self._2y + self._3y, self._5y)
        self.assertEqual(self._1y + '6m', self._1y6m)
        self.assertEqual(self._1y, BusinessPeriod('1y'))
        self.assertRaises(TypeError, lambda: '6m' + self._1y)
        self.assertEquals(self._1y, self._3y - self._2y)
        self.assertEquals(self._1y, self._3y - '2y')

    def test_cast(self):
        self.assertEqual(BusinessPeriod.from_string('1y'), BusinessPeriod(years=1))
        self.assertEqual(self._1y.to_businessdate(BusinessDate(20150101)), BusinessDate(20160101))
        self.assertEqual(self._1y6m.to_businessdate(BusinessDate(20150101)), BusinessDate(20160701))
        self.assertEqual(self._1y.to_businessdate(BusinessDate(20160229)), BusinessDate(20170228))
        self.assertEqual(self._1y.to_date(BusinessDate(20160229)), date(2017, 02, 28))
        self.assertEqual(self._1y.to_businessperiod(BusinessDate(20160229)), self._1y)
        self.assertEqual(self._1y.to_string(), '1Y')
        self.assertEqual(self._2q.to_string(), '6M')
        self.assertEqual(self._2w.to_string(), '14D')
示例#14
0
 def __init__(self, object_name_str=''):
     super(SwapRateIndex, self).__init__(object_name_str)
     self._calendar_ = TAR()
     self._tenor_ = BusinessPeriod(years=5)
     self._rolling_frequency_ = BusinessPeriod(years=1)
     self._rolling_method_ = ModFollow()
     self._accrued_day_count_ = D30360()
     self._rate_index_ = RateIndex()
     self._float_rolling_frequency_ = BusinessPeriod(months=6)
     self._float_rolling_method_ = ModFollow()
     self._float_accrued_day_count_ = Act360()
     self._reset_offset_ = self._rate_index_._spot_
     self._discount_index_ = ZeroBondIndex()
     self._payer_receiver_ = "Payer"
示例#15
0
 def test_calculations(self):
     self.assertEqual(self.jan01.add_days(1), self.jan02)
     self.assertEqual(self.jan01.add_months(1), self.feb01)
     self.assertEqual(self.jan01.add_years(1).to_string(), '20170101')
     self.assertEqual(self.jan01.add_period('2D'),
                      self.jan02 + BusinessPeriod('1D'))
     self.assertEqual(self.jan02.add_period('-2D'),
                      self.jan01 - BusinessPeriod('1D'))
     self.assertEqual(self.jan02.add_period('-1b'),
                      self.jan01 - BusinessPeriod('1b'))
     self.assertNotEqual(
         BusinessDate(20160630).add_period(BusinessPeriod('2B')),
         BusinessDate(20160630).add_period(
             BusinessPeriod('2B', BusinessHolidays(['20160704']))))
     self.assertEqual(self.jan01 + '1b', self.jan02 + '1b')
示例#16
0
 def test_add(self):
     p = BusinessPeriod(years=1, months=2, days=3)
     self.assertRaises(TypeError, lambda: p + 2)
     p = BusinessPeriod(years=1, months=2, days=3)
     self.assertEqual(p.add_months(1),
                      BusinessPeriod(years=1, months=3, days=3))
     self.assertEqual(p.add_years(10),
                      BusinessPeriod(years=11, months=3, days=3))
示例#17
0
 def test_calculations(self):
     self.assertEqual(self._2y + self._3y, self._5y)
     self.assertEqual(self._1y + '6m', self._1y6m)
     self.assertEqual(self._1y, BusinessPeriod('1y'))
     self.assertRaises(TypeError, lambda: '6m' + self._1y)
     self.assertEqual(self._1y, self._3y - self._2y)
     self.assertEqual(self._1y, self._3y - '2y')
示例#18
0
 def test_cast(self):
     self.assertEqual(BusinessPeriod.from_string('1y'),
                      BusinessPeriod(years=1))
     self.assertEqual(self._1y.to_businessdate(BusinessDate(20150101)),
                      BusinessDate(20160101))
     self.assertEqual(self._1y6m.to_businessdate(BusinessDate(20150101)),
                      BusinessDate(20160701))
     self.assertEqual(self._1y.to_businessdate(BusinessDate(20160229)),
                      BusinessDate(20170228))
     self.assertEqual(self._1y.to_date(BusinessDate(20160229)),
                      date(2017, 2, 28))
     self.assertEqual(self._1y.to_businessperiod(BusinessDate(20160229)),
                      self._1y)
     self.assertEqual(self._1y.to_string(), '1Y')
     self.assertEqual(self._2q.to_string(), '6M')
     self.assertEqual(self._2w.to_string(), '14D')
示例#19
0
    def test_calculations(self):
        self.assertEqual(self.jan01.add_days(1), self.jan02)
        self.assertEqual(self.jan01.add_months(1), self.feb01)
        self.assertEqual(self.jan01.add_years(1).to_string(), '20170101')
        self.assertEqual(self.jan01.add_period('2D'),
                         self.jan02 + BusinessPeriod('1D'))
        self.assertEqual(self.jan02.add_period('-2D'),
                         self.jan01 - BusinessPeriod('1D'))
        self.assertEqual(self.jan02.add_period('-1b'),
                         self.jan01 - BusinessPeriod('1b'))
        self.assertNotEqual(
            BusinessDate(20160630).add_period(BusinessPeriod('2B')),
            BusinessDate(20160630).add_period(
                BusinessPeriod('2B', BusinessHolidays(['20160704']))))
        self.assertEqual(self.jan01 + '1b', self.jan02 + '1b')

        rng = list(range(1, 10))
        periods = list()
        for y in rng:
            for m in rng:
                for d in rng:
                    periods.append(
                        BusinessPeriod(
                            str(y) + 'y' + str(m) + 'm' + str(d) + 'd'))

        for d in self.dates:
            for p in periods:
                c = d + p
                self.assertEqual((c - d), BusinessPeriod(p), (c, d, c - d, p))

        d = BusinessDate(20160229)
        self.assertEqual(d + '1y' + '1m', BusinessDate(20170328))
        self.assertEqual(d + '1m' + '1y', BusinessDate(20170329))
        self.assertEqual(d + '1y1m', BusinessDate(20170329))
示例#20
0
 def __init__(self, object_name_str=''):
     super(Vol, self).__init__(object_name_str)
     self._calendar_ = TAR()
     self._spot_ = BusinessPeriod(businessdays=2, holiday=self._calendar_)
     self._rolling_method_ = ModFollow()
     self._day_count_ = Act360()
     self._surface_ = DataTable()
     self._surface = Surface([0.], [0.], [[0.]])
     self._option_pricer = OptionValuatorLN()
示例#21
0
    def _rate_end_date(self, reset_date):
        """

        :param BuisnessDate _rate_start_date:
        :return: BuisnessDate
        """
        tenor = BusinessPeriod(self._tenor_, self._calendar_)
        start = self._rate_start_date(reset_date).add_period(tenor)
        return self._rolling_method_.adjust(start, self._calendar_)
示例#22
0
    def _rate_start_date(self, reset_date):
        """

        :param BuisnessDate reset_date:
        :return: BuisnessDate
        """
        spot = BusinessPeriod(self._spot_, self._calendar_)
        return reset_date.add_period(
            spot)  # todo shouldn't we adjust reset_date, too?
示例#23
0
 def test_cast(self):
     self.assertEqual(BusinessPeriod.from_string('1y'), BusinessPeriod(years=1))
     self.assertEqual(self._1y.to_businessdate(BusinessDate(20150101)), BusinessDate(20160101))
     self.assertEqual(self._1y6m.to_businessdate(BusinessDate(20150101)), BusinessDate(20160701))
     self.assertEqual(self._1y.to_businessdate(BusinessDate(20160229)), BusinessDate(20170228))
     self.assertEqual(self._1y.to_date(BusinessDate(20160229)), date(2017, 02, 28))
     self.assertEqual(self._1y.to_businessperiod(BusinessDate(20160229)), self._1y)
     self.assertEqual(self._1y.to_string(), '1Y')
     self.assertEqual(self._2q.to_string(), '6M')
     self.assertEqual(self._2w.to_string(), '14D')
示例#24
0
 def test_max_min_days(self):
     jan31 = BusinessDate(20010131)
     for date in BusinessRange(jan31, jan31 + '5y'):
         for m in range(12):
             period = BusinessPeriod(months=m)
             mn, mx = period.min_days(), period.max_days()
             fwd, bck = date.diff_in_days(date + period), (
                 date - period).diff_in_days(date)
             self.assertTrue(mn <= fwd <= mx)
             self.assertTrue(mn <= bck <= mx)
     self.assertEqual(BusinessPeriod('3m').min_days(), 89)
     self.assertEqual(BusinessPeriod('-3m').min_days(), -90)
     self.assertEqual(BusinessPeriod('3m').max_days(), 92)
     self.assertEqual(BusinessPeriod('-3m').max_days(), -92)
示例#25
0
 def test_constructors(self):
     self.assertEqual(self._1y, BusinessPeriod(years=1))
     self.assertEqual(self._1y6m, BusinessPeriod(years=1, months=6))
     self.assertEqual(self._1y6m, BusinessPeriod('6m', years=1))
     self.assertEqual(self._1y6m, BusinessPeriod('18m'))
     self.assertEqual(-1 * self._1y6m, BusinessPeriod('-6m', years=-1))
     self.assertEqual(self._2q, BusinessPeriod(months=6))
示例#26
0
    def get_cash_rate(self,
                      start_date,
                      end_date=None,
                      tenor_period=BusinessPeriod('1y')):
        """

        :param start_date:
        :param end_date:
        :param tenor_period:
        :return:
        """
        end_date = start_date + tenor_period if end_date is None else end_date
        df = self.get_discount_factor(start_date, end_date)
        t = self._day_count(start_date, end_date)
        return simple_rate(df, t)
示例#27
0
    def test_operators(self):
        self.assertEqual(self.jan01 + '2D', self.jan02 + '1D')
        self.assertEqual(self.jan01 - '1D', self.jan02 - '2D')
        self.assertEqual(self.jan02 - '1D' + '1M', self.feb01)
        self.assertRaises(TypeError, lambda: '1D' + self.jan02)
        self.assertEqual(self.jan01 - BusinessPeriod('1D'), self.jan02 - '2D')
        self.assertRaises(TypeError, lambda: BusinessPeriod('1D') + self.jan02)
        self.assertRaises(TypeError, lambda: BusinessPeriod('1D') - self.jan01)
        self.assertEqual(
            self.dec31_15.add_period(BusinessPeriod('2B'), BusinessHolidays()),
            self.dec31_15.add_period(BusinessPeriod('2B'),
                                     BusinessHolidays([self.jan02])))

        d = BusinessDate(20160229)
        self.assertEqual(d + '1y' + '1m', BusinessDate(20170328))
        self.assertEqual(d + '1m' + '1y', BusinessDate(20170329))
        self.assertEqual(d + '1y1m', BusinessDate(20170329))

        self.assertEqual(d + ['1y', '1m'],
                         [BusinessDate(20170228),
                          BusinessDate(20160329)])
        self.assertEqual(d - ['-1y', '-1m'],
                         [BusinessDate(20170228),
                          BusinessDate(20160329)])
示例#28
0
    def __init__(self, object_name_str=''):
        """
        list of the cash flows in this leg,
        reconstructed in _rebuild_object with every change of an object attribute
        """
        super(CashFlowLeg, self).__init__(object_name_str)
        # cash flow list property (new for CashFlowList)
        self._cashflow_list_ = CashFlowList()

        # index property (same for CashFlow)
        self._rate_index_ = CashRateIndex()

        # payoff properties (new for CashFlowLeg)
        self._pay_rec_ = "Pay" if self._notional_ >= 0. else "Rec"

        # schedule properties (same for CashFlow)
        self._start_date_ = BusinessDate()

        # self.SetDate  replaced by ResetOffset
        self._end_date_ = BusinessDate.add_period(self._start_date_,
                                                  self.rate_index.tenor)
        # self._pay_date_  replaced by PayOffset

        # roll properties (new for CashFlowLeg)
        self._rolling_date_ = self._end_date_
        self._rolling_frequency_ = self.rate_index.tenor
        self._rolling_method_ = ModFollow()
        self._rolling_calendar_ = TAR()

        self._reset_offset_ = self.rate_index._spot_
        self._reset_method_ = self._rolling_method_
        self._reset_calendar_ = self._rolling_calendar_

        self._pay_offset_ = BusinessPeriod(businessdays=0)
        self._pay_method_ = self._rolling_method_
        self._pay_calendar_ = self._rolling_calendar_

        # payoff properties (same for CashFlow)
        self._spread_ = 0.00
        self._constant_rate_ = 0.0
        self._amount_ = 0.00
        self._multiplier_ = 1.00
        self._cap_ = NO_CAP_VALUE
        self._floor_ = NO_FLOOR_VALUE
        self._accrued_day_count_ = Act360()
        # self.YearFraction  calculated in CashFlow

        self._rebuild_object()
示例#29
0
 def test_wrapper_methods(self):
     p = BusinessPeriod(years=1, months=1, days=1)
     self.assertEqual(
         p.add_years(p.years).add_months(p.months).add_days(p.days),
         BusinessPeriod(years=2, months=2, days=2))
     self.assertEqual(p * 2, BusinessPeriod(years=4, months=4, days=4))
     self.assertEqual(
         BusinessDate.add_period(BusinessDate('20160110'),
                                 BusinessPeriod(days=-1)),
         BusinessDate('20160109'))
示例#30
0
    def __init__(self, object_name_str=''):
        super(YieldCurve, self).__init__(object_name_str)
        self._calendar_ = TAR()
        self._spot_ = BusinessPeriod(businessdays=2, holiday=self._calendar_)
        self._rolling_method_ = ModFollow()
        self._day_count_ = Act360()

        self._compounding_ = Continuous()
        self._rate_type_ = "Zero"
        self._interpolation_ = Linear()
        self._extrapolation_ = Constant()
        self._curve_ = DataRange([['', 'Rate'], [self.origin, 0.0001],
                                  [self.origin + '11y', 0.01]])
        self._inner_curve = None

        self._rebuild_object()
示例#31
0
    def setUp(self):
        testfile = open(TEST_DATA + 'daycount_test_data.csv')
        header = testfile.readline().rstrip().split(';')

        start = header.index('StartDate')
        header.pop(start)
        period = header.index('Period')
        header.pop(period)
        end = header.index('EndDate')
        header.pop(end)

        self.test_data = list()
        for line in testfile:
            data = line.rstrip().split(';')
            start_date = BusinessDate(data.pop(start))
            _ = BusinessPeriod(data.pop(period))
            end_date = BusinessDate(data.pop(end))
            daycount = dict(zip(header, data))
            self.test_data.append((start_date, end_date, daycount))
        testfile.close()
示例#32
0
    def get_forward_default_probability(self,
                                        pd_value,
                                        start_date,
                                        end_date=None,
                                        tenor_period=BusinessPeriod('1y')):
        """ return annualized forward default probability

        Parameters:
            pd_value (float): pd for the calculation
            start_date (BusinessDate): start date of conditional deflaut period
            end_date (BusinessDate): end date of conditional deflaut period
            tenor_period (BusinessPeriod): period length of conditional deflaut period

        Returns:
            float: forward default probability
        """
        end_date = start_date + tenor_period if end_date is None else end_date
        sv = self.get_survival_probability(pd_value, end_date, start_date)
        t = self._day_count(start_date, end_date)
        return (1.0 - sv) / t
示例#33
0
 def __init__(self, object_name_str=''):
     super(SwaptionVolN, self).__init__(object_name_str)
     self._swap_tenor_ = BusinessPeriod()
示例#34
0
 def test_to_date(self):
     p = BusinessPeriod(years=1, months=2, days=3)
     self.assertEqual(p.to_businessdate(BusinessDate('20160101')),
                      BusinessDate('20170304'))
示例#35
0
 def __init__(self, object_name_str=''):
     super(OverNightRateIndex, self).__init__(object_name_str)
     self._tenor_ = BusinessPeriod(days=1)
示例#36
0
 def test_to_date(self):
     p = BusinessPeriod(years=1, months=2, days=3)
     self.assertEqual(p.to_businessdate(BusinessDate('20160101')), BusinessDate('20170304'))
示例#37
0
 def test_add(self):
     p = BusinessPeriod(years=1, months=2, days=3)
     self.assertRaises(TypeError, lambda: p + 2)
     p = BusinessPeriod(years=1, months=2, days=3)
     self.assertEqual(p.add_months(1), BusinessPeriod(years=1, months=3, days=3))
     self.assertEqual(p.add_years(10), BusinessPeriod(years=11, months=3, days=3))
示例#38
0
 def test_validations(self):
     self.assertTrue(BusinessPeriod.is_businessperiod('1y'))
     self.assertTrue(BusinessPeriod.is_businessperiod('-1y'))
     self.assertFalse(BusinessPeriod.is_businessperiod('1y-6m'))