蒙特卡洛模拟如何作图?


def simulation(S, r, T, sigma, I,dn = 0, steps = 1000, plotpath = False, plothist = False):
    """

    :param S: 初始价格
    :param r: 无风险收益率
    :param T: 到期期限(年)
    :param sigma: 波动率
    :param I: 路径
    :param dn: 敲入点
    :param steps:步骤
    :param plotpath:绘图路径
    :param plothist:
    :return:
    """
    delta_t = float(T)/steps
    Spath = np.zeros((steps + 1, I))
    Spath[0] = S

    for t in range(1, steps + 1):
        z = np.random.standard_normal(I)#返回指定形状的标准正态分布的数组。
        middle1 = Spath[t-1, 0:I] * np.exp((r - 0.5 * sigma ** 2) * delta_t + sigma * np.sqrt(delta_t) * z)
        uplimit = Spath[t-1] * 1.1
        lowlimit = Spath[t-1] * 0.9
        temp = np.where(uplimit < middle1, uplimit, middle1)
        temp = np.where(lowlimit > middle1, lowlimit, temp)
        Spath[t, 0:I] = temp

    if plotpath:#直线图
        plt.plot(Spath[:, :])
        plt.plot([dn]*len(Spath))
        plt.xlabel('time')
        plt.ylabel('price')
        plt.title('Price Simulation')#价格模拟,,,价格路径
        plt.grid(True)
        plt.show()
        plt.close()

    if plothist:#直方图
        plt.hist(Spath[int(T*steps)], bins=50)
        plt.title('T moment price Histogram')#T 时刻矩价格直方图,,,期末价格分布
        plt.show()
        plt.close()

    return Spath

然后我想做出这两个图

img

img

simulation(S=1, r=0.03, T=1, sigma=0.13, I=30000,dn = 0, steps = 1000, plotpath = False, plothist = False)

做到这我就不会了