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

testxia 2018-10-14

什么是随机游走?

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

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

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

设置您的jupyter notebook:

%pylab inline
from itertools import cycle
from mpl_toolkits.mplot3d import Axes3D
colors = cycle(‘bgrcmykbgrcmykbgrcmykbgrcmyk’)

1-D随机游走:

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

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

Python实现如下:

# Define parameters for the walk
dims = 1
step_n = 10000
step_set = [-1, 0, 1]
origin = np.zeros((1,dims))
# Simulate steps in 1D
step_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 path
fig = 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);

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

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模拟随机游走(Random walks)

Python代码如下:

# Define parameters for the walk
dims = 2
step_n = 10000
step_set = [-1, 0, 1]
origin = np.zeros((1,dims))
# Simulate steps in 2D
step_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 path
fig = 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);

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

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

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

3-D随机游走:

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

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

Python代码如下:

# Define parameters for the walk
dims = 3
step_n = 1000
step_set = [-1, 0, 1]
origin = np.zeros((1,dims))
# Simulate steps in 3D
step_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 path
fig = 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 = False
ax.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);

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

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

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

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

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

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

# Define parameters for the walk
dims = 3
n_runs = 10
step_n = 1000
step_set = [-1, 0 ,1]
runs = np.arange(n_runs)
step_shape = (step_n,dims)
# Plot
fig = 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 = False
ax.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);

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

相关推荐