prerequisite
Probability basics
Sample spaces, events, conditional probability, independence, the chain rule, and Bayes' rule, worked through with dice and a small tile chunk
Before this
Nothing beyond first-year college math. This is a starting page.
Why you need this
Every generator in this cluster makes choices at random, and every choice is governed by a number: how likely a gap column is to follow a flat run, how likely a forest chunk is to sit beside a town chunk. Those numbers are probabilities. A Markov chain is a table of conditional probabilities, a hidden Markov model is two such tables, and Wave Function Collapse weights its tile choices with them. This page gives you the vocabulary and the handful of rules everything else relies on.
The idea
Sample space, outcomes, events
An experiment is anything with an uncertain result: rolling a die, picking the next column of a side-scroller level. The sample space is the set of every result that could happen. An outcome is one element of that set. An event is a subset of outcomes, which is the same as a yes-or-no question about the result.
For one die, . The outcome might be 4. The event "rolled an even number" is .
For one column of a side-scroller, . The event "the column is dangerous" is .
The probability of an event
is the probability that event happens. It is always a number in : 0 means never, 1 means always, and because something has to happen.
When every outcome is equally likely, is the count of outcomes in divided by the count in . For the even die roll, .
When outcomes are not equally likely, each outcome carries its own probability and is the sum over the outcomes in . Say a column is flat with probability 0.5, gap 0.2, climb 0.2, hazard 0.1. Then .
Complement
The event "not ", written , has . The chance a column is safe is .
"Or" for disjoint events
Two events are disjoint if they share no outcome. For disjoint and , . That is exactly why above was a plain sum: "gap" and "hazard" cannot both be the same column.
If the events overlap, adding double-counts the overlap, so subtract it once: . On a die, let and . Then , and you can check by listing: has four outcomes.
Conditional probability
reads "the probability of given that happened". It is defined as a ratio:
In words: throw away every outcome outside , then ask what fraction of what is left also lies in .
Two dice. Let be "the sum is 8" and be "the first die shows 6". There are 36 equally likely pairs. Five of them sum to 8: (2,6), (3,5), (4,4), (5,3), (6,2). Only (6,2) also has a first die of 6. So
Compare . Learning that the sum is 8 raised the odds of a 6 on the first die from 0.167 to 0.2.
Rearranging the definition gives the multiplication rule: .
Independence
and are independent when knowing tells you nothing about : , or equivalently . Two separate dice both showing 6 has probability . The sum-is-8 example was not independent, since the conditional probability moved.
The product rule for a sequence
Apply the multiplication rule twice and you get the probability of three events in order:
Say the first column is flat with probability 0.5, a gap follows a flat with probability 0.3, and a flat follows the pair (flat, gap) with probability 0.6. Then . The pattern extends to any length: each new factor is conditioned on everything before it. A Markov chain is the special case where each factor is allowed to look only at the single previous event.
Law of total probability
If are disjoint and together cover all of , then
You split the world into cases, find the probability of in each case, and weight the cases by how likely they are.
Bayes' rule
Writing two ways, as and as , and dividing gives
This flips a conditional around. You know how likely an observation is under each hidden cause; Bayes tells you how likely each cause is given the observation.
Worked example
A top-down chunk is a 10 by 10 grid of 100 cells. Each cell has a biome, forest or grass, and may or may not carry a tree decoration. The counts:
| tree | no tree | total | |
|---|---|---|---|
| forest | 25 | 15 | 40 |
| grass | 5 | 55 | 60 |
| total | 30 | 70 | 100 |
Pick one cell uniformly at random. Each count divided by 100 is a probability.
Conditional probability. . And .
Independence check. but . Not equal, so biome and tree are dependent, which is what you want from decoration rules.
Total probability. Split on biome: It matches the table's 30 out of 100.
Bayes' rule. You see a tree. How likely is the cell forest? Check against the table directly: 25 of the 30 tree cells are forest, and . Bayes' rule is just that column of the table, written so it still works when you only have the conditionals and not the raw counts.
In a map generator
- Catalog. Tile weights in a tileset are probabilities of one tile per cell. The "safe or dangerous" event over columns is a union of outcomes.
- Layout. The product rule scores a whole level: the probability of a column sequence is the first column's probability times each conditional in turn. Bayes' rule is what a hidden Markov model does when it looks at emitted tiles and asks which section type produced them.
- Fill. Wave Function Collapse's tile weights, restricted to the tiles still allowed in a cell, are conditional probabilities: .
- Regime over time. The chance a calm region turns contested this tick is .
Every one of those is a table with one row per "given" and one column per "next". A Markov chain is nothing but a set of tables, one row per current tile, each row summing to 1. See Markov chains.
Common mistakes
- Adding probabilities of overlapping events. The total comes out above 1. Symptom: a sampler that never reaches its last option, or a "probability" of 1.3 in a debug print.
- Swapping for . is 0.625 but is 0.833; if forest were rare the two would differ wildly. Symptom: an inference step labels every tree cell "forest" even in a map that is 95 percent grass.
- Assuming independence that is not there. Estimating "two gaps in a row" as ignores that gaps may cluster or repel. Symptom: the predicted frequency of double gaps disagrees with what the generator actually produces.
- Conditioning on an impossible event. means dividing by zero. Symptom:
NaNin a transition row for a state that never appeared in the training data. - Editing one entry and not renormalizing. A row that sums to 1.1 is not a distribution. Symptom: the chain drifts toward whichever state got the extra mass.
- Reading 0.1 per column as "one hazard every ten columns". Independent draws cluster. Three hazards in a row has probability 0.001 per triple, but a 300-column level has almost 300 triples. Symptom: a playtester reports "hazards come in clumps" and the numbers say that is expected.
Cost
Nothing here is an algorithm, so the cost is storage. A table of over states holds numbers. Conditioning on the two previous states instead of one needs . With section types that is 36 versus 216, both trivial; with tiles and three steps of history it is 2.5 million entries, most of which no training example ever touches, so the estimates become noise. That is the practical reason the one-step Markov assumption is the default.
Going further
- Random variables and distributions, for turning a row of probabilities into an actual sample.
- Markov chains, where the conditional table becomes a generator.
- Hidden Markov models, where Bayes' rule does real work.
- The chapters on conditional probability and independence in any first-year probability text.
Leads to
- prerequisiteLogarithms and underflowWhy multiplying hundreds of probabilities gives zero on a computer, and how adding logs, comparing in log space, and the log-sum-exp trick fix it
- prerequisiteRandom variables and distributionsDiscrete random variables, probability mass functions, categorical distributions, expectation, and how to sample any of them with a single uniform draw