Will it rain today? Until you know, “rain” and “no rain” are both possible outcomes. A random variable gives the unknown outcome a value: 1 for rain and 0 for no rain. More generally, a random variable is a variable whose value depends on the outcome of a probabilistic experiment. section 1.2
A probability distribution describes which values are possible and how likely they are. For the rain variable, a Bernoulli distribution uses one parameter, p: the probability of rain is p, and the probability of no rain is 1 − p. In Bayesian reasoning, the same approach can represent a belief about an unknown quantity, not just an event such as tomorrow’s weather. section 1.5
Choose values that match the question and the detail you need:
- Use a binary variable for whether it rains. section 1.2
- Use rainfall amounts for a more detailed forecast. Although an amount could be modeled continuously, rounded amounts form practical categories; finer categories provide more detail but can make a model harder to run. section 1.2 section 1.3
An unknown preference
Suppose p is the proportion of people in an office who prefer tea to coffee. Treating p as a random variable lets you assign probabilities to possible proportions. If you start with eleven values—0, 0.1, …, 1—you can update their probabilities using data, such as three tea preferences among five people. Those eleven values are a convenient approximation: they cannot express every proportion between 0 and 1. section 3.1
An unknown effect
A random variable can also stand for a model parameter. In a pooled analysis of eight schools, θ represents one shared average effect of coaching on exam scores. A prior expresses uncertainty about θ; observations then inform its posterior distribution. The pooled assumption treats the schools as having the same effect. section 7.3
y = np.array(
[28, 8, -3, 7, -1, 1, 18, 12] #A
)
sigma = np.array(
[15, 10, 16, 11, 9, 11, 10, 18] #B
)
with pm.Model() as pooled:
theta = pm.Normal("theta", 0, sigma=1e6) #C
obs = pm.Normal(
"obs",
theta,
sigma=sigma, #D
observed=y, #E
)
trace_p = pm.sample() #F
The PyMC listing makes the shared effect concrete: `theta` is given a Normal prior, the observed school results are modeled around it, and `pm.sample()` draws posterior samples. section 7.3
The idea extends to mixture models: component weights, centers, and widths can all be treated as random variables. Keeping distributions over unknowns makes it possible to describe uncertainty rather than commit to one set of estimates. section 8.4 section 8.2





