Skip to content

Instantly share code, notes, and snippets.

@grey-area
Last active May 15, 2019 11:59
Show Gist options
  • Save grey-area/f2d29431e747a264c438f450e37bf027 to your computer and use it in GitHub Desktop.
Save grey-area/f2d29431e747a264c438f450e37bf027 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import gamma
trials = 200
steps = 1000
xs = np.arange(steps)
ds = [1, 10, 100]
fig, axes = plt.subplots(1, 3, sharey=True)
for d, ax in zip(ds, axes):
ys = np.cumsum(np.random.normal(size=(trials, steps, d)), axis=1)
dists = np.sqrt(np.sum(ys**2, axis=-1))
mean = np.sqrt(2 * xs) * gamma((d + 1) / 2) / gamma(d / 2)
ax.plot(xs, dists.T, c='k', alpha=0.02)
ax.plot(xs, mean, c='r')
ax.set_xlabel('Time')
ax.set_ylabel('Distance from origin')
ax.set_title(f'{d} dimensions')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment