random pythonpython游⾛代码_⽤Python模拟随机游⾛(Randomwalks)什么是随机游⾛?
随机游⾛(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 =
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);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论