Skip to content
Menu

14. Review of Probability Theory

Real systems face noise and uncertainty that deterministic state equations can’t capture. This chapter reviews the probability theory needed to describe stochastic systems, preparing the ground for state estimation.

link to download code

The Central Limit Theorem says that summing enough independent, identically distributed random variables produces something that looks Gaussian, regardless of the original distribution. To see this in action, start with two random variables X1, X2, each uniform on [0, 1], and look at the distribution of their sum Z = X1 + X2.

Below, 100,000 samples of each are drawn, summed, and binned into a normalized histogram (an empirical estimate of the pdf p_Z(x)). The mean and variance are checked against the theoretical values for a sum of two independent Uniform(0,1) variables: mean = 1, variance = 1/6.

probability_review/sum_2rv.py
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import io, base64
# Generate two random arrays of size 1e5, uniform on [0, 1]
X1 = np.random.rand(1, int(1e5))
X2 = np.random.rand(1, int(1e5))
# Compute the sum of X1 and X2
Z = X1 + X2
# Compute the histogram of Z with 100 bins
fz, x = np.histogram(Z, bins=100)
# Normalize the histogram by the area and the bin width
w_fz = x[-1] / len(fz)
fz = fz / np.sum(fz) / w_fz
print("mean(Z) =", np.mean(Z), " (theory: 1.0)")
print("var(Z) =", np.var(Z), " (theory: 1/6 =", 1 / 6, ")")
# Plot the histogram as a bar chart
plt.figure(figsize=(6, 4))
plt.bar(x[:-1], fz, width=w_fz)
plt.xlabel("$x$")
plt.ylabel("$p_Z(x)$")
plt.title("Sum of 2 uniform random variables")
buf = io.BytesIO()
plt.savefig(buf, format="png", dpi=90)
plot_b64 = base64.b64encode(buf.getvalue()).decode()

Sum of two uniform random variables (mcimp/codes/probability_review/sum_2rv.py)

Click "Run" to execute.
link to download code

Adding one more uniform random variable pushes the distribution noticeably closer to a bell curve. Here Z = X1 + X2 + X3, with each X_i uniform on [0, 1].

Compare the shape below to the two-variable case above: the single “tent” shape has smoothed into something visibly closer to Gaussian, illustrating the Central Limit Theorem’s convergence even after just three terms. Theoretical mean = 1.5, variance = 1/4.

probability_review/sum_3rv.py
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import io, base64
# Generate three random arrays of size 1e5, uniform on [0, 1]
X1 = np.random.rand(1, int(1e5))
X2 = np.random.rand(1, int(1e5))
X3 = np.random.rand(1, int(1e5))
# Compute the sum of the three arrays
Z = X1 + X2 + X3
# Compute the histogram of Z with 100 bins
fz, x = np.histogram(Z, bins=100)
# Normalize the histogram by the area and the bin width
w_fz = x[-1] / len(fz)
fz = fz / np.sum(fz) / w_fz
print("mean(Z) =", np.mean(Z), " (theory: 1.5)")
print("var(Z) =", np.var(Z), " (theory: 1/4 =", 1 / 4, ")")
# Plot the histogram as a bar chart
plt.figure(figsize=(6, 4))
plt.bar(x[:-1], fz, width=w_fz)
plt.xlabel("$x$")
plt.ylabel("$p_Z(x)$")
plt.title("Sum of 3 uniform random variables (looks Gaussian already)")
buf = io.BytesIO()
plt.savefig(buf, format="png", dpi=90)
plot_b64 = base64.b64encode(buf.getvalue()).decode()

Sum of three uniform random variables (mcimp/codes/probability_review/sum_3rv.py)

Click "Run" to execute.

← All chapters