Python 3.x IPython生成的图形不正确

Python 3.x IPython生成的图形不正确,python-3.x,numpy,matplotlib,Python 3.x,Numpy,Matplotlib,我有两个文件GBM\u simulation.py和test\u GBM.py GBM_模拟.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):

我有两个文件
GBM\u simulation.py
test\u GBM.py

GBM_模拟.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
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()
测试\u GBM.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
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笔记本上进行了测试(做了一些小的修改),它工作正常。为什么会发生这种情况

编辑:这是我在Jupyter笔记本上看到的图表


我真不敢相信我没有立即注意到这一点。缩进问题-返回语句在循环中。正确的函数定义当然是:

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

为了将来参考我的调试过程,我打印了函数循环中每个数据点的值,以查看为什么值会立即降为零。Et voilá-只计算了一个数据点,因此您的循环定义一定有问题。

您所说的“它看起来不正确”是什么意思?它在Eclipse上看起来完全一样。Jupyter笔记本的输出有什么不同?那些“小改动”是什么?我在Jupyter笔记本上添加了图表。我所说的小改动只是在取代sim.GBM。。。按GBM,sim.Euler\u GBM按Euler\u GBM,没有行:将GBM\u模拟导入为sim,并将这两个文件.py放在笔记本中。Matplotlib仅打印您告诉它要打印的内容。在上述两个示例中,您不可能绘制相同的数据点。检查你的代码,你正在做一些不同的事情。。。此外,您所说的来自jupyter的情节似乎是由Seaborn(实际上使用Matplotlib)完成的。多么愚蠢的错误!!!当我从笔记本上复制粘贴代码时,我也没有注意到。