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:
- a random variable is a function;
- expectation is an averaging operator;
- variance and covariance are geometric measurements in a vector space of random variables.
That perspective makes the later probability lessons feel less like a branch change and more like the same ideas in new clothing.
Prerequisites
- Lesson 11: Principal Component Analysis.
- Lesson 12: Covariance.
Notation / Symbol Table
| Symbol | Plain-English name | Meaning |
|---|---|---|
| sample space | Set of all possible outcomes. | |
| outcome | One element of the sample space. | |
| random variable value | The number assigned by to outcome . | |
| expectation | Weighted average of the values of . | |
| or | centered random variable | The mean-zero version of , defined by . |
| variance | Average squared distance from the mean. | |
| covariance | Average product of centered values. | |
| covariance matrix | Covariance collected across all coordinates of a random vector. |
Read aloud as “X tilde.” In this lesson it always means the centered version of :
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:
For a die roll, one random variable might return the face value. Another might return for an odd roll and 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,
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 even though no roll produces .
Expectation is linear
Expectation respects linear combinations:
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
Then
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
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
Compare that with a dot product:
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 with mean ,
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
with equal probability.
First compute the expectation:
Now center the values:
Then compute the variance:
This example is small, but it shows the whole pipeline clearly:
- define a variable;
- average it with expectation;
- center it;
- square the centered values;
- 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:
- compute average loss over a dataset;
- measure variance of errors or features;
- build Gaussian models from means and covariances;
- define correlations, principal directions, and whitened coordinates;
- treat gradients, losses, and predictions as random quantities over sampled data.
The practical win is that probability stops feeling like isolated formulas. It becomes geometry plus averaging.
Exercises
-
A fair coin uses Heads and Tails . Compute .
-
For the variable taking values with equal probability, compute the centered values again without looking back.
-
Using the same variable, recompute and say which step turns a list of numbers back into one scalar.
-
If two centered random variables are usually positive together or negative together, should their covariance be positive, negative, or near zero?
-
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:
- expectation;
- centering a discrete random variable;
- variance.
Use .reduce for the weighted sums so the code mirrors the mathematical definition.
Reflection Questions
- Did it help to think of a random variable as a function instead of as a “random number”?
- Which feels clearer: variance as a formula or variance as squared distance from the mean?
- What does expectation do that centering and squaring do not?
- When you see covariance now, do you read it more like a statistic or more like a weighted inner product?
Completed Exercises
Handwritten work submitted after completing this lesson.


