Question

Problem 1: Random Walk A random walk is a stochastic process. A stochastic process is a series of values that are not determiStepl Start Step1 Step 3 Step 2 Step 4 Step 5 Figure 1: The first few steps of a random walk 1 import numpy as np 2 import maplt.plot([x[n-1]-10,x[n-11+10], [y[n-1.y[n-1]], r-) plt.plot([x[n-1].x[n-1],[y[n-11-10,y[n-1]+10], r- plt.savefig(rand_wa

Programming Problem 1: Random Walk Complete the step function. Experiment with different numbers of steps. Do you see any num

python / visual studio

Problem 1: Random Walk A random walk is a stochastic process. A stochastic process is a series of values that are not determined functionally, but probabilistically. The random walk is supposed to describe an inebriated person who, starting from the bar, intends to walk home, but because of intoxication instead randomly takes single steps either forward or backward, left or right. The person has no memory of any steps taken, so theoretically, the person shouldn't move too far from where he or she starts. Random walks are used to model many phenomena, like the size of the web or changes in financial instruments. We will model a 2D random walk with two arrays x and y where x represents moving left or right and y represents forward or backward. The index i will represent the step and xy[ will represent the location at step i. So, for i-0, we have x[0],y[o] (starting place). Using random we choose from the list [1,2,3,4]. If the value is one then we move right from the previous x position 1x[i-xi-11 If the value is two, then we move left from the previous x position: 1 x[i] x[i-1] -1 = If the value is three, we move up from the previous y position: 1 x[i]-x[i-1] 2 y[i]-yi-11 And when the value is four, we move down from the previous y position Here is another way to describe this step(0)0 eft step(n 1) step/(n)right step/n -1) up step(n -1) down step(n 1)
Stepl Start Step1 Step 3 Step 2 Step 4 Step 5 Figure 1: The first few steps of a random walk 1 import numpy as np 2 import matplotlib.pyplot as plt 3 import random as rn 5 6 #translates a random int into a step along the random walk 7 #parameters: int i for the step index, numpy array x for tracking the left. /right location at index i, 8 #numpy array y for tracking the forward/backward location at index i 9 #return: none 10 def step(x.y,i) direction rn.randint(1,4) #70 D0 : implement this function 12 13 15 def graphit(x,y,n) 16 plt . title( "Random {0} Walk1nLast Location {1},{2)".format(n, int(x(n- 17 18 19 1]),int(yn))) plt.plot(x.y) plt.plot([x[1],x y11-10,y[1+10], "b-" plt.plot([x[1]-10,x[1]+10], [y1].y "b-") Page 4 Assignment No10 Maps and Matrices and Correlation
plt.plot([x[n-1]-10,x[n-11+10], [y[n-1.y[n-1]], "r-") plt.plot([x[n-1].x[n-1],[y[n-11-10,y[n-1]+10], "r- plt.savefig("rand_walk"+str(n)+" .png", bbox_inches-"tight" ,dpi-600) plt.show() 20 21 23 25 26 nint (input("Number of steps:")) 27 28 xnp.zeros(n) 29 y - np.zeros (n) 30 31 for i in range(1,n) 32 step(x,y,i) 34 graphit(x,y,n) Session Output Number of Steps: 100000 Random 100000 Walk Last Location 236,27 150 100 50 -100 100-50 50 00 150 200 250 Figure 2: The blue cross is where we started and the red cross is where we ended
Programming Problem 1: Random Walk Complete the step function. Experiment with different numbers of steps. Do you see any number of steps that seems to always move significantly away from the start? Put your code into a new module named randomwalk.py
0 0
Add a comment Improve this question Transcribed image text
Answer #1

On experimenting over different number of steps, it is being observed that even for a large number of steps, the maximum distance in x-direction is +/- 11 and same goes for y direction over multiple runs. This may vary but not much.

The code with step function completed is present below

import numpy as np
import random as rn
import matplotlib.pyplot as plt

def step (x,y,i):
    direction = rn.randint(1,4)
    if (direction == 1) :
        x[i] = x[i-1] + 1
        y[i] = y[i-1]
    elif (direction == 2) :
        x[i] = x[i-1] - 1
        y[i] = y[i-1]
    elif (direction == 2) :
        x[i] = x[i-1]
        y[i] = y[i-1] + 1
    elif (direction == 3) :
        x[i] = x[i-1]
        y[i] = y[i-1] - 1


def graphit(x,y,n):
    plt.title("Random {0} Walk\nLast Location {1},{2}".format(n,
        int(x[n-1]),int(y[n-1])) )
    plt.plot(x,y)
    plt.plot([x[1],x[1]], [y[1]-10, y[1]+10], "b-")
    plt.plot([x[1]-10, x[1]+10], [y[1], y[1]], "b-")
    plt.plot([x[n-1]-10,x[n-1]+10], [y[n-1], y[n-1]], "r-")
    plt.plot([x[n-1], x[n-1]], [y[n-1]-10, y[n-1]+10], "r-")
    plt.savefig("rand_walk"+ str(n)+".png", bbox_inches="tight",dpi=600)
    plt.show()

n = int(input("Number of steps"))

x = np.zeros(n)
y = np.zeros(n)

for i in range(1,n):
    step(x,y,i)

graphit(x,y,n)

Add a comment
Know the answer?
Add Answer to:
Problem 1: Random Walk A random walk is a stochastic process. A stochastic process is a series of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python / visual studio Problem 1: Random Walk A random walk is a stochastic process. A...

    python / visual studio Problem 1: Random Walk A random walk is a stochastic process. A stochastic process is a series of values that are not determined functionally, but probabilistically. The random walk is supposed to describe an inebriated person who, starting from the bar, intends to walk home, but because of intoxication instead randomly takes single steps either forward or backward, left or right. The person has no memory of any steps taken, so theoretically, the person shouldn't move...

  • A random walk is a particular kind of probabilistic (pseudo-random) simulation that models certai...

    A random walk is a particular kind of probabilistic (pseudo-random) simulation that models certain statistical systems, such as Brownian motion of particles or molecules. Coin flipping is an example of a one-dimensional random walk--one dimensional because you only can go forward (when you flip heads) or backward (when you flip tails) along a straight line. Suppose you take a random walk of nsteps. How many steps away from your starting point would you expect to end up on average, if...

  • 1-D Random Walk: Consider a random walk described by the following probability rules: P(+x) = 0.5...

    1-D Random Walk: Consider a random walk described by the following probability rules: P(+x) = 0.5; P(-x) = 0.1 ; P(ty) = 0.2; P(-y) = 0.2 (a) Is the walk biased? If so in which direction? Explain. (b) Compute the following for N steps if the step size is equal to a: <x, y>, <x>, <y'> (c) After long time (after large number of steps, where would the object be found? (find Ox, Ox I. 1-D Random Walk: Consider a...

  • 1. Random Walk: Consider a random walk described by the following probability rules: P(+x) 0.5; P...

    1. Random Walk: Consider a random walk described by the following probability rules: P(+x) 0.5; P(-x) 0.1; P(ty) 0.2; P(-y) 0.2 (a) Is the walk biased? If so in which direction? Explain. (b) Compute the following for N steps if the step size is equal to a: <x>, <y>, <x>, <y (c) After long time (after large number of steps, where would the object be found? (find σ, and 1. Random Walk: Consider a random walk described by the following...

  • (Intro to Java help?) Define a class named RandomWalker. A RandomWalker object should keep track of...

    (Intro to Java help?) Define a class named RandomWalker. A RandomWalker object should keep track of its (x, y) location. All walkers start at the coordinates (0, 0). When a walker is asked to move, it randomly moves either left, right, up or down. Each of these four moves should occur with equal probability. The resulting behavior is known as a "random walk." (A 2-dimensional random walk example is pictured at right.) Each RandomWalker object should have the following public...

  • An ant is taking a two-dimensional random walk on a flat surface. We will distinguish steps...

    An ant is taking a two-dimensional random walk on a flat surface. We will distinguish steps that the ant takes with its feet from the random walk (RW) steps, of length δ, which are made up of lots of footsteps. In a RW step, the ant choose a direction randomly and walks distance δ in that direction. She then choose another direction randomly and walks distance δ in that direction. She repeats this for a total of n RW steps....

  • We will be using Matlab as the software tool for this lab project. 1.Explore and become...

    We will be using Matlab as the software tool for this lab project. 1.Explore and become familiar with the random number generators in Matlab such as “rand”, “randn”, and “randi” (in fact all software tools can only generate pseudo-random numbers). Notice that to randomize the generators the use of “rng” function is encouraged as the previous techniques are discouraged. 2. “Random walk” is one of the primary and relative simple random processes in which the process starts from a neutral...

  • Simulate how many steps its take a random walker starting at the center of an 10x10...

    Simulate how many steps its take a random walker starting at the center of an 10x10 grid to visit every cell of the grid. If the walker tries to go outside of the grid then it doesn't move in that step. Write a java program which simulates th steps of the random walker, and keeps hold about the grid with 2D boolean array and write out the steps when the random walker have walked all the cells. ----------------------------------------------------------------------------------------------- I have...

  • I have the following java-program, that creates a random walk starting at (0,0) and continuing until...

    I have the following java-program, that creates a random walk starting at (0,0) and continuing until x or y is greater than abs(n). First: I get an error, when I try closing the Scanner by inserting "reader.close(); " in line 28. It says "Unreachable code". How can I fix that? Second: Is it possible to create a different colour for the StdDraw.point when it reaches a point on one of the edges "n+1" or "n-1" ? 1 import java.util.*; 3...

  • 2. Problem 2.5. Consider a random walk on 10..... which movies left and right with respective...

    2. Problem 2.5. Consider a random walk on 10..... which movies left and right with respective probabilities a and p. If the walk is at 0 it transitions to 1 on the next step. If the walk is at k it transitions to k-1 on the next step. This is called random walk with reflecting boundaries. Assume that k 3, =1/4, p = 3/4, and the initial distribution is uniform. For the following, use technology if needed. (a) (10.1.X2 }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT