Module 3 — Uncertainty, probability and inference
Lesson 3 of 13
Random variables
Probability gives us a language for uncertainty.
A random variable gives us a way to attach numbers to uncertain outcomes.
The name can sound more complicated than the idea really is.
A random variable is simply:
A numerical quantity whose value is not yet known with certainty.
For example:
- tomorrow's temperature,
- the number rolled on a die,
- electricity demand at 18:00,
- the number of customers arriving in the next hour,
- whether a machine fails,
- the future price of an asset.
Before the outcome is observed, the value is uncertain.
Once the outcome occurs, the random variable takes a particular value.
A simple example: rolling a die
Suppose we roll a six-sided die.
Before the roll, the outcome could be:
1, 2, 3, 4, 5 or 6
Define a random variable:
X = number shown on the die
Before we roll:
X is uncertain.
After the die lands:
X might equal 4.
The random variable gives us a compact mathematical way of representing the uncertain quantity.
Why call it a variable?
A variable can take different values.
For example:
temperature
might be:
18°C
or:
19°C
or:
20°C.
A random variable is similar, except that before we observe the outcome, we do not know which value it will take.
So:
X = tomorrow's temperature
might ultimately become:
X = 21.4°C
But today, that value remains uncertain.
The randomness is in our uncertainty
It is useful not to become too attached to the word random.
Sometimes the underlying system may genuinely contain randomness.
Sometimes the uncertainty arises because we lack information.
Suppose:
X = number of people who will enter a shop tomorrow
That outcome depends on many things:
- weather,
- time,
- individual decisions,
- transport,
- promotions,
- chance events.
We may not know all of those factors.
A random variable allows us to represent the uncertainty regardless of exactly where it comes from.
Random variables map outcomes to numbers
Consider a coin flip.
The possible outcomes are:
Heads
and:
Tails.
Those are not numbers.
But suppose we define:
X = 1 if Heads
X = 0 if Tails
Now the uncertain event has been represented numerically.
So:
OUTCOME → NUMBER
This is what a random variable does.
It gives numerical structure to uncertain events.
Binary random variables
A random variable with two possible values is especially common.
Suppose:
X = 1 if a machine fails tomorrow
X = 0 if it does not fail
Or:
Y = 1 if a customer defaults
Y = 0 otherwise
Or:
Z = 1 if it rains
Z = 0 if it does not
These are binary random variables.
They appear constantly in machine learning and decision systems.
The value is uncertain, not the definition
This distinction is useful.
Suppose:
X = tomorrow's electricity demand at 18:00
The meaning of X is perfectly clear.
What is uncertain is its value.
Perhaps tomorrow:
X = 42.3 GW
Before then, we might assign probabilities to different possible values.
So a random variable has:
a definition
plus:
an uncertain future value.
Discrete random variables
A discrete random variable takes values from a countable set.
Examples include:
- number rolled on a die,
- number of customers,
- number of machine failures,
- number of hospital admissions,
- number of messages received.
Suppose:
X = number of customers arriving in the next hour.
Possible values include:
0, 1, 2, 3, 4, ...
We can assign probabilities to each possibility.
A probability table
Suppose we estimate:
| Customers arriving | Probability |
|---|---|
| 0 | 0.05 |
| 1 | 0.15 |
| 2 | 0.30 |
| 3 | 0.30 |
| 4 | 0.15 |
| 5 | 0.05 |
This table describes the probability distribution of the random variable.
We might write:
X = number of customers arriving
and then specify:
P(X = 2) = 0.30
meaning:
The probability that exactly two customers arrive is 30%.
Continuous random variables
A continuous random variable can take values across a continuous range.
Examples include:
- temperature,
- speed,
- electricity demand,
- rainfall,
- time to failure,
- journey duration.
Suppose:
X = tomorrow's maximum temperature.
Possible values might include:
18.0°C
18.1°C
18.12°C
and infinitely many values in between.
We therefore reason about ranges rather than assigning probability to every exact value individually.
Random variables connect probability to quantities
In the previous lesson, we might have discussed an event such as:
Will it rain?
Now we can define:
X = amount of rainfall tomorrow
This gives us richer information.
Perhaps:
X = 0 mm
or:
X = 2.4 mm
or:
X = 18.7 mm
Now probability can describe uncertainty about a quantity rather than only a yes-or-no event.
One event can be represented in different ways
Suppose we care about tomorrow's rainfall.
We could define:
Binary variable
X = 1 if rainfall > 0
X = 0 otherwise
Or:
Continuous variable
Y = total rainfall in millimetres
Both describe the same underlying weather event.
But they preserve different amounts of information.
This connects directly to our earlier discussion of representation.
The representation should match the decision
Suppose the decision is:
Should I take an umbrella?
A binary variable such as:
rain / no rain
may be enough.
But suppose the decision is:
Should a reservoir operator release water before a storm?
Then the amount of rainfall matters.
A binary representation may be far too crude.
So:
The random variable we define should reflect the question we are trying to answer.
Random variables can describe the future
Many forecasting problems can be written naturally using random variables.
For example:
D = electricity demand at 18:00 tomorrow
W = wind generation tomorrow morning
P = market price next hour
T = temperature tomorrow
J = journey time to work
Each variable has a value that is not yet known.
Probability distributions describe our uncertainty about those future values.
Random variables can describe hidden present states too
Random variables are not only about the future.
Suppose a robot is uncertain about its current position.
We might represent:
X = current horizontal position
Y = current vertical position
The robot does not know X and Y exactly.
It has probabilities over possible values.
So random variables can describe uncertainty about:
- present state,
- hidden state,
- past events,
- future events.
A patient's condition can be a random variable
Suppose:
D = 1 if the patient has a disease
D = 0 otherwise
The patient already either has the disease or does not.
There may be nothing physically random about their current state.
But the clinician does not know which state is true.
Probability can still be used to represent that uncertainty.
This is a good example of why random variables are not limited to future events.
Random variables can be related
Suppose:
T = outside temperature
and:
D = electricity demand.
These two random variables may be related.
Higher temperature might:
- increase cooling demand,
- reduce heating demand.
We may therefore care about the probability of demand given temperature:
P(D | T)
This is how probability begins to describe relationships between uncertain quantities.
Joint uncertainty
Suppose we want to forecast both:
electricity demand
and:
wind generation.
We might define:
D = demand
W = wind generation
The system outcome depends on both.
For example:
shortage = high demand + low wind
So we need to reason about combinations such as:
What is the probability that demand is high while wind generation is low?
This is a joint probability problem.
Correlation between random variables
Random variables can move together.
Suppose:
T = temperature
and:
C = cooling demand.
On hot days, cooling demand tends to be high.
The variables may therefore be positively correlated.
Probability gives us tools to describe not only individual uncertainty but also relationships between uncertain quantities.
Dependence matters
Suppose:
A = electricity demand
and:
B = electricity price.
We should not necessarily assume they are independent.
High demand may increase price.
Price may also influence demand.
The joint behaviour matters.
This becomes especially important when predicting complex systems.
A random vector
Sometimes one random variable is not enough to describe the state of a system.
Suppose an autonomous vehicle has uncertain:
- position,
- velocity,
- acceleration.
We could represent these together as:
X = [position, velocity, acceleration]
This is a random vector.
Each component is uncertain.
Together, they describe uncertainty about the system state.
This connects directly to the state vectors we will study later.
From state vector to probability over states
Suppose the state is:
x = [position, velocity]
If we knew the state perfectly, we could specify exact values.
But perhaps sensors are noisy.
Instead, we might have a probability distribution over possible states.
Conceptually:
POSSIBLE STATE 1 → probability
POSSIBLE STATE 2 → probability
POSSIBLE STATE 3 → probability
This is a powerful way to represent partial observability.
Random variables can change through time
Suppose:
X(t) = electricity demand at time t
Then:
X(09:00)
X(09:30)
X(10:00)
are uncertain quantities indexed through time.
Taken together, they form a stochastic process.
We do not need the full mathematics yet.
The idea is simply:
A random process is a sequence of random variables evolving through time.
This connects probability directly to time-series data.
Future trajectories can be random
Recall the idea of possible future trajectories.
Suppose a vehicle's position tomorrow could follow many possible paths.
Instead of predicting one path:
PATH A
we may assign probabilities to many trajectories:
PATH A → 50%
PATH B → 30%
PATH C → 20%
Now the future trajectory itself is uncertain.
Robotics, weather forecasting and energy systems frequently involve this kind of reasoning.
Random variables can represent model outputs
Machine-learning models often output uncertain quantities.
Suppose a classifier predicts whether an image contains a dog.
Define:
Y = class label
Possible values:
- dog,
- cat,
- fox.
The model estimates probabilities:
P(Y = dog) = 0.80
P(Y = cat) = 0.15
P(Y = fox) = 0.05
The target label is effectively treated as an uncertain variable.
Regression predicts random variables too
Suppose we predict house price.
Define:
Y = future sale price.
A simple model might output:
€500,000.
But a probabilistic model could say:
The most plausible value is around €500,000, with meaningful probability between €450,000 and €560,000.
The output is no longer just a number.
It is a distribution over a random variable.
A random variable has a distribution
Once we define a random variable, we want to know:
Which values are possible, and how likely is each?
That information is contained in its probability distribution.
For a die:
| X | P(X) |
|---|---|
| 1 | 1/6 |
| 2 | 1/6 |
| 3 | 1/6 |
| 4 | 1/6 |
| 5 | 1/6 |
| 6 | 1/6 |
For electricity demand, the distribution may be continuous and much more complicated.
The principle is the same.
The distribution contains more information than the average
Suppose two random variables both have an average value of:
10.
Variable A is usually between:
9 and 11.
Variable B ranges from:
0 to 20.
Their average is the same.
Their uncertainty is very different.
So knowing:
E[X] = 10
is not enough.
We also want to understand the distribution.
This will lead later to variance and other measures of uncertainty.
A probability mass function
For a discrete random variable, we can assign a probability to each value.
This is called a probability mass function.
For example:
P(X = x)
tells us the probability that X takes the value x.
For a fair die:
P(X = 4) = 1/6.
The probabilities across all possible values sum to:
1.
Continuous variables work differently
For a continuous random variable, there may be infinitely many possible exact values.
So rather than asking:
What is the probability that temperature is exactly 20.000000°C?
we usually ask about a range:
What is the probability that temperature lies between 19°C and 21°C?
Probability is spread continuously across the range.
We will explore these distributions in the next lesson.
A random variable is not "random data"
This terminology can cause confusion.
A random variable does not mean:
a randomly chosen column in a dataset.
It is a mathematical representation of an uncertain quantity.
For example:
X = tomorrow's demand
may not yet have any observed data value at all.
The variable represents something whose value will eventually become known.
Observations are realised values
Suppose:
X = tomorrow's temperature.
Today, X is uncertain.
Tomorrow arrives.
The measured temperature is:
21.3°C.
We call:
21.3°C
a realisation or observed value of X.
Before observation:
random variable + distribution
After observation:
realised outcome.
This distinction is fundamental in probability.
Past observations can reveal a distribution
Suppose we observe daily demand at 18:00 for many years.
Those historical observations give us evidence about the behaviour of:
D = demand at 18:00 on a future comparable day.
We can use the historical data to estimate:
- typical values,
- variation,
- probabilities.
The past helps us construct a distribution for an uncertain future variable.
But history is not automatically the future
Suppose historical demand ranged mostly between:
35 and 45 GW.
Then large numbers of electric vehicles and heat pumps enter the system.
The future distribution may change.
The random variable is the same concept:
future demand.
But its probability distribution may no longer resemble the historical one.
This is another example of distribution shift.
Random variables depend on context
Suppose:
D = electricity demand tomorrow at 18:00.
Its distribution depends on information such as:
- temperature,
- weekday,
- season,
- price,
- holidays.
So we might really care about:
P(D | current information)
As new information arrives, the distribution changes.
Random variables become part of stateful probabilistic reasoning.
Before and after new information
Suppose our initial forecast for demand is broad:
35–45 GW plausible.
Then we learn tomorrow will be unusually cold.
The distribution shifts upward.
Then we learn it is a public holiday.
The distribution changes again.
The quantity being predicted is unchanged.
Our probability distribution changes because our information changes.
Random variables can be transformed
Suppose:
D = demand
and:
S = available supply.
We may define a new random variable:
R = S - D
where R represents spare capacity.
If:
R > 0
the system has surplus.
If:
R < 0
the system has shortage.
This is powerful because we can derive uncertain quantities from other uncertain quantities.
System risk comes from combinations of random variables
Suppose:
Demand = uncertain
Wind = uncertain
Generator availability = uncertain
Interconnector availability = uncertain
The overall system risk depends on all of them together.
So an electricity system may need to reason about:
many interacting random variables.
This is much closer to the real world than assuming one perfectly known forecast.
Shared resources are uncertain too
Suppose a hospital has:
50 beds.
Tomorrow's demand for beds is uncertain.
Length of stay is uncertain.
Emergency admissions are uncertain.
Staff availability may also be uncertain.
Service design therefore has to match:
uncertain demand
with:
constrained resources.
This is one reason probability becomes important for resource allocation.
Random variables support scenario thinking
Suppose demand could be:
- low,
- medium,
- high.
Rather than committing to one forecast, a planner can evaluate each scenario.
For example:
LOW DEMAND → Action A works
MEDIUM DEMAND → Action A still works
HIGH DEMAND → Action A fails
Now uncertainty becomes part of planning.
We can ask whether a decision is robust across possible values of the random variable.
The most likely value is not always enough
Suppose:
most likely demand = 40 GW.
But there is a:
5% probability of demand above 50 GW.
If failure above 50 GW would be catastrophic, that tail may strongly influence the decision.
Again:
The entire distribution can matter, not merely the most likely value.
Random variables and expected value
Once outcomes have numerical values and probabilities, we can calculate weighted averages.
Suppose X can be:
€0 with probability 0.5
and:
€100 with probability 0.5.
The expected value is:
€50.
This does not mean we will ever actually receive €50.
It is the probability-weighted average across possible outcomes.
Expected value will be the subject of a later lesson.
The expected value can be impossible as an observation
Suppose:
X = number shown on a die.
The expected value is:
3.5.
But a standard die can never show:
3.5.
This demonstrates something important.
The expected value is a summary of the distribution.
It is not necessarily a possible realised outcome.
Random variables and decisions
Suppose:
X = future electricity price.
A battery operator may decide whether to:
- charge,
- wait,
- discharge.
If X were known perfectly, the decision might be easy.
But X is uncertain.
So the decision depends on:
the distribution of X
and:
the consequences of each action under different values of X.
This is decision-making under uncertainty.
Prediction produces information about random variables
One useful way to understand machine learning is:
A predictive model tries to estimate something about an unknown random variable.
For regression:
estimate the value or distribution of Y.
For classification:
estimate probabilities across possible classes of Y.
For forecasting:
estimate future values of Y over time.
This gives us a common mathematical language across many AI problems.
Large language models predict random variables
Suppose an LLM has a vocabulary containing many possible next tokens.
Define:
X = next token.
Given the current context, the model estimates:
P(X = token | context)
for many possible tokens.
For example:
Paris → 0.94
London → 0.01
Berlin → 0.01
other tokens → remaining probability
The next token is an uncertain discrete variable from the model's perspective.
Then the next random variable appears
Once a token is selected, the context changes.
Now define another variable:
X₂ = next token after that.
Then:
X₃
and so on.
Language generation becomes a sequence of conditional random variables.
Conceptually:
X₁ → X₂ → X₃ → X₄ → ...
with each probability depending on the context created by previous tokens.
AI turns uncertainty into sequences of decisions
A language model predicts a distribution.
Then a decoding rule selects a token.
The selected token becomes part of the next state.
So:
PROBABILITY DISTRIBUTION
↓
SELECTION
↓
NEW CONTEXT
↓
NEW PROBABILITY DISTRIBUTION
This is a small but powerful example of how uncertainty, prediction and sequential decisions interact.
Random variables are everywhere
Once you start looking for them, random variables appear throughout intelligent systems.
Weather
T = tomorrow's temperature
Healthcare
D = whether patient has disease
Energy
E = electricity demand next hour
Transport
J = journey time
Finance
R = future return
Robotics
X = position of moving object in two seconds
Language models
W = next token
The mathematics differs by application.
The underlying concept is the same.
Ask what the uncertain quantity actually is
Whenever we discuss probability, ask:
- What is the random variable?
- What does it represent?
- Is it discrete or continuous?
- What values can it take?
- What information do we currently have?
- What distribution describes our uncertainty?
- Does the distribution depend on context?
- Does it change through time?
- Is the expected value enough?
- Are rare outcomes important?
- What decision will ultimately depend on this variable?
These questions help turn vague uncertainty into a clearly defined modelling problem.
The central idea
Probability tells us how uncertainty is distributed across possibilities.
Random variables tell us what uncertain quantity we are talking about.
Instead of saying:
Tomorrow is uncertain.
we can define:
T = tomorrow's temperature
R = tomorrow's rainfall
D = tomorrow's electricity demand
and describe probability distributions for each.
So the conceptual progression becomes:
UNCERTAIN WORLD
↓
DEFINE QUANTITY OF INTEREST
↓
RANDOM VARIABLE
↓
POSSIBLE VALUES
↓
PROBABILITIES
↓
DISTRIBUTION
Random variables therefore provide the basic objects on which probabilistic models operate.
In the next lesson, we will examine probability distributions in more detail: how probability is spread across possible values, why the shape of a distribution matters, and why two predictions with the same average can imply radically different futures.