python - 定量库 : building discount_curve from spots

标签 python quantlib

我刚刚开始使用 QuantLib 并了解各种功能。问题是我有一个假设的现货曲线,如下所示

spot_tenors = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5、 4.0, 4.5、 5.0, 5.5、 6.0, 6.5、 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0]

Blob = [0.0, 5.25, 5.43, 5.76, 6.02, 6.28, 6.55, 6.82, 6.87, 7.1, 7.21, 7.26, 7.31, 7.43, 7.48, 7.54, 7.67, 7.8, 7.79, 7.93, 8.07]

当我尝试创建discount_curve和discount_handle时,我收到错误,因为我的许多期限都是 float 1.5、2.5等

import matplotlib
matplotlib.use('macosx')
import matplotlib.pyplot as plt
import QuantLib as ql

#create a bond
issueDate = ql.Date(15, 1, 2015)
maturityDate = ql.Date(15, 1, 2025)
tenor = ql.Period(ql.Semiannual)
calendar = ql.UnitedStates()
businessConvention = ql.Unadjusted
dateGeneration = ql.DateGeneration.Backward
monthEnd = False
schedule = ql.Schedule (issueDate, maturityDate, tenor, calendar, businessConvention, businessConvention, dateGeneration, monthEnd)

# Now lets build the coupon
dayCount = ql.Thirty360()
couponRate = .06
coupons = [couponRate]

settlementDays = 0
faceValue = 100
bond = ql.FixedRateBond(settlementDays, faceValue, schedule, coupons, dayCount)

today = ql.Date(30, ql.January, 2020)
nodes = [today + Period(n, Years) for n in spot_tenors] #this is where I get the error

discount_curve = ql.ZeroCurve(nodes, spots, ql.Actual360())
discount_handle = ql.YieldTermStructureHandle(discount_curve)
bond.setPricingEngine(ql.DiscountingBondEngine(discount_handle))

我得到的错误是

Traceback (most recent call last):
  File "/Users/prasadkamath/anaconda2/envs/Pk/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3319, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-301-cc1508b12a56>", line 1, in <module>
    [today + Period(n, Years) for n in tenors]
  File "<ipython-input-301-cc1508b12a56>", line 1, in <listcomp>
    [today + Period(n, Years) for n in tenors]
  File "/Users/prasadkamath/anaconda2/envs/Pk/lib/python3.6/site-packages/QuantLib/QuantLib.py", line 183, in __init__
    _QuantLib.Period_swiginit(self, _QuantLib.new_Period(*args))
TypeError: Wrong number or type of arguments for overloaded function 'new_Period'.
  Possible C/C++ prototypes are:
    Period::Period()
    Period::Period(Integer,TimeUnit)
    Period::Period(Frequency)
    Period::Period(std::string const &)

知道如何解决这个问题还是我遗漏了什么?

最佳答案

您发布的代码中一定缺少某些内容,因为它不会产生该错误。您的错误与 Period 对象的构造有关,该对象应该是:

  • ql.Period('6M') 作为输入的字符串
  • ql.Period(6, ql.Months) 表示周期数和 TimeUnit 对象的整数

无论如何,构造 ZeroCurve 的方法是:

dates = [ql.Date(31,12,2019),  ql.Date(31,12,2020),  ql.Date(31,12,2021)]
zeros = [0.01, 0.02, 0.03]
curve = ql.ZeroCurve(dates, zeros, ql.ActualActual(), ql.TARGET())

然后您可以使用年份分数或日期来获取折扣系数:

curve.discount(1.5)
curve.discount(ql.Date(15,6,2021))

关于python - 定量库 : building discount_curve from spots,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59985828/

相关文章:

excel - 从 Excel 中的 INDEX 函数返回数组?

python-3.6 - python 3.6 : No module named _QuantLib after installation of QuantLib and QuantLib-SWIG

r - 在 Mac 10.12.1 上安装 RQuantlib 包

python - 理解字典赋值与元组和列表

python - Python 中的快速隐含波动率计算

python:将日期时间戳转换为纪元unix时间并计算剩余天数?

python - 按升序对 pandas DataMatrix 进行排序

c++ - QuantLib 多线程/并发

python - 如何使用 matplotlib 正确显示此线图

python - OpenERP 主干服务器给出导入错误 psutil