Ejemplo n.º 1
0
    def test_bucket_analysis_option(self):

        settings = Settings()

        calendar = TARGET()

        todays_date = Date(15, May, 1998)
        settlement_date = Date(17, May, 1998)

        settings.evaluation_date = todays_date

        option_type = Put
        underlying = 40
        strike = 40
        dividend_yield = 0.00
        risk_free_rate = 0.001
        volatility = 0.20
        maturity = Date(17, May, 1999)
        daycounter = Actual365Fixed()

        underlyingH = SimpleQuote(underlying)

        payoff = PlainVanillaPayoff(option_type, strike)

        flat_term_structure = FlatForward(reference_date=settlement_date,
                                          forward=risk_free_rate,
                                          daycounter=daycounter)
        flat_dividend_ts = FlatForward(reference_date=settlement_date,
                                       forward=dividend_yield,
                                       daycounter=daycounter)

        flat_vol_ts = BlackConstantVol(settlement_date, calendar, volatility,
                                       daycounter)

        black_scholes_merton_process = BlackScholesMertonProcess(
            underlyingH, flat_dividend_ts, flat_term_structure, flat_vol_ts)

        european_exercise = EuropeanExercise(maturity)
        european_option = VanillaOption(payoff, european_exercise)
        analytic_european_engine = AnalyticEuropeanEngine(
            black_scholes_merton_process)

        european_option.set_pricing_engine(analytic_european_engine)

        ba_eo = bucket_analysis([[underlyingH]], [european_option], [1], 0.50,
                                1)

        self.assertTrue(2, ba_eo)
        self.assertTrue(type(tuple), ba_eo)
        self.assertEqual(1, len(ba_eo[0][0]))
        self.assertAlmostEqual(-0.4582666150152517, ba_eo[0][0][0])
Ejemplo n.º 2
0
    def test_bucketanalysis_bond(self):

        face_amount = 100.0
        redemption = 100.0
        issue_date = Date(27, January, 2011)
        maturity_date = Date(1, January, 2021)
        coupon_rate = 0.055

        fixed_bond_schedule = Schedule.from_rule(
            issue_date,
            maturity_date,
            Period(Semiannual),
            UnitedStates(market=GovernmentBond),
            Unadjusted,
            Unadjusted,
            Backward,
            False)

        bond = FixedRateBond(
            self.settlement_days,
            face_amount,
            fixed_bond_schedule,
            [coupon_rate],
            ActualActual(Bond),
            Unadjusted,
            redemption,
            issue_date
        )

        pricing_engine = DiscountingBondEngine(self.ts)
        bond.set_pricing_engine(pricing_engine)

        self.assertAlmostEqual(bond.npv, 100.82127876105724)
        quotes = [rh.quote for rh in self.rate_helpers]
        delta, gamma = bucket_analysis(quotes, [bond])
        self.assertEqual(len(quotes), len(delta))
        old_values = [q.value for q in quotes]
        delta_manual = []
        gamma_manual = []
        pv = bond.npv
        shift = 1e-4
        for v, q in zip(old_values, quotes):
            q.value = v + shift
            pv_plus = bond.npv
            q.value = v - shift
            pv_minus = bond.npv
            delta_manual.append((pv_plus - pv_minus) * 0.5 / shift)
            gamma_manual.append((pv_plus - 2 * pv + pv_minus) / shift ** 2)
            q.value = v
        assert_allclose(delta, delta_manual)
        assert_allclose(gamma, gamma_manual, atol=1e-4)
    def test_bucket_analysis_option(self):

        settings = Settings()

        calendar = TARGET()

        todays_date = Date(15, May, 1998)
        settlement_date = Date(17, May, 1998)

        settings.evaluation_date = todays_date

        option_type = Put
        underlying = 40
        strike = 40
        dividend_yield = 0.00
        risk_free_rate = 0.001
        volatility = SimpleQuote(0.20)
        maturity = Date(17, May, 1999)
        daycounter = Actual365Fixed()

        underlyingH = SimpleQuote(underlying)

        payoff = PlainVanillaPayoff(option_type, strike)

        flat_term_structure = FlatForward(reference_date=settlement_date,
                                          forward=risk_free_rate,
                                          daycounter=daycounter)
        flat_dividend_ts = FlatForward(reference_date=settlement_date,
                                       forward=dividend_yield,
                                       daycounter=daycounter)

        flat_vol_ts = BlackConstantVol(settlement_date, calendar, volatility,
                                       daycounter)

        black_scholes_merton_process = BlackScholesMertonProcess(
            underlyingH, flat_dividend_ts, flat_term_structure, flat_vol_ts)

        european_exercise = EuropeanExercise(maturity)
        european_option = VanillaOption(payoff, european_exercise)
        analytic_european_engine = AnalyticEuropeanEngine(
            black_scholes_merton_process)

        european_option.set_pricing_engine(analytic_european_engine)

        delta, gamma = bucket_analysis([underlyingH, volatility],
                                       [european_option],
                                       shift=1e-4,
                                       type=Centered)
        self.assertAlmostEqual(delta[0], european_option.delta)
        self.assertAlmostEqual(delta[1], european_option.vega)
        self.assertAlmostEqual(gamma[0], european_option.gamma, 5)
Ejemplo n.º 4
0
    def test_bucketanalysis_bond(self):

        settings = Settings()

        calendar = TARGET()


        settlement_date = calendar.adjust(Date(28, January, 2011))
        simple_quotes = []

        fixing_days = 1
        settlement_days = 1

        todays_date = calendar.advance(
            settlement_date, -fixing_days, Days
        )

        settings.evaluation_date = todays_date

        face_amount = 100.0
        redemption = 100.0
        issue_date = Date(27, January, 2011)
        maturity_date = Date(1, January, 2021)
        coupon_rate = 0.055
        bond_yield = 0.034921

        flat_discounting_term_structure = YieldTermStructure(relinkable=True)
        flat_term_structure = FlatForward(
            reference_date = settlement_date,
            forward        = bond_yield,
            daycounter     = Actual365Fixed(), 
            compounding    = Compounded,
            frequency      = Semiannual)

        flat_discounting_term_structure.link_to(flat_term_structure)

        fixed_bond_schedule = Schedule(
            issue_date,
            maturity_date,
            Period(Semiannual),
            UnitedStates(market=GOVERNMENTBOND),
            Unadjusted,
            Unadjusted,
            Backward,
            False);


        bond = FixedRateBond(
            settlement_days,
                    face_amount,
                    fixed_bond_schedule,
                    [coupon_rate],
            ActualActual(Bond),
                    Unadjusted,
            redemption,
            issue_date
        )


        zspd=bf.zSpread(bond, 100.0, flat_term_structure, Actual365Fixed(),
        Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5)

             
        depositData = [[ 1, Months, 4.581 ],
                        [ 2, Months, 4.573 ],
                        [ 3, Months, 4.557 ],
                        [ 6, Months, 4.496 ],
                        [ 9, Months, 4.490 ]]

        swapData = [[ 1, Years, 4.54 ],
                    [ 5, Years, 4.99 ],
                    [ 10, Years, 5.47 ],
                    [ 20, Years, 5.89 ],
                    [ 30, Years, 5.96 ]]

        rate_helpers = []

        end_of_month = True
        for m, period, rate in depositData:
            tenor = Period(m, Months)
            sq_rate = SimpleQuote(rate/100)
            helper = DepositRateHelper(sq_rate, 
                        tenor, 
                        settlement_days,
                        calendar,
                        ModifiedFollowing,
                        end_of_month,
                        Actual360())
            simple_quotes.append(sq_rate)
            rate_helpers.append(helper)

        liborIndex = Libor('USD Libor', Period(6, Months), settlement_days,
                            USDCurrency(), calendar, Actual360(),
                            YieldTermStructure(relinkable=False))

        spread = SimpleQuote(0)
        fwdStart = Period(0, Days)

        for m, period, rate in swapData:
            sq_rate = SimpleQuote(rate/100)
            helper = SwapRateHelper.from_tenor(
                sq_rate, Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex,
                spread, fwdStart
            )
            simple_quotes.append(sq_rate)
            rate_helpers.append(helper)

        ts_day_counter = ActualActual(ISDA)
        tolerance = 1.0e-15

        ts = PiecewiseYieldCurve(
            'discount', 'loglinear', settlement_date, rate_helpers,
            ts_day_counter, tolerance)   

        discounting_term_structure = YieldTermStructure(relinkable=True)
        discounting_term_structure.link_to(ts)
        pricing_engine = DiscountingBondEngine(discounting_term_structure)
        bond.set_pricing_engine(pricing_engine)
                                   
                                                            

        self.assertAlmostEqual(bond.npv, 100.83702940160767)
    

        ba =  bucket_analysis([simple_quotes], [bond], [1], 0.0001, 1)
        
        self.assertTrue(2, ba) 
        self.assertTrue(type(tuple), ba) 
        self.assertEqual(len(simple_quotes), len(ba[0][0]))
        self.assertEqual(0, ba[0][0][8])
