Create plot for random walk

39 Views Asked by At

enter image description here

Is there any tool, that can create such a plot?

1

There are 1 best solutions below

5
On BEST ANSWER

The position of a randomly walking object is given by: $$ S_n=\sum_{i=1}^{n}X_i $$where in its simplest form, it is assumed that $X_i$s are iid and $\Pr\{X_i=1\}=\Pr\{X_i=-1\}={1\over 2}$. To sketch $S_n$ in terms of $n$, you can consider the following code blocks:

MATLAB

N = 70;                     % Number of steps
X = 2*(rand(1,N)<0.5)-1;    % For producing i.i.d. X random variables
S = cumsum(X);              % For the random walk sequence
stem(S)
title('Random walk vs time')

enter image description here

Python

import numpy
import matplotlib.pyplot as plt

N = 70                                # Number of steps
X = 2*(numpy.random.rand(N)<0.5)-1    # For producing i.i.d. X random variables
S = numpy.cumsum(X)                   # For the random walk sequence
plt.stem(S)
plt.title('Random walk vs time',fontsize=14)

enter image description here