You can score any single guess for a weight, but you can't add up the scores of every possible guess. A walker that only ever compares two neighbours ends up spending its time in proportion to the answer anyway. Predict where it goes on five islands, then on forty house sales.
mcmcbayesiansamplinglessonuncertainty
Live/Interactive · drag, toggle, run it
Say you have 40 house sales and a price formula with four unknown weights: a base price, a price per square metre, a price per bedroom and a price per km from the city. You don't only want the best value for each weight. You want to know how sure to be about it.
For any guess at the weights you can compute a score: how well that guess explains the 40 sales. What you can't do is add up the score of every possible guess, and that total is what turns scores into probabilities. MCMC (Markov chain Monte Carlo) gets around the missing total with a walk. Start smaller than houses, though.
01Five islands and one rule
A walker hops along a chain of five islands with 1,000 to 5,000 people on them. Every step follows the same rule:
Flip a coin to pick a neighbour: heads left, tails right.
If the neighbour has more people, move there.
If it has fewer, move with chance (their people ÷ your people). Otherwise stay put.
Your call
The walker is on island 4 (4,000 people). The coin points to island 3 (3,000 people). What's the chance it moves?
Pick an answer to see what happens.
Your call
Let the walker take 10,000 steps. What share of those steps will it spend on island 5?
Pick an answer to see what happens.
Your call
When the walker turns a move down and stays, what goes in the tally for that step?
Pick an answer to see what happens.
02Swap the islands for prices per square metre
Now the islands are possible values for one weight, the price per square metre. For the moment, pretend the other three weights are known, so there's one number to find. An island's population becomes the score of that value: how believable it was before seeing any sales (the prior), times how likely the 40 sale prices are if this value were true (the likelihood).
The walker proposes a nearby value, compares its score to the current one, and uses the same rule as on the islands. With a single weight we can also score every value from $6,500 to $8,500 one by one, which lets us check the walker against the full answer.
Your call
The walker starts at $5,000 per square metre, where the score is tiny. After 3,000 steps, where will its tally pile up?
Pick an answer to see what happens.
Checking 100 values one by one was cheap. So why walk at all?
Your call
Scoring 100 values took 100 checks for one weight. How many checks to cover all four weights at 100 values each?
Pick an answer to see what happens.
03All four weights at once
Each step now nudges all four weights together, scores the new combination, and applies the same rule. The walker starts from a bad guess: $3,000 per square metre, $10,000 per bedroom, $0 per km and a $150k base price.
Your call
Which steps should go into the final tally?
Pick an answer to see what happens.
Your call
After 3,000 steps, the 90% range for price per square metre is $6,690 to $8,122. Run 30,000 steps instead. The range will be:
Pick an answer to see what happens.
04Which summary is right?
Your call
Pick the sentence you'd use to explain MCMC to a colleague.
Pick an answer to see what happens.
05A question the samples can answer
Every kept step is a full set of four weights, so every step can price a house. Put a house through all 2,500 kept steps and you get 2,500 prices, and their spread is how unsure the model is about that house.
Your call
A 90 sqm, 3-bedroom house 10 km from the city has an average-price range of $980k to $1,025k. Move the same house to 40 km. Every sale in the data was between 3 and 24 km. The range will be:
Pick an answer to see what happens.
Now take the full sampler apart yourself. House Prices with MCMC runs the same model as a full workbench: the data, the running chain, each weight's distribution, and a price prediction you can steer.
The island walk comes from John Kruschke's Doing Bayesian Data Analysis (2nd edition, 2015, chapter 7). The house sales are synthetic: 40 houses made from the true weights shown plus $60k of random noise each. This draw was picked so the true weights land inside their 90% ranges; with real data each range misses the truth about one time in ten. The model is deliberately simple: a straight-line price formula with known noise. Everything on this page is computed in your browser with seeded random numbers, so the numbers are the same on every visit.