Python: repeating 2D random walk simulation

I am simulating a 2D random walk, with direction 0 <θ <2π and T = 1000 steps. I already have it:

a=np.zeros((1000,2), dtype=np.float)

def randwalk(x,y):
    theta=2*math.pi*rd.rand() # Theta is a random angle between 0 and 2pi
    x+=math.cos(theta);          # Since spatial unit = 1
    y+=math.sin(theta);          # Since spatial unit = 1
    return (x,y)

x, y = 0., 0.
for i in range(1000):
    x, y = randwalk(x,y)
    a[i,:] = x, y

      

This generates one walk and stores all intermediate coordinates in a numpy array a. How can I edit my code to repeat the walk 12 times (using each new random seed each time) and then save each run in a separate text file? Do I need a while loop in my randwalk function?

Guess:

rwalkrepeat = []

for _ in range(12):
    a=np.zeros((1000,2), dtype=np.float)
    x, y = 0., 0.
    for i in range(1000):
        x, y = randwalk(x,y)
        a[i,:] = x, y

rwalkrepeat.append(a)

print rwalkrepeat

      

+3


source to share


3 answers


An approach to this that follows the general form of your code:

import numpy as np
import matplotlib.pyplot as plt
import random as rd
import math

a=np.zeros((1000,2), dtype=np.float)

def randwalk(x,y):
    theta=2*math.pi*rd.random() 
    x+=math.cos(theta);          
    y+=math.sin(theta);          
    return (x,y)

fn_base = "my_random_walk_%i.txt"
for j in range(12):
    rd.seed(j)
    x, y = 0., 0.
    for i in range(1000):
        x, y = randwalk(x,y)
        a[i,:] = x, y
    fn = fn_base % j
    np.savetxt(fn, a)

      

For a basic calculation, panda -34 and NPE's answers are also good, and use numpy digitizing.



Here I used seed(j)

to explicitly set the seed to random numbers. The advantage of this is that each result will repeat itself as long as the seed is the same, even if, say, they are not executed sequentially, or you change the length of the array, etc. This is not necessary, 't want repeatable runs - then the random will be just a seed from time to time, and all random numbers in all runs will be different.

Explanation for filenames: Since the OP requested to save each of the multiple runs to different files, I thought it would be nice to have numbered files, like here my_random_walk_0.txt

, my_random_walk_1.txt

etc. In my example, I used name fn_base

as a variable to store the general format of the filename, so let's say the code fn = fn_base % 17

would set fn

equal my_random_walk_17.txt

(this is a bit old school in python, read about "string formatting" in python for more).

0


source


You don't need explicit loops. The whole solution can be vectorized (untested):



nsteps = 1000
nwalks = 12
theta = 2 * np.pi * np.random.rand(nwalks, nsteps - 1)
xy = np.dstack((np.cos(theta), np.sin(theta)))
a = np.hstack((np.zeros((nwalks, 1, 2)), np.cumsum(xy, axis=1)))

      

+2


source


If you are using numpy, why aren't you using numpy? I would do it like this:

n_moves = 1000
a = np.zeros((n_moves, 2))

for i in range(12):
    thetas = (2*np.pi) * np.random.rand(n_moves-1)
    a[1:,0] = np.cos(thetas)
    a[1:,1] = np.sin(thetas)
    a = np.add.accumulate(a, 0)

      

0


source







All Articles