Random Variables · Empirical Distributions
Lesson 2
For a discrete random variable, the empirical probability mass function is obtained by counting how many times each value appears: \[\widehat d_{N}(a)=\frac{|\{i \colon x_i=a\}|}{N}.\] If the variable takes many different values, it is usually better to group values into intervals and draw a histogram. The area of a histogram column then estimates the probability of falling into the corresponding interval.
For example, suppose we observed lengths of several hundred messages. There may be too many different lengths to list their empirical probabilities one by one. A histogram gives a much more readable picture: most messages are short, but a few are much longer.
from random import expovariate, seed
import matplotlib.pyplot as plt
seed(23)
message_lengths = [round(5 + expovariate(1 / 80)) for _ in range(400)]
plt.hist(
message_lengths,
bins=range(0, 401, 40),
density=True,
edgecolor="black",
color="#7fb3d5",
)
plt.xlabel("message length")
plt.ylabel("empirical density")
plt.tight_layout()
plt.savefig("empirical_histogram.png")