Why It Matters
Expected Value
Properties of Expectation
Variance
Properties of Variance
Standard Deviation
Moments
Moment Generating Function
Python Implementation
import numpy as np
from scipy import stats
# === Theoretical Values ===
# Normal distribution: mu=5, sigma=2
mu, sigma = 5, 2
print(f"E[X] = {mu}")
print(f"Var(X) = {sigma**2}")
# === Empirical Verification via Sampling ===
np.random.seed(42)
samples = np.random.normal(mu, sigma, size=100_000)
print(f"Sample mean: {samples.mean():.4f}")
print(f"Sample variance: {samples.var():.4f}")
print(f"Sample std dev: {samples.std():.4f}")
# === Discrete Distribution Moments ===
# Fair die
die = np.arange(1, 7)
prob = np.ones(6) / 6
E_die = np.sum(die * prob)
E_die2 = np.sum(die**2 * prob)
Var_die = E_die2 - E_die**2
print(f"E[die] = {E_die:.4f}, Var(die) = {Var_die:.4f}")
# === Custom Random Variable ===
def compute_expectation(values, probs):
"""Compute E[X] for a discrete random variable."""
return np.sum(values * probs)
def compute_variance(values, probs):
"""Compute Var(X) for a discrete random variable."""
mean = compute_expectation(values, probs)
return compute_expectation((values - mean)**2, probs)
values = np.array([0, 1, 2, 3, 4])
probs = np.array([0.1, 0.2, 0.3, 0.25, 0.15])
print(f"E[X] = {compute_expectation(values, probs):.4f}")
print(f"Var(X) = {compute_variance(values, probs):.4f}")
# === Moment Generating Function ===
def mgf_normal(t, mu, sigma):
"""MGF of N(mu, sigma^2)."""
return np.exp(mu * t + 0.5 * sigma**2 * t**2)
t = 0.1
print(f"M_X({t}) = {mgf_normal(t, mu, sigma):.6f}")
# === Empirical MGF ===
empirical_mgf = np.mean(np.exp(samples * t))
print(f"Empirical M_X({t}) = {empirical_mgf:.6f}")
# === Higher Moments with Scipy ===
from scipy.stats import skew, kurtosis
print(f"Skewness: {skew(samples):.4f}")
print(f"Excess Kurtosis: {kurtosis(samples):.4f}")
Applications in AI/ML
Common Mistakes
| Mistake | Why It's Wrong | Correct Approach |
|---|---|---|
| always | Only true for independent (or uncorrelated) variables | |
| always | Only true for independent (or uncorrelated) variables | Compute directly or use covariance: |
| Variance is in the same units as | Variance has units of | Use standard deviation for interpretable units |
| Expected value always exists | Some distributions (e.g., Cauchy) have no finite mean | Check convergence before using expectation-based results |
| "Variance = standard deviation" | They are different quantities; | , |
| Forgetting in | , not | The factor is because variance involves squaring deviations |
| Confusing moments with central moments | Raw moments and central moments are different | Use the correct definition for skewness, kurtosis, etc. |
Interview Questions
Practice Problems
Quick Reference
| Quantity | Formula | Python |
|---|---|---|
| Expectation (discrete) | np.sum(x * p) | |
| Expectation (continuous) | np.trapz(x * pdf, x) | |
| Variance | np.var(x) | |
| Standard Deviation | np.std(x) | |
| -th raw moment | np.mean(x**n) | |
| -th central moment | np.mean((x - mu)**n) | |
| Skewness | scipy.stats.skew(x) | |
| Kurtosis | scipy.stats.kurtosis(x) | |
| MGF | np.mean(np.exp(x * t)) | |
| Linearity of | β | |
| Variance scaling | β | |
| Covariance rule | np.cov(x, y) |
Cross-References
- Probability Distributions: 030-discrete-distributions β Bernoulli, Binomial, Poisson distributions and their moments
- Continuous Distributions: 031-continuous-distributions β Normal, Exponential, Uniform distributions
- Law of Large Numbers: 040-lln-clt β Why sample means converge to the expected value
- Central Limit Theorem: 040-lln-clt β Normal approximation for sums of random variables
- Covariance and Correlation: 038-probability-covariance β Joint distributions and dependence
- Bayesian Inference: 041-bayesian-inference β Posterior expectations and credible intervals
- Information Theory: 042-information-theory β Entropy, KL divergence, and their expectations