In [1]:
import scipy as sp, numpy as np, pandas as pd
from matplotlib import pyplot as plt

1D Random walk

smaller title

$ \sum_1^100{1+i}$


In [22]:
np.random.rand()


Out[22]:
0.6076265125647292

In [39]:
stepsize = 1
numsteps = 100
position = []
x = 0
for i in range(numsteps):
    number = np.random.rand()
    if number < 0.5:
        x = x - stepsize
    else:
        x = x + stepsize
    position = position + [x]
    # could have typed
    # position.append(x)

In [40]:
plt.plot(position)


Out[40]:
[<matplotlib.lines.Line2D at 0x1198b1ac8>]

In [41]:
pos = np.array(position)

In [34]:
np.average(pos)


Out[34]:
8.322666666666667

In [35]:
pos[-10]


Out[35]:
51

In [36]:
np.std(pos)


Out[36]:
17.009053066594337

In [42]:
df = pd.DataFrame(pos)

In [43]:
df.plot()


Out[43]:
<matplotlib.axes._subplots.AxesSubplot at 0x119669710>

In [44]:
df.rolling(window=10).mean().plot()


Out[44]:
<matplotlib.axes._subplots.AxesSubplot at 0x119669668>

In [45]:
roll = df.rolling(window=10)

In [46]:
m = roll.mean()

In [47]:
m.plot()


Out[47]:
<matplotlib.axes._subplots.AxesSubplot at 0x11af67cf8>

In [ ]: