打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用Python模拟随机游走(Random walks)

什么是随机游走?

随机游走(random walk)也称随机漫步,随机行走等,是以随机的方式采取连续步骤的过程。然后,可以将其他条件应用于此描述,以为您的特定用例创建一个随机遍历。粒子的布朗运动,股票代码运动,基质中的活细胞运动只是在现实世界中看到的一些更为人所知的随机游走。

在这里,我们模拟从原点开始的1-D,2-D和3-D的简化随机游走以及从[-1,0,1]中选择的具有相等概率的离散步长。起点表示+,停止点表示o。

对于不同的应用程序,这些条件会根据需要发生变化,例如从选定的股票价格开始游走,用显微镜检测到的初始细胞位置等,steps的选择通常是概率性的,并且取决于来自past data, projection assumptions, hypothesis being tested等的附加信息。

设置您的jupyter notebook:

%pylab inlinefrom itertools import cyclefrom mpl_toolkits.mplot3d import Axes3Dcolors = cycle(‘bgrcmykbgrcmykbgrcmykbgrcmyk’)

1-D随机游走:

我们从原点出发(y=0),并选择一个step,以相等的概率移动每一个连续的step。起点用红色表示,终点用黑色表示。在下面的图中绘制了一个累加和,其中显示了在1D中10k步之间的轨迹。

Python实现如下:

# Define parameters for the walkdims = 1step_n = 10000step_set = [-1, 0, 1]origin = np.zeros((1,dims))# Simulate steps in 1Dstep_shape = (step_n,dims)steps = np.random.choice(a=step_set, size=step_shape)path = np.concatenate([origin, steps]).cumsum(0)start = path[:1]stop = path[-1:]# Plot the pathfig = plt.figure(figsize=(8,4),dpi=200)ax = fig.add_subplot(111)ax.scatter(np.arange(step_n+1), path, c=’blue’,alpha=0.25,s=0.05);ax.plot(path,c=’blue’,alpha=0.5,lw=0.5,ls=’ — ‘,);ax.plot(0, start, c=’red’, marker=’+’)ax.plot(step_n, stop, c=’black’, marker=’o’)plt.title(‘1D Random Walk’)plt.tight_layout(pad=0)plt.savefig(‘plots/random_walk_1d.png’,dpi=250);

2-D随机游走:

我们从原点(x = 0,y = 0)开始,并在每个方向上采取随机步骤,给出9个可能的每个步骤移动方向(Δx,Δy)⋲{-1,0,1} :

(-1,-1), (-1,0), (-1,1),

(0,-1), (0,0), (0,1),

(1,-1), (1,0), (1,1)

超过10k步的模拟为我们提供了以下轨迹。在流体表面运动的粒子具有二维随机游走,并显示如下轨迹。

Python代码如下:

# Define parameters for the walkdims = 2step_n = 10000step_set = [-1, 0, 1]origin = np.zeros((1,dims))# Simulate steps in 2Dstep_shape = (step_n,dims)steps = np.random.choice(a=step_set, size=step_shape)path = np.concatenate([origin, steps]).cumsum(0)start = path[:1]stop = path[-1:]# Plot the pathfig = plt.figure(figsize=(8,8),dpi=200)ax = fig.add_subplot(111)ax.scatter(path[:,0], path[:,1],c=’blue’,alpha=0.25,s=0.05);ax.plot(path[:,0], path[:,1],c=’blue’,alpha=0.5,lw=0.25,ls=’ — ‘);ax.plot(start[:,0], start[:,1],c=’red’, marker=’+’)ax.plot(stop[:,0], stop[:,1],c=’black’, marker=’o’)plt.title(‘2D Random Walk’)plt.tight_layout(pad=0)plt.savefig(‘plots/random_walk_2d.png’,dpi=250);

多个2D随机游走的示例:

3-D随机游走:

我们从原点(x = 0,y = 0,z = 0)开始,并从一组27个方向(Δx,Δy,Δz)⋲{-1,0,1}中 选择一个随机方式的steps:

Python代码如下:

# Define parameters for the walkdims = 3step_n = 1000step_set = [-1, 0, 1]origin = np.zeros((1,dims))# Simulate steps in 3Dstep_shape = (step_n,dims)steps = np.random.choice(a=step_set, size=step_shape)path = np.concatenate([origin, steps]).cumsum(0)start = path[:1]stop = path[-1:]# Plot the pathfig = plt.figure(figsize=(10,10),dpi=200)ax = fig.add_subplot(111, projection=’3d’)ax.grid(False)ax.xaxis.pane.fill = ax.yaxis.pane.fill = ax.zaxis.pane.fill = Falseax.set_xlabel(‘X’)ax.set_ylabel(‘Y’)ax.set_zlabel(‘Z’)ax.scatter3D(path[:,0], path[:,1], path[:,2], c=’blue’, alpha=0.25,s=1)ax.plot3D(path[:,0], path[:,1], path[:,2], c=’blue’, alpha=0.5, lw=0.5)ax.plot3D(start[:,0], start[:,1], start[:,2], c=’red’, marker=’+’)ax.plot3D(stop[:,0], stop[:,1], stop[:,2], c=’black’, marker=’o’)plt.title(‘3D Random Walk’)plt.savefig(‘plots/random_walk_3d.png’,dpi=250);

在3D中模拟k个随机游走

现在我们在3D中模拟多个随机游走。每个随机游走表示点源的运动同时开始,起点设置在从(x,y,z)⋲[-10,10]中选择的点。

一些细胞/粒子在没有任何持续方向力的情况下运动,会出现这样的轨迹。三维随机游走的一个有趣的方面是,即使起点很近,随着时间的推移,对象会散开。

我们可以通过不同的测量方法来进行描述分析(距离,位移,速度,速度,角度分布,指示器计数,约束比等等)。我们还可以模拟directed/biased 随机游走,其中下一步取决于当前位置,或者由于某种形式的现有梯度或方向力。

# Define parameters for the walkdims = 3n_runs = 10step_n = 1000step_set = [-1, 0 ,1]runs = np.arange(n_runs)step_shape = (step_n,dims)# Plotfig = plt.figure(figsize=(10,10),dpi=250)ax = fig.add_subplot(111, projection=’3d’)ax.grid(False)ax.xaxis.pane.fill = ax.yaxis.pane.fill = ax.zaxis.pane.fill = Falseax.set_xlabel(‘X’)ax.set_ylabel(‘Y’)ax.set_zlabel(‘Z’) for i, col in zip(runs, colors): # Simulate steps in 3D origin = np.random.randint(low=-10,high=10,size=(1,dims)) steps = np.random.choice(a=step_set, size=step_shape) path = np.concatenate([origin, steps]).cumsum(0) start = path[:1] stop = path[-1:] # Plot the path ax.scatter3D(path[:,0], path[:,1], path[:,2], c=col,alpha=0.15,s=1); ax.plot3D(path[:,0], path[:,1], path[:,2], c=col, alpha=0.25,lw=0.25) ax.plot3D(start[:,0], start[:,1], start[:,2], c=col, marker=’+’) ax.plot3D(stop[:,0], stop[:,1], stop[:,2], c=col, marker=’o’);plt.title(‘3D Random Walk - Multiple runs’)plt.savefig(‘plots/random_walk_3d_multiple_runs.png’,dpi=250);

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
如何驾驭Matplotlib?<Part4>
小米机械狗CyberDog云体验
SciencePlots | 科研样式绘图库
Python数据可视化之matplotlib
A D3 Viewer for Matplotlib Visualizations
要想成为一名月入几万的数据分析师,绘制图表是必会的!实例教程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服