Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make_zero_coupon.py not working out of the box! #116

Open
hmarrao opened this issue Jan 27, 2016 · 3 comments
Open

make_zero_coupon.py not working out of the box! #116

hmarrao opened this issue Jan 27, 2016 · 3 comments

Comments

@hmarrao
Copy link

hmarrao commented Jan 27, 2016

to make it work had to perform the following changes which I don't know if they are fully valid:

diff --git a/examples/make_zero_coupon.py b/examples/make_zero_coupon.py
index 6004ffb..2d72510 100644
--- a/examples/make_zero_coupon.py
+++ b/examples/make_zero_coupon.py
@@ -15,14 +15,13 @@ from __future__ import print_function

 from quantlib.settings import Settings
 from quantlib.termstructures.yields.rate_helpers import DepositRateHelper, SwapRateHelper
-from quantlib.termstructures.yields.piecewise_yield_curve import \
-    term_structure_factory
+from quantlib.termstructures.yields.piecewise_yield_curve import PiecewiseYieldCurve
 from quantlib.time.api import Date, TARGET, Period, Months, Years, Days
 from quantlib.time.api import (ModifiedFollowing, Unadjusted, Actual360,
                                Thirty360, ActualActual)

 from quantlib.time.api import September, ISDA, today
-from quantlib.currency import USDCurrency
+from quantlib.currency.api import USDCurrency
 from quantlib.quotes import SimpleQuote

 from quantlib.indexes.libor import Libor
@@ -80,7 +79,7 @@ def get_term_structure(df_libor, dtObs):
     for m, period, label in depositData:
         tenor = Period(m, Months)
         rate = df_libor.get_value(dtObs, label)
-        helper = DepositRateHelper(float(rate/100.0), tenor,
+        helper = DepositRateHelper(SimpleQuote(rate/100.0), tenor,
                  settlement_days,
                  calendar, ModifiedFollowing,
                  end_of_month,
@@ -92,27 +91,31 @@ def get_term_structure(df_libor, dtObs):

     liborIndex = Libor('USD Libor', Period(6, Months),
                        settlement_days,
-                       USDCurrency(), calendar,
-                       ModifiedFollowing,
-                       endOfMonth, Actual360())
+                       USDCurrency(),
+                       calendar,
+                       Actual360(),
+                       #ModifiedFollowing,
+                       #endOfMonth, 
+                       )

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

     for m, period, label in swapData:
         rate = df_libor.get_value(dtObs, label)
-        helper = SwapRateHelper(SimpleQuote(rate/100.0),
+        helper = SwapRateHelper.from_tenor(SimpleQuote(rate/100.0),
                  Period(m, Years), 
-            calendar, Annual,
-            Unadjusted, Thirty360(),
-            liborIndex, spread, fwdStart)
+                 calendar, Annual,
+                 Unadjusted, Thirty360(),
+                 liborIndex, spread, fwdStart)
+        #helper = SwapRateHelper.from_index(SimpleQuote(rate/100.0),liborIndex)

         rate_helpers.append(helper)

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

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

@@ -120,7 +123,7 @@ def get_term_structure(df_libor, dtObs):

 def zero_curve(ts, dtObs):
     calendar = TARGET()
-    days = range(10, 365*20, 30)
+    days = range(10, 365*17, 30)
     dtMat = [calendar.advance(dateToDate(dtObs), d, Days) for d in days]
     df = np.array([ts.discount(dt) for dt in dtMat])
     dtMat = [QLDateTodate(dt) for dt in dtMat]
@@ -131,7 +134,7 @@ def zero_curve(ts, dtObs):

 if __name__ == '__main__':

-    df_libor = pandas.load('data/df_libor.pkl')
+    df_libor = pandas.read_pickle('data/df_libor.pkl')
     dtObs = df_libor.index

     fig = plt.figure()
@@ -150,7 +153,8 @@ if __name__ == '__main__':
     ax.set_xlim(dtMin, dtMax)
     ax.set_ylim(0.0, 0.1)

-    dtI = dtObs[range(0, len(dtObs)-1, 100)]
+    #dtI = dtObs[range(0, len(dtObs)-1, 100)]
+    dtI = dtObs[range(0, len(dtObs)-1, 99)]
     for dt in dtI:
         ts = get_term_structure(df_libor, dt)
         (dtMat, zc) = zero_curve(ts, dt)
@dpinte
Copy link
Member

dpinte commented Jan 27, 2016

@hmarrao thanks! If you could submit the patch in a pull request, I'll be happy to merge it! The examples are not always kept in sync with the code. Thanks for the cleanup.

@hmarrao
Copy link
Author

hmarrao commented Jan 27, 2016

I'm testing all available examples and notebooks.
I will "pull request" all fixes that I can manage to perform!

@phenaff
Copy link
Contributor

phenaff commented Feb 13, 2016

It now works in the pull request "stovol", but ....

$ ipython examples/scripts/make_zero_coupon.py
works fine, but

$ python examples/scripts/make_zero_coupon.py
raises an error about quantlib.currency.api module not found! Not sure why, this api is used all over the place.

@thrasibule thrasibule mentioned this issue Mar 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants