関数の練習
債券価格の利回りの算出を行う。
まあ、将来のキャッシュフローを割引いて、現在価値を算出するという考え方は前回のNPVやIRRの算出時と変わらないだろう
なので、前回の内容を元に、見本のコードを見ずに自分でかけるかやってみることにした
最初の債券利回りの算出。キャッシュフローの配列を作れれば大丈夫だろう。
こんな感じでキャッシュフローは作った
price=90.0 maturity=10 couponrate=0.05 facevalue=100.0 cf0 = np.full([maturity], couponrate * facevalue) print(cf0) cf = np.insert(cf0, 0, price * -1) print (cf) cf[maturity] += facevalue print(cf)
結果
[5. 5. 5. 5. 5. 5. 5. 5. 5. 5.] [-90. 5. 5. 5. 5. 5. 5. 5. 5. 5. 5.] [-90. 5. 5. 5. 5. 5. 5. 5. 5. 5. 105.]
あとは、前回の成果物を参考に
def Bond_Yield(price,maturity,couponrate,facevalue): cf0 = np.full([maturity], couponrate * facevalue) cf = np.insert(cf0, 0, price * -1) cf[maturity] += facevalue roots=pol.polyroots(cf) real=np.real(roots[np.isreal(roots)]) positive=np.asscalar(real[real>0.0]) return (1/positive-1)*100 price=90.0 maturity=10 couponrate=0.05 facevalue=100.0 print (Bond_Yield(price,maturity,couponrate,facevalue))
結果
6.383471023015663
順調。続いて、債券価格の算出。これも同様にまずキャッシュフローから
Yield=0.07 Marurity=7 Couponrate=0.05 Facevalue=100.0 Cf0=np.full([Marurity],Couponrate*Facevalue) print (Cf0) Cf=np.insert(Cf0,0,0) print (Cf) Cf[Marurity]+=Facevalue print (Cf)
結果
[5. 5. 5. 5. 5. 5. 5.] [0. 5. 5. 5. 5. 5. 5. 5.] [ 0. 5. 5. 5. 5. 5. 5. 105.]
あとは前回の成果物を参考に
#債券価格の算出 def Bond_Price(Yield,Maturity,Couponrate,Facevalue): Cf0 = np.full([Marurity], Couponrate * Facevalue) Cf = np.insert(Cf0, 0, 0) Cf[Marurity] += Facevalue x=1.0/(1.0+Yield) return pol.polyval(x,Cf) Yield=0.07 Marurity=7 Couponrate=0.05 Facevalue=100.0 print (Bond_Price(Yield,Marurity,Couponrate,Facevalue))
結果
89.2214211967026
最後にグラフで可視化
#利回りvs価格を可視化 V_yield=np.linspace(0,0.12,100) V_price=np.array([Bond_Price(Yield,7,0.05,100) for Yield in V_yield]) fig1=plt.figure(1,facecolor='w') plt.plot(V_yield,V_price,'k-') plt.xlabel('利回り',fontproperties=jpfont) plt.ylabel('価格',fontproperties=jpfont) plt.show()
結果
図にすること考えると、利回りは%表記のがよかったなあ
ってか、forにはこんな使い方もあるのかあ
それにしても水曜は早帰りデーだから時間があってよかったよかった
最後にコード全体
# coding; utf-8 import numpy as np import numpy.polynomial.polynomial as pol import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties FontPath='/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc' jpfont=FontProperties(fname=FontPath) #利回りの算出 def Bond_Yield(price,maturity,couponrate,facevalue): cf0 = np.full([maturity], couponrate * facevalue) cf = np.insert(cf0, 0, price * -1) cf[maturity] += facevalue print (cf) roots=pol.polyroots(cf) real=np.real(roots[np.isreal(roots)]) positive=np.asscalar(real[real>0.0]) return (1/positive-1)*100 price=98.0 maturity=5 couponrate=0.05 facevalue=100.0 print (Bond_Yield(price,maturity,couponrate,facevalue)) #債券価格の算出 def Bond_Price(Yield,Maturity,Couponrate,Facevalue): Cf0 = np.full([Marurity], Couponrate * Facevalue) Cf = np.insert(Cf0, 0, 0) Cf[Marurity] += Facevalue x=1.0/(1.0+Yield) return pol.polyval(x,Cf) Yield=0.07 Marurity=7 Couponrate=0.05 Facevalue=100.0 print (Bond_Price(Yield,Marurity,Couponrate,Facevalue)) #利回りvs価格を可視化 V_yield=np.linspace(0,0.12,100) V_price=np.array([Bond_Price(Yield,7,0.05,100) for Yield in V_yield]) fig1=plt.figure(1,facecolor='w') plt.plot(V_yield,V_price,'k-') plt.xlabel('利回り',fontproperties=jpfont) plt.ylabel('価格',fontproperties=jpfont) plt.show()