Probability and Statistics for Machine Learning

Probability as Linear Algebra

Probability reinterpreted through linear algebra: random variables as vectors, expectation as a linear functional, and covariance as a weighted inner product.

Lesson
13

Goal

Understand probability as a continuation of the linear-algebra story: random variables behave like vectors, expectation behaves like a linear functional, and covariance behaves like a weighted dot product after centering.

Motivation

Machine learning uses probability constantly, but the formulas become much easier to manage when they are tied back to familiar objects.

This lesson gives a unifying view:

That perspective makes the later probability lessons feel less like a branch change and more like the same ideas in new clothing.

Prerequisites

Notation / Symbol Table

SymbolPlain-English nameMeaning
Ω\Omegasample spaceSet of all possible outcomes.
ω\omegaoutcomeOne element of the sample space.
X(ω)X(\omega)random variable valueThe number assigned by XX to outcome ω\omega.
E[X]E[X]expectationWeighted average of the values of XX.
X~\tilde{X} or X~centered random variableThe mean-zero version of XX, defined by XE[X]X - E[X].
Var(X)\operatorname{Var}(X)varianceAverage squared distance from the mean.
Cov(X,Y)\operatorname{Cov}(X, Y)covarianceAverage product of centered values.
Σ\Sigmacovariance matrixCovariance collected across all coordinates of a random vector.

Read X~\tilde{X} aloud as “X tilde.” In this lesson it always means the centered version of XX:

X~=XE[X].\tilde{X} = X - E[X].

Core Ideas

A random variable is a function

A random variable is not “a random number” in the abstract. It is a function from outcomes to numbers:

X:ΩR.X : \Omega \to \mathbb{R}.

For a die roll, one random variable might return the face value. Another might return 11 for an odd roll and 00 for an even roll.

The important point is that the same sample space can support many different random variables.

Expectation is a weighted average

For a discrete variable,

E[X]=ixiP(xi).E[X] = \sum_i x_i P(x_i).

Expectation is the center of mass of the distribution, not necessarily a value that the variable ever literally takes.

That is why a fair die has expectation 3.53.5 even though no roll produces 3.53.5.

Expectation is linear

Expectation respects linear combinations:

E[aX+bY]=aE[X]+bE[Y].E[aX + bY] = aE[X] + bE[Y].

This is a strong hint that expectation should be treated as a linear map, not only as a recipe.

If you like the programming analogy, expectation is a reducer from a structured random object down to one scalar summary.

Centering puts the mean at zero

Define

X~=XE[X].\tilde{X} = X - E[X].

Then

E[X~]=0.E[\tilde{X}] = 0.

This is the same centering step we used with datasets. Probability now inherits the same workflow: first subtract the mean, then measure geometry around that center.

Variance is squared length after centering

Variance can be written as

Var(X)=E[(XE[X])2]=E[X~2].\operatorname{Var}(X) = E[(X - E[X])^2] = E[\tilde{X}^2].

That is the probability version of squared length. If the centered variable usually stays close to zero, the variance is small. If it often wanders far away, the variance is large.

Covariance is a weighted dot product

Covariance is

Cov(X,Y)=E[(XE[X])(YE[Y])]=E[X~Y~].\operatorname{Cov}(X, Y) = E[(X - E[X])(Y - E[Y])] = E[\tilde{X}\tilde{Y}].

Compare that with a dot product:

ab=iaibi.a \cdot b = \sum_i a_i b_i.

The structure is the same except that probability adds weights through expectation. That is why covariance behaves so much like an inner product and why the covariance matrix acts like a Gram matrix.

The covariance matrix is an averaged outer product

For a random vector XX with mean μ\mu,

Σ=E[(Xμ)(Xμ)T].\Sigma = E[(X - \mu)(X - \mu)^T].

This is the probabilistic version of multiplying centered data by its transpose and averaging. Once you write it this way, PCA looks almost inevitable.

Worked Example

Let a discrete random variable take values

1, 3, 51,\ 3,\ 5

with equal probability.

First compute the expectation:

E[X]=1+3+53=3.E[X] = \frac{1 + 3 + 5}{3} = 3.

Now center the values:

X~{2,0,2}.\tilde{X} \in \{-2, 0, 2\}.

Then compute the variance:

Var(X)=E[X~2]=(2)2+02+223=83.\operatorname{Var}(X) = E[\tilde{X}^2] = \frac{(-2)^2 + 0^2 + 2^2}{3} = \frac{8}{3}.

This example is small, but it shows the whole pipeline clearly:

  1. define a variable;
  2. average it with expectation;
  3. center it;
  4. square the centered values;
  5. take expectation again.

Why This Shows Up In ML

Machine learning constantly averages over data, noise, and uncertainty.

This point of view shows up when we:

The practical win is that probability stops feeling like isolated formulas. It becomes geometry plus averaging.

Exercises

  1. A fair coin uses Heads =1= 1 and Tails =0= 0. Compute E[X]E[X].

  2. For the variable taking values 1,3,51, 3, 5 with equal probability, compute the centered values again without looking back.

  3. Using the same variable, recompute Var(X)\operatorname{Var}(X) and say which step turns a list of numbers back into one scalar.

  4. If two centered random variables are usually positive together or negative together, should their covariance be positive, negative, or near zero?

  5. Explain why expectation is a linear map in one sentence and one equation.

Implementation Exercise

Represent a discrete distribution in TypeScript as an array of objects like:

type WeightedValue = {
  value: number;
  probability: number;
};

Then implement helpers for:

Use .reduce for the weighted sums so the code mirrors the mathematical definition.

Reflection Questions