由价格倒推收益率
市场上债券按价格报价,但投资者按收益率比较债券。到期收益率(YTM)是唯一一个使债券所有现金流的现值等于其价格的折现率:即现在买入、持有到期且所有款项都兑付时你获得的年化收益。对于票息 5%、价格 925.61 的 10 年期债券,它是 6%。与价格不同,YTM 没有闭式公式,因为未知数藏在一个高次多项式里。
import numpy as np
def bond_price(face, coupon_rate, ytm, years, freq=2):
n = int(round(years * freq))
times = np.arange(1, n + 1) / freq
flows = np.full(n, face * coupon_rate / freq)
flows[-1] += face
return (flows / (1 + ytm / freq) ** (freq * times)).sum()
price = bond_price(1000, 0.05, 0.06, 10)
print(round(price, 4)) # 925.6126 the market price we pretend to observe
# A back-of-the-envelope yield: annual coupon plus the yearly share of the discount, over the average of price and par
coupon = 1000 * 0.05
print(round((coupon + (1000 - price) / 10) / ((1000 + price) / 2), 5)) # 0.05966 close to 6%, but not exact搜索:二分法
收益率上升时价格稳步下降,所以可以搜索。取一个一定包含答案的区间 [low, high](0% 到 100%)。在中点给债券定价:如果价格高于目标,收益率一定更高,就把 low 提到中点;否则把 high 降到中点。每一步都把区间减半,40 步就缩小到万亿分之一。二分法比其他方法慢,但只要答案在区间内就不会失败,因此是稳妥的后备方法。
low, high = 0.0, 1.0
for step in range(6):
mid = (low + high) / 2
price_mid = bond_price(1000, 0.05, mid, 10)
print(step, round(mid, 5), round(price_mid, 2))
if price_mid > price: # priced too high: the yield must be higher
low = mid
else:
high = mid
# 0 0.5 110.38
# 1 0.25 275.86
# 2 0.125 578.47
# 3 0.0625 908.08
# 4 0.03125 1159.97
# 5 0.04688 1024.72 the interval keeps shrinking around 6% (the target price is 925.61)更快的搜索:牛顿法
牛顿法利用斜率。令 f(y) = bond_price(y) - 目标,我们要解 f(y) = 0。在当前猜测处沿切线下降到零:y_new = y - f(y) / f'(y)。斜率可以用一个极小的步长 h 数值估计:(f(y + h) - f(y - h)) / (2h)。接近答案时,正确的位数大约每步翻倍,所以二分法需要四十步的地方,牛顿法四五步就够。若起点不好或斜率平缓,牛顿法可能失败,所以要设置步数上限,不收敛时退回二分法。
y, h = 0.05, 1e-6 # start at the coupon rate
for step in range(5):
f = bond_price(1000, 0.05, y) - price
slope = (bond_price(1000, 0.05, y + h) - bond_price(1000, 0.05, y - h)) / (2 * h)
print(step, round(y, 8), round(f, 6))
y = y - f / slope
# 0 0.05 74.387374
# 1 0.05954347 3.245932
# 2 0.05999903 0.006916
# 3 0.06 0.0
# 4 0.06 0.0 converged to 6% after 3 steps# Check the answer by pricing the bond at the yield we found
print(round(bond_price(1000, 0.05, y, 10), 4), round(price, 4)) # 925.6126 925.6126 the same price: the yield is right收益率可以比较,价格不能到期收益率假设你能以同一收益率再投资每笔票息,且发行人足额兑付,因此它是一个承诺而不是预测。收益率特别高的债券通常是市场怀疑其能否还本付息的债券。收益率只应在风险和期限相近的债券之间比较。
- YTM 是使债券现值等于价格的折现率;它没有闭式公式,只能搜索求解。
- 二分法:不断把
[low, high]减半(价格高于目标则抬高low);慢但稳妥。 - 牛顿法:用数值斜率做
y = y - f / slope;几步即可收敛,但需要步数上限和合适的起点。
下一节:久期与凸性,衡量债券价格对收益率的敏感程度。
实验:评分内容
- 用
approx_ytm粗略估算收益率25 分 - 用
bisect_yield通过二分法求收益率25 分 - 用
newton_yield更快地求收益率25 分 - 用
classify_price为债券的状态命名25 分