Exemple #1
0
 def test_mean_reversion(self):
     res = list()
     res.append([
         self.hull_white_curve.get_zero_rate(self.today, t)
         for t in self.grid
     ])
     for short_rate in [-.01, .0, .01]:
         for mr in [0.01, 0.02, 0.05, 0.1, 0.2, 0.5]:
             hwc = HullWhiteCurveFactorModel(self.zero_curve,
                                             mean_reversion=mr,
                                             volatility=self.volatility)
             hwc.set_risk_factor(self.today + '1y', short_rate)
             res.append(
                 [hwc.get_zero_rate(self.today, t) for t in self.grid])
     self.plot['test_mean_reversion'] = res
     self.assertTrue(True)
Exemple #2
0
 def test_volatility(self):
     res = list()
     res.append([
         self.hull_white_curve.get_zero_rate(self.today, t)
         for t in self.grid
     ])
     for short_rate in [-.01, .0, .01]:
         for v in [.005, .01, .05, .1]:
             hwc = HullWhiteCurveFactorModel(
                 self.zero_curve,
                 mean_reversion=self.mean_reversion,
                 volatility=v)
             hwc.set_risk_factor(self.today + '1y', short_rate)
             res.append(
                 [hwc.get_zero_rate(self.today, t) for t in self.grid])
     self.plot['test_volatility'] = res
     self.assertTrue(True)
Exemple #3
0
class HullWhiteCurveUnitTests(TestCase):
    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()

    def tearDown(self):
        if __name__ == '__main__':
            _try_plot(self.plot, self.grid, self.today)

    def test_curve(self):
        for d in self.grid:
            zr = self.zero_curve.get_discount_factor(self.today, d)
            hw = self.hull_white_curve.get_discount_factor(self.today, d)
            self.assertAlmostEqual(zr, hw)

            zr = self.zero_curve.get_discount_factor(d, self.termday)
            hw = self.hull_white_curve.get_discount_factor(d, self.termday)
            self.assertAlmostEqual(zr, hw)

            zr = self.zero_curve.get_cash_rate(d)
            hw = self.hull_white_curve.get_cash_rate(d)
            self.assertAlmostEqual(zr, hw)

            # assert self.zero_curve.get_discount_factor(self.today, d) == \
            #        self.hull_white_curve.get_discount_factor(self.today, d), \
            #     'HullWhiteCurve fails at get_discount_factor(self.today, d) = get_discount_factor(%s, %s)' \
            #     % (str(self.today), str(d))
            # assert self.zero_curve.get_discount_factor(d, self.termday) == \
            #        self.hull_white_curve.get_discount_factor(d, self.termday), \
            #     'HullWhiteCurve fails at get_discount_factor(d, self.termday) = get_discount_factor(%s, %s)' \
            #     % (str(d), str(self.termday))
            # assert self.zero_curve.get_cash_rate(d) == self.hull_white_curve.get_cash_rate(d), \
            #     'HullWhiteCurve fails at get_cash_rate(d) = get_cash_rate(%s)' % (str(d))

            if not self.today == d and not d == self.termday:
                zr = self.zero_curve.get_zero_rate(self.today, d)
                hw = self.hull_white_curve.get_zero_rate(self.today, d)
                self.assertAlmostEqual(zr, hw)

                zr = self.zero_curve.get_zero_rate(d, self.termday)
                hw = self.hull_white_curve.get_zero_rate(d, self.termday)
                self.assertAlmostEqual(zr, hw)

                # assert self.zero_curve.get_zero_rate(self.today, d) == \
                #        self.hull_white_curve.get_zero_rate(self.today, d), \
                #     'HullWhiteCurve fails at get_zero_rate(self.today, d) = get_zero_rate(%s, %s)' \
                #     % (str(self.today), str(d))
                # assert self.zero_curve.get_zero_rate(d, self.termday) == \
                #        self.hull_white_curve.get_zero_rate(d, self.termday), \
                #     'HullWhiteCurve fails at get_zero_rate(d, self.termday) = get_zero_rate(%s, %s)' \
                #     % (str(d), str(self.termday))

    def test_short_rate(self):
        res = list()
        res.append([
            self.hull_white_curve.get_zero_rate(self.today, t)
            for t in self.grid
        ])
        for short_rate in [-.01, .0, .01]:
            self.hull_white_curve.set_risk_factor(self.today, short_rate)
            res.append([
                self.hull_white_curve.get_zero_rate(self.today, t)
                for t in self.grid
            ])
            res.append(
                [self.hull_white_curve.get_cash_rate(t) for t in self.grid])
        self.plot['test_short_rate'] = res
        self.assertTrue(True)

    def test_forward_rate(self):
        res = list()
        res.append([
            self.hull_white_curve.get_zero_rate(self.today, t)
            for t in self.grid
        ])
        for short_rate in [-.01, .0, .01]:
            for p in ['0d', '1y', '2y', '3y']:
                self.hull_white_curve.set_risk_factor(self.today + p,
                                                      short_rate)
                res.append([
                    self.hull_white_curve.get_zero_rate(self.today, t)
                    for t in self.grid
                ])
        self.plot['test_forward_rate'] = res
        self.assertTrue(True)

    def test_mean_reversion(self):
        res = list()
        res.append([
            self.hull_white_curve.get_zero_rate(self.today, t)
            for t in self.grid
        ])
        for short_rate in [-.01, .0, .01]:
            for mr in [0.01, 0.02, 0.05, 0.1, 0.2, 0.5]:
                hwc = HullWhiteCurveFactorModel(self.zero_curve,
                                                mean_reversion=mr,
                                                volatility=self.volatility)
                hwc.set_risk_factor(self.today + '1y', short_rate)
                res.append(
                    [hwc.get_zero_rate(self.today, t) for t in self.grid])
        self.plot['test_mean_reversion'] = res
        self.assertTrue(True)

    def test_volatility(self):
        res = list()
        res.append([
            self.hull_white_curve.get_zero_rate(self.today, t)
            for t in self.grid
        ])
        for short_rate in [-.01, .0, .01]:
            for v in [.005, .01, .05, .1]:
                hwc = HullWhiteCurveFactorModel(
                    self.zero_curve,
                    mean_reversion=self.mean_reversion,
                    volatility=v)
                hwc.set_risk_factor(self.today + '1y', short_rate)
                res.append(
                    [hwc.get_zero_rate(self.today, t) for t in self.grid])
        self.plot['test_volatility'] = res
        self.assertTrue(True)