前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python数据分析(中英对照)·Random Walks 随机游走

Python数据分析(中英对照)·Random Walks 随机游走

作者头像
数媒派
发布2022-12-01 15:13:02
4720
发布2022-12-01 15:13:02
举报
文章被收录于专栏:产品优化

2.4.5: Random Walks 随机游走

This is a good point to introduce random walks. 这是引入随机游动的一个很好的观点。 Random walks have many uses. 随机游动有许多用途。 They can be used to model random movements of molecules, 它们可以用来模拟分子的随机运动, but they can also be used to model spatial trajectories of people, 但它们也可以用来模拟人的空间轨迹, the kind we might be able to measure using GPS or similar technologies. 我们可以用GPS或类似的技术来测量。 There are many different kinds of random walks, and properties of random walks 有许多不同种类的随机游动,以及随机游动的性质 are central to many areas in physics and mathematics. 是物理学和数学许多领域的核心。 Let’s look at a very basic type of random walk on the white board. 让我们看看白板上一种非常基本的随机行走。 We’re first going to set up a coordinate system. 我们首先要建立一个坐标系。 Let’s call this axis "y" and this "x". 我们把这个轴叫做“y”,这个叫做“x”。 We’d like to have the random walk start from the origin. 我们想让随机游动从原点开始。 So this is position 1 for the random walk. 这是随机游动的位置1。 To get the position of the random walker at time 1, we can pick a step size. 为了得到时间1时随机行走者的位置,我们可以选择一个步长。 In this case, I’m just going to randomly draw an arrow. 在这种情况下,我将随机画一个箭头。 And this gives us the location of the random walker at time 1. 这给了我们时间1的随机游走者的位置。 So this point here is time is equal to 0. 这里的时间等于0。 And this point here corresponds to time equal to 1. 这一点对应于等于1的时间。 We can take another step. 我们可以再走一步。 Perhaps in this case, we go down, say over here. 也许在这种情况下,我们下去,比如说在这里。 And this is our location for the random walker at time t is equal to 2. 这是时间t等于2时,随机游走者的位置。 This is the basic idea behind all random walks. 这是所有随机游动背后的基本思想。 You have some location at time t, and from that location 你在时间t有一个位置,从这个位置开始 you take a step in a random direction and that generates your location 你在一个随机的方向上迈出一步,这就产生了你的位置 at time t plus 1. 在时间t加1时。 Let’s look at these a little bit more mathematically. 让我们从数学的角度来看这些。 First, we’re going to start with the location of the random walk at time t 首先,我们从时间t的随机游动的位置开始 is equal to 0. 等于0。 So position x at time t is equal to 0 is whatever 所以时间t处的位置x等于0是什么 the location of the random walker is at the beginning of the random walk. 随机行走的位置在随机行走的开始处。 We’re going to call that x0. 我们称之为x0。 These are step 0. 这些是步骤0。 The next step is going to be the location of the random walker at time t 下一步将是时间t处随机行走器的位置 is equal to 1. 等于1。 This will be given to us by the location of the random walk at the previous time 这将由上一次随机行走的位置提供给我们 step, t is equal to 0, plus some displacement, delta x, 阶跃t等于0,加上一些位移,δx, at time t equal to 1. 在时间t等于1时。 Because x at t0 is simply given by this term over here, 因为t0处的x由这里的这个项给出, we can simplify this to x0 plus delta x at time t equal to 1. 我们可以将其简化为时间t等于1时的x0加δx。 Let’s then look at the next step. 然后让我们看下一步。 The location of the random walker at time t is equal to 2. 时间t处随机行走者的位置等于2。 Again, the basic idea is the same. 同样,基本思想是相同的。 The new location is going to be the old location, meaning x at time t 新位置将是旧位置,表示时间t的x is equal to 1 plus some random displacement delta x at time t 等于1加上时间t的一些随机位移δx is equal to 2. 等于2。 Note that the index of the random displacement 注意,随机位移的指数 matches the index of time over here. 匹配这里的时间索引。 From what we have above, we see that we have the location at time t equal to 1 从上面我们可以看到,时间t的位置等于1 here, which we know to be equal to this term over here. 这里,我们知道它等于这里的这个项。 What that means, I can now rewrite this part as this expression here. 这意味着,我现在可以把这部分改写成这个表达式。 So we have x0 plus delta x at time t equal to 1 plus delta x at t 所以我们有x0加上时间t的δx等于1加上时间t的δx equal to 2. 等于2。 This term over here is just the same term that we had above here. 这里的术语和上面的术语是一样的。 All we’ve done is we’ve just copied that term over here. 我们所做的只是在这里复制了这个术语。 We can see that a common pattern starts to emerge. 我们可以看到,一个共同的模式开始出现。 To note the location of the random walker at time 2, 要记下随机行走器在时间2的位置, we had the initial location plus displacement at time t equal to 1 初始位置加上时间t的位移等于1 and t equal to 2. t等于2。 From this we can write down the general equation 由此我们可以写出一般方程 for the location of the random walk. 用于随机行走的位置。 We start with x at some point in time, t is equal to k, for example. 我们从某个时间点的x开始,例如,t等于k。 We will always have the initial location x0, 我们将始终拥有初始位置x0, and this is going to be followed by a sum of displacements. 然后是位移的总和。 The first displacement is going to be this term delta x at time t equal to 1. 第一个位移是时间t等于1时的δx。 This will be added to the displacement delta x at t equal to 2 all the way 这将被添加到t等于2的位移增量x中 to displacement delta x that corresponds to t equal to k. 对应于t等于k的位移增量x。 In other words, the location of the random walker at any point in time 换句话说,随机游走者在任何时间点的位置 is given by the initial location x0 plus the sum of all of the displacements 由初始位置x0加上所有位移之和得出 from 1 to k. 从1到k。 Let’s review the logic of the argument here. 让我们回顾一下这个论点的逻辑。 First we start at time t is equal to 0, which 首先,我们从时间t等于0开始,这 we decided we’re going to call x0. 我们决定打电话给x0。 Then at time t equals 1, we take the previous location 然后在时间t等于1时,我们取上一个位置 that we had at time t equal to 0, and then we 时间t等于0,然后我们 add a random displacement to that. 添加一个随机的位移。 That gives us location at time is equal to 1. 这使得我们在时间上的位置等于1。 I’m going to use the blue color for this. 我打算用蓝色来做这个。 Then when we look at the location of the random walker at time t is equal to 2, 然后当我们看随机游走者在时间t等于2的位置时, we take the location at t is equal to 1 as a starting point. 我们以t等于1的位置作为起点。 Again we add the random displacement. 我们再加上随机位移。 The final result is the following. 最终结果如下。 To find out the location of the random walker at time t 找出随机行走者在时间t的位置 is equal to k, we take the initial locations of the random walk, 等于k,我们取随机游动的初始位置, and to that we add a sequence of random displacements. 在此基础上,我们添加了一系列随机位移。 If we’re interested in the location after k steps, 如果我们对k步后的位置感兴趣, then we will have k such random displacements in the sum. 那么我们将有k个这样的随机位移。 We’ve just seen that the location of the random walker, at any given time t, 我们刚刚看到随机游走者的位置,在任何给定的时间t, is given by a cumulative sum of the previous t displacements 由先前t位移的累积和得出 leading up to that point. 直到那一点。 Let’s try coding up random walker in NumPy, where we start at the origin, 让我们试着在NumPy编写random walker,从原点开始, take 100 steps, and where each step is sampled 采取100个步骤,每一步都取样 from the standard normal distribution. 从标准正态分布。 The normal distribution having mean 0 and standard deviation equal to 1. 平均值为0且标准偏差等于1的正态分布。 We’re also going to assume that the x displacement and the y 我们还要假设x位移和y displacement for any given step are independent, such 任何给定阶跃的位移都是独立的,例如 that the displacement in the horizontal direction 水平方向上的位移 has nothing to do with the displacement in the vertical direction. 与垂直方向上的位移无关。 We can start by generating the displacements. 我们可以从产生位移开始。 I’m again going to keep the numbers small and started with just five steps. 我将再次保持数字较小,从五个步骤开始。 Let’s generate a two by five table of numbers for each element, 让我们为每个元素生成一个二乘五的数字表, each of the 10 numbers is drawn independently 10个数字中的每一个都是独立绘制的 from a standard normal distribution. 从标准正态分布。 To do this, we will use the np.random.normal function. 为此,我们将使用np.random.normal函数。 In this case, we’ll be typing np.random.normal. 在本例中,我们将键入np.random.normal。 The first argument is the location or the mean. 第一个参数是位置或平均值。 The second one is the standard deviation, which is 1. 第二个是标准偏差,即1。 And we wanted to generate a two by five table, two rows and five columns. 我们想要生成一个二乘五的表,两行五列。 And we can try running this a couple of times 我们可以试着运行几次 to see that the dimension of the table matches what the expectation was. 查看表的维度是否与预期相符。 I’m going to call this table delta x, because this encodes 我将这个表称为delta x,因为它编码 the displacements of the random walker. 随机行走者的位移。 So delta x equals np.random.normal. 所以δx等于np.random.normal。 Let’s then pull up these displacements. 让我们把这些位移拉上来。 We can take the first component of each vector 我们可以取每个向量的第一个分量 as the x-coordinate and the second component as the y coordinate. 作为x坐标,第二个组件作为y坐标。 We’re going to be using plt plot for this. 我们要用plt图来做这个。 So we’ll say plt.plot. 所以我们说plt.plot。 We’ll be taking the delta x array. 我们将使用delta x阵列。 We would like to get row 0 from that array. 我们希望从该数组中获取第0行。 That’s our x component. 这是我们的x分量。 And our y component, the second argument of the plot function, y分量,plot函数的第二个参数, is going to be delta x1, which is row one of delta x. 将是δx1,它是δx的第一行。 Then as the third argument, I can specify the color and marker. 然后作为第三个参数,我可以指定颜色和标记。 I like to be using green circles for this plot. 我喜欢在这个图中使用绿色圆圈。 And then we run the code. 然后我们运行代码。 What is shown here are the displacements that we 这里显示的是我们所看到的位移 generated from the normal distribution. 由正态分布生成。 It may seem like we’re just getting started with this example, 看起来我们才刚刚开始这个例子, but in fact we’re almost done. 但事实上我们差不多完成了。 The one missing piece that we need is what is called a cumulative sum. 我们需要的一个缺失部分是所谓的累积总和。 Let’s first look at what that means. 让我们先看看这意味着什么。 Let’s have some numbers– 2, 4, 1, 3, and 2. 让我们看一些数字——2,4,1,3和2。 Underneath we’ll write the cumulative sum. 我们将在下面写下累计金额。 The first element is just going to be equal to 2, the first element here. 第一个元素等于2,这里的第一个元素。 The next element is going to be whatever we had here plus the element above. 下一个元素是我们在这里得到的,加上上面的元素。 2 plus 4 gives us 6. 2加4等于6。 This element is going to be 6 plus 1, that’s 7. 这个元素是6加1,也就是7。 7 plus 3, that’s 10. 7加3等于10。 10 plus 2, that’s 12. 10加2等于12。 But how can we do a cumulative sum in NumPy? 但是我们怎么能用NumPy来计算累积总数呢? Let’s just Google that to find out. 让我们用谷歌搜索一下。 Cumulative sum in NumPy. 以努比为单位的累计金额。 In this case the first hit gives us what we would like. 在这种情况下,第一次点击给了我们想要的。 NumPy has a function called cumulative sum, NumPy有一个名为累积和的函数, and it requires at least one argument. 它至少需要一个参数。 In this case the function requires one compulsory argument, which 在这种情况下,函数需要一个强制参数,该参数 is an array, in this case called a. 是一个数组,在本例中称为a。 The second argument, axis, which is optional, 第二个参数axis是可选的, specifies the axis along which the cumulative sum will be computed. 指定将沿其计算累积和的轴。 Let’s practice the use of the cumulative sum. 让我们练习累计和的用法。 np.cumsum. np.cumsum。 It’s going to be applied to delta x, which is the first argument. 它将应用于delta x,这是第一个参数。 And the second argument specifies the axis. 第二个参数指定轴。 We’re going to be using axis equals 1 because we 我们将使用轴等于1,因为我们 would like to take the cumulative sum over the columns of this array. 要获取此数组列的累积和。 I would like to store this result. I’m going 我想存储这个结果。我要走了 to store that in X– that’s capital X. And if we 将其存储在X中,即大写X,如果我们 look at the contents of the array, we will 看看数组的内容,我们将 see that we now have a cumulative sum taking over the columns. 请注意,我们现在有一个累积的总和接管了这些列。 We can contrast this with delta x, which only gives us the increments that we 我们可以将其与delta x进行对比,它只提供我们需要的增量 sampled from the normal distribution. 从正态分布中取样。 Let’s now put the different pieces together. 现在让我们把不同的部分放在一起。 First we generate the random displacements, np.random.normal, 首先我们生成随机位移,np.random.normal, mean 0, standard deviation 1. 平均值为0,标准偏差为1。 And the size of the array is two by five. 数组的大小是2乘5。 This gives us our delta x, our random placements. 这给了我们δx,我们的随机位置。 On the following line we’d like to define 在下面的一行中,我们要定义 x, which is the position of the random walker at any given time. x、 这是任意给定时间内随机行走者的位置。 So we apply the cumulative sum here, applied to delta x, 我们在这里应用累积和,应用于δx, and axis being set to 1. 轴被设置为1。 Finally, we want to plot this using plt plot. 最后,我们希望使用plt plot来绘制此图。 First we’ll plot the row 0 of our X array 首先,我们将绘制X数组的第0行 and then we plot row 1 of the array. 然后我们画出数组的第1行。 Let’s try running the code. 让我们试着运行代码。 Let’s try making a small modification to the plot. 让我们试着对情节稍作修改。 I would like to be using red circular markers, 我想用红色的圆形标记, and I would like them to be connected with straight lines. 我希望它们用直线连接。 I would then like to save this figure. 然后我想保存这个数字。 So I’m going to be using this Savefig command. 所以我将使用这个Savefig命令。 And I’m just going to call this rw, short for random walk. 我把这个叫做rw,是随机行走的缩写。 And I’d like that to be a PDF file. 我希望这是一个PDF文件。 I will then run the code. 然后我将运行代码。 And let’s look for the PDF file on my computer. 让我们在我的电脑上查找PDF文件。 This shows us the first four steps of the random walk, 这向我们展示了随机行走的前四个步骤, but there is one problem here. 但这里有一个问题。 Remember how the random walk was supposed to start at the origin, 还记得随机行走是如何从原点开始的吗, at location is 0,0? 位置是0,0? Right now we’re missing that very first point. 现在我们错过了第一点。 But let’s add that element to our array. 但让我们将该元素添加到数组中。 In this case the origin is represented by a two by one array consisting of 0s. 在这种情况下,原点由一个由0组成的二乘一数组表示。 And we’d like to concatenate that to our array called X. Let’s again use Google 我们想把它连接到我们称为X的数组中。让我们再次使用Google to find out how to do that. 想知道怎么做。 How to concatenate NumPy arrays? 如何连接NumPy数组? Again, the first hit looks promising. 再一次,第一个打击看起来很有希望。 In this case we need to provide two arguments. 在本例中,我们需要提供两个参数。 The first argument is a tuple of the arrays 第一个参数是数组的元组 that we would like to concatenate. 我们想要连接的。 The second argument, axis, is the axis along which the arrays will be joined. 第二个参数axis是阵列将沿其连接的轴。 So the missing step for us to do is to generate the array X_0. 因此,我们缺少的步骤是生成数组X_0。 And we know that this is an NumPy array. 我们知道这是一个NumPy数组。 It consists of two rows, each having just one element, 0. 它由两行组成,每行只有一个元素0。 These array X_0 gives us the initial location of the random walker. 这些数组X_0为我们提供了随机游走器的初始位置。 Let me take this line of code and move the top here in my window above. 让我把这行代码移到上面窗口的顶部。 And we can now write the concatenation function. 现在我们可以编写连接函数了。 We start with np.concatenate and we need to provide two arguments. 我们从np.concatenate开始,需要提供两个参数。 First is a tuple of the arrays that we would like to concatenate. 第一个是我们想要连接的数组元组。 And a second argument, axis, specifies the dimension of concatenation. 第二个参数axis指定连接的维度。 In this case, axis is going to be equal to 1. 在这种情况下,轴将等于1。 Let’s now look at the tuple argument a little bit more carefully. 现在让我们更仔细地看一下元组参数。 The first array is going to be X0, the starting point of the random walk. 第一个数组是X0,随机游动的起点。 For the second argument, we will need to insert our cumulative sum from above. 对于第二个参数,我们需要插入上面的累积总和。 So this is the cumulative sum applied over delta x along axis 1. 这是沿轴1的增量x的累积和。 This entire expression here will give us the location 这里的整个表达式将为我们提供位置 of the random walker, which is x, this time including 随机游走者,也就是x,这次包括 the origin as the starting point of the walk. 原点作为行走的起点。 We can now inspect the array X. And we’ll see that the first element is 0. 现在我们可以检查数组X。我们将看到第一个元素是0。 We are now ready for our final solution. 我们现在已经为最终解决方案做好了准备。 Let’s first copy this bit of code from here. 让我们首先从这里复制这段代码。 This replaces our previous definition of X. So we now define X0, 这取代了我们之前对X的定义。所以我们现在定义X0, we generate our delta x’s, we then define 我们生成delta x,然后定义 X the location of the random walk, and we then make the plot. X随机游动的位置,然后我们画出图。 Finally, we’ll save the figure. 最后,我们将保存该数字。 I’m going to save the figure now to a file called rw2 我现在将把这个数字保存到一个名为rw2的文件中 and we can run the code. 我们可以运行代码。 Let’s again look at the PDF version of the plot that we just generated. 让我们再来看看刚才生成的绘图的PDF版本。 We’re looking for rw2. 我们在找rw2。 And we can see that this time the random walk does start at the location 0,0, 我们可以看到这次随机游动确实从0,0开始, at the bottom of the figure. 在图的底部。 Let’s now run this 100 times. 现在让我们运行100次。 We’re going to change the argument here that specifies 我们将在这里更改指定 how many random displacements I would like to generate from my random walker. 我想从我的随机漫游器生成多少随机位移。 I’m going to save this in file rw3 我将把它保存在rw3文件中 and then we’ll run the code. 然后我们将运行代码。 We can look at the file, 我们可以看看档案, searching for rw3, 正在搜索rw3, and this is our random walker which takes, in this case, 100 steps. 这是我们的随机步行器,在这个例子中,需要100步。 To wrap this up, let’s generate a couple of realizations of random walk 最后,让我们生成两个随机行走的实现 taking 10,000 steps. 采取10000个步骤。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2.4.5: Random Walks 随机游走
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档