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]:
In [41]:
plt.ylim(-10, 10)
plt.xlim(0, 100)
plt.plot(x)
Out[41]:
In [46]:
plt.ylim(-30, 30)
plt.xlim(0, 1000)
plt.plot(x)
Out[46]:
In [50]:
plt.ylim(-150, 150)
plt.xlim(0, 10000)
plt.plot(x)
Out[50]:
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]:
In [66]:
plt.hist(pdf_1000)
Out[66]:
In [67]:
plt.hist(pdf_10000)
Out[67]:
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]:
In [161]:
x, y = random_walk_2d()
plt.plot(x, y)
Out[161]:
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]:
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]:
In [187]:
np.average([return_0() for i in range(10)])
Out[187]: