コード例 #1
0
    def test_reference_evaluation_data_changed(self):
        """Testing term structure against evaluation date change... """

        quote = SimpleQuote()
        term_structure = FlatForward(settlement_days=self.settlement_days,
            forward=quote, calendar=NullCalendar(), daycounter=Actual360())

        quote.value = 0.03

        expected = []
        for days in [10, 30, 60, 120, 360, 720]:
            expected.append(
                term_structure.discount(self.adjusted_today + days)
            )

        Settings().evaluation_date = self.adjusted_today + 30

        calculated = []
        for days in [10, 30, 60, 120, 360, 720]:
            calculated.append(
                term_structure.discount(self.adjusted_today+ 30 + days)
            )

        for i, val in enumerate(expected):
            self.assertAlmostEqual(val, calculated[i])
コード例 #2
0
    def test_reference_evaluation_data_changed(self):
        """Testing term structure against evaluation date change... """

        quote = SimpleQuote()
        term_structure = FlatForward(settlement_days=self.settlement_days,
            forward=quote, calendar=NullCalendar(), daycounter=Actual360())

        quote.value = 0.03

        expected = []
        for days in [10, 30, 60, 120, 360, 720]:
            expected.append(
                term_structure.discount(self.adjusted_today + days)
            )

        Settings().evaluation_date = self.adjusted_today + 30

        calculated = []
        for days in [10, 30, 60, 120, 360, 720]:
            calculated.append(
                term_structure.discount(self.adjusted_today+ 30 + days)
            )

        for i, val in enumerate(expected):
            self.assertAlmostEqual(val, calculated[i])
コード例 #3
0
ファイル: test_cds.py プロジェクト: ChinaQuants/pyql
 def test_flat_hazard_with_quote(self):
     Settings.instance().evaluation_date = self.todays_date
     hazard_rate = SimpleQuote()
     flat_curve = FlatHazardRate(2, self.calendar, hazard_rate, Actual365Fixed())
     for h in [0.01, 0.02, 0.03]:
         hazard_rate.value =  h
         self.assertAlmostEqual(flat_curve.survival_probability(self.d),
                                math.exp(-h * flat_curve.time_from_reference(self.d)))
コード例 #4
0
    def test_quote_observability(self):
        self.count = 0
        q = SimpleQuote(0.1)

        def counter():
            self.count += 1

        obs = Observer(counter)
        obs.register_with(q)
        self.assertEqual(self.count, 0)
        q.value = 0.2
        self.assertEqual(self.count, 1)
        q.value = 0.3
        self.assertEqual(self.count, 2)
        obs.unregister_with(q)
        q.value = 0.4
        self.assertEqual(self.count, 2)
コード例 #5
0
 def test_flat_hazard_with_quote(self):
     Settings.instance().evaluation_date = self.todays_date
     hazard_rate = SimpleQuote()
     flat_curve = FlatHazardRate(2, self.calendar, hazard_rate, Actual365Fixed())
     for h in [0.01, 0.02, 0.03]:
         hazard_rate.value =  h
         self.assertAlmostEqual(flat_curve.survival_probability(self.d),
                                math.exp(-h * flat_curve.time_from_reference(self.d)))
コード例 #6
0
ファイル: test_termstructures.py プロジェクト: lnsongxf/pyql
    def test_using_simple_quote(self):

        quote = SimpleQuote(10)

        self.assertEqual(10, quote.value)

        quote.value = 15

        self.assertEqual(15, quote.value)
        self.assertTrue(quote.is_valid)
コード例 #7
0
ファイル: test_termstructures.py プロジェクト: GuidoE/pyql
    def test_using_simple_quote(self):

        quote = SimpleQuote(10)

        self.assertEquals(10, quote.value)

        quote.value = 15

        self.assertEquals(15, quote.value)
        self.assertTrue(quote.is_valid)
コード例 #8
0
 def test_empty_constructor(self):
     quote = SimpleQuote()
     self.assertFalse(quote.is_valid)
     with self.assertRaisesRegexp(RuntimeError, 'invalid SimpleQuote'):
         x = quote.value
     # test quote reset
     quote.value = 1.
     quote.reset()
     self.assertFalse(quote.is_valid)
     with self.assertRaisesRegexp(RuntimeError, 'invalid SimpleQuote'):
         x = quote.value
コード例 #9
0
ファイル: test_quotes.py プロジェクト: ChinaQuants/pyql
 def test_empty_constructor(self):
     quote = SimpleQuote()
     self.assertFalse(quote.is_valid)
     with self.assertRaisesRegexp(RuntimeError, 'invalid SimpleQuote'):
         x = quote.value
     # test quote reset
     quote.value = 1.
     quote.reset()
     self.assertFalse(quote.is_valid)
     with self.assertRaisesRegexp(RuntimeError, 'invalid SimpleQuote'):
         x = quote.value
コード例 #10
0
ファイル: settings_test.py プロジェクト: AlexArgus/pyql
date_payment    = Date(6,10,2011)
settlement_days = 2

settings.evaluation_date = date_today
quote = SimpleQuote(value=0.03)

term_structure = FlatForward(
    settlement_days = settlement_days,
    quote           = quote,
    calendar        = NullCalendar(),
    daycounter      = Actual360()
)

df_1 = term_structure.discount(date_payment)

date_today = Date(19,9,2011)
settings.evaluation_date = date_today

date_payment = Date(19,10,2011)
df_2 = term_structure.discount(date_payment)

# df_1 and df_2 should be identical:
print('rate: %f df_1: %f df_2 %f difference: %f' % (quote.value, df_1, df_2, df_2-df_1))

# the term structure registers a listener on the quote: a change in quote
# triggers a lazy recalculation of the discount factor

quote.value = .05
df_2 = term_structure.discount(date_payment)
print('rate: %f df_2: %f' % (quote.value, df_2))
コード例 #11
0
date_payment    = Date(6,10,2011)
settlement_days = 2

settings.evaluation_date = date_today
quote = SimpleQuote(value=0.03)

term_structure = FlatForward(
    settlement_days = settlement_days,
    quote           = quote,
    calendar        = NullCalendar(),
    daycounter      = Actual360()
)

df_1 = term_structure.discount(date_payment)

date_today = Date(19,9,2011)
settings.evaluation_date = date_today

date_payment = Date(19,10,2011)
df_2 = term_structure.discount(date_payment)

# df_1 and df_2 should be identical:
print('rate: %f df_1: %f df_2 %f difference: %f' % (quote.value, df_1, df_2, df_2-df_1))

# the term structure registers a listener on the quote: a change in quote
# triggers a lazy recalculation of the discount factor

quote.value = .05
df_2 = term_structure.discount(date_payment)
print('rate: %f df_2: %f' % (quote.value, df_2))
コード例 #12
0
ファイル: test_asian.py プロジェクト: gregKiely/pyql
    def test_analytic_cont_geo_av_price_greeks(self):
        
        tolerance = {}
        tolerance["delta"]  = 1.0e-5
        tolerance["gamma"]  = 1.0e-5
        # tolerance["theta"]  = 1.0e-5
        tolerance["rho"]    = 1.0e-5
        tolerance["divRho"] = 1.0e-5
        tolerance["vega"]   = 1.0e-5

        opt_types = [Call, Put]
        underlyings = [100.0]
        strikes = [90.0, 100.0, 110.0]
        q_rates = [0.04, 0.05, 0.06]
        r_rates = [0.01, 0.05, 0.15]
        lengths = [1, 2]
        vols = [0.11, 0.50, 1.20]
       
        spot = SimpleQuote(0.0)
        q_rate = SimpleQuote(0.0)
        r_rate = SimpleQuote(0.0)
        vol = SimpleQuote(0.0)

        q_ts = flat_rate(q_rate, self.daycounter)
        r_ts =  flat_rate(r_rate, self.daycounter)
        vol_ts = BlackConstantVol(self.today, self.calendar, vol, self.daycounter)

        process = BlackScholesMertonProcess(spot, q_ts, r_ts, vol_ts)

        calculated = {}
        expected = {}
        for opt_type, strike, length in product(opt_types, strikes, lengths):
            
            maturity = EuropeanExercise(self.today + length*Years)

            payoff = PlainVanillaPayoff(opt_type, strike)

            engine = AnalyticContinuousGeometricAveragePriceAsianEngine(process)

            option = ContinuousAveragingAsianOption(Geometric, payoff, maturity)
                
            option.set_pricing_engine(engine)

            for u, m, n, v in product(underlyings, q_rates, r_rates, vols):

                q = m 
                r = n
                spot.value = u
                q_rate.value = q
                r_rate.value = r
                vol.value = v

                value = option.npv
                calculated["delta"] = option.delta
                calculated["gamma"] = option.gamma
                # calculated["theta"] = option.theta
                calculated["rho"] = option.rho
                calculated["divRho"] = option.dividend_rho
                calculated["vega"] = option.vega

                if (value > spot.value*1.0e-5):
                    # perturb spot and get delta and gamma
                    du = u*1.0e-4
                    spot.value = u + du
                    value_p = option.npv
                    delta_p = option.delta
                    spot.value = u - du
                    value_m = option.npv
                    delta_m = option.delta
                    spot.value = u
                    expected["delta"] = (value_p - value_m)/(2*du)
                    expected["gamma"] = (delta_p - delta_m)/(2*du)

                    # perturb rates and get rho and dividend rho
                    dr = r*1.0e-4
                    r_rate.value = r + dr
                    value_p = option.npv
                    r_rate.value = r - dr
                    value_m = option.npv
                    r_rate.value = r
                    expected["rho"] = (value_p - value_m)/(2*dr)

                    dq = q*1.0e-4
                    q_rate.value = q + dq
                    value_p = option.npv
                    q_rate.value = q - dq
                    value_m = option.npv
                    q_rate.value = q
                    expected["divRho"] = (value_p - value_m)/(2*dq)

                    # perturb volatility and get vega
                    dv = v*1.0e-4
                    vol.value = v + dv
                    value_p = option.npv
                    vol.value = v - dv
                    value_m = option.npv
                    vol.value = v
                    expected["vega"] = (value_p - value_m)/(2*dv)

                    # perturb date and get theta
                    dt = self.daycounter.year_fraction(self.today - 1, self.today + 1)
                    self.settings.evaluation_date = self.today - 1
                    value_m = option.npv
                    self.settings.evaluation_date = self.today + 1
                    value_p = option.npv
                    self.settings.evaluation_date = self.today
                    expected["theta"] = (value_p - value_m)/dt

                    # compare
                    for greek, calcl in calculated.items():
                        expct = expected[greek]
                        tol = tolerance[greek]
                        error = relative_error(expct, calcl, u)
                        self.assertTrue(error < tol)