In [4]:
from __future__ import division
import scipy as sp, numpy as np, pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('darkgrid')
In [12]:
stepsize = 1.0
numsteps = 100
position = np.zeros(numsteps)
x = 0.0
for i in range(1,numsteps):
if np.random.rand() > 0.5:
x = x + stepsize
if np.random.rand() <= 0.5:
x = x - stepsize
position[i] = x
plt.plot(position)
Out[12]:
In [26]:
stepsize = 1.0
numsteps = 1000000
position = np.zeros(numsteps)
x = 0.0
for i in range(1,numsteps):
if np.random.rand() > 0.5:
x = x + stepsize
if np.random.rand() <= 0.5:
x = x - stepsize
position[i] = x
plt.plot(position)
Out[26]:
In [27]:
junk = plt.hist(position,50)
In [30]:
steps = np.random.rand(10)
print steps
In [33]:
steps[steps>0.5] = 1
print steps
In [34]:
steps[steps<0.5] = -1
print steps
In [35]:
onetraj = np.cumsum(steps)
plt.plot(onetraj)
Out[35]:
In [38]:
steps = np.random.rand(100000)
steps[steps>0.5] = 1
steps[steps<=0.5] = -1
onetraj = np.cumsum(steps)
plt.plot(onetraj)
plt.figure()
junk= plt.hist(onetraj,50)
In [39]:
plt.plot(onetraj**2)
Out[39]:
In [47]:
numtrajs = 500
numsteps = 2000
times = range(numsteps)
steps = np.random.random((numtrajs,numsteps))
steps[steps>0.5] = 1
steps[steps<=0.5] = -1
trajs = np.cumsum(steps,axis=1)
for traj in trajs:
plt.plot(times,traj,alpha=0.5)
plt.plot(times,np.average(trajs,axis=0),color='black',lw=2)
plt.title('individual trajectories')
plt.ylabel(r'$\left<x\right>$')
plt.xlabel('time')
plt.figure()
for traj in trajs:
plt.plot(times,traj**2,alpha=0.5)
plt.plot(times,np.average(trajs**2,axis=0),color='black',lw=2)
plt.title('means squared displacement')
plt.ylabel(r'$\left<x^2\right>$')
plt.xlabel('time')
Out[47]:
In [ ]: