import numpy as np
import matplotlib.pyplot as plt
Make $n$ realizations of a uniformly distributed 'random variable' between $[0, 1)$:
n = 1000000
x = np.random.rand(n)
Now plot a histogram of the values of $x$. Explore the normed=True
and bins=N
arguments to hist()
.
plt.hist(x, normed=True, bins=20)
Now plot the distributions of some expressions involving $x$:
#expr = -1 + 2 * x
#expr = x**2
#expr = np.sqrt(np.abs(x))
#expr = np.sin(2*np.pi*x)
#expr = np.log(evens)
if 1:
evens = x[::2]
odds = x[1::2]
r = np.sqrt(-2*np.log(evens))
expr = np.empty_like(x)
expr[::2] = r*np.cos(2*np.pi*odds)
expr[1::2] = r*np.sin(2*np.pi*odds)
plt.hist(x, label="$x$", normed=True, bins=20)
plt.hist(expr, label="Expression", normed=True, bins=20)
plt.legend(loc="best")
r
?