Model dependenceLesson 29 of 76 · 7 min

Sample one condition after another

Use conditional sampling to preserve relationships and constraints.

Conditional sampling specifies a distribution after a relevant condition has been drawn. It gives a transparent sequence: simulate the state, select the appropriate input model, then simulate the outcome from that model.

In this event example, rain is drawn once per event. Attendance probability is lower on rainy days. The guest count is then drawn using the event’s probability. Marginally, guests share a weather influence, so the resulting attendance distribution is not the same as one fixed-probability binomial in every respect.

The law of total expectation gives a useful exact mean check: average the conditional expectations using the state probabilities. Matching that mean does not establish the correct tail shape. Compare both the overall distribution and the conditional groups when checking a model.

X∣pX\mid p reads ‘attendance X given guest chance p’. The weather selects p once for each complete event.

Condition first, then draw
  1. Weather
    P(rain)=0.3P(\text{rain})=0.3
  2. Attendance chance
    p=0.5  or  0.8p=0.5\;\text{or}\;0.8
  3. Event attendance
    X∣p∼Binomial⁡(100,p)X\mid p\sim\operatorname{Binomial}(100,p)
Reference illustration. The authored weather state selects the attendance probability for an entire event. Replacing it with the average probability preserves a mean but generally changes the variance and tails.

Can replacing the two weather-dependent probabilities with their average preserve the mean while changing the spread?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Simulated / exact mean attendance: 71.132 71.0
Mean on rainy / dry days: 49.97 80.0
Overall attendance SD: 14.369
P(attendance > 80): 0.3248

Read the first result

Compare the rainy-day and dry-day means in Mean on rainy / dry days. The shared weather condition affects the spread of the overall counts. P(attendance > 80) gives the fraction of simulated events with more than 80 attendees.

Try an edit

Replace attendance = rng.binomial(100, attendance_probability) with attendance = rng.binomial(100, .71, size=len(rainy)). Select Run Python again. Compare Overall attendance SD and P(attendance > 80) with the first run. Why can the probability of a high-attendance event change even when the model mean stays 71?

Back to the Python editor

Scope. The rain probabilities are assumptions, and weather effects are simplified to two event states.

Try a fresh case

A delivery duration depends on its route. Which generation order preserves that model?

Choose an answer
Need a hint?
Hint 1

Identify which condition chooses the distribution.

Hint 2

Preserve that condition within the same trial.

Your notes