0

I'm trying to basically code multiple random walks into my python code, I've been using the brownian motion scipy page as a starting point. I know I'm basically making a mistake in the updating of each random walk array in def updateData().

import math
import numpy as np
import matplotlib
import matplotlib.animation as animation
import pylab as plt
from matplotlib.pylab import *
from scipy.stats import norm
from mpl_toolkits.axes_grid1 import host_subplot


def brownian(x0, n, dt, delta, out=None):

    x0 = np.asarray(x0)

    r = norm.rvs(size=x0.shape + (n,), scale=delta*sqrt(dt))    

    if out is None:
        out = np.empty(r.shape)

    np.cumsum(r, axis=-1, out=out)

    out += np.expand_dims(x0, axis=-1)

    return out

x = numpy.empty((2,N+1))
x[:, 0] = 0.0

f0 = figure()
ax01 = subplot2grid((1, 1), (0, 0))

p011, = ax01.plot([],[],'b-')
p012, = ax01.plot([],[],'g-')

arr = []
def updateData(self):
    global yo
    global br


    for i in range(0,2):
        br = brownian(x[:,0], 10, 1, 0.25, out=None)
        arr.append(br)

    p011.set_data(arr[0])
    p012.set_data(arr[1])
    return p011,p012

simulation = animation.FuncAnimation(f0, updateData, 
                                     blit=False, frames=200, 
                                     interval=20000, repeat=False)

plt.show()

If I write following loop outside updateData:

arr = []
for i in range(0,2):
    br = brownian(x[:,0], 10, 1, 0.25, out=None)
    arr.append(br)

, I get the correct arrays for each random walk (arr[0], and arr[1]), however I am having a lot of trouble actually getting these to work in a subplot animation.

1 Answer 1

1

your problem is that you keep append'ing your new random walks to the array arrbut you always plot the same results (namely arr[0] and arr[1]) if you want to plot the latest results you should use:

def updateData(frame):
    for i in range(0,2):
        br = brownian(x[:,0], 10, 1, 0.25, out=None)
        arr.append(br)

    p011.set_data(arr[-2])
    p012.set_data(arr[-1])
    return p011,p012

EDIT: here is the full code that seem to be doing what you want

import math
import numpy as np
import matplotlib
import matplotlib.animation as animation
import pylab as plt
from matplotlib.pylab import *
from scipy.stats import norm
from mpl_toolkits.axes_grid1 import host_subplot
from JSAnimation.IPython_display import display_animation

def brownian(x0, n, dt, delta, out=None):
    x0 = np.asarray(x0)
    r = norm.rvs(size=x0.shape + (n,), scale=delta*sqrt(dt))    
    if out is None:
        out = np.empty(r.shape)
    np.cumsum(r, axis=-1, out=out)
    out += np.expand_dims(x0, axis=-1)
    return out

N=1
x = np.empty((2,N+1))
x[:, 0] = 0.0

f0 = figure()
ax01 = subplot2grid((1, 1), (0, 0))
ax01.set_xlim(-2,2)
ax01.set_ylim(-2,2)
p011, = ax01.plot([],[],'b-')
p012, = ax01.plot([],[],'g-')

arr = []
def updateData(self):
    for i in range(0,2):
        br = brownian(x[:,0], 100, 1, 0.25, out=None)
        arr.append(br)

    p011.set_data(arr[-2])
    p012.set_data(arr[-1])
    return p011,p012

simulation = animation.FuncAnimation(f0, updateData, blit=False, frames=200, interval=500, repeat=False)
Sign up to request clarification or add additional context in comments.

4 Comments

Hey, thank you for reply. Your answer makes sense, but when I try running my code I get this error; if self._size is not None and self._size > 1: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Hey, maybe I'm not fully describing my problem (or else I'm just confusing myself). My arr is an array containing two 2x(N+1) arrays. One for each random walk. The 2x(N+1) array represents the x and y co-ordinate (the 2) at each time step (N+1) of the random walk. I want my updateData() function to be able to continuously update the last x-y co-ordinate of each random walk, so both can be displayed together on the animation.
I've edited my answer to include the full code. As far as I understand, this does what you want. What I'm not clear about is whether you want to plot a new pair of random walks on each frame (that's what the code is doing) or if you want to add more steps to the random walk already started before?
Hey, thanks again for the reply. I just tried running your code, and it's not what I want the output to be. The code is generating two random walks, and plotting every data point in one animation, and then restarting. I want the animation to be a video of the random walk progression from time t0 to tN . It will look like this : youtube.com/watch?v=U2PbqzaLYCo but I want to be able to have two random walks on the same video (I am trying to do with with the for loop)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.