IV.7 · Probability
How does an uncertain outcome become a number?
Map each die face to a value, then watch probability flow into the value groups. The function is fixed; uncertainty comes from the outcome that occurs.
A random variable maps outcomes to values
Numerical values of X · bar height is probability mass
Face 1 maps to 0, with mass 1/6; Face 2 maps to 0, with mass 1/6; Face 3 maps to 1, with mass 1/6; Face 4 maps to 1, with mass 1/6; Face 5 maps to 2, with mass 1/6; Face 6 maps to 2, with mass 1/6. Six outcomes induce 3 numerical value(s) with positive mass. Matching values combine their original masses; the induced distribution still totals one.
- Expected numerical value
- 1
- Variance
- 2/30.666667…
= exact model quantity · ≈ numerical companion or observed estimate
from −4 to 6, up to 2 decimal places
from 0 to 100, whole numbers only
Keep the original outcomes
The six die faces remain distinguishable, each with its model probability.
Before editing, predict how many faces feed the value 1. Move just one face into another group: does the original die become less fair, or does only its numerical summary change?
Intuition: an outcome and its value
A die roll produces a face. A program might record the face itself, whether it is even, or a score assigned to it. Those are different numerical descriptions of the same experiment. The assignment does not have to give different numbers to different faces.
The canonical stage assigns faces 1 through 6 the values . There are six equally likely outcomes but only three value groups. Keeping those two spaces distinct explains why each group has mass , rather than .
Definition
A real-valued random variable is a function . On a finite sample space, it assigns exactly one real number to each outcome. For a value , its induced probability is
More generally, . The set inside the probability is the preimage of the value event. Several outcomes can share a value; the function need not be one-to-one. Here every finite assignment defines measurable events automatically; general infinite-space measurability is outside this chapter.
Mechanics: group, then add
Read each row as an original outcome, its probability weight, and its assigned value. Edit one value or weight and compare the induced bars, probability inspector, formula, and trace. Raw weights are normalized by their positive total; they are not probabilities until normalized. Changing a value redirects a face’s existing mass. Changing a weight changes the experiment’s mass distribution.
The trace first normalizes the source weights, then groups outcomes by equal value, and finally adds each group’s masses. The PMF describes the numerical variable, while the original outcome labels still describe the experiment. Reset restores the same mapping and equal weights.
Derivation: why total mass survives
Every outcome belongs to exactly one value group. Summing across the distinct values therefore counts every source mass exactly once:
This establishes normalization without assuming equally likely value labels. Nonnegative source masses also make every induced mass nonnegative. A many-to-one mapping combines mass; it never duplicates mass.
Worked examples
For the canonical fair die, on faces , on , and on . Each pair contributes . The event pulls back to , giving probability .
Worked example: A random variable maps outcomes to values
Numerical values of X · bar height is probability mass
Face 1 maps to 0, with mass 1/6; Face 2 maps to 0, with mass 1/6; Face 3 maps to 1, with mass 1/6; Face 4 maps to 1, with mass 1/6; Face 5 maps to 2, with mass 1/6; Face 6 maps to 2, with mass 1/6. Six outcomes induce 3 numerical value(s) with positive mass. Matching values combine their original masses; the induced distribution still totals one.
- Expected numerical value
- 1
- Variance
- 2/30.666667…
= exact model quantity · ≈ numerical companion or observed estimate
from −4 to 6, up to 2 decimal places
from 0 to 100, whole numbers only
Keep the original outcomes
The six die faces remain distinguishable, each with its model probability.
For a weighted experiment with outcomes of masses , map to and to . The resulting masses are and . The number of labels has fallen to two, but equal-label counting would discard the source weights.
Executable reference code
The standard-library program explicitly loops over outcome/value pairs and accumulates exact Fraction masses. It asserts the canonical PMF, the pulled-back event probability, unequal-weight grouping, zero-mass labels, and rejection of invalid models. The NumPy version uses unique to form groups and bincount to accumulate their masses. Read the loop first; the array indices implement the same grouping mechanics.
"""Push a finite experiment through a random-variable function, exactly."""
from fractions import Fraction as F
def induced_distribution(values, masses):
if not values or len(values) != len(masses):
raise ValueError("One value and one mass are required per outcome")
if any(not isinstance(x, (int, F)) or isinstance(x, bool) for x in values):
raise ValueError("Values must be integers or exact Fractions")
if any(not isinstance(p, F) or p < 0 for p in masses) or sum(masses, F(0)) != 1:
raise ValueError("Nonnegative Fraction masses must sum to 1")
result = {}
for value, mass in zip(values, masses):
result[value] = result.get(value, F(0)) + mass
return result
def main():
values = [0, 0, 1, 1, 2, 2]
masses = [F(1, 6)] * 6
induced = induced_distribution(values, masses)
assert induced == {0: F(1, 3), 1: F(1, 3), 2: F(1, 3)}
assert sum(p for x, p in induced.items() if x >= 1) == F(2, 3)
assert sum(p for x, p in zip(values, masses) if x >= 1) == F(2, 3)
assert induced_distribution([0, 0, 1], [F(1, 2), F(1, 4), F(1, 4)]) == {0: F(3, 4), 1: F(1, 4)}
assert induced_distribution([2, 2], [F(0), F(1)]) == {2: F(1)}
assert induced_distribution([9, 0], [F(0), F(1)])[9] == 0
for values_bad, masses_bad in (([], []), ([0], [F(1, 2)]), ([0], [1.0]),
([0, 1], [F(-1), F(2)]), ([0, 1], [F(1)]), ([float('nan')], [F(1)])):
try:
induced_distribution(values_bad, masses_bad)
except ValueError:
pass
else:
raise AssertionError("Invalid finite mapping accepted")
print("Exact induced PMF:", induced)
print("P(X >= 1) =", sum(p for x, p in induced.items() if x >= 1))
if __name__ == "__main__":
main()"""NumPy groups equal values with unique/inverse indices and weighted bincount."""
import numpy as np
def induced_distribution(values, masses):
x, p = np.asarray(values, dtype=float), np.asarray(masses, dtype=float)
if x.ndim != 1 or p.shape != x.shape or x.size == 0 or not np.all(np.isfinite(x)):
raise ValueError("Provide a nonempty finite value vector and matching masses")
if not np.all(np.isfinite(p)) or np.any(p < 0) or abs(p.sum() - 1) > 1e-12:
raise ValueError("Nonnegative finite masses must sum to 1 within 1e-12")
labels, inverse = np.unique(x, return_inverse=True)
return labels, np.bincount(inverse, weights=p, minlength=labels.size)
def main():
labels, masses = induced_distribution([0, 0, 1, 1, 2, 2], [1/6] * 6)
np.testing.assert_array_equal(labels, [0, 1, 2])
np.testing.assert_allclose(masses, [1/3, 1/3, 1/3], rtol=0, atol=1e-12)
np.testing.assert_allclose(masses[labels >= 1].sum(), 2/3, rtol=0, atol=1e-12)
np.testing.assert_allclose(induced_distribution([0, 0, 1], [0.5, 0.25, 0.25])[1], [0.75, 0.25], rtol=0, atol=1e-12)
np.testing.assert_array_equal(induced_distribution([9, 0], [0, 1])[1], [1, 0])
for x, p in (([], []), ([0], [0.5]), ([0, 1], [-1, 2]), ([np.nan], [1]), ([0], [np.inf])):
try:
induced_distribution(x, p)
except ValueError:
pass
else:
raise AssertionError("Invalid mapping accepted")
print("Value labels:", labels, "induced masses ≈", masses)
if __name__ == "__main__":
main()Explore
Choose Every face maps to 2. Predict the sole induced mass before inspecting it: , although the die still has six source outcomes. Choose Distinct values to recover one value per face. Compare the bar count and the source-outcome count in both states.
Reset and change one source weight. Explain how that face’s value group gains or loses probability after normalization. Then move that face to another value without changing its weight. Trace the mass through both operations; do not infer an event probability from the number of bars.
Common mistakes and counterexamples
“Random” does not mean the assignment itself must be redrawn after every roll. Once the model fixes , observing a face fixes its value. Assigning two values to one outcome without a selection rule would fail the definition of a function.
A value is not its probability: can have mass , and a negative score can have positive probability. A constant random variable is valid. A listed value whose only preimages have zero mass is also valid and has probability zero; this differs from an invalid all-zero weight model.
Numerical considerations
Grouping needs a rule for equality. These examples use integer or exact rational values, so equal labels can be identified exactly. With floating-point measurements, two nearly equal numbers are not automatically the same category; a rounding or binning policy would define a different variable and must be stated.
Exact Fraction arithmetic preserves the finite sums. NumPy uses finite floating-point inputs and checks mass normalization within absolute tolerance . A tolerance in a numerical check does not turn probability-zero values into missing observations or justify merging nearby labels.
Engineering and ML connections
A loss function turns an uncertain example into a number. An indicator maps an event to and its complement to . A class-count variable groups detailed trial sequences by their success totals. In each case, probabilities belong to the underlying experiment and flow through the chosen function.
Changing a scoring function changes the random variable, while changing the data-generating model changes the source probabilities. Keeping these decisions separate prevents a visualization or evaluation metric from silently inventing equal weights.
Exercises
Use exact fractions for finite masses. Each attempt provides corrective feedback and a worked solution.
Summary and checkpoints
A random variable assigns numbers to outcomes. Its distribution sums source masses over equal-value groups, and a value event pulls back to an outcome event. Check that you can recover the canonical three masses, preserve unequal weights, and distinguish constant or zero-mass values from invalid models.