Numpy線形代数
acidmanの赤橙を初めて聴いてどハマりしてる今日この頃の俺だ〜
さ、ということで今回は債権の利回りのイールドかーぶを算出していくう
その上でNumpy線形代数を勉強していくう
線形代数とか大学の時全くできなかったけど!!!
#-coding UTF-8 import numpy as np import numpy.polynomial.polynomial as pol import numpy.linalg as lin 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 roots=pol.polyroots(cf) real=np.real(roots[np.isreal(roots)]) positive=np.asscalar(real[real>0.0]) return (1/positive-1)*100 #債券データ #債券データ[債券価格,残存期間,表面利率] Bond=np.array([ [99.90,1,2.0], [100.10,2,2.3], [100.66,3,2.6], [99.77,4,2.4], [98.38,5,2.2], [96.00,6,1.9], [93.70,7,1.7], [95.32,8,2.1], [95.21,9,2.2], [97.00,10,2.5] ]) F=100 #額面 print (Bond.shape[0]) #利付債利回りの計算 Yield=np.array([Bond_Yield(Bond[index,0],Bond[index,1],Bond[index,2],F) for index in range(0,Bond.shape[0])])
結果
10 Traceback (most recent call last): a = empty(shape, dtype, order) TypeError: 'numpy.float64' object cannot be interpreted as an integer
エラーきた
おそらく関数Bond_Yield
の引数の2番目のところが整数じゃなくて少数の型になってたんじゃないかと。
42行目らへんを
ビフォー
#利付債利回りの計算 Yield=np.array([Bond_Yield(Bond[index,0],Bond[index,1],Bond[index,2],F) for index in range(0,Bond.shape[0])])
アフター
#利付債利回りの計算 Yield=np.array([Bond_Yield(Bond[index,0],int(Bond[index,1]),Bond[index,2],F) for index in range(Bond.shape[0])])
としたらエラーは消えた。
続きは明日!明日はハナキン!!
#割引債利回りの計算 Bond=np.array([ [99.90,1,2.0], [100.10,2,2.3], [100.66,3,2.6], [99.77,4,2.4], [98.38,5,2.2], [96.00,6,1.9], [93.70,7,1.7], [95.32,8,2.1], [95.21,9,2.2], [97.00,10,2.5] ]) print (np.identity(Bond.shape[0])) print (np.tile(0.01*Bond[:,2]*F,(Bond.shape[0]))) test=np.tile(0.01*Bond[:,2]*F,(Bond.shape[0],1)) print (test) test1=np.transpose(test) print(test1) print (np.tril(test1))
結果
[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]] [2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5 2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5 2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5 2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5 2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5 2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5 2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5 2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5 2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5 2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5] [[2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5] [2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5] [2. 2.3 2.6 2.4 2.2 1.9 1.7 2.1 2.2 2.5] [2. 2.3 2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 2.5 2.5 2.5 2.5 2.5 2.5]]
print (np.tile(0.01*Bond[:,2]*F,(Bond.shape[0]))) test=np.tile(0.01*Bond[:,2]*F,(Bond.shape[0],1))
のところは、np.tile
の第2引数のところがタプルとなっているバターンが初だったので、その比較で試した
ここら辺の配列を用いて割引債の利回りを求める
Bond=np.array([ [99.90,1,2.0], [100.10,2,2.3], [100.66,3,2.6], [99.77,4,2.4], [98.38,5,2.2], [96.00,6,1.9], [93.70,7,1.7], [95.32,8,2.1], [95.21,9,2.2], [97.00,10,2.5] ]) F=100 #額面 #割引債利回りの計算 P=Bond[:,0] E=np.identity(Bond.shape[0]) C1=np.tril(np.transpose(np.tile(0.01*Bond[:,2]*100,1))) C=E+C1 V=lin.solve(C,P) print (V) ZeroRate=(np.power(1.0/V,Bond[:,1])-1)*100 print (ZeroRate)
結果
[ 3.33000000e+01 1.01515152e+01 2.97542088e+00 6.13359081e-01 -2.42700287e-01 -9.04379409e-01 -1.18680719e+00 1.39739617e-01 9.29363018e-03 5.14083894e-01] [-9.69969970e+01 -9.90296280e+01 -9.62037502e+01 6.06546585e+02 -1.18854083e+05 8.27662515e+01 -1.30153576e+02 6.87770524e+08 1.93344366e+20 7.74639680e+04]
できた。 あとは、グラフにでもしてみようかなあ