【问题标题】:Computing the accrued interest in trinomial tree计算三叉树的应计利息
【发布时间】:2022-01-04 17:12:22
【问题描述】:


我目前正在尝试计算三项式 Hull-White 树的应计利息。

设置是一种面值为 100 的债券,在 6 年内到期,票面利率为 5%(年复利)。
这棵树有 10 个时间步长,这意味着 dt = 0.6。 对于每个节点时间,即 0,0.6,1.2,1.6 等。我做了一个循环,询问节点时间是否已达到优惠券日期,即 0,1,2,3.. 等。

如果达到优惠券(例如在节点时间 1.2 和 1.6 达到优惠券日期 1),应计应分别为 (1.2-1)*5=1 和 (1.6-1)*5=4。我想最终在每个节点从 [0,3,1,4,2,0,3,1,4,2] 的 0 到 9 累积一个

但我现在得到的是 [0,3,1,9,2,0,3,1,24,2]。

我的代码是:

´´´

timesteps = 10
maturity = 6
coupon_dates = np.full(maturity+1, range(maturity+1))
dt = maturity/timesteps
node_times = [i * dt for i in range(timesteps)]
accrued = np.zeros(timesteps)

  for i in range(len(node_times)):
        accrued[i] = round(node_times[i] * 5)
        for j in range(len(coupon_dates)):
            if (node_times[i]-dt < coupon_dates[j] and node_times[i] >= coupon_dates[j]):
                accrued[i] = round((node_times[i] - coupon_dates[j]) * 5)

´´´

谁能帮我解决这个问题?有人有更有效的方法来计算应计金额吗?

提前致谢!

【问题讨论】:

  • 我将从一个您可以手动计算的简单示例开始,然后为每个循环中的应计值创建一个表并写出您手动计算的值(您计算 accrued[i] 两次,所以表中每个列两列),然后添加打印语句并编写代码计算的实际值。拥有一个预期与实际的表格可能足以准确确定哪个迭代存在问题。可能是类似于 float 舍入错误,但不调试测试用例就不容易看到。

标签: python for-loop


【解决方案1】:

我自己解决了这个问题。

´´´

    coupon_dates = [0,1,2,3,4,5,6]
    timesteps = 10
    accrued = []
    dt = 6/10
    coupon = 0.05
    face_value = 100
    for t in range(timesteps):
        coupon_date = max(i for i in coupon_dates if i <= t * dt)
        accrued.append((t * dt - coupon_date) * coupon * 100)

´´´

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多