πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Expectation and Variance

ProbabilityMoments🟒 Free Lesson

Advertisement

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

MistakeWhy It's WrongCorrect Approach
alwaysOnly true for independent (or uncorrelated) variables
alwaysOnly true for independent (or uncorrelated) variablesCompute directly or use covariance:
Variance is in the same units as Variance has units of Use standard deviation for interpretable units
Expected value always existsSome distributions (e.g., Cauchy) have no finite meanCheck 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 momentsRaw moments and central moments are differentUse the correct definition for skewness, kurtosis, etc.

Interview Questions


Practice Problems


Quick Reference

QuantityFormulaPython
Expectation (discrete)np.sum(x * p)
Expectation (continuous)np.trapz(x * pdf, x)
Variancenp.var(x)
Standard Deviationnp.std(x)
-th raw momentnp.mean(x**n)
-th central momentnp.mean((x - mu)**n)
Skewnessscipy.stats.skew(x)
Kurtosisscipy.stats.kurtosis(x)
MGFnp.mean(np.exp(x * t))
Linearity of β€”
Variance scalingβ€”
Covariance rulenp.cov(x, y)

Cross-References

Need Expert Mathematics Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement