私はGBM_simulation.py
とtest_GBM.py
という2つのファイルを持っています。IPythonで作成されたグラフが正しくない
GBM_simulation.py
import numpy as np
# Return path as numpy array of length and step predefined
# simulate exact GBM
def GBM(n, dt, s0, r, sigma):
path = np.zeros(n)
s_old = s0
for i in range(n):
s_new = s_old*np.exp((r-sigma**2/2)*dt + sigma*(dt)**(1/2)*np.random.normal())
path[i] = s_new
s_old = s_new
return path
# simulate Euler scheme GBM:
def Euler_GBM(n, dt, s0, r, sigma):
path = np.zeros(n)
s_old = s0
for i in range(n):
s_new = s_old*(1 + r*dt + sigma*(dt)**(1/2)*np.random.normal())
path[i] = s_new
s_old = s_new
return path
test_GBM.py
import matplotlib.pyplot as plt
import GBM_simulation as sim
s0 = 2
n = 500
r = 1
sigma = 1
dt = 0.001
exact = sim.GBM(n, dt, s0, r, sigma)
euler = sim.Euler_GBM(n, dt, s0, r, sigma)
# Now plot
plt.plot(exact, label = 'Exact')
plt.plot(euler, label = 'Euler')
plt.legend(loc = 'best')
plt.show()
私はIPythonコンソール上test_GBM.py
を実行したときしかし、私が手グラフはまったく正しく見えません。私はJupyter Notebook(いくつかのマイナーな変更を加えて)でテストしたところ、正しく動作しました。なぜこれが起こるのですか?
編集:これは私が、私はすぐにこのことを気づかなかったことを信じることができないJupyterノート
「正しいとは思わない」とはどういう意味ですか?それはEclipseで全く同じように見えます。 Jupyterノートブックの出力との違いは何ですか?そしてそれらの「マイナーな変更」は何でしたか? – MrT
Jupyter Notebookにグラフを追加しました。マイナーな変更が意味することは、単にsim.GBMを置き換えることです... GBM、sim.Euler_GBMをEuler_GBMで置き換え、行はありません:simとしてGBM_simluationをインポートし、これらの2つのファイルをノートブックに入れます。 – SiXUlm
Matplotlibは、それをプロットするように指示するものだけをプロットします。上記の両方の例で同じデータポイントをプロットする方法はありません。あなたのコードをチェックすると、あなたはどこか別の何かをしている...また、あなたが言うプロットは、Seaborn(Matplotlibを実際に使っている)と一緒に行ったようだ。 – tiago