Exemple #1
0
    def test_multi_simulation(self):

        res = list()
        for vol in [.005, .01]:
            for mr in [0.01, 0.1]:
                hwc = HullWhiteCurveFactorModel(self.zero_curve,
                                                mean_reversion=mr,
                                                volatility=vol)
                func = (lambda x: hwc.get_discount_factor(x, self.termday) *
                        hwc.get_discount_factor(self.today, x))
                func = (lambda x: hwc.get_cash_rate(self.termday - '1y'))
                producer = RiskFactorProducer(hwc)
                fs_consumer = Consumer(lambda x: func(x.date))
                avg_consumer = ValuationConsumer(func)

                consumer = ConsumerConsumer([fs_consumer, avg_consumer])

                # run the simulation
                fs, avg = Engine(producer,
                                 consumer).run(self.grid, self.num_of_paths)
                res.append(avg)
                self.plot['func sample v: %0.3f mr: %0.3f ' % (vol, mr)] = fs

        self.plot['func avg'] = res
Exemple #2
0
class MultiCcyHullWhiteSimulationUnitTests(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.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()

    def tearDown(self):
        _try_plot(self.plot, self.grid, self.today)

    def test_simulation(self):
        # setup timewave framework
        corr = dict()
        corr_list = list()
        corr_list.append((.0, .0, .0))
        corr_list.append((-.2, -.5, .7))
        corr_list.append((.2, .5, .7))
        corr_list.append((.2, .5, -.3))
        corr_list.append((-.2, .5, -.3))
        for dx, fx, df in corr_list:
            corr[self.hull_white_curve, self.gbm_fx_curve] = dx
            corr[self.hull_white_curve_2, self.gbm_fx_curve] = fx
            corr[self.hull_white_curve, self.hull_white_curve_2] = df
            hull_white_fx_curve = HullWhiteFxRateFactorModel(
                self.gbm_fx_curve,
                self.hull_white_curve,
                self.hull_white_curve_2,
                correlation=corr)

            hull_white_mc_curve = HullWhiteMultiCurrencyCurveFactorModel(
                self.hull_white_curve_2, self.hull_white_curve,
                hull_white_fx_curve)

            factors = [
                self.hull_white_curve, hull_white_mc_curve, hull_white_fx_curve
            ]
            # build producer
            producer = MultiRiskFactorProducer(factors, corr)
            # for fact in factors:
            #     print repr(fact) , repr(fact.diffusion_driver)
            # for k, v in producer._driver_index.iteritems():
            #     print repr(k.process), v
            # build consumer
            consumer = MultiConsumer(Consumer(), Consumer(), Consumer())
            # run engine
            result = Engine(producer, consumer).run(self.grid,
                                                    self.num_of_paths)
            c = repr((dx, fx, df))
            self.plot['multi hull white d corr=%s' % c] = result[0]
            self.plot['multi hull white f corr=%s' % c] = result[1]
            self.plot['multi hull white x corr=%s' % c] = result[2]

    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()
Exemple #3
0
class HullWhiteSimulationUnitTests(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.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 tearDown(self):
        _try_plot(self.plot, self.grid, self.today)

    def test_simulation(self):
        # setup timewave framework
        producer = RiskFactorProducer(self.hull_white_curve)
        x_consumer = Consumer(lambda x: x.value)
        df_consumer = Consumer(lambda x: self.df_func(x.date))
        pv_consumer = ValuationConsumer(self.df_func)
        fw_consumer = Consumer(lambda x: self.fw_func(x.date))
        af_consumer = ValuationConsumer(self.fw_func)

        consumer = ConsumerConsumer(
            [x_consumer, df_consumer, pv_consumer, fw_consumer, af_consumer])

        # run the simulation
        x, df_sim, df_avg, fwd_sim, fwd_avg = Engine(producer, consumer).run(
            self.grid, self.num_of_paths)

        # self.plot['state_avg'] = [[sum(xx) / len(xx) for xx in map(list, zip(*x))]]
        # self.plot['state_path'] = x

        self.plot['libor_avg'] = [
            fwd_avg, [self.zero_curve.get_cash_rate(d) for d in self.grid]
        ]
        self.plot['libor_path'] = fwd_sim

        self.plot['df_avg'] = [
            df_avg,
            [
                self.zero_curve.get_discount_factor(self.today, self.termday)
                for d in self.grid
            ]
        ]
        self.plot['df_path'] = df_sim

        dfc = DiscountFactorCurve(self.grid, df_avg)
        self.plot['zero_avg'] = [[
            dfc.get_zero_rate(self.today, d) for d in self.grid
        ], [self.zero_curve.get_zero_rate(self.today, d) for d in self.grid]]
        zr_list = list()
        for dfs in df_sim:
            dfc = DiscountFactorCurve(self.grid, dfs)
            zr_list.append(
                [dfc.get_zero_rate(self.today, d) for d in self.grid])
        self.plot['zero_path'] = zr_list

    def test_single_hull_white_model(self):
        producer = RiskFactorProducer(self.hull_white_curve)
        consumer = Consumer()
        result = Engine(producer, consumer).run(self.grid, self.num_of_paths)
        self.plot['single rf hull white'] = result

    def test_multi_hull_white_model(self):
        producer = MultiRiskFactorProducer([self.hull_white_curve])
        consumer = MultiConsumer(Consumer())
        result = Engine(producer, consumer).run(self.grid, self.num_of_paths)
        self.plot['multi rf hull white'] = result[0]

    def test_multi_simulation(self):

        res = list()
        for vol in [.005, .01]:
            for mr in [0.01, 0.1]:
                hwc = HullWhiteCurveFactorModel(self.zero_curve,
                                                mean_reversion=mr,
                                                volatility=vol)
                func = (lambda x: hwc.get_discount_factor(x, self.termday) *
                        hwc.get_discount_factor(self.today, x))
                func = (lambda x: hwc.get_cash_rate(self.termday - '1y'))
                producer = RiskFactorProducer(hwc)
                fs_consumer = Consumer(lambda x: func(x.date))
                avg_consumer = ValuationConsumer(func)

                consumer = ConsumerConsumer([fs_consumer, avg_consumer])

                # run the simulation
                fs, avg = Engine(producer,
                                 consumer).run(self.grid, self.num_of_paths)
                res.append(avg)
                self.plot['func sample v: %0.3f mr: %0.3f ' % (vol, mr)] = fs

        self.plot['func avg'] = res
Exemple #4
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)