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')

1D diffusion


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]:
[<matplotlib.lines.Line2D at 0x109eacf90>]

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]:
[<matplotlib.lines.Line2D at 0x10eb52e50>]

In [27]:
junk = plt.hist(position,50)



In [30]:
steps = np.random.rand(10)
print steps


[ 0.06953614  0.34051282  0.61525575  0.65629535  0.6770988   0.96105553
  0.99355651  0.13719106  0.38005113  0.32881058]

In [33]:
steps[steps>0.5] = 1
print steps


[ 0.06953614  0.34051282  1.          1.          1.          1.          1.
  0.13719106  0.38005113  0.32881058]

In [34]:
steps[steps<0.5] = -1
print steps


[-1. -1.  1.  1.  1.  1.  1. -1. -1. -1.]

In [35]:
onetraj = np.cumsum(steps)
plt.plot(onetraj)


Out[35]:
[<matplotlib.lines.Line2D at 0x10df6d150>]

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]:
[<matplotlib.lines.Line2D at 0x114fec250>]

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]:
<matplotlib.text.Text at 0x11efdab90>

In [ ]: