import numpy as npThe formula for a mean \(\frac{1}{N} \sum_{n=1}^N x_n\) naturally relates to the definition of an expectation
\[\mathbb{E}[X] = \sum_{x \in S} x \cdot f(x)\]
you just have to squint.
# X ~ Uniform(1, 6)
x = np.arange(6) + 1
fx = 1/6np.sum(x * fx) # E[X]3.5rng = np.random.default_rng()
x = rng.integers(1, 7, size = 1000000)np.mean(x)3.500221np.mean(x == 4) # proportion of x values equal to 4, estimates 1/60.166322The formula for the mean estimates the density function at each value of \(x\), e.g. \(x = 4\). As such, here are various (numerically inefficient) ways to think about the formula for a mean.
s = 0
for i in range(1, 7): # Python for-loop
    s += np.mean(x == i) * i # s = s + np.mean(x == i) * i
s3.500221np.sum([np.mean(x == i) * i for i in range(1, 7)]) # list comprehension3.500221sum(np.mean(x == i) * i for i in range(1, 7)) # generator3.500221p = np.zeros(6)
N = np.size(x)
for xi in x:
    p[xi-1] += 1 / N
np.sum(np.arange(1, 7) * p) 
# we should be able to re-use variables from above to simplify this, but I used x for both
# the random numbers and the values in the support of the Uniform(1, 6) distribution.
# For shame, Edward.3.500221000003066