def test_providing_holidays(self, holidays): cal = dates.HolidayCalendar2( weekend_mask=_SATURDAY_SUNDAY_MASK, holidays=holidays) date_tensor = dates.from_tuples([(2020, 1, 1), (2020, 5, 1), (2020, 12, 25), (2021, 3, 8), (2021, 1, 1)]) self.assertAllEqual([False, True, False, True, False], cal.is_business_day(date_tensor))
def test_holidays_intersect_with_weekends(self): holidays = [(2020, 1, 4)] # Saturday. cal = dates.HolidayCalendar2( weekend_mask=_SATURDAY_SUNDAY_MASK, holidays=holidays) date_tensor = dates.from_tuples([(2020, 1, 3), (2020, 1, 4), (2020, 1, 5), (2020, 1, 6)]) self.assertAllEqual([True, False, False, True], cal.is_business_day(date_tensor))
def test_custom_weekend_mask(self): weekend_mask = [0, 0, 0, 0, 1, 0, 1] # Work Saturdays instead of Fridays. cal = dates.HolidayCalendar2(weekend_mask=weekend_mask) date_tensor = dates.from_tuples([(2020, 1, 2), (2020, 1, 3), (2020, 1, 4), (2020, 1, 5), (2020, 1, 6), (2020, 5, 1), (2020, 5, 2)]) self.assertAllEqual([True, False, True, False, True, False, True], cal.is_business_day(date_tensor))
def __init__(self, settlement_date, maturity_date, coupon_spec, start_date=None, first_coupon_date=None, penultimate_coupon_date=None, holiday_calendar=None, dtype=None, name=None): """Initialize a batch of fixed coupon bonds. Args: settlement_date: A rank 1 `DateTensor` specifying the settlement date of the bonds. maturity_date: A rank 1 `DateTensor` specifying the maturity dates of the bonds. The shape of the input should be the same as that of `settlement_date`. coupon_spec: A list of `FixedCouponSpecs` specifying the coupon payments. The length of the list should be the same as the number of bonds being created. start_date: An optional `DateTensor` specifying the dates when the interest starts to accrue for the coupons. The input can be used to specify a forward start date for the coupons. The shape of the input correspond to the numbercof instruments being created. Default value: None in which case the coupons start to accrue from the `settlement_date`. first_coupon_date: An optional rank 1 `DateTensor` specifying the dates when first coupon will be paid for bonds with irregular first coupon. penultimate_coupon_date: An optional rank 1 `DateTensor` specifying the dates when the penultimate coupon (or last regular coupon) will be paid for bonds with irregular last coupon. holiday_calendar: An instance of `dates.HolidayCalendar` to specify weekends and holidays. Default value: None in which case a holiday calendar would be created with Saturday and Sunday being the holidays. dtype: `tf.Dtype`. If supplied the dtype for the real variables or ops either supplied to the bond object or created by the bond object. Default value: None which maps to the default dtype inferred by TensorFlow. name: Python str. The name to give to the ops created by this class. Default value: `None` which maps to 'bond'. """ self._name = name or 'bond' if holiday_calendar is None: holiday_calendar = dates.HolidayCalendar2( weekend_mask=dates.WeekendMask.SATURDAY_SUNDAY) with tf.name_scope(self._name): self._dtype = dtype self._settlement_date = dates.convert_to_date_tensor( settlement_date) self._maturity_date = dates.convert_to_date_tensor(maturity_date) self._holiday_calendar = holiday_calendar self._setup(coupon_spec, start_date, first_coupon_date, penultimate_coupon_date)
def test_is_business_day(self): data = test_data.is_business_day_data date_tensor = dates.from_tuples([item[0] for item in data]) expected = [item[1] for item in data] cal = dates.HolidayCalendar2( weekend_mask=_SATURDAY_SUNDAY_MASK, holidays=test_data.holidays) actual = cal.is_business_day(date_tensor) self.assertEqual(tf.bool, actual.dtype) self.assertAllEqual(expected, actual)
def test_business_days_between(self): data = test_data.days_between_data date_tensor1 = dates.from_tuples([item["date1"] for item in data]) date_tensor2 = dates.from_tuples([item["date2"] for item in data]) expected_days_between = [item["days"] for item in data] cal = dates.HolidayCalendar2( weekend_mask=_SATURDAY_SUNDAY_MASK, holidays=test_data.holidays) actual_days_between = cal.business_days_between(date_tensor1, date_tensor2) self.assertAllEqual(expected_days_between, actual_days_between)
def __init__(self, start_date, maturity_date, pay_leg, receive_leg, holiday_calendar=None, dtype=None, name=None): """Initialize a batch of IRS contracts. Args: start_date: A rank 1 `DateTensor` specifying the dates for the inception (start of the accrual) of the swap contracts. The shape of the input correspond to the number of instruments being created. maturity_date: A rank 1 `DateTensor` specifying the maturity dates for each contract. The shape of the input should be the same as that of `start_date`. pay_leg: A scalar or a list of either `FixedCouponSpecs` or `FloatCouponSpecs` specifying the coupon payments for the payment leg of the swap. If specified as a list then the length of the list should be the same as the number of instruments being created. If specified as a scalar, then the elements of the namedtuple must be of the same shape as (or compatible to) the shape of `start_date`. receive_leg: A scalar or a list of either `FixedCouponSpecs` or `FloatCouponSpecs` specifying the coupon payments for the receiving leg of the swap. If specified as a list then the length of the list should be the same as the number of instruments being created. If specified as a scalar, then the elements of the namedtuple must be of the same shape as (or compatible with) the shape of `start_date`. holiday_calendar: An instance of `dates.HolidayCalendar` to specify weekends and holidays. Default value: None in which case a holiday calendar would be created with Saturday and Sunday being the holidays. dtype: `tf.Dtype`. If supplied the dtype for the real variables or ops either supplied to the IRS object or created by the IRS object. Default value: None which maps to the default dtype inferred by TensorFlow. name: Python str. The name to give to the ops created by this class. Default value: `None` which maps to 'interest_rate_swap'. """ self._name = name or 'interest_rate_swap' if holiday_calendar is None: holiday_calendar = dates.HolidayCalendar2( weekend_mask=dates.WeekendMask.SATURDAY_SUNDAY) with tf.name_scope(self._name): self._dtype = dtype self._start_date = dates.convert_to_date_tensor(start_date) self._maturity_date = dates.convert_to_date_tensor(maturity_date) self._holiday_calendar = holiday_calendar self._floating_leg = None self._fixed_leg = None self._pay_leg = self._setup_leg(pay_leg) self._receive_leg = self._setup_leg(receive_leg) self._is_payer = isinstance(self._pay_leg, cs.FixedCashflowStream)
def test_add_business_days_raises_on_invalid_input(self): data = test_data.add_days_data # Contains some holidays. date_tensor = dates.from_tuples([item["date"] for item in data]) days = tf.constant([item["days"] for item in data]) cal = dates.HolidayCalendar2( weekend_mask=_SATURDAY_SUNDAY_MASK, holidays=test_data.holidays) with self.assertRaises(tf.errors.InvalidArgumentError): new_dates = cal.add_business_days( date_tensor, days, roll_convention=dates.BusinessDayConvention.NONE) self.evaluate(new_dates.ordinal())
def test_roll_to_business_days(self, rolling_enum_value, data_key): data = test_data.adjusted_dates_data date_tensor = dates.from_tuples([item["date"] for item in data]) expected_dates = dates.from_tuples([item[data_key] for item in data]) cal = dates.HolidayCalendar2( weekend_mask=_SATURDAY_SUNDAY_MASK, holidays=test_data.holidays) actual_dates = cal.roll_to_business_day( date_tensor, roll_convention=rolling_enum_value) self.assertAllEqual(expected_dates.ordinal(), actual_dates.ordinal())
def test_add_months_and_roll(self, rolling_enum_value, data_key): data = test_data.add_months_data date_tensor = dates.from_tuples([item["date"] for item in data]) durations = periods.PeriodTensor([item["months"] for item in data], dates.PeriodType.MONTH) expected_dates = dates.from_tuples([item[data_key] for item in data]) cal = dates.HolidayCalendar2( weekend_mask=_SATURDAY_SUNDAY_MASK, holidays=test_data.holidays) actual_dates = cal.add_period_and_roll( date_tensor, durations, roll_convention=rolling_enum_value) self.assertAllEqual(expected_dates.ordinal(), actual_dates.ordinal())
def test_add_business_days(self): data = test_data.add_days_data date_tensor = dates.from_tuples([item["date"] for item in data]) days = tf.constant([item["days"] for item in data]) expected_dates = dates.from_tuples([item["shifted_date"] for item in data]) cal = dates.HolidayCalendar2( weekend_mask=_SATURDAY_SUNDAY_MASK, holidays=test_data.holidays) actual_dates = cal.add_business_days( date_tensor, days, roll_convention=dates.BusinessDayConvention.MODIFIED_FOLLOWING) self.assertAllEqual(expected_dates.ordinal(), actual_dates.ordinal())
def __init__(self, contract_start_date, contract_end_date, daycount_convention=None, averaging_type=None, contract_unit=1., holiday_calendar=None, dtype=None, name=None): """Initialize the Overnight index futures object. Args: contract_start_date: A Rank 1 `DateTensor` specifying the start dates of the reference period (or delivery period) of each futures contract. The published overnight index during the reference period determines the final settlement price of the futures contract. contract_end_date: A Rank 1 `DateTensor` specifying the ending dates of the reference period (or delivery period) of each futures contract. daycount_convention: An optional scalar `DayCountConvention` corresponding to the day count convention for the underlying rate for each contract. Default value: None in which case each the day count convention equal to DayCountConvention.ACTUAL_360 is used. averaging_type: An optional `AverageType` corresponding to how the final settlement rate is computed from daily rates. Default value: None, in which case `AverageType.COMPOUNDING` is used. contract_unit: An optional scalar or Rank 1 `Tensor` of real dtype specifying the notional amount for the contract. If the notional is entered as a scalar, it is assumed that all of the contracts have a notional equal to the input value. Default value: 1.0 holiday_calendar: An instance of `dates.HolidayCalenday` to specify weekends and holidays. Default value: None in which case a holiday calendar would be created with Saturday and Sunday being the holidays. dtype: `tf.Dtype`. If supplied the dtype for the real variables or ops either supplied to the EurodollarFuture object or created by the EurodollarFuture object. Default value: None which maps to the default dtype inferred by TensorFlow. name: Python str. The name to give to the ops created by this class. Default value: `None` which maps to 'eurodollar_future'. """ self._name = name or 'overnight_rate_futures' with tf.compat.v2.name_scope(self._name): self._contract_unit = tf.convert_to_tensor(contract_unit, dtype=dtype) self._dtype = dtype if dtype else self._contract_unit.dtype self._start_date = dates.convert_to_date_tensor( contract_start_date) self._end_date = dates.convert_to_date_tensor(contract_end_date) self._batch_size = self._start_date.shape[0] if daycount_convention is None: daycount_convention = rc.DayCountConvention.ACTUAL_360 if averaging_type is None: averaging_type = rc.AverageType.COMPOUNDING if holiday_calendar is None: holiday_calendar = dates.HolidayCalendar2( weekend_mask=dates.WeekendMask.SATURDAY_SUNDAY) self._daycount_convention = daycount_convention self._averaging_type = averaging_type self._holiday_calendar = holiday_calendar self._rate_tenor = dates.periods.PeriodTensor( 1, dates.PeriodType.DAY) self._setup()
def test_no_holidays_specified(self): cal = dates.HolidayCalendar2(weekend_mask=_SATURDAY_SUNDAY_MASK) date_tensor = dates.from_tuples([(2020, 1, 3), (2020, 1, 4), (2021, 12, 24), (2021, 12, 25)]) self.assertAllEqual([True, False, True, False], cal.is_business_day(date_tensor))