Model dependenceLesson 26 of 76 · 7 min

Make the shared cause visible

Represent common conditions once per trial and propagate them to related inputs.

A common cause is a simulated condition that influences several outcomes. It should be drawn once for the relevant unit. Drawing a fresh weather condition for every calculation in one event would erase the relationship the model intends to represent.

Here a busy-day factor affects both arrival counts and average spending. Each trial draws one factor and passes it into both calculations. Conditional on that factor, additional randomness can still remain in each outcome.

This hierarchical model draws a shared condition first, then outcomes given that condition. The factor is latent, meaning unobserved in the scenario, rather than a separate measured input. It is one possible mechanism, not proof about a real shop. Inspect intermediate values and challenge the shared-cause assumption.

F is the shared busy-day multiplier; log(F) is normal with mean 0 and SD .25. The arrows mean use that same F in both calculations.

One shared condition per world
  1. Draw the common condition
    log⁡F∼N(0,0.252)\log F\sim N(0,0.25^2)
  2. Use it in both inputs
    F→arrivalsF→spending\begin{gathered}F\to\text{arrivals}\\F\to\text{spending}\end{gathered}
  3. Propagate the relationship
    revenue=arrivals×spending\begin{gathered}\text{revenue}\\=\text{arrivals}\times\text{spending}\end{gathered}
Reference illustration. The same factor feeds both inputs in each simulated world. Redrawing it independently for spending changes the joint model and can change the revenue tail.

What happens to the intended dependence if you redraw the day factor separately for spending?

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 ↗

Arrival/spending correlation: 0.84
Mean and P90 revenue (£): 545.45 929.91
First three shared factors: [0.574 1.007 0.874]

Read the first result

The model makes the common factor inspectable. Revenue inherits both its direct and indirect effects.

Try an edit

On the spend_per_customer line, replace 12 * busy_factor with 12 * rng.lognormal(0, .25, len(busy_factor)). The arrival calculation stays fixed. Select Run Python again. Compare Arrival/spending correlation and Mean and P90 revenue (£) with the first run. What changed when spending used a fresh factor with the same distribution?

Back to the Python editor

Scope. The busy-day mechanism is invented, and the chosen spending noise is not fitted to observations.

Try a fresh case

A regional storm affects several deliveries. Where should its state be drawn?

Choose an answer
Need a hint?
Hint 1

Name the unit to which the storm belongs.

Hint 2

Keep all outcomes from that unit attached to the same state.

Your notes