import numpy as np
import matplotlib.pyplot as pltProbability (as a limit point)
def uniform_density(x, a = 1, b = 6):
xa = x < a
xb = x > b
idx = xa | xb
d = np.zeros_like(x)
d[~idx] = 1 / (b - a + 1)
return dx = np.array([1., 3, 6, 10]) # still doesn't work, but I know why
uniform_density(x)array([0.16666667, 0.16666667, 0.16666667, 0. ])
x1 = x < 1
x6 = x > 6
x1, x6(array([False, False, False, False]), array([False, False, False, True]))
idx = x1 | x6
~idxarray([ True, True, True, False])
np.arange(np.size(x))[~idx]array([0, 1, 2])
xarray([ 1., 3., 6., 10.])
np.ones_like(x)array([1., 1., 1., 1.])
np.zeros_like(x)array([0., 0., 0., 0.])
\[\lim_{N \rightarrow \infty} \frac{1}{N} \sum_{n=1}^N 1_A(x_n) = \mathbb{P}[X \in A]\]
rng = np.random.default_rng()
N = 1000
x = rng.integers(1, 7, size = N) # Uniform(1, 6)np.mean(x == 6)0.164
ndx = np.arange(1, N + 1)
cm = np.cumsum(x == 6) / ndx # mean of data == 6
plt.plot(ndx, cm);ndx = np.arange(1, N + 1)
m = (6 + 1) / 2 # mean of distribution
v = ((6 - 1 + 1) ** 2 - 1) / 12 # variance of distribution
cm = np.cumsum((x - m) ** 2) / ndx # variance of data
plt.plot(ndx, cm);
plt.axhline(v, color = "black");