若是未作特别说明,文中的程序都是 Python3 代码。函数
InterestRate
类围绕收益率展开的若干计算(如计算贴现因子)是固定收益分析中最基础的部分。同时,因为固定收益产品在付息频率、计息方式、天数计算规则等细节方面的多样性,这一块的计算显得更加复杂繁琐。QuantLib 将与收益率有关的计算整合封装在 InterestRate
类,用户所做的只是按照规定配置特定的参数。ui
载入 QuantLib:spa
import QuantLib as ql print(ql.__version__)
1.12
InterestRate
对象的构造InterestRate
对象的构造须要四个参数,rest
InterestRate(r, dc, comp, freq)
这些变量的类型和解释以下:code
r
,浮点数,收益率大小;dc
,DayCounter
对象,配置天数计算规则;comp
,整数,配置计息方式,取值范围是 quantlib-python 的一些预留变量;freq
,整数,配置付息频率,取值范围是 quantlib-python 的一些预留变量。目前 quantlib-python 支持的计息方式有:对象
Simple
,\(1 + r\tau\),单利Compounded
,\((1 + r)^\tau\),复利Continuous
,\(e^{r\tau}\),连续复利目前 quantlib-python 支持的计息方式有不少:产品
NoFrequency
,无付息;Once
,付息一次,常见于零息债券;Annual
,每一年付息一次;Semiannual
,每半年付息一次;EveryFourthMonth
,每 4 个月年付息一次;Quarterly
,每季度付息一次;Bimonthly
,每两个月付息一次;Monthly
,每个月付息一次;EveryFourthWeek
,每 4 周付息一次;Biweekly
,每两周付息一次;Weekly
,每周付息一次;Daily
,天天付息一次。下面是一些经常使用的成员函数:it
rate()
:浮点数,返回收益率的值;dayCounter()
:DayCounter
对象,返回控制天数计算规则的成员变量;compounding()
:整数,返回计息方式;frequency()
:整数,返回付息频率。discountFactor(d1, d2)
:浮点数,d1
和 d2
都是 Date
型对象(d1
< d2
),返回 d1
到 d2
的贴现因子大小;compoundFactor(d1, d2)
:浮点数,d1
和 d2
都是 Date
型对象(d1
< d2
),返回 d1
到 d2
的付息因子大小;equivalentRate(resultDC, comp, freq, d1, d2)
:InterestRate
对象,返回某个与当前对象等价的 InterestRate
对象,该对象的配置参数包括 resultDC
、comp
、freq
:
d1
和 d2
都是 Date
型对象(d1
< d2
)resultDC
,DayCounter
对象,配置天数计算规则;comp
,整数,配置计息方式,取值范围是 quantlib-python 的一些预留变量;freq
,整数,配置付息频率,取值范围是 quantlib-python 的一些预留变量。某些状况下须要根据付息因子的大小逆算收益率,InterestRate
类提供了函数 impliedRate
实现这一功能:io
impliedRate(compound, resultDC, comp, freq, d1, d2)
:InterestRate
对象,返回逆算出的 InterestRate
对象,该对象的配置参数包括 resultDC
、comp
、freq
:
d1
和 d2
都是 Date
型对象(d1
< d2
)resultDC
,DayCounter
对象,配置天数计算规则;comp
,整数,配置计息方式,取值范围是 quantlib-python 的一些预留变量;freq
,整数,配置付息频率,取值范围是 quantlib-python 的一些预留变量。例子1:
def InterestRate1(): dc = ql.ActualActual() myRate = ql.InterestRate( 0.0341, dc, ql.Simple, ql.Annual) print('Rate:', myRate) d1 = ql.Date(10, ql.September, 2009) d2 = d1 + ql.Period(3, ql.Months) compFact = myRate.compoundFactor(d1, d2) print('Compound Factor: ', compFact) print('Discount Factor: ', myRate.discountFactor(d1, d2)) print( 'Equivalent Rate: ', myRate.equivalentRate( dc, ql.Continuous, ql.Semiannual, d1, d2)) implRate = ql.InterestRate.impliedRate( compFact, dc, ql.Simple, ql.Annual, d1, d2) print('Implied Rate from Comp Fact : ', implRate) InterestRate1()
Rate: 3.410000 % Actual/Actual (ISDA) simple compounding Compound Factor: 1.0085016438356165 Discount Factor: 0.9915700248109837 Equivalent Rate: 3.395586 % Actual/Actual (ISDA) continuous compounding Implied Rate from Comp Fact : 3.410000 % Actual/Actual (ISDA) simple compounding