Appendix G: Statistics & Probability Formula Reference
Descriptive Statistics
Measures of Central Tendency
Sample Mean
Weighted Mean
Sample Median — the middle value when data is sorted. For even $n$, average the two middle values.
Sample Mode — the most frequently occurring value.
Geometric Mean (useful for growth rates, ratios)
Measures of Spread
Sample Variance (unbiased, Bessel's correction)
Population Variance
Standard Deviation — $s = \sqrt{s^2}$, same units as the data.
Coefficient of Variation
Interquartile Range — $IQR = Q_3 - Q_1$, robust to outliers.
Standard Error of the Mean
Shape
Sample Skewness
Sample Excess Kurtosis
A normal distribution has $g_1 = 0$, $g_2 = 0$.
Covariance and Correlation
Sample Covariance
Pearson Correlation Coefficient
$r \in [-1, 1]$. Measures linear association only.
Spearman Rank Correlation — apply Pearson's formula to the ranks of $x$ and $y$. Robust to monotone nonlinearity and outliers.
import numpy as np
from scipy import stats
x = np.array([2.1, 3.4, 5.0, 1.2, 4.7])
y = np.array([1.8, 3.1, 4.9, 1.5, 4.2])
print(f"mean={np.mean(x):.3f}, std={np.std(x, ddof=1):.3f}")
print(f"pearson r={np.corrcoef(x, y)[0,1]:.4f}")
print(f"spearman r={stats.spearmanr(x, y).statistic:.4f}")Probability Rules
Complement Rule — $P(A^c) = 1 - P(A)$
Addition Rule — $P(A \cup B) = P(A) + P(B) - P(A \cap B)$
Multiplication Rule — $P(A \cap B) = P(A)\,P(B \mid A)$
Conditional Probability
Independence — $A \perp B$ iff $P(A \cap B) = P(A)P(B)$
Law of Total Probability (partition $\{B_i\}$)
Bayes' Theorem
In data science this is central to Naive Bayes classifiers, Bayesian updating, and posterior inference. $P(B_i)$ is the prior, $P(A \mid B_i)$ is the likelihood, and $P(B_i \mid A)$ is the posterior.
Expectation and Variance Rules
For random variables $X$, $Y$ and constants $a$, $b$:
If $X \perp Y$: $\text{Cov}(X,Y)=0$, so $\text{Var}(X+Y)=\text{Var}(X)+\text{Var}(Y)$.
Law of Total Expectation
Law of Total Variance
Common Probability Distributions
Discrete Distributions
Bernoulli($p$)
- PMF: <!--MATHBLOCK88-->, <!--MATHBLOCK89-->
- Mean: <!--MATHBLOCK90--> — Variance: <!--MATHBLOCK91-->
Binomial($n$, $p$)
- Mean: <!--MATHBLOCK94--> — Variance: <!--MATHBLOCK95-->
Geometric($p$) — number of trials until first success
- Mean: <!--MATHBLOCK97--> — Variance: <!--MATHBLOCK98-->
Poisson($\lambda$) — counts of rare events
- Mean: <!--MATHBLOCK100--> — Variance: <!--MATHBLOCK101-->
Binomial($n$,$p$) $\to$ Poisson($\lambda=np$) when $n\to\infty$, $p\to 0$, $np$ fixed.
Continuous Distributions
Uniform($a$, $b$)
- Mean: <!--MATHBLOCK111--> — Variance: <!--MATHBLOCK112-->
Normal($\mu$, $\sigma^2$)
- Mean: <!--MATHBLOCK115--> — Variance: <!--MATHBLOCK116-->
- Standard Normal: <!--MATHBLOCK117-->
- 68-95-99.7 rule: <!--MATHBLOCK118-->, <!--MATHBLOCK119-->, <!--MATHBLOCK120--> cover roughly 68%, 95%, 99.7% of the distribution.
Exponential($\lambda$)
- Mean: <!--MATHBLOCK122--> — Variance: <!--MATHBLOCK123-->
- Memoryless: <!--MATHBLOCK124-->
Gamma($\alpha$, $\beta$) — sum of $\alpha$ independent Exp($\beta$) variables
- Mean: <!--MATHBLOCK129--> — Variance: <!--MATHBLOCK130-->
Beta($\alpha$, $\beta$) — models probabilities, conjugate prior to Binomial
- Mean: <!--MATHBLOCK133--> — Variance: <!--MATHBLOCK134-->
Chi-Squared($k$) — $\chi^2_k = \sum_{i=1}^k Z_i^2$, $Z_i \sim N(0,1)$ i.i.d.
- Mean: <!--MATHBLOCK138--> — Variance: <!--MATHBLOCK139-->
- Used in goodness-of-fit tests and sampling distributions of <!--MATHBLOCK140-->.
Student's $t$($\nu$) — $t = Z/\sqrt{\chi^2_\nu/\nu}$
- Mean: <!--MATHBLOCK144--> (<!--MATHBLOCK145-->) — Variance: <!--MATHBLOCK146--> (<!--MATHBLOCK147-->)
- Heavier tails than Normal; <!--MATHBLOCK148--> as <!--MATHBLOCK149-->
$F$($d_1$, $d_2$) — ratio of two chi-squared variables; appears in ANOVA and regression $F$-tests.
from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
# Quick CDF / PPF lookups — bread and butter in practice
print(stats.norm.ppf(0.975)) # 1.96 (z critical value)
print(stats.t.ppf(0.975, df=29)) # t critical for n=30
print(stats.chi2.ppf(0.95, df=5)) # chi-sq critical value
print(stats.binom.pmf(k=3, n=10, p=0.3)) # P(X=3)
print(stats.poisson.cdf(k=4, mu=3)) # P(X<=4) with lambda=3Sampling Distributions and the Central Limit Theorem
Sampling Distribution of $\bar{X}$
For i.i.d. $X_i \sim (\mu, \sigma^2)$:
Central Limit Theorem (CLT)
This holds for any distribution with finite mean and variance. In practice, $n \geq 30$ is often sufficient unless the population is highly skewed.
Sampling Distribution of $s^2$
Point Estimation
Method of Moments — set sample moments equal to population moments and solve for parameters. For $\bar{x}$ and $s^2$, this gives $\hat{\mu}=\bar{x}$, $\hat{\sigma}^2=s^2$.
Maximum Likelihood Estimator (MLE) — maximize the log-likelihood:
- Normal MLE: <!--MATHBLOCK162-->, <!--MATHBLOCK163--> (biased).
- Binomial MLE: <!--MATHBLOCK164--> (sample proportion).
- Poisson MLE: <!--MATHBLOCK165-->.
Bias, Variance, MSE of an Estimator
Confidence Intervals
All formulas below assume two-sided intervals at confidence level $1-\alpha$.
Mean, $\sigma$ known (z-interval)
Mean, $\sigma$ unknown (t-interval)
Proportion (large $n$, Wald interval)
Difference of two means (independent, equal variance)
where the pooled standard deviation is:
Variance ($\chi^2$ interval)
import scipy.stats as stats
import numpy as np
data = np.random.default_rng(42).normal(loc=5, scale=2, size=40)
n, xbar, s = len(data), np.mean(data), np.std(data, ddof=1)
# 95% t-interval
ci = stats.t.interval(0.95, df=n-1, loc=xbar, scale=s/np.sqrt(n))
print(f"95% CI for mu: ({ci[0]:.3f}, {ci[1]:.3f})")Hypothesis Testing
Framework
- State <!--MATHBLOCK171--> (null) and <!--MATHBLOCK172--> (alternative).
- Choose significance level <!--MATHBLOCK173--> (typically 0.05).
- Compute test statistic from data.
- Find p-value = <!--MATHBLOCK174-->.
- Reject <!--MATHBLOCK175--> if p-value <!--MATHBLOCK176-->.
Type I Error — reject $H_0$ when true: probability $= \alpha$. Type II Error — fail to reject $H_0$ when false: probability $= \beta$. Power $= 1 - \beta$.
Common Test Statistics
One-sample z-test (known $\sigma$)
One-sample t-test (unknown $\sigma$)
Two-sample t-test (independent groups, pooled)
Paired t-test — compute $d_i = x_{1i} - x_{2i}$ and apply one-sample t-test to $d_i$:
Proportion z-test
Chi-Square Goodness-of-Fit
Chi-Square Test of Independence (contingency table with $r$ rows, $c$ cols)
degrees of freedom $= (r-1)(c-1)$.
F-test for equality of variances
One-Way ANOVA F-statistic
where $k$ = number of groups, $N$ = total observations.
from scipy import stats
import numpy as np
rng = np.random.default_rng(0)
a = rng.normal(5, 1, 30)
b = rng.normal(5.5, 1, 30)
t_stat, p_val = stats.ttest_ind(a, b, equal_var=True)
print(f"Two-sample t: t={t_stat:.3f}, p={p_val:.4f}")
# Chi-square test of independence
observed = np.array([[20, 30], [15, 35]])
chi2, p, dof, expected = stats.chi2_contingency(observed)
print(f"Chi-sq={chi2:.3f}, p={p:.4f}, df={dof}")Simple Linear Regression
Model: $Y_i = \beta_0 + \beta_1 x_i + \varepsilon_i$, where $\varepsilon_i \sim N(0, \sigma^2)$ i.i.d.
OLS Estimates
Residual Standard Error
Standard Errors of Coefficients
t-test for slope ($H_0: \beta_1 = 0$)
Coefficient of Determination
Adjusted $R^2$ (penalizes extra predictors in multiple regression)
where $p$ = number of predictors.
Multiple Linear Regression (Matrix Form)
Model: $\mathbf{y} = \mathbf{X}\boldsymbol{\beta} + \boldsymbol{\varepsilon}$
OLS solution
Covariance of estimates
Overall F-test ($H_0$: all slope coefficients $= 0$)
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
import scipy.stats as stats
rng = np.random.default_rng(7)
x = rng.uniform(0, 10, 50)
y = 3 + 1.5 * x + rng.normal(0, 1.5, 50)
X = x.reshape(-1, 1)
model = LinearRegression().fit(X, y)
yhat = model.predict(X)
n, p = len(y), 1
SSE = np.sum((y - yhat)**2)
SST = np.sum((y - np.mean(y))**2)
se = np.sqrt(SSE / (n - p - 1))
print(f"beta_0={model.intercept_:.3f}, beta_1={model.coef_[0]:.3f}")
print(f"R^2={r2_score(y, yhat):.4f}, sigma_hat={se:.3f}")Key Identities and Inequalities
Markov's Inequality (non-negative $X$, $a > 0$)
Chebyshev's Inequality
Jensen's Inequality (convex $\phi$)
This immediately shows $E[X]^2 \leq E[X^2]$ and justifies that the log of an average exceeds the average of the log.
Bayesian Conjugate Pairs
Common prior-likelihood pairs where the posterior stays in the same family:
- Beta–Binomial: prior <!--MATHBLOCK203-->, observe <!--MATHBLOCK204--> successes in <!--MATHBLOCK205--> trials, posterior <!--MATHBLOCK206-->.
- Gamma–Poisson: prior <!--MATHBLOCK207-->, observe <!--MATHBLOCK208--> counts with sum <!--MATHBLOCK209-->, posterior <!--MATHBLOCK210-->.
- Normal–Normal: prior <!--MATHBLOCK211-->, observe <!--MATHBLOCK212--> samples with known <!--MATHBLOCK213-->, posterior mean is a precision-weighted average of prior mean and sample mean.
Bayesian point estimates
- MAP (Maximum A Posteriori): mode of the posterior.
- Posterior mean: <!--MATHBLOCK214-->, minimizes squared-error loss.
- Posterior median: minimizes absolute-error loss.
import numpy as np
from scipy import stats
# Beta-Binomial updating
alpha_prior, beta_prior = 2, 2 # prior: Beta(2,2)
k, n = 7, 10 # 7 successes in 10 trials
alpha_post = alpha_prior + k
beta_post = beta_prior + (n - k)
posterior = stats.beta(alpha_post, beta_post)
print(f"Posterior: Beta({alpha_post},{beta_post})")
print(f"Posterior mean={posterior.mean():.3f}, 95% CI={posterior.interval(0.95)}")Quick-Reference: Critical Values
For the standard Normal $Z \sim N(0,1)$:
- <!--MATHBLOCK216--> (one-tailed 90%)
- <!--MATHBLOCK217--> (one-tailed 95%)
- <!--MATHBLOCK218--> (two-tailed 95%)
- <!--MATHBLOCK219--> (two-tailed 99%)
For $t$ with $\nu$ degrees of freedom at $\alpha/2 = 0.025$ (two-tailed 95%):
- <!--MATHBLOCK223-->: <!--MATHBLOCK224-->
- <!--MATHBLOCK225-->: <!--MATHBLOCK226-->
- <!--MATHBLOCK227-->: <!--MATHBLOCK228-->
- <!--MATHBLOCK229-->: <!--MATHBLOCK230-->
- <!--MATHBLOCK231-->: <!--MATHBLOCK232-->
from scipy import stats
# Reproduce the table above programmatically
for nu in [10, 20, 30, 60, 120]:
t_crit = stats.t.ppf(0.975, df=nu)
print(f"df={nu:4d} t*={t_crit:.4f}")Common Pitfalls
- Confusing standard deviation and standard error. <!--MATHBLOCK233--> describes the spread of individual observations; <!--MATHBLOCK234--> describes the spread of the sample mean.
- Using z when you should use t. Whenever <!--MATHBLOCK235--> is estimated from data (almost always), use the <!--MATHBLOCK236-->-distribution. For large <!--MATHBLOCK237--> the difference is negligible.
- Forgetting Bessel's correction. Dividing by <!--MATHBLOCK238--> gives a biased estimator of <!--MATHBLOCK239-->; divide by <!--MATHBLOCK240--> for the unbiased estimator.
- Interpreting <!--MATHBLOCK241--> as causal. High <!--MATHBLOCK242--> only means the predictors explain variance in <!--MATHBLOCK243-->, not that they cause changes in <!--MATHBLOCK244-->.
- p-value misinterpretation. The p-value is not the probability that <!--MATHBLOCK245--> is true. It is <!--MATHBLOCK246-->.
- Multiple comparisons inflation. Running <!--MATHBLOCK247--> tests at <!--MATHBLOCK248--> inflates the familywise error rate. Apply Bonferroni (<!--MATHBLOCK249-->) or Benjamini-Hochberg FDR correction.