Ejemplo n.º 5
0
    def test_bucket_analysis_option(self):
        
        settings = Settings()
        
        calendar = TARGET()
        
        todays_date = Date(15, May, 1998)
        settlement_date = Date(17, May, 1998)
        
        settings.evaluation_date = todays_date

        option_type = Put
        underlying = 40
        strike = 40
        dividend_yield = 0.00
        risk_free_rate = 0.001
        volatility = 0.20
        maturity = Date(17, May, 1999)
        daycounter = Actual365Fixed()
        
        underlyingH = SimpleQuote(underlying)
        
        payoff = PlainVanillaPayoff(option_type, strike)
        
        
        flat_term_structure = FlatForward(
            reference_date = settlement_date,
            forward        = risk_free_rate,
            daycounter     = daycounter
        )
        flat_dividend_ts = FlatForward(
            reference_date = settlement_date,
            forward        = dividend_yield,
            daycounter     = daycounter
        )
        
        flat_vol_ts = BlackConstantVol(
            settlement_date,
            calendar,
            volatility,
            daycounter
        )
        
        black_scholes_merton_process = BlackScholesMertonProcess(
            underlyingH,
            flat_dividend_ts,
            flat_term_structure,
            flat_vol_ts
        )
        
        european_exercise = EuropeanExercise(maturity)
        european_option = VanillaOption(payoff, european_exercise)
        analytic_european_engine = AnalyticEuropeanEngine(
                    black_scholes_merton_process
                )
        
        european_option.set_pricing_engine(analytic_european_engine)
        
        
        ba_eo= bucket_analysis(
                [[underlyingH]], [european_option], [1], 0.50, 1)

        self.assertTrue(2, ba_eo)
        self.assertTrue(type(tuple), ba_eo) 
        self.assertEqual(1, len(ba_eo[0][0]))
        self.assertEqual(-0.4582666150152517, ba_eo[0][0][0])
Ejemplo n.º 6
0
    def test_bucketanalysis_bond(self):

        settings = Settings()

        calendar = TARGET()

        settlement_date = calendar.adjust(Date(28, January, 2011))
        simple_quotes = []

        fixing_days = 1
        settlement_days = 1

        todays_date = calendar.advance(settlement_date, -fixing_days, Days)

        settings.evaluation_date = todays_date

        face_amount = 100.0
        redemption = 100.0
        issue_date = Date(27, January, 2011)
        maturity_date = Date(1, January, 2021)
        coupon_rate = 0.055
        bond_yield = 0.034921

        flat_discounting_term_structure = YieldTermStructure()
        flat_term_structure = FlatForward(reference_date=settlement_date,
                                          forward=bond_yield,
                                          daycounter=Actual365Fixed(),
                                          compounding=Compounded,
                                          frequency=Semiannual)

        flat_discounting_term_structure.link_to(flat_term_structure)

        fixed_bond_schedule = Schedule.from_rule(
            issue_date, maturity_date, Period(Semiannual),
            UnitedStates(market=GovernmentBond), Unadjusted, Unadjusted,
            Backward, False)

        bond = FixedRateBond(settlement_days, face_amount,
                             fixed_bond_schedule, [coupon_rate],
                             ActualActual(Bond), Unadjusted, redemption,
                             issue_date)

        zspd = bf.zSpread(bond, 100.0, flat_term_structure, Actual365Fixed(),
                          Compounded, Semiannual, settlement_date, 1e-6, 100,
                          0.5)

        depositData = [[1, Months, 4.581], [2, Months, 4.573],
                       [3, Months, 4.557], [6, Months, 4.496],
                       [9, Months, 4.490]]

        swapData = [[1, Years, 4.54], [5, Years, 4.99], [10, Years, 5.47],
                    [20, Years, 5.89], [30, Years, 5.96]]

        rate_helpers = []

        end_of_month = True
        for m, period, rate in depositData:
            tenor = Period(m, Months)
            sq_rate = SimpleQuote(rate / 100)
            helper = DepositRateHelper(sq_rate, tenor, settlement_days,
                                       calendar, ModifiedFollowing,
                                       end_of_month, Actual360())
            simple_quotes.append(sq_rate)
            rate_helpers.append(helper)

        liborIndex = Libor('USD Libor', Period(6, Months), settlement_days,
                           USDCurrency(), calendar, Actual360())

        spread = SimpleQuote(0)
        fwdStart = Period(0, Days)

        for m, period, rate in swapData:
            sq_rate = SimpleQuote(rate / 100)
            helper = SwapRateHelper.from_tenor(sq_rate, Period(m, Years),
                                               calendar, Annual, Unadjusted,
                                               Thirty360(), liborIndex, spread,
                                               fwdStart)
            simple_quotes.append(sq_rate)
            rate_helpers.append(helper)

        ts_day_counter = ActualActual(ISDA)
        tolerance = 1.0e-15

        ts = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount,
                                                     Interpolator.LogLinear,
                                                     settlement_date,
                                                     rate_helpers,
                                                     ts_day_counter, tolerance)

        discounting_term_structure = YieldTermStructure()
        discounting_term_structure.link_to(ts)
        pricing_engine = DiscountingBondEngine(discounting_term_structure)
        bond.set_pricing_engine(pricing_engine)

        self.assertAlmostEqual(bond.npv, 100.83702940160767)

        ba = bucket_analysis([simple_quotes], [bond], [1], 0.0001, 1)

        self.assertTrue(2, ba)
        self.assertTrue(type(tuple), ba)
        self.assertEqual(len(simple_quotes), len(ba[0][0]))
        self.assertEqual(0, ba[0][0][8])
Ejemplo n.º 7
0
    def test_bucket_analysis_option(self):

        settings = Settings()

        calendar = TARGET()

        todays_date = Date(15, May, 1998)
        settlement_date = Date(17, May, 1998)

        settings.evaluation_date = todays_date

        option_type = Put
        underlying = 40
        strike = 40
        dividend_yield = 0.00
        risk_free_rate = 0.001
        volatility = SimpleQuote(0.20)
        maturity = Date(17, May, 1999)
        daycounter = Actual365Fixed()

        underlyingH = SimpleQuote(underlying)

        payoff = PlainVanillaPayoff(option_type, strike)


        flat_term_structure = FlatForward(
            reference_date = settlement_date,
            forward        = risk_free_rate,
            daycounter     = daycounter
        )
        flat_dividend_ts = FlatForward(
            reference_date = settlement_date,
            forward        = dividend_yield,
            daycounter     = daycounter
        )

        flat_vol_ts = BlackConstantVol(
            settlement_date,
            calendar,
            volatility,
            daycounter
        )

        black_scholes_merton_process = BlackScholesMertonProcess(
            underlyingH,
            flat_dividend_ts,
            flat_term_structure,
            flat_vol_ts
        )

        european_exercise = EuropeanExercise(maturity)
        european_option = VanillaOption(payoff, european_exercise)
        analytic_european_engine = AnalyticEuropeanEngine(
            black_scholes_merton_process
        )

        european_option.set_pricing_engine(analytic_european_engine)


        delta, gamma = bucket_analysis(
            [underlyingH, volatility], [european_option], shift=1e-4,
            type=Centered)
        self.assertAlmostEqual(delta[0], european_option.delta)
        self.assertAlmostEqual(delta[1], european_option.vega)
        self.assertAlmostEqual(gamma[0], european_option.gamma, 5)