St_S0.py 717 B

123456789101112131415161718192021222324252627
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # 设置参数
  4. T = 1.0 # 终止时间
  5. N = 1000 # 时间步数
  6. dt = T / N # 每一步的时间间隔
  7. sigma = 0.2 # 波动率
  8. S0 = 100 # 初始价格
  9. # 生成布朗运动路径
  10. t = np.linspace(0, T, N+1) # 时间点
  11. W = np.random.randn(N) # 生成N个标准正态分布的随机数
  12. W = np.insert(W, 0, 0) # 将初始值0插入到W的开头
  13. W = np.cumsum(W) * np.sqrt(dt) # 计算布朗运动的累加和并乘以sqrt(dt)
  14. # 计算参考价格路径
  15. S = S0 + sigma * W
  16. # 绘制参考价格变化
  17. plt.figure(figsize=(10, 6))
  18. plt.plot(t, S)
  19. plt.title('Reference Price Path')
  20. plt.xlabel('Time')
  21. plt.ylabel('Price')
  22. plt.grid(True)
  23. plt.show()