Ejemplo n.º 1
0
 def setUp(self):
     """"""
     self.today = today()
     self.settings = Settings()
     self.settings.evaluation_date = self.today
     self.dc = Actual365Fixed()
     self.spot = SimpleQuote(0.0)
     self.q_rate = SimpleQuote(0.0)
     self.q_ts = FlatForward(self.today, self.q_rate, self.dc)
     self.r_rate = SimpleQuote(0.0)
     self.r_ts = FlatForward(self.today, self.r_rate, self.dc)
     self.values = {
         'type': SwapType.Long,
         'strike': 0.04,
         'nominal': 50000,
         's': 100.0,
         'q': 0.00,
         'r': 0.05,
         't': 0.246575,
         'v': 0.20,
         'result': 0.04189,
         'tol': 1.0e-4,
     }
     self.spot.value = self.values['s']
     self.q_rate.value = self.values['q']
     self.r_rate.value = self.values['r']
     self.ex_date = self.today + int(self.values['t'] * 365 + 0.5)
Ejemplo n.º 2
0
    def setUp(self):

        self.settings = Settings()

        self.calendar = TARGET()

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

        self.settings.evaluation_date = self.todays_date

        # options parameters
        self.option_type = Put
        self.underlying = 36
        self.strike = 40
        self.dividend_yield = 0.00
        self.risk_free_rate = 0.06
        self.volatility = 0.20
        self.maturity = Date(17, May, 1999)
        self.daycounter = Actual365Fixed()

        self.underlyingH = SimpleQuote(self.underlying)

        # bootstrap the yield/dividend/vol curves
        self.flat_term_structure = FlatForward(
            reference_date=self.settlement_date,
            forward=self.risk_free_rate,
            daycounter=self.daycounter)
        self.flat_dividend_ts = FlatForward(
            reference_date=self.settlement_date,
            forward=self.dividend_yield,
            daycounter=self.daycounter)

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

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

        self.payoff = PlainVanillaPayoff(self.option_type, self.strike)

        #Additional parameters for testing DividendVanillaOption
        self.dividend_dates = []
        self.dividends = []
        self.american_time_steps = 600
        self.american_grid_points = 600

        #Parameters for implied volatility:
        self.accuracy = 0.001
        self.max_evaluations = 1000
        self.min_vol = 0.001
        self.max_vol = 4
        self.target_price = 4.485992
Ejemplo n.º 3
0
    def setUp(self):

        self.settings = Settings()

        self.calendar = TARGET()

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

        self.settings.evaluation_date = self.todays_date

        # options parameters
        self.option_type = Put
        self.underlying = 36
        self.strike = 40
        self.dividend_yield = 0.00
        self.risk_free_rate = 0.06
        self.volatility = 0.20
        self.maturity = Date(17, May, 1999)
        self.daycounter = Actual365Fixed()

        self.underlyingH = SimpleQuote(self.underlying)

        # bootstrap the yield/dividend/vol curves
        self.flat_term_structure = FlatForward(
            reference_date = self.settlement_date,
            forward        = self.risk_free_rate,
            daycounter     = self.daycounter
        )
        self.flat_dividend_ts = FlatForward(
            reference_date = self.settlement_date,
            forward        = self.dividend_yield,
            daycounter     = self.daycounter
        )

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

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

        self.payoff = PlainVanillaPayoff(self.option_type, self.strike)
Ejemplo n.º 4
0
def flat_rate(forward, daycounter):
    return FlatForward(
        forward=SimpleQuote(forward),
        settlement_days=0,
        calendar=NullCalendar(),
        daycounter=daycounter
    )
Ejemplo n.º 5
0
 def setUp(self):
     Settings().evaluation_date = Date(27, 4, 2018)
     self.yc = FlatForward(forward=0.03, settlement_days=2,
                           daycounter=Actual365Fixed(),
                           calendar=UnitedStates())
     self.index = UsdLiborSwapIsdaFixAm(Period(10, Years), self.yc)
     self.factory = (MakeSwaption(self.index, Period(2, Years), strike=0.0206).
                     with_underlying_type(Receiver).
                     with_settlement_type(Cash).
                     with_settlement_method(ParYieldCurve))
    def test_hull_white_calibration(self):
        """
        Adapted from ShortRateModelTest::testCachedHullWhite()
        """

        today = Date(15, February, 2002)
        settlement = Date(19, February, 2002)
        self.settings.evaluation_date = today
        yield_ts = FlatForward(settlement,
                               forward=0.04875825,
                               settlement_days=0,
                               calendar=NullCalendar(),
                               daycounter=Actual365Fixed())

        model = HullWhite(yield_ts, a=0.05, sigma=.005)

        data = [[1, 5, 0.1148 ],
                [2, 4, 0.1108 ],
                [3, 3, 0.1070 ],
                [4, 2, 0.1021 ],
                [5, 1, 0.1000 ]]

        index = Euribor6M(yield_ts)

        engine = JamshidianSwaptionEngine(model)

        swaptions = []
        for start, length, volatility in data:
            vol = SimpleQuote(volatility)
            helper = SwaptionHelper(Period(start, Years),
                                    Period(length, Years),
                                    vol,
                                    index,
                                    Period(1, Years), Thirty360(),
                                    Actual360(), yield_ts)

            helper.set_pricing_engine(engine)
            swaptions.append(helper)

        # Set up the optimization problem
        om = LevenbergMarquardt(1.0e-8, 1.0e-8, 1.0e-8)
        endCriteria = EndCriteria(10000, 100, 1e-6, 1e-8, 1e-8)

        model.calibrate(swaptions, om, endCriteria)

        print('Hull White calibrated parameters:\na: %f sigma: %f' %
              (model.a, model.sigma))

        cached_a = 0.0464041
        cached_sigma = 0.00579912

        tolerance = 1.0e-5

        self.assertAlmostEqual(cached_a, model.a, delta=tolerance)
        self.assertAlmostEqual(cached_sigma, model.sigma, delta=tolerance)
