jjyyyyaa 2022-12-17 22:53 采纳率: 50%
浏览 15
已结题

蒙特卡洛模拟如何作图?


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)

做到这我就不会了

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月25日
    • 修改了问题 12月17日
    • 创建了问题 12月17日

    悬赏问题

    • ¥15 找别人艾特你然后删除的微博
    • ¥15 idea做图书管理系统,要求如下
    • ¥15 最短路径分配法——多路径分配
    • ¥15 SQL server 2022安装程序(英语)无法卸载
    • ¥15 关于#c++#的问题:把一个三位数的素数写在另一个三位数素数的后面
    • ¥15 求一个nao机器人跳舞的程序
    • ¥15 anaconda下载后spyder内无法正常运行
    • ¥20 统计PDF文件指定词语的出现的页码
    • ¥50 分析一个亿级消息接收处理策略的问题?
    • ¥20 uniapp 朋友圈分享单页面自定义操作