0

我是 Python 新手,正在尝试编写代码,我使用给定的价差计算了债券价格,现在如果我要反算并在给出债券价格的情况下找到价差,我该怎么做?我从这里获取了债券定价的代码。请帮忙

代码

import numpy as np
import scipy.optimize as opt

priceinBBG = input()
npv_bb=float(priceinBBG)

fixed_rate_bond = ql.FixedRateBond(
    settlement_days, 
    face_value, 
    schedule, 
    coupons, 
    day_count)
    
bond_engine = ql.DiscountingBondEngine(ts_handle)
fixed_rate_bond.setPricingEngine(bond_engine)
fixed_rate_bond.NPV()

def rootFunction(s):
    spread1.setValue(s) 
    npv=fixed_rate_bond.NPV()
    return npv-npv_bb
    
root_spread=opt.bisect(rootFunction, 0, 10) 
#from 0% to 1,000
print(root_spread)

错误

ValueError                                Traceback (most recent call last)
<ipython-input-142-52f15668e229> in <module>
----> 1 root_spread=opt.bisect(rootFunction, 0, 10)
      2 #from 0% to 1,000
      3 print(root_spread)

~\Anaconda3\lib\site-packages\scipy\optimize\zeros.py in bisect(f, a, b, args, xtol, rtol, maxiter, full_output, disp)
    527     if rtol < _rtol:
    528         raise ValueError("rtol too smal
4

0 回答 0