Python QuantLib:从点构建折扣曲线
我刚刚开始使用QuantLib并开始了解各种功能。问题是我有一个假设的点曲线,如下所示 即期利率=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] 斑点= [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] 当我尝试创建折扣曲线和折扣句柄时,我得到了错误,因为我的许多期限是浮动1.5、2.5等等Python QuantLib:从点构建折扣曲线,python,quantlib,Python,Quantlib,我刚刚开始使用QuantLib并开始了解各种功能。问题是我有一个假设的点曲线,如下所示 即期利率=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] 斑点= [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.
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 &)
回溯(最近一次呼叫最后一次):
文件“/Users/prasadkamath/anaconda2/envs/Pk/lib/python3.6/site packages/IPython/core/interactiveshell.py”,第3319行,运行代码
exec(代码对象、self.user\u全局、self.user\n)
文件“”,第1行,在
[今天+期限(n年)为n年,以期为单位]
文件“”,第1行,在
[今天+期限(n年)为n年,以期为单位]
文件“/Users/prasadkamath/anaconda2/envs/Pk/lib/python3.6/site packages/QuantLib/QuantLib.py”,第183行,在__
_量程周期\开关(自身、量程周期、新量程周期(*args))
TypeError:重载函数“new_Period”的参数数量或类型错误。
可能的C/C++原型包括:
句号::句号()
句点::句点(整数,时间单位)
周期::周期(频率)
句点::句点(标准::字符串常量&)
你知道我该如何解决这个问题,还是我遗漏了什么吗?你发布的代码中肯定遗漏了什么,因为它不会产生那个错误。您的错误与
Period
对象的构造有关,该对象应为:
用于作为输入的字符串ql.Period('6M')
用于整数作为期间数和时间单位对象ql.Period(6,ql.Months)
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))
非常感谢David,这非常有帮助-非常感谢。奇怪的是使用相同的代码,如果我使用非浮点数,我得到了答案,也就是说,如果我使用的是[0,1,2,3,4,5,6,7,8,9,10]和相应的点,我不会得到错误。因此,出于某种原因,代码
节点=[今天+期间(n,年)为n(按即期期限)]
不喜欢n的浮点值。关于Period对象构造注释,我看不到在任何地方都明确构造了它,我是否又遗漏了什么?