matplot,Numpy配列

この本を買ったので、この内容に沿って勉強していくことにした

www.amazon.co.jp

今日のところは利息を単利と複利それぞれで計算したときに、どれほど差が発生するか可視化してみようというもの。

#coding: utf-8

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
import sys

#macなのでOS名はdarwinから始まるはず
if sys.platform.startswith(('darwin')):
    #Trueとなっているか確認
    print (sys.platform.startswith(('darwin')))
    FontPath='/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc'

jpfont=FontProperties(fname=FontPath)

12行目のFontPath='/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc'ってところはまだ詳しく理解できん。。

FontPathって変数にパスを入れているのはわかるけど、「日本語フォントのある場所」って日本語が理解できん。。

まあ、深追いはしないでいいや。

#coding: utf-8

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
import sys

#macなのでOS名はdarwinから始まるはず
if sys.platform.startswith(('darwin')):
    #Trueとなっているか確認
    print (sys.platform.startswith(('darwin')))
    FontPath='/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc'

jpfont=FontProperties(fname=FontPath)

#単利と複利比較
r=0.2 #利率20%
Maturity=10 #運用期間10年

Simple_Rate=1.0+r*np.linspace(0,Maturity,Maturity+1)

linspaceメソッドでは等差数列を作成。最後の引数で要素数指定。

とりあえず今日の朝はここまで。

思ったより時間ないな、、

>続き

compound_1year=np.r_[1.0,np.cumprod(np.tile(1.0+r,Maturity))]
compound_6month=np.r_[1.0,np.cumprod(np.tile((1.0+r/2.0)**2,Maturity))]

np.cumprodは累積積。この累積積は現在価値の計算とかではめちゃくちゃ使うんだろうな。等比数列の各項を一気に算出できるやつと覚えておく。

np.r_[]はNumpy配列を結合してくれる。

自分用のメモとして以下を残しておく

print (np.cumprod(np.tile(1.0+r,Maturity)))
print (compound_1year)

結果

[1.2        1.44       1.728      2.0736     2.48832    2.985984
 3.5831808  4.29981696 5.15978035 6.19173642]
[1.         1.2        1.44       1.728      2.0736     2.48832
 2.985984   3.5831808  4.29981696 5.15978035 6.19173642]

最後は連続複利

continuous_rate=np.exp(r*np.linspace(0,Maturity,Maturity+1))
print (continuous_rate)

結果

[1.         1.22140276 1.4918247  1.8221188  2.22554093 2.71828183
 3.32011692 4.05519997 4.95303242 6.04964746 7.3890561 ]

ここまででとりあえずグラフに必要な配列の作成は完成。

現状まとめるとこんな感じ

#coding: utf-8

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
import sys

#macなのでOS名はdarwinから始まるはず
if sys.platform.startswith(('darwin')):
    #Trueとなっているか確認
    print (sys.platform.startswith(('darwin')))
    FontPath='/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc'

jpfont=FontProperties(fname=FontPath)

r=0.2 #利率20%
Maturity=10 #運用期間10年

#単利
simple_rate=1.0+r*np.linspace(0,Maturity,Maturity+1)

#複利(1年複利と半年複利)
compound_1year=np.r_[1.0,np.cumprod(np.tile(1.0+r,Maturity))]
compound_6month=np.r_[1.0,np.cumprod(np.tile((1.0+r/2.0)**2,Maturity))]

#連続複利
continuous_rate=np.exp(r*np.linspace(0,Maturity,Maturity+1))

ここからは、matplotで作図。

fig1=plt.figure(1,facecolor='w')#作図を行うウィンドウの用意

#ウィンドウ上に点をプロット
plt.plot(simple_rate,'k-')
plt.plot(compound_1year,'k--')
plt.plot(compound_6month,'k-.')
plt.plot(continuous_rate,'k:')

plt.legend(['単利','1年複利','半年複利','連続複利'],prop=jpfont)#凡例

#軸ラベル
plt.xlabel('時点 t')
plt.ylabel('総収益 W(t)/W(0)')

plt.show()

結果 f:id:iiiiikamirin:20200203224629p:plain

ちなみに、一度フォント指定せずに軸ラベル設定してみた

plt.xlabel('時点 t')
plt.ylabel('総収益 W(t)/W(0)')

結果 f:id:iiiiikamirin:20200203224317p:plain やっぱり文字化けした

ということで、今回のまとめはこんな感じ

#coding: utf-8

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
import sys

#macなのでOS名はdarwinから始まるはず
if sys.platform.startswith(('darwin')):
    #Trueとなっているか確認
    print (sys.platform.startswith(('darwin')))
    FontPath='/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc'

jpfont=FontProperties(fname=FontPath)

r=0.2 #利率20%
Maturity=10 #運用期間10年

#単利
simple_rate=1.0+r*np.linspace(0,Maturity,Maturity+1)

#複利(1年複利と半年複利)
compound_1year=np.r_[1.0,np.cumprod(np.tile(1.0+r,Maturity))]
compound_6month=np.r_[1.0,np.cumprod(np.tile((1.0+r/2.0)**2,Maturity))]

#連続複利
continuous_rate=np.exp(r*np.linspace(0,Maturity,Maturity+1))

#ここから作図
fig1=plt.figure(1,facecolor='w')#作図を行うウィンドウの用意

#ウィンドウ上に点をプロット
plt.plot(simple_rate,'k-')
plt.plot(compound_1year,'k--')
plt.plot(compound_6month,'k-.')
plt.plot(continuous_rate,'k:')

plt.legend(['単利','1年複利','半年複利','連続複利'],prop=jpfont)#凡例

#軸ラベル
plt.xlabel('時点 t',fontproperties=jpfont)
plt.ylabel('総収益 W(t)/W(0)',fontproperties=jpfont)

plt.show()

結果 f:id:iiiiikamirin:20200203224629p:plain