LaTeX for Statistics and Probability
✅ Copy-ready LaTeX (MathJax) code for Statistics and Probability, organised from beginner to advanced, with where each formula is used in maths and in Machine Learning, AI and Data Science. 😻
New to LaTeX? Start with the LaTeX (MathJax) Basics for Greek letters, dots and spaces.
Just click the "copy code" button below 'markdown'!
Summary Overview
1. Descriptive Statistics (Beginner)
1.1 Mean
Mean: "\bar{x}" and "\mu"
Example:
Where it's used: describing the centre of a dataset.
$$
\bar{x} = \frac{1}{n} \sum_{i=1}^{n} x_i \qquad \mu = \frac{1}{N} \sum_{i=1}^{N} x_i
$$
$$
\bar{x} = \frac{2 + 4 + 4 + 4 + 5 + 5 + 7 + 9}{8} = 5
$$1.2 Median and Mode
Median: "\tilde{x}"
Example:
Where it's used: describing the centre of skewed data, such as incomes, where the mean is pulled by extreme values.
$$
\tilde{x} = x_{\left( \frac{n+1}{2} \right)} \quad (n \text{ odd}) \qquad \{ 3, 1, 4, 1, 5 \} \to \{ 1, 1, 3, 4, 5 \} \;\Rightarrow\; \tilde{x} = 3, \; \operatorname{Mode} = 1
$$1.3 Variance and Standard Deviation
Variance: "\sigma^2" and "s^2"
Example:
Where it's used: describing how spread out the data is.
$$
\sigma^2 = \frac{1}{N} \sum_{i=1}^{N} (x_i - \mu)^2 \qquad s^2 = \frac{1}{n - 1} \sum_{i=1}^{n} (x_i - \bar{x})^2 \qquad s = \sqrt{s^2}
$$1.4 Standardisation (z-score)
z-score: "\frac{x - \mu}{\sigma}"
Example:
A z-score of 1.5 means the value is 1.5 standard deviations above the mean.
Where it's used: comparing values on different scales, and scaling features before training a model.
$$
z = \frac{x - \mu}{\sigma} \qquad z = \frac{85 - 70}{10} = 1.5
$$1.5 Quartiles and Interquartile Range
Quartiles and IQR
Example:
Where it's used: box plots and finding outliers during data cleaning.
$$
\text{IQR} = Q_3 - Q_1 \qquad x < Q_1 - 1.5 \, \text{IQR} \quad \text{or} \quad x > Q_3 + 1.5 \, \text{IQR}
$$1.6 Covariance and Correlation
Covariance and Correlation: "\operatorname{Cov}"
Example:
The correlation
Where it's used: measuring how two features move together, and choosing features for a model.
$$
\operatorname{Cov}(X, Y) = \frac{1}{n - 1} \sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y}) \qquad r = \frac{\operatorname{Cov}(X, Y)}{s_X \, s_Y}
$$2. Probability Basics (Beginner)
2.1 Probability Rules
Complement and Union Rules
Example:
Where it's used: calculating the chance of "not" and "or" events.
$$
0 \le P(A) \le 1 \qquad P(A^{c}) = 1 - P(A) \qquad P(A \cup B) = P(A) + P(B) - P(A \cap B)
$$2.2 Conditional Probability
Conditional Probability: "\mid"
Example:
Use \mid instead of | for the correct spacing. It reads "the probability of
Where it's used: updating a probability after new information arrives.
$$
P(A \mid B) = \frac{P(A \cap B)}{P(B)}
$$2.3 Independence
Independent Events
Example:
Two events are independent when knowing one tells you nothing about the other.
Where it's used: simplifying calculations, and the main assumption behind Naive Bayes.
$$
P(A \cap B) = P(A) \, P(B)
$$2.4 Bayes' Theorem
Bayes' Theorem
Example:
Where it's used: spam filters, medical test results, Naive Bayes classifiers and Bayesian statistics.
$$
P(A \mid B) = \frac{P(B \mid A) \, P(A)}{P(B)}
$$2.5 Law of Total Probability
Law of Total Probability
Example:
The events
Where it's used: finding the denominator
$$
P(B) = \sum_{i} P(B \mid A_i) \, P(A_i)
$$2.6 Permutations and Combinations
Combinations: "\binom{n}{k}"
Example:
Use permutations when order matters and combinations when it does not.
Where it's used: counting outcomes, and the binomial distribution.
$$
P(n, k) = \frac{n!}{(n - k)!} \qquad \binom{n}{k} = \frac{n!}{k! \, (n - k)!} \qquad \binom{5}{2} = 10
$$3. Random Variables and Distributions (Intermediate)
3.1 Expected Value
Expected Value: "\mathbb{E}"
Example:
Use the sum for discrete variables and the integral for continuous ones.
Where it's used: the average outcome of a random variable, such as the expected loss of a model.
$$
\mathbb{E}[X] = \sum_{x} x \, P(X = x) \qquad \mathbb{E}[X] = \int_{-\infty}^{\infty} x \, f(x) \, dx
$$3.2 Variance of a Random Variable
Variance: "\operatorname{Var}"
Example:
The second form is usually quicker to calculate.
Where it's used: measuring the uncertainty of a prediction or an estimate.
$$
\operatorname{Var}(X) = \mathbb{E}\big[ (X - \mu)^2 \big] = \mathbb{E}[X^2] - \big( \mathbb{E}[X] \big)^2
$$3.3 PMF, PDF and CDF
Probability Functions
Example:
Where it's used: describing any probability distribution.
$$
p(x) = P(X = x) \qquad F(x) = P(X \le x) = \int_{-\infty}^{x} f(t) \, dt
$$3.4 Bernoulli and Binomial Distributions
Binomial: "X \sim \operatorname{Bin}(n, p)"
Example:
A Bernoulli variable is a single yes or no trial, the binomial with
Where it's used: counting successes, such as clicks, conversions and correct predictions.
$$
X \sim \operatorname{Bin}(n, p) \qquad P(X = k) = \binom{n}{k} p^{k} (1 - p)^{n - k}
$$
$$
\mathbb{E}[X] = np \qquad \operatorname{Var}(X) = np(1 - p)
$$3.5 Poisson Distribution
Poisson Distribution
Example:
Where it's used: counting events in a fixed time, such as website visits per hour or emails per day.
$$
X \sim \operatorname{Poisson}(\lambda) \qquad P(X = k) = \frac{\lambda^{k} e^{-\lambda}}{k!} \qquad \mathbb{E}[X] = \operatorname{Var}(X) = \lambda
$$3.6 Normal Distribution
Normal Distribution: "\mathcal{N}"
Example:
\sim reads as "is distributed as".
Where it's used: modelling data, hypothesis testing and initialising neural network weights.
$$
X \sim \mathcal{N}(\mu, \sigma^2) \qquad f(x) = \frac{1}{\sigma \sqrt{2\pi}} \, e^{-\frac{(x - \mu)^2}{2\sigma^2}} \qquad Z \sim \mathcal{N}(0, 1)
$$3.7 Uniform and Exponential Distributions
Uniform and Exponential
Example:
The exponential distribution has mean
Where it's used: random initialisation and sampling (uniform), and waiting times between events (exponential).
$$
X \sim U(a, b), \quad f(x) = \frac{1}{b - a} \qquad X \sim \operatorname{Exp}(\lambda), \quad f(x) = \lambda e^{-\lambda x}, \; x \ge 0
$$3.8 Beta Distribution
Beta Distribution: "\operatorname{Beta}(\alpha, \beta)"
Example:
The Beta distribution describes an unknown probability, so its values are between 0 and 1.
Where it's used: Bayesian A/B testing, and Thompson sampling in recommender systems and reinforcement learning.
$$
X \sim \operatorname{Beta}(\alpha, \beta) \qquad f(x) = \frac{x^{\alpha - 1} (1 - x)^{\beta - 1}}{B(\alpha, \beta)}, \; 0 \le x \le 1 \qquad \mathbb{E}[X] = \frac{\alpha}{\alpha + \beta}
$$
$$
\text{prior } \operatorname{Beta}(\alpha, \beta) \;+\; k \text{ successes in } n \text{ trials} \;\Rightarrow\; \text{posterior } \operatorname{Beta}(\alpha + k, \; \beta + n - k)
$$4. Statistical Inference (Intermediate)
4.1 Law of Large Numbers
Law of Large Numbers
Example:
As the sample gets bigger, the sample mean gets closer to the true mean
Where it's used: the reason averages from large datasets are reliable, and Monte Carlo simulation.
$$
\bar{X}_n = \frac{1}{n} \sum_{i=1}^{n} X_i \to \mu \quad \text{as } n \to \infty \qquad \lim_{n \to \infty} P\big( \lvert \bar{X}_n - \mu \rvert > \varepsilon \big) = 0
$$4.2 Central Limit Theorem and Standard Error
Central Limit Theorem
Example:
For a large sample, the sample mean is approximately normal, whatever the shape of the original data.
Where it's used: the reason confidence intervals and many tests work.
$$
\bar{X} \approx \mathcal{N}\left( \mu, \frac{\sigma^2}{n} \right) \qquad \text{SE} = \frac{\sigma}{\sqrt{n}}
$$4.3 Confidence Interval
Confidence Interval: "\pm"
Example:
Write \% for the percent sign, because % starts a comment in LaTeX.
Where it's used: reporting an estimate with its uncertainty, such as model accuracy across test sets.
$$
\bar{x} \pm z_{\alpha/2} \, \frac{\sigma}{\sqrt{n}} \qquad 95\% \text{ CI}: \; \bar{x} \pm 1.96 \, \frac{\sigma}{\sqrt{n}}
$$4.4 Hypothesis Testing
Hypotheses: "H_0" and "H_1"
Example:
Use
Where it's used: A/B testing, and checking whether a new model is really better than the old one.
$$
H_0: \mu = \mu_0 \quad \text{vs} \quad H_1: \mu \neq \mu_0 \qquad z = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}} \qquad t = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}
$$4.5 p-value and Significance Level
p-value
Example:
This is a two-sided test.
Where it's used: deciding whether a result is statistically significant.
$$
p = P\big( \lvert Z \rvert \ge \lvert z \rvert \mid H_0 \big) \qquad \text{reject } H_0 \text{ if } p < \alpha
$$4.6 Type I and Type II Errors
Errors and Power
Example:
A Type I error is a false positive and a Type II error is a false negative. Power is the chance of detecting a real effect.
Where it's used: planning sample sizes for experiments and A/B tests.
$$
\alpha = P(\text{reject } H_0 \mid H_0 \text{ true}) \qquad \beta = P(\text{fail to reject } H_0 \mid H_0 \text{ false}) \qquad \text{Power} = 1 - \beta
$$4.7 Chi-Squared Test
Chi-Squared: "\chi^2"
Example:
Where it's used: testing whether two categorical variables are related, such as in feature selection.
$$
\chi^2 = \sum_{i} \frac{(O_i - E_i)^2}{E_i}
$$4.8 ANOVA (F-test)
One-Way ANOVA
Example:
Where it's used: comparing three or more groups at once, such as several model versions or marketing campaigns.
$$
F = \frac{\text{MS}_{\text{between}}}{\text{MS}_{\text{within}}} = \frac{\sum_{j=1}^{k} n_j (\bar{x}_j - \bar{x})^2 \, / \, (k - 1)}{\sum_{j=1}^{k} \sum_{i=1}^{n_j} (x_{ij} - \bar{x}_j)^2 \, / \, (N - k)}
$$4.9 Simple Linear Regression
Least Squares Line
Example:
Where it's used: predicting one variable from another, the starting point of Machine Learning.
$$
\hat{y} = \beta_0 + \beta_1 x \qquad \beta_1 = \frac{\sum_{i} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i} (x_i - \bar{x})^2} \qquad \beta_0 = \bar{y} - \beta_1 \bar{x}
$$
$$
R^2 = 1 - \frac{\sum_{i} (y_i - \hat{y}_i)^2}{\sum_{i} (y_i - \bar{y})^2}
$$5. Markov Chains and Advanced Probability (Advanced)
5.1 Joint and Marginal Distributions
Joint and Marginal Distributions
Example:
Where it's used: working with several random variables at once, such as features and labels.
$$
p(x) = \sum_{y} p(x, y) \qquad f_X(x) = \int_{-\infty}^{\infty} f(x, y) \, dy
$$5.2 Conditional Expectation
Conditional Expectation: "\mathbb{E}[X \mid Y]"
Example:
The second formula is the law of total expectation: average the conditional averages to get the overall average.
Where it's used: regression, which predicts
$$
\mathbb{E}[X \mid Y = y] = \sum_{x} x \, P(X = x \mid Y = y) \qquad \mathbb{E}[X] = \mathbb{E}\big[ \mathbb{E}[X \mid Y] \big]
$$5.3 Markov Property
Markov Property
Example:
The next state depends only on the current state, not on the whole history.
Where it's used: modelling processes that move between states, such as weather, customer behaviour and text.
$$
P(X_{t+1} = j \mid X_t = i, X_{t-1}, \ldots, X_0) = P(X_{t+1} = j \mid X_t = i)
$$5.4 Transition Matrix
Transition Matrix
Example:
Each row adds up to 1. In this example, state 1 is "sunny" and state 2 is "rainy".
Where it's used: PageRank, weather models and customer journeys.
$$
p_{ij} = P(X_{t+1} = j \mid X_t = i) \qquad P = \begin{bmatrix} 0.9 & 0.1 \\ 0.5 & 0.5 \end{bmatrix} \qquad P^{(n)} = P^{n}
$$5.5 Stationary Distribution
Stationary Distribution: "\boldsymbol{\pi}"
Example:
For the transition matrix above, the chain spends
Where it's used: long-run behaviour of a Markov chain, and PageRank.
$$
\boldsymbol{\pi} = \boldsymbol{\pi} P, \qquad \sum_{i} \pi_i = 1 \qquad \boldsymbol{\pi} = \left( \frac{5}{6}, \frac{1}{6} \right)
$$5.6 Markov's and Chebyshev's Inequalities
Markov's and Chebyshev's Inequalities
Example:
These give limits on probabilities using only the mean and the variance.
Where it's used: proving how fast algorithms converge, and bounds in learning theory.
$$
P(X \ge a) \le \frac{\mathbb{E}[X]}{a} \quad (X \ge 0, \; a > 0) \qquad P\big( \lvert X - \mu \rvert \ge k\sigma \big) \le \frac{1}{k^2}
$$5.7 Markov Chain Monte Carlo
Metropolis-Hastings Acceptance
Example:
Where it's used: Bayesian statistics, when the posterior is too complex to calculate directly.
$$
A(x \to x') = \min\left( 1, \; \frac{p(x') \, q(x \mid x')}{p(x) \, q(x' \mid x)} \right)
$$6. Probability for Machine Learning (Applied)
6.1 Likelihood and Log-Likelihood
Likelihood
Example:
Taking the log turns a product into a sum, which is easier to work with.
Where it's used: measuring how well model parameters
$$
L(\theta) = \prod_{i=1}^{n} p(x_i \mid \theta) \qquad \ell(\theta) = \log L(\theta) = \sum_{i=1}^{n} \log p(x_i \mid \theta)
$$6.2 Maximum Likelihood Estimation
MLE
Example:
Minimising cross-entropy loss is the same as maximising the log-likelihood.
Where it's used: training most machine learning models.
$$
\hat{\theta}_{\text{MLE}} = \underset{\theta}{\arg\max} \; \sum_{i=1}^{n} \log p(x_i \mid \theta)
$$6.3 Posterior and MAP Estimation
Posterior: "\propto"
Example:
Posterior is proportional to likelihood times prior. A prior on the weights acts like regularisation.
Where it's used: Bayesian machine learning and regularised models.
$$
p(\theta \mid D) \propto p(D \mid \theta) \, p(\theta) \qquad \hat{\theta}_{\text{MAP}} = \underset{\theta}{\arg\max} \; p(D \mid \theta) \, p(\theta)
$$6.4 Naive Bayes Classifier
Naive Bayes
Example:
It assumes the features are independent given the class.
Where it's used: text classification and spam filtering.
$$
\hat{y} = \underset{k}{\arg\max} \; P(C_k) \prod_{i=1}^{d} P(x_i \mid C_k)
$$6.5 Entropy and Cross-Entropy
Entropy
Example:
Where it's used: measuring uncertainty, decision tree splits, and the cross-entropy loss for classification.
$$
H(p) = -\sum_{x} p(x) \log p(x) \qquad H(p, q) = -\sum_{x} p(x) \log q(x)
$$6.6 KL Divergence
KL Divergence: "D_{\mathrm{KL}}"
Example:
It measures how different
Where it's used: variational autoencoders, knowledge distillation and fine-tuning large language models.
$$
D_{\mathrm{KL}}(P \parallel Q) = \sum_{x} P(x) \log \frac{P(x)}{Q(x)}
$$6.7 Multivariate Normal Distribution
Multivariate Normal
Example:
Where it's used: Gaussian processes, Gaussian mixture models and anomaly detection.
$$
\mathbf{x} \sim \mathcal{N}(\boldsymbol{\mu}, \Sigma) \qquad f(\mathbf{x}) = \frac{1}{(2\pi)^{d/2} \lvert \Sigma \rvert^{1/2}} \exp\left( -\frac{1}{2} (\mathbf{x} - \boldsymbol{\mu})^{\top} \Sigma^{-1} (\mathbf{x} - \boldsymbol{\mu}) \right)
$$