Ejemplo n.º 7
0
def flat_rate(today, forward, daycounter):
    return FlatForward(reference_date=today,
                       forward=SimpleQuote(forward),
                       daycounter=daycounter)
Ejemplo n.º 8
0
    def test_DAX_calibration(self):

        # this example is taken from A. Sepp
        # Pricing European-Style Options under Jump Diffusion Processes
        # with Stochstic Volatility: Applications of Fourier Transform
        # http://math.ut.ee/~spartak/papers/stochjumpvols.pdf

        settlement_date = Date(5, July, 2002)

        self.settings.evaluation_date = settlement_date

        daycounter = Actual365Fixed()
        calendar = TARGET()

        t = [13, 41, 75, 165, 256, 345, 524, 703]
        r = [0.0357,0.0349,0.0341,0.0355,0.0359,0.0368,0.0386,0.0401]

        dates = [settlement_date] + [settlement_date + val for val in t]
        rates = [0.0357] + r

        risk_free_ts = ZeroCurve(dates, rates, daycounter)
        dividend_ts = FlatForward(
            settlement_date, forward=0.0, daycounter=daycounter
        )

        v = [
            0.6625,0.4875,0.4204,0.3667,0.3431,0.3267,0.3121,0.3121,
            0.6007,0.4543,0.3967,0.3511,0.3279,0.3154,0.2984,0.2921,
            0.5084,0.4221,0.3718,0.3327,0.3155,0.3027,0.2919,0.2889,
            0.4541,0.3869,0.3492,0.3149,0.2963,0.2926,0.2819,0.2800,
            0.4060,0.3607,0.3330,0.2999,0.2887,0.2811,0.2751,0.2775,
            0.3726,0.3396,0.3108,0.2781,0.2788,0.2722,0.2661,0.2686,
            0.3550,0.3277,0.3012,0.2781,0.2781,0.2661,0.2661,0.2681,
            0.3428,0.3209,0.2958,0.2740,0.2688,0.2627,0.2580,0.2620,
            0.3302,0.3062,0.2799,0.2631,0.2573,0.2533,0.2504,0.2544,
            0.3343,0.2959,0.2705,0.2540,0.2504,0.2464,0.2448,0.2462,
            0.3460,0.2845,0.2624,0.2463,0.2425,0.2385,0.2373,0.2422,
            0.3857,0.2860,0.2578,0.2399,0.2357,0.2327,0.2312,0.2351,
            0.3976,0.2860,0.2607,0.2356,0.2297,0.2268,0.2241,0.2320
        ]

        s0 = SimpleQuote(4468.17)
        strikes = [
            3400, 3600, 3800, 4000, 4200, 4400, 4500, 4600, 4800, 5000, 5200,
            5400, 5600
        ]

        options = []

        for s, strike in enumerate(strikes):
            for m in range(len(t)):
                vol = SimpleQuote(v[s * 8 + m])
                # round to weeks
                maturity = Period((int)((t[m] + 3) / 7.), Weeks)
                options.append(
                    HestonModelHelper(
                        maturity, calendar, s0.value, strike, vol,
                        risk_free_ts, dividend_ts,
                        ImpliedVolError
                    )
                )

        v0    = 0.1
        kappa = 1.0
        theta = 0.1
        sigma = 0.5
        rho   = -0.5

        process = HestonProcess(
            risk_free_ts, dividend_ts, s0, v0, kappa, theta, sigma, rho
        )

        model = HestonModel(process)

        engine = AnalyticHestonEngine(model, 64)

        for option in options:
            option.set_pricing_engine(engine)

        om = LevenbergMarquardt(1e-8, 1e-8, 1e-8)

        model.calibrate(
            options, om, EndCriteria(400, 40, 1.0e-8, 1.0e-8, 1.0e-8)
        )

        sse = 0
        for i in range(len(strikes) * len(t)):
            diff = options[i].calibration_error() * 100.0
            sse += diff * diff

        expected = 177.2  # see article by A. Sepp.
        self.assertAlmostEqual(expected, sse, delta=1.0)
Ejemplo n.º 9
0
from quantlib.termstructures.volatility.equityfx.local_vol_surface import LocalVolSurface

dc = Actual365Fixed()
calendar = UnitedStates()

calculation_date = Date(6, 11, 2015)

spot = 659.37
Settings.instance().evaluation_date = calculation_date

dividend_yield = SimpleQuote(0.0)
risk_free_rate = 0.01
dividend_rate = 0.0
# bootstrap the yield/dividend/vol curves
flat_term_structure = FlatForward(reference_date=calculation_date,
                                  forward=risk_free_rate,
                                  daycounter=dc)

flat_dividend_ts = FlatForward(reference_date=calculation_date,
                               forward=dividend_yield,
                               daycounter=dc)

dates = [
    Date(6, 12, 2015),
    Date(6, 1, 2016),
    Date(6, 2, 2016),
    Date(6, 3, 2016),
    Date(6, 4, 2016),
    Date(6, 5, 2016),
    Date(6, 6, 2016),
    Date(6, 7, 2016),