In [167]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import math

In [120]:
def random_walk():
    x = np.array([0])
    for i in range(10000):
        x = np.append(x, x[i]+np.random.choice([1, -1]))
    return x

In [40]:
x = random_walk()
plt.plot(x)


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

In [41]:
plt.ylim(-10, 10)
plt.xlim(0, 100)
plt.plot(x)


Out[41]:
[<matplotlib.lines.Line2D at 0x10babe390>]

In [46]:
plt.ylim(-30, 30)
plt.xlim(0, 1000)
plt.plot(x)


Out[46]:
[<matplotlib.lines.Line2D at 0x10d9d6710>]

In [50]:
plt.ylim(-150, 150)
plt.xlim(0, 10000)
plt.plot(x)


Out[50]:
[<matplotlib.lines.Line2D at 0x11039cc90>]

In [28]:
for i in range(5):
    x = random_walk()
    plt.plot(x)



In [29]:
for i in range(10):
    x = random_walk()
    plt.plot(x)



In [55]:
for i in range(20):
    x = random_walk()
    plt.plot(x)



In [64]:
pdf_100 = np.array([])
pdf_1000 = np.array([])
pdf_10000 = np.array([])
for i in range(100):
    x = random_walk()
    pdf_100 = np.append(pdf_100, x[99])
    pdf_1000 = np.append(pdf_1000, x[999])
    pdf_10000 = np.append(pdf_10000, x[9999])
    plt.plot(x)



In [65]:
plt.hist(pdf_100)


Out[65]:
(array([  3.,   4.,  11.,  17.,  18.,  20.,  15.,   4.,   5.,   3.]),
 array([-23. , -18.4, -13.8,  -9.2,  -4.6,   0. ,   4.6,   9.2,  13.8,
         18.4,  23. ]),
 <a list of 10 Patch objects>)

In [66]:
plt.hist(pdf_1000)


Out[66]:
(array([  5.,   9.,  18.,  24.,  12.,  15.,  11.,   3.,   2.,   1.]),
 array([-65. , -48.6, -32.2, -15.8,   0.6,  17. ,  33.4,  49.8,  66.2,
         82.6,  99. ]),
 <a list of 10 Patch objects>)

In [67]:
plt.hist(pdf_10000)


Out[67]:
(array([  2.,   2.,   7.,  19.,  23.,  24.,  12.,   6.,   4.,   1.]),
 array([-279., -222., -165., -108.,  -51.,    6.,   63.,  120.,  177.,
         234.,  291.]),
 <a list of 10 Patch objects>)

In [160]:
def random_walk_2d():
    x = [0]
    y = [0]
    for i in range(1000):
        x = np.append(x, x[i]+np.random.choice([1, -1]))
        y = np.append(y, y[i]+np.random.choice([1, -1]))
    return x, y

In [163]:
x = random_walk()
plt.plot(x)


Out[163]:
[<matplotlib.lines.Line2D at 0x131e75050>]

In [161]:
x, y = random_walk_2d()
plt.plot(x, y)


Out[161]:
[<matplotlib.lines.Line2D at 0x131aa3b50>]

In [174]:
def random_walk_3d():
    x = [0]
    y = [0]
    z = [0]
    for i in range(1000):
        x = np.append(x, x[i]+np.random.choice([1, -1]))
        y = np.append(y, y[i]+np.random.choice([1, -1]))
        z = np.append(z, z[i]+np.random.choice([1, -1]))
    return x, y, z

In [175]:
x, y, z = random_walk_3d()
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter3D(x, y, z)


Out[175]:
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x133135690>

In [185]:
def return_0():
    return_0 = np.array([0])
    for i in range(100):
        x, y, z = random_walk_3d()
        for i in range(1,1000):
            if x[i] == 0 and y[i] == 0 and z[i] == 0:
                return_0 += 1
                break
    return return_0
return_0()


Out[185]:
array([27])

In [187]:
np.average([return_0() for i in range(10)])


Out[187]:
26.199999999999999