当前位置: 首页 > news >正文

numpy学习笔记14:模拟随机游走过程(一次实验)

numpy学习笔记14:模拟随机游走过程(一次实验)

随机游走是一种数学统计模型,其中的每一步方向和大小都是随机的。下面使用 NumPy 模拟一维和二维的随机游走过程:

1.代码示例

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = Falsedef simulate_1d_random_walk(num_steps):"""模拟一维随机游走:param num_steps: 游走的步数:return: 一维随机游走的位置数组"""steps = np.random.choice([-1, 1], size=num_steps)positions = np.cumsum(steps)return positionsdef simulate_2d_random_walk(num_steps):"""模拟二维随机游走:param num_steps: 游走的步数:return: 二维随机游走的 x 和 y 坐标数组"""steps_x = np.random.choice([-1, 1], size=num_steps)steps_y = np.random.choice([-1, 1], size=num_steps)positions_x = np.cumsum(steps_x)positions_y = np.cumsum(steps_y)return positions_x, positions_y# 模拟一维随机游走
num_steps_1d = 1000
positions_1d = simulate_1d_random_walk(num_steps_1d)# 绘制一维随机游走轨迹
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(positions_1d)
plt.title('一维随机游走')
plt.xlabel('步数')
plt.ylabel('位置')# 模拟二维随机游走
num_steps_2d = 1000
positions_x, positions_y = simulate_2d_random_walk(num_steps_2d)# 绘制二维随机游走轨迹
plt.subplot(1, 2, 2)
plt.plot(positions_x, positions_y)
plt.title('二维随机游走')
plt.xlabel('X 位置')
plt.ylabel('Y 位置')plt.tight_layout()
plt.show()

  1. simulate_1d_random_walk 函数:该函数通过 np.random.choice 从 [-1, 1] 中随机选择 num_steps 个步长,然后使用 np.cumsum 计算累积和,得到一维随机游走的位置数组。
  2. simulate_2d_random_walk 函数:分别为 x 和 y 方向生成随机步长,再分别计算它们的累积和,得到二维随机游走的 x 和 y 坐标数组。
  3. 可视化部分:使用 matplotlib 绘制一维和二维随机游走的轨迹图。

2. 分步解释

(1) 生成随机步长
steps = np.random.choice([-1, 1], size=1000)
  • 功能:生成包含 1000 个元素的数组,每个元素随机为 -1(向左移动)或 1(向右移动)。

  • 概率:默认均匀分布,即 -1 和 1 出现的概率均为 50%。

(2) 计算累积位移
positions = np.cumsum(steps)
  • 功能:通过 np.cumsum() 对步长数组逐步累加,生成随时间变化的位置序列

(3) 可视化结果
plt.plot(positions)
  • 输出:绘制位置随时间变化的折线图,展示粒子的随机运动轨迹。

3. 示例输出图形

横轴为步数,纵轴为位置,展示粒子在直线上的随机移动轨迹。

4. 扩展分析

(1) 多次模拟实验的统计特性
# 模拟100次随机游走,观察平均行为
n_simulations = 100
final_positions = [np.sum(np.random.choice([-1,1], 1000)) for _ in range(n_simulations)]plt.hist(final_positions, bins=20, density=True)
plt.title("Distribution of Final Positions (100 Simulations)")
plt.xlabel("Final Position")
plt.ylabel("Probability Density")
plt.show()
  • 结果:最终位置近似服从正态分布(中心极限定理)。

(2) 均方位移分析

5. 关键参数调整

  • 非对称概率(如向右概率 70%):

    steps = np.random.choice([-1,1], size=1000, p=[0.3, 0.7])
  • 可变步长(如步长为 0.5 或 2):

    steps = np.random.choice([-0.5, 2], size=1000)

6. 应用场景

  1. 金融价格模型:模拟股票价格的随机波动。

  2. 分子扩散:研究微粒在液体中的布朗运动。

  3. 算法测试:评估路径规划或搜索算法的性能。


通过上述代码和分析,你可以灵活模拟不同条件下的随机游走,并深入理解其统计特性!


http://www.mrgr.cn/news/95117.html

相关文章:

  • std::expected
  • [入门]NUC13配置Ubuntu20.04详细步骤
  • 让AI看见世界:MCP协议与服务器的工作原理
  • AI学习——卷积神经网络(CNN)入门
  • P2786 英语1(eng1)- 英语作文
  • STM32原理性知识
  • SAP 附件增删改查与文件服务器交互应用
  • dijkstra算法——47. 参加科学大会
  • PostgreSQL:语言基础与数据库操作
  • 数据大屏标题加载顶部流光
  • LEDNet总结
  • Python Pyecharts面试题及参考答案
  • 数据结构-------栈
  • 【C++】动态规划从入门到精通
  • 详解Sympy:符号计算利器
  • MySQL 调优
  • Firebase崩溃:ViewBinding not init!!
  • Quartus + VScode 实现模块化流水灯
  • MySQL 入门大全:查询语言分类
  • 【免费网址/插件】视频和图片数据采集推荐~