[MMD]
ML Probability & Statistics Chapter 2: Expected Value, Variance, and Covariance
In Chapter 1 we covered the fundamentals of probability and key probability distributions. This post focuses on summarizing distributions with numbers (expected value, variance, skewness, kurtosis) and on handling relationships between two or more variables through joint distributions, covariance, and correlation.
This post is based on Week 2 of DeepLearning.AI's Mathematics for Machine Learning and Data Science — Probability & Statistics.
What You'll Learn#
- Expected value — understanding the mean as a weighted average
- Variance and standard deviation — measuring the spread of data
- Skewness and kurtosis — higher-order moments that describe shape
- Visualization — quantiles, box plots, KDE, QQ plots
- Joint distributions — the simultaneous distribution of two variables
- Marginal distributions / Conditional distributions
- Covariance and correlation
- Multivariate normal distribution
Expected Value#
Expected Value = Weighted Average#
The expected value of a random variable is its long-run average. You multiply each outcome by its probability and sum them up.
Discrete:
Continuous:
Example — coin flip game: heads pays 0:
→ The maximum you should rationally pay to play this game = $5
Linearity of Expectation#
Example — coin (expected value 3.50) game:
Expected Value of the Uniform Distribution#
For a uniform distribution :
When the distribution is asymmetric the mean is no longer at the center → the median is a better representative in such cases.
Measures of Central Tendency#
| Measure | Definition | Sensitivity to Outliers |
|---|---|---|
| Mean | Sum ÷ count | High |
| Median | Middle value after sorting | Low |
| Mode | Most frequently occurring value | None |
Multimodal distribution: a distribution with two or more modes.
In practice: for right-skewed data like income or housing prices, the median is more representative than the mean.
Variance and Standard Deviation#
Variance#
Measures the spread of a distribution.
Discrete form:
Properties of variance:
- Adding a constant does not change the variance (it only shifts the location)
- Multiplying by a constant scales the variance by
Standard Deviation#
- Variance has units of , which is hard to interpret → taking the square root restores the original unit
- For the normal distribution, the 68-95-99.7 rule: / /
Sum of Gaussians#
The sum of two independent normal distributions is also normal:
Scaling by :
Skewness and Kurtosis — Moments of a Distribution#
Moments#
The -th moment:
| Moment | Name | Meaning |
|---|---|---|
| 1st | Mean | Location of the distribution |
| 2nd | Variance | Spread of the distribution |
| 3rd | Skewness | Asymmetry of the distribution |
| 4th | Kurtosis | Thickness of the tails |
Skewness#
- Positive skew (right-skewed): longer tail on the right (income distributions, housing prices, etc.)
- Negative skew (left-skewed): longer tail on the left
- Symmetric distribution: skewness = 0
Two distributions can have the same mean and variance yet differ in skewness, allowing us to distinguish them.
Kurtosis#
- High kurtosis: heavy tails and a sharp central peak → more extreme values (outliers)
- Low kurtosis: thin tails and a flat center
Even when two distributions share the same skewness and kurtosis, their shapes can differ, so additional descriptors may be needed.
Visualizing Distributions#
Quantiles and Box Plots#
Quantile: the value below which a given proportion of sorted data falls.
Box plot components:
| Element | Description |
|---|---|
| Minimum | Lowest value excluding outliers |
| Q1 (25%) | First quartile |
| Q2 (50%) | Median |
| Q3 (75%) | Third quartile |
| Maximum | Highest value excluding outliers |
| IQR | (interquartile range) |
Kernel Density Estimation (KDE)#
KDE addresses the discontinuity of histograms by providing a smooth PDF estimate. It places a small Gaussian (kernel) at each data point and sums them up.
Violin plot: a visualization that combines KDE with a box plot.
QQ Plot (Quantile-Quantile Plot)#
A visual method to check whether data follows a normal distribution.
- Points close to the diagonal → close to normal
- Points forming a curve → non-normal (skewness present)
Multivariate Probability Distributions#
Joint Distribution#
The simultaneous probability of two random variables and :
Discrete:
When independent:
When dependent: tabulate observed frequencies and compute the probability for each cell.
Continuous: represented by a 2D PDF , visualized with histograms, heatmaps, scatter plots, or density plots.
Marginal Distribution#
The distribution of a single variable obtained by summing (integrating) out the other variable.
Intuition: summing across a row (or column) of a joint distribution table.
Conditional Distribution#
The distribution of one variable given a fixed value of the other.
The key idea is normalizing by the marginal distribution (an extension of conditional probability).
Intuition: taking a specific row (or column) of a joint distribution table and dividing by the sum of that row (or column).
Covariance and Correlation#
Covariance#
Measures whether two variables move together in the same direction.
| Sign | Meaning |
|---|---|
| When X increases, Y tends to increase (positive relationship) | |
| When X increases, Y tends to decrease (negative relationship) | |
| No linear relationship |
Limitation: covariance depends on the units of measurement, so its magnitude alone cannot be used to compare the strength of relationships.
Covariance Matrix#
The covariances among variables organized as a matrix:
\text{Var}(X_1) & \text{Cov}(X_1, X_2) & \cdots \\ \text{Cov}(X_2, X_1) & \text{Var}(X_2) & \cdots \\ \vdots & \vdots & \ddots \end{bmatrix}$$ - **Diagonal entries**: variance of each variable - **Off-diagonal entries**: covariance between pairs of variables - Always a **symmetric matrix** (the key matrix in PCA) ### Correlation Coefficient Standardizes covariance to remove unit dependence. <br /> $$\rho_{XY} = \frac{\text{Cov}(X, Y)}{\sigma_X \cdot \sigma_Y}$$ <br /> $$-1 \leq \rho \leq 1$$ | Value | Meaning | |---|---| | $\rho = 1$ | Perfect positive linear relationship | | $\rho = 0$ | No linear relationship | | $\rho = -1$ | Perfect negative linear relationship | > **Covariance** = direction of the relationship / **Correlation** = direction + strength --- ## Multivariate Gaussian Distribution An extension of the univariate normal distribution to $n$ dimensions: <br /> $$f(\mathbf{x}) = \frac{1}{(2\pi)^{n/2} |\Sigma|^{1/2}} \exp\left(-\frac{1}{2}(\mathbf{x} - \boldsymbol{\mu})^T \Sigma^{-1} (\mathbf{x} - \boldsymbol{\mu})\right)$$ <br /> - $\boldsymbol{\mu}$: mean vector - $\Sigma$: covariance matrix **When independent**: $\Sigma$ is diagonal → contours are circular **When dependent**: $\Sigma$ has off-diagonal covariance terms → contours are elliptical (tilted) > A larger determinant $|\Sigma|$ means the distribution is more spread out. --- ## Key Formulas at a Glance | Concept | Formula / Description | |---|---| | **Expected value** | $E[X] = \sum x_i P(x_i)$ | | **Variance** | $\text{Var}(X) = E[(X-\mu)^2]$ | | **Standard deviation** | $\sigma = \sqrt{\text{Var}(X)}$ | | **Skewness** | 3rd standardized moment; direction of asymmetry | | **Kurtosis** | 4th standardized moment; tail thickness | | **Marginal distribution** | Sum out one variable from the joint distribution | | **Conditional distribution** | Normalize by the marginal distribution | | **Covariance** | $E[(X-\mu_X)(Y-\mu_Y)]$ | | **Correlation** | $\text{Cov}/(\sigma_X \sigma_Y) \in [-1,1]$ | --- <br /> ## Quiz **Q1. You flip a coin 3 times and win $1 for each heads. What is the expected value of your winnings?** <details> <summary>Show answer</summary> <br /> $$E[X] = 0 \times \frac{1}{8} + 1 \times \frac{3}{8} + 2 \times \frac{3}{8} + 3 \times \frac{1}{8} = \frac{0+3+6+3}{8} = \frac{12}{8} = \$1.50$$ <br /> Or using linearity: $E[X] = 3 \times E[\text{one flip}] = 3 \times 0.5 = 1.5$ </details> --- **Q2. If $\text{Var}(X) = 4$, what is $\text{Var}(3X + 5)$?** <details> <summary>Show answer</summary> <br /> $$\text{Var}(3X + 5) = 3^2 \cdot \text{Var}(X) = 9 \times 4 = 36$$ <br /> The constant 5 has no effect on the variance (it only shifts the location). </details> --- **Q3. In a box plot comparing test scores for two classes A and B, the median of A is 75, the median of B is 85, and the box (IQR) for A is larger than for B. Which statement is correct?** 1. B's IQR is larger than A's 2. B's median is higher than A's 3. A's median is higher than B's <details> <summary>Show answer</summary> **Option 2** — B's median (85) is higher than A's (75). A's IQR is larger than B's, so option 1 is wrong. A's median (75) is lower than B's (85), so option 3 is also wrong. </details> --- **Q4. If the covariance between two variables is 0, are they independent?** <details> <summary>Show answer</summary> **Not necessarily.** Covariance only measures **linear relationships**. For a nonlinear relationship like $Y = X^2$, covariance can be 0 even though the two variables are strongly dependent. However, for a multivariate normal distribution, $\text{Cov} = 0 \Leftrightarrow$ independence does hold. </details> --- **Q5. In a QQ plot, the points form an S-shaped curve above and below the diagonal. What does this indicate?** <details> <summary>Show answer</summary> The data **does not follow a normal distribution**. Specifically: - An S-curve → the distribution has skewness - Points fanning out above/below both ends of the diagonal → heavy tails (high kurtosis) Points should lie close to the diagonal to conclude the data is approximately normal. </details> --- In the next post we'll cover **populations and samples**, the **law of large numbers**, the **central limit theorem**, **MLE**, and **MAP estimation and Bayesian statistics**.
// Related Posts
Probability & Statistics in Practice: Inference Problems from the ML Trenches
From probability fundamentals and Bayes' theorem to distributions, MLE/MAP, confidence intervals, and hypothesis testing — a collection of practice problems grounded in real ML and data analysis scenarios.
Probability & Statistics Coding Assignments: Building ML Statistical Tools in Python
Bayesian updates, distribution simulation, CLT verification, MLE/MAP implementation, confidence intervals, hypothesis testing, and a full A/B test pipeline — implementing probability & statistics chapters 1–4 in code.
ML Probability & Statistics Chapter 4: Confidence Intervals and Hypothesis Testing
A complete guide to confidence intervals, the t-distribution, hypothesis testing fundamentals (null/alternative hypotheses, p-values, rejection regions, statistical power), various t-tests, and A/B testing.