Exemplo n.º 1
0
    def setUp(self):
        self.today = _today
        self.grid = _grid
        self.termday = self.grid[-1]
        self.flat_zero_curve = ZeroRateCurve([self.today], [0.05])
        self.term_zero_curve = ZeroRateCurve(self.grid, _term)
        self.zero_curve = self.flat_zero_curve
        self.fx_curve = FxCurve([self.today], [1.],
                                domestic_curve=self.zero_curve,
                                foreign_curve=self.zero_curve)
        self.fx_rate = FxRate(1., self.today)
        self.fx_volatility = .3
        self.gbm_fx_curve = GeometricBrownianMotionFxRateFactorModel(
            self.fx_rate, volatility=self.fx_volatility)
        self.mean_reversion = .1
        self.volatility = .005
        self.hull_white_curve = HullWhiteCurveFactorModel(
            self.zero_curve, self.mean_reversion, self.volatility)
        z = ZeroRateCurve([self.today], [0.03])
        self.hull_white_curve_2 = HullWhiteCurveFactorModel(
            z, self.mean_reversion * 2, self.volatility * 0.5)
        self.df_func = (
            lambda x: self.hull_white_curve.get_discount_factor(
                x, self.termday) * self.hull_white_curve.inner_factor.
            get_discount_factor(self.today, self.termday) / self.
            hull_white_curve.inner_factor.get_discount_factor(x, self.termday))
        self.fw_func = (
            lambda x: self.hull_white_curve.get_cash_rate(self.termday - '1y'))

        self.num_of_paths = 100
        self.plot = dict()
Exemplo n.º 2
0
 def __init__(self, inner_factor, drift=0.0, volatility=0.0):
     GaussRiskFactorModel.__init__(self,
                                   inner_factor,
                                   drift,
                                   volatility,
                                   start=0.0)
     ZeroRateCurve.__init__(self, inner_factor.domain,
                            inner_factor(inner_factor.domain),
                            inner_factor.interpolation, inner_factor.origin,
                            inner_factor.day_count,
                            inner_factor.forward_tenor)
Exemplo n.º 3
0
    def setUp(self):
        self.today = _today
        self.grid = BusinessRange(_today, _today + '10y', step='3m')

        domestic = ZeroRateCurve([self.today], [0.05])
        foreign = ZeroRateCurve(_grid, _term)
        self.model = GeometricBrownianMotionFxRate(1.34,
                                                   self.today,
                                                   domestic_curve=domestic,
                                                   foreign_curve=foreign,
                                                   volatility=0.2 / 250)
        self.num_of_paths = 100
        self.plot = dict()
Exemplo n.º 4
0
    def test_raise(self):
        d = HullWhiteCurveFactorModel(ZeroRateCurve())
        f = HullWhiteCurveFactorModel(ZeroRateCurve())
        x = HullWhiteFxRateFactorModel(FxRate(), d, f)

        HullWhiteCurveFactorModel(ZeroRateCurve())
        self.assertRaises(TypeError, HullWhiteCurveFactorModel, FxRate())

        HullWhiteFxRateFactorModel(FxRate(), d, f)
        self.assertRaises(TypeError, HullWhiteFxRateFactorModel,
                          ZeroRateCurve(), d, f)
        self.assertRaises(TypeError, HullWhiteFxRateFactorModel, FxRate(),
                          ZeroRateCurve(), f)
        self.assertRaises(TypeError, HullWhiteFxRateFactorModel, FxRate(), d,
                          ZeroRateCurve())

        HullWhiteMultiCurrencyCurveFactorModel(f, d, x)
        self.assertRaises(TypeError, HullWhiteMultiCurrencyCurveFactorModel, d,
                          f, FxRate())
        self.assertRaises(TypeError, HullWhiteMultiCurrencyCurveFactorModel, d,
                          ZeroRateCurve(), x)
        self.assertRaises(TypeError, HullWhiteMultiCurrencyCurveFactorModel,
                          ZeroRateCurve(), f, x)

        HullWhiteCurve()
        HullWhiteFxRate()
        HullWhiteMultiCurrencyCurve()
Exemplo n.º 5
0
 def setUp(self):
     self.today = _today
     self.grid = _grid
     self.termday = self.grid[-1]
     self.flat_zero_curve = ZeroRateCurve([self.today], [0.05])
     self.term_zero_curve = ZeroRateCurve(self.grid, _term)
     self.zero_curve = self.flat_zero_curve
     self.mean_reversion = .1
     self.volatility = .005
     self.hull_white_curve = HullWhiteCurveFactorModel(
         self.zero_curve,
         mean_reversion=self.mean_reversion,
         volatility=self.volatility)
     self.plot = dict()
Exemplo n.º 6
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
Exemplo n.º 7
0
 def yield_to_maturity(self, valuation_date):
     # todo bracketing on yield
     last = None
     ytm = None
     for ytm in _frange(-0.1, 0.1, 0.001):
         value = self.get_value(ZeroRateCurve([ytm], [valuation_date]))
         if last is None or abs(last) >= abs(value):
             last = value
     return ytm
Exemplo n.º 8
0
 def setUp(self):
     self.today = _today
     self.grid = BusinessRange(_today, _today + '10b', step='1b')
     self.term_zero_curve = ZeroRateCurve(_grid, _term)
     self.model = GaussFlatSpreadZeroRateCurve(_grid,
                                               _term,
                                               volatility=0.2 / 250)
     self.num_of_paths = 10
     self.plot = dict()
Exemplo n.º 9
0
    def __init__(self,
                 inner_factor,
                 domestic_curve=None,
                 foreign_curve=None,
                 volatility=0.0):
        domestic_curve = ZeroRateCurve(
            [inner_factor.origin],
            [0.]) if domestic_curve is None else domestic_curve
        foreign_curve = domestic_curve if foreign_curve is None else foreign_curve

        diff_curve = foreign_curve.cast(ZeroRateCurve) - domestic_curve.cast(
            ZeroRateCurve)
        domain = diff_curve.domain
        data = list(diff_curve.derivative(d) for d in domain)
        drift = DateCurve(domain,
                          data,
                          origin=inner_factor.origin,
                          day_count=domestic_curve.day_count).to_curve()

        FxRate.__init__(self, inner_factor.value, inner_factor.origin)
        GeometricBrownianMotionRiskFactorModel.__init__(
            self, inner_factor, drift, volatility, start=inner_factor.value)
Exemplo n.º 10
0
 def __init__(self,
              domain=None,
              data=None,
              interpolation=None,
              origin=None,
              day_count=None,
              forward_tenor=None,
              drift=0.0,
              volatility=0.0):
     inner_factor = ZeroRateCurve(domain, data, interpolation, origin,
                                  day_count, forward_tenor)
     super(GaussFlatSpreadZeroRateCurve,
           self).__init__(inner_factor, drift, volatility)
Exemplo n.º 11
0
 def __init__(self,
              domain=(),
              data=(),
              interpolation=None,
              origin=None,
              day_count=None,
              forward_tenor=None,
              mean_reversion=0.0,
              volatility=0.0,
              terminal_date=None):
     inner_factor = ZeroRateCurve(domain, data, interpolation, origin,
                                  day_count, forward_tenor)
     super(HullWhiteCurve, self).__init__(inner_factor, mean_reversion,
                                          volatility, terminal_date)
Exemplo n.º 12
0
    def setUp(self):
        self.today = _today
        self.grid = _grid
        self.termday = self.grid[-1]
        self.flat_zero_curve = ZeroRateCurve([self.today], [0.05])
        self.term_zero_curve = ZeroRateCurve(self.grid, _term)
        self.zero_curve = self.flat_zero_curve
        self.mean_reversion = .1
        self.volatility = .005
        self.hull_white_curve = HullWhiteCurveFactorModel(
            self.zero_curve,
            mean_reversion=self.mean_reversion,
            volatility=self.volatility)

        self.df_func = (
            lambda x: self.hull_white_curve.get_discount_factor(
                x, self.termday) * self.hull_white_curve.inner_factor.
            get_discount_factor(self.today, self.termday) / self.
            hull_white_curve.inner_factor.get_discount_factor(x, self.termday))
        self.fw_func = (
            lambda x: self.hull_white_curve.get_cash_rate(self.termday - '1y'))

        self.num_of_paths = 100
        self.plot = dict()
    def test_zero_rate_curve(self):
        rate = 0.02
        curve = ZeroRateCurve(self.domain, [rate] * self.len)
        for d in self.domain:
            for p in self.periods:
                self.assertAlmostEqual(curve.get_discount_factor(d + p, d + p),
                                       1.)

                self.assertAlmostEqual(curve.get_zero_rate(self.today, d + p),
                                       rate)
                self.assertAlmostEqual(curve.get_short_rate(d + p), rate)
                yf = curve.day_count(d + p, (d + p) + curve.forward_tenor)
                cr = simple_compounding(curve.get_cash_rate(d + p), yf)
                self.assertAlmostEqual(continuous_rate(cr, yf), rate)
    def __init__(self,
                 domain=(),
                 data=(),
                 interpolation=None,
                 origin=None,
                 day_count=None,
                 forward_tenor=None,
                 mean_reversion=0.0001,
                 volatility=0.0,
                 domestic_hw_curve=None,
                 hw_fx_curve=None):
        """

        :param list(BusinesDate) domain:
        :param list(float) data:
        :param (dcf.interpolation, dcf.interpolation, dcf.interpolation) interpolation:
        :param BusinessDate origin:
        :param function day_count:
        :param BusinessPeriod forward_tenor:
        :param float mean_reversion:
        :param TimeDependentParameter or float volatility:
        :param HullWhiteCurveFactorModel domestic_hw_curve:
        :param HullWhiteFxRateFactorModel hw_fx_curve:
        :param float foreign_correlation:
        :param float rate_correlation:

        """
        terminal_date = domain[-1] if domain else None
        terminal_date = terminal_date if domestic_hw_curve is None else domestic_hw_curve.terminal_date
        domestic_hw_curve = HullWhiteCurve(
        ) if domestic_hw_curve is None else domestic_hw_curve
        hw_fx_curve = HullWhiteFxRate() if hw_fx_curve is None else hw_fx_curve
        inner_factor = ZeroRateCurve(domain, data, interpolation, origin,
                                     day_count, forward_tenor)
        inner_factor = HullWhiteCurveFactorModel(inner_factor, mean_reversion,
                                                 volatility, terminal_date)

        super(HullWhiteMultiCurrencyCurve,
              self).__init__(inner_factor=inner_factor,
                             domestic_hw_curve=domestic_hw_curve,
                             hw_fx_curve=hw_fx_curve)
 def test_discount_factor_curve(self):
     zr_curve = ZeroRateCurve([self.today, self.today + '1d'], [.02, .02])
     df_curve = DiscountFactorCurve(
         zr_curve.domain,
         [1., zr_curve.get_discount_factor(self.today + '1d')])
     for p in self.periods:
         x = self.today + p
         self.assertAlmostEqual(df_curve.get_discount_factor(x, x), 1.)
         z = zr_curve.get_zero_rate(x)
         d = df_curve.get_zero_rate(x)
         self.assertAlmostEqual(z, d)
         z = zr_curve.get_discount_factor(x)
         d = df_curve.get_discount_factor(x)
         self.assertAlmostEqual(z, d)
         z = zr_curve.get_short_rate(x)
         d = df_curve.get_short_rate(x)
         self.assertAlmostEqual(z, d)
         z = zr_curve.get_cash_rate(x)
         d = df_curve.get_cash_rate(x)
         self.assertAlmostEqual(z, d)
Exemplo n.º 16
0
    def __init__(self,
                 inner_factor,
                 mean_reversion=0.0,
                 volatility=0.0,
                 terminal_date=None):
        """
        :param list(float) domain:
        :param list(float) data:
        :param list(interpolation) interpolation:
        :param BusinessDate origin:
        :param DayCount day_count:
        :param BusinessPeriod forward_tenor: standard forward
        :param mean_reversion: mean reversion speed of short rate process
        :type  mean_reversion: float or function
        :param volatility: short rate volatility
        :type  volatility: float or function
        :param BusinessDate terminal_date: date of terminal measure
        :param RateCurve inner_factor:

        initializes Hull White drift
        """

        if not isinstance(inner_factor, InterestRateCurve):
            names = self.__class__.__name__, InterestRateCurve.__name__, inner_factor.__class__.__name__
            raise TypeError('%s requires inner_factor of type %s. %s given.' %
                            names)

        RiskFactorModel.__init__(self, inner_factor, 0.0)

        data = list(
            ZeroRateCurve.get_storage_type(inner_factor, x)
            for x in inner_factor.domain)
        super(HullWhiteCurveFactorModel,
              self).__init__(inner_factor.domain, data,
                             inner_factor.interpolation, inner_factor.origin,
                             inner_factor.day_count,
                             inner_factor.forward_tenor)

        if not isinstance(mean_reversion, float):
            raise NotImplementedError(
                'Mean reversion function or term structure not yet supported.')

        # init volatility
        self.volatility = TimeDependentParameter(volatility)

        # init mean reversion
        self.mean_reversion = float(mean_reversion)

        # init terminal_date
        self.terminal_date = self.domain[
            -1] if terminal_date is None and self.domain else terminal_date

        # init integration caches
        self._pre_calc_diffusion = dict()
        self._pre_calc_drift = dict()
        self._integral_vol_squared_I1 = dict()

        # init factor state variables
        self._factor_date = self.origin
        self._factor_yf = 0.0
        self._factor_value = 0.0
        self._integral = 0.0

        # init diffusion driver
        if isinstance(self.inner_factor, RiskFactorModel):
            self._diffusion_driver = self.inner_factor,
        else:
            self._diffusion_driver = self,
Exemplo n.º 17
0
 def setUp(self):
     self.today = BusinessDate(20161231)
     self.schedule = BusinessSchedule(self.today, self.today + '5y', '1m')
     self.rate = 0.01
     self.df = DiscountFactorCurve(ZeroRateCurve([self.today], [self.rate]))
     self.curve = CashRateCurve([self.today], [self.rate])
 def test_zero_cast(self):
     for p in self.periods:
         curve = self.curve(p)
         cast = self.cast_type(ZeroRateCurve(curve))
         for d in curve.domain[1:]:
             self.assertAlmostEqual(cast(d), curve(d), self.precision)