03_04.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. A = 1.0 # 基准强度
  10. k = 0.5 # 衰减系数
  11. delta = 1.0 # 价格距离
  12. # 生成布朗运动路径
  13. t = np.linspace(0, T, N+1) # 时间点
  14. W = np.random.randn(N) # 生成N个标准正态分布的随机数
  15. W = np.insert(W, 0, 0) # 将初始值0插入到W的开头
  16. W = np.cumsum(W) * np.sqrt(dt) # 计算布朗运动的累加和并乘以sqrt(dt)
  17. # 计算参考价格路径
  18. S = S0 + sigma * W
  19. # 计算捕获流强度
  20. Lambda_plus = A * np.exp(-k * delta + k * (S - S[0]))
  21. Lambda_minus = A * np.exp(-k * delta - k * (S - S[0]))
  22. # 绘制参考价格和捕获流强度
  23. fig, ax1 = plt.subplots(figsize=(10, 6))
  24. # 绘制参考价格
  25. ax1.set_xlabel('Time')
  26. ax1.set_ylabel('Reference Price', color='tab:blue')
  27. ax1.plot(t, S, label='Reference Price', color='tab:blue')
  28. ax1.tick_params(axis='y', labelcolor='tab:blue')
  29. # 创建第二个y轴
  30. ax2 = ax1.twinx()
  31. ax2.set_ylabel('Intensity', color='tab:red')
  32. ax2.plot(t, Lambda_plus, label=r'$\Lambda^+(\delta, t, t + \Delta T)$', color='tab:red')
  33. ax2.plot(t, Lambda_minus, label=r'$\Lambda^-(\delta, t, t + \Delta T)$', color='tab:green')
  34. ax2.tick_params(axis='y', labelcolor='tab:red')
  35. # 设置图例
  36. fig.tight_layout()
  37. plt.title('Reference Price and Captured Flow Intensities')
  38. fig.legend(loc='upper left', bbox_to_anchor=(0.1,0.9))
  39. plt.grid(True)
  40. plt.show()