Beer Game: The Bullwhip Effect
A Beer Game simulation of the bullwhip effect: one demand step whips factory orders from a surge to zero unless each tier counts stock already in transit.
Level: Advanced
Delays
Learn about delays in systems, how they create oscillations, and their impact on system behavior and decision-making.
Take the QuizFeedback Loops
Understand the balancing and reinforcing feedback loops that drive system behavior and create complex dynamics in systems thinking.
Take the QuizBeer Game: why a small demand step whipsaws a supply chain
Four tiers pass beer up a chain: orders go upstream, cases come back, and
every hop takes weeks. Customer demand sits at 4 cases a week, then steps
once to 8. With account_for_pipeline off, factory orders surge past
60 and stall at zero — the bullwhip effect, made inside the chain.
Flip that one boolean and the same step settles near 8; the single-store
version of the mistake is
Inventory Oscillation.
import math
from tys import probe, progress
Retailer → wholesaler → distributor → factory. The middle two are simulated so the wave can grow, and only the ends are charted.
STAGES = ["retailer", "wholesaler", "distributor", "factory"]
SHIP_DELAY = 2 # weeks a shipment is on the truck
ORDER_DELAY = 2 # weeks an order spends in the mail
PROD_DELAY = 2 # weeks the brewery needs to make a batch
BASE_DEMAND = 4.0
STEP_DEMAND = 8.0
STEP_WEEK = 5
TARGET_INVENTORY = 12.0
INV_ADJ_TIME = 4.0 # weeks to close an inventory gap
SL_ADJ_TIME = 4.0 # weeks to close a supply-line gap
SMOOTHING = 0.25
SIM_TIME = 52Chart the two ends. Wholesaler and distributor still order; they are not plotted.
CHART = {0, 3}
def _variance(values):
count = len(values)
if count == 0:
return 0.0
mean = sum(values) / count
return sum((value - mean) ** 2 for value in values) / count
def simulate(cfg: dict):
import simpy
env = simpy.Environment()
n = len(STAGES)Downstream tiers wait out the mail and the truck. The factory has no supplier, so its lag is only the brew. This week's delivery has already landed when the order is chosen, so a calm pipeline holds lag − 1 weeks.
lags = [
float(PROD_DELAY if i == n - 1 else ORDER_DELAY + SHIP_DELAY)
for i in range(n)
]The knob. False ignores beer already on the way; true counts all of it.
account_for_pipeline = bool(cfg.get("account_for_pipeline", False))
inventory = [TARGET_INVENTORY] * n
backlog = [0.0] * n
supply_line = [BASE_DEMAND * lags[i] for i in range(n)]
forecast = [BASE_DEMAND] * n
order_inbox = [simpy.Store(env) for _ in range(n)]
ship_inbox = [simpy.Store(env) for _ in range(n)]
def courier(dest, weeks, qty):Tiers act at week + 0.5; couriers deliver on the week boundary.
arrival = math.floor(env.now) + weeks
yield env.timeout(arrival - env.now)
dest.put(qty)
def prime(dest, week, qty):
yield env.timeout(week)
dest.put(qty)
for i in range(n):
inbound = PROD_DELAY if i == n - 1 else SHIP_DELAY
for week in range(inbound):
env.process(prime(ship_inbox[i], week, BASE_DEMAND))
for i in range(1, n):
for week in range(ORDER_DELAY):
env.process(prime(order_inbox[i], week, BASE_DEMAND))
def collect(inbox):
received = 0.0
while inbox.items:
received += yield inbox.get()
return received
customer_orders = []
factory_orders = []
peak_orders = [0.0] * n
max_backlog = 0.0
def tier(i):
nonlocal max_backlog
yield env.timeout(0.5)
while True:
week = math.floor(env.now)
arrived = yield from collect(ship_inbox[i])
inventory[i] += arrived
supply_line[i] = max(supply_line[i] - arrived, 0.0)
if i == 0:
incoming = STEP_DEMAND if week >= STEP_WEEK else BASE_DEMAND
customer_orders.append(incoming)
else:
incoming = yield from collect(order_inbox[i])
backlog[i] += incoming
forecast[i] += SMOOTHING * (incoming - forecast[i])
shipment = min(inventory[i], backlog[i])
inventory[i] -= shipment
backlog[i] -= shipment
if i > 0:
env.process(courier(ship_inbox[i - 1], SHIP_DELAY, shipment))
order = forecast + stock gap + (optional) supply-line gap. Dropping the last term re-orders a hole the trucks will fill.
net_stock = inventory[i] - backlog[i]
stock_gap = (TARGET_INVENTORY - net_stock) / INV_ADJ_TIME
weeks_still_out = max(lags[i] - 1.0, 0.0)
pipeline_gap = 0.0
if account_for_pipeline:
pipeline_gap = (
forecast[i] * weeks_still_out - supply_line[i]
) / SL_ADJ_TIME
order = max(0.0, forecast[i] + stock_gap + pipeline_gap)
supply_line[i] += order
if i < n - 1:
env.process(courier(order_inbox[i + 1], ORDER_DELAY, order))
else:
env.process(courier(ship_inbox[i], PROD_DELAY, order))
if i in CHART:
probe(f"order_{STAGES[i]}", week, order)
probe(f"inventory_{STAGES[i]}", week, net_stock)
if i == n - 1:
factory_orders.append(order)
peak_orders[i] = max(peak_orders[i], order)
max_backlog = max(max_backlog, -net_stock)
if i == n - 1:
message = None
if week == STEP_WEEK:
message = "Customer demand steps to 8 — the only shock"
elif week + 1 == SIM_TIME:
message = (
"Factory orders settled near the new demand"
if account_for_pipeline
else "Factory orders whipsawed, then stalled"
)
progress(int(100 * (week + 1) / SIM_TIME), message)
yield env.timeout(1)
for i in range(n):
env.process(tier(i))
env.run(until=SIM_TIME)
customer_var = _variance(customer_orders) or 1.0
peak = max(factory_orders) if factory_orders else 0.0
final = factory_orders[-1] if factory_orders else 0.0
return {
"peak_factory_order": float(peak),
"factory_order_final": float(final),
"bullwhip_ratio": float(_variance(factory_orders) / customer_var),
"max_backlog": float(max_backlog),
"order_peaks": [float(value) for value in peak_orders],
"whipsawed": bool(peak > 3.0 * STEP_DEMAND and abs(final - STEP_DEMAND) > 2.0),
}
def requirements():
return {"external": ["simpy==4.1.1"]}
# bullwhip.yaml — the default. Ignore beer already on the truck.
# A 4 → 8 customer step then whips the factory from ~67 cases down to 0.
account_for_pipeline: false
Retailer orders, cases per week. The first amplification of the 4 → 8 step: a hump, not yet a cliff.
| Samples | 52 @ 0.00–51.00 |
|---|---|
| Values | min 0.00, mean 7.93, median 8.39, max 15.35, σ 4.91 |
Retailer net stock (on-hand minus backlog). Below zero the retailer owes cases it does not have.
| Samples | 52 @ 0.00–51.00 |
|---|---|
| Values | min -17.69, mean 13.30, median 8.00, max 84.02, σ 26.50 |
Factory orders — the glance test. Pipeline-blind, this surges past 60 and stalls at zero. Pipeline-aware, it tops out near 18 and settles at 8.
| Samples | 52 @ 0.00–51.00 |
|---|---|
| Values | min 0.00, mean 15.11, median 4.00, max 67.03, σ 22.09 |
| Metric | Value |
|---|---|
| peak_factory_order | 67.03 |
| factory_order_final | 0.00 |
| bullwhip_ratio | 351.04 |
| max_backlog | 103.79 |
| order_peaks | 15.350603207945824,33.325004235841334,56.36667316034436,67.02629413094837 |
| whipsawed | true |
# coordinated.yaml — the same chain, counting the pipeline.
# The 4 → 8 step humps and settles near 8 instead of stalling at 0.
account_for_pipeline: true
Retailer orders, cases per week. The first amplification of the 4 → 8 step: a hump, not yet a cliff.
| Samples | 52 @ 0.00–51.00 |
|---|---|
| Values | min 4.00, mean 7.92, median 8.02, max 9.82, σ 1.40 |
Retailer net stock (on-hand minus backlog). Below zero the retailer owes cases it does not have.
| Samples | 52 @ 0.00–51.00 |
|---|---|
| Values | min -15.10, mean 4.91, median 11.73, max 12.00, σ 9.72 |
Factory orders — the glance test. Pipeline-blind, this surges past 60 and stalls at zero. Pipeline-aware, it tops out near 18 and settles at 8.
| Samples | 52 @ 0.00–51.00 |
|---|---|
| Values | min 4.00, mean 8.22, median 7.43, max 18.11, σ 4.02 |
| Metric | Value |
|---|---|
| peak_factory_order | 18.11 |
| factory_order_final | 8.07 |
| bullwhip_ratio | 11.61 |
| max_backlog | 26.78 |
| order_peaks | 9.8193359375,12.27642822265625,15.629786014556885,18.114974707365036 |
| whipsawed | false |
- What sets the tipping point between a calm chain and a whipsaw?
- Whether the order rule counts the whole supply line. Mail plus truck is 4 weeks downstream and the brew is 2, while each tier tries to close its stock gap in 4 weeks, so ignoring beer already on the way double-orders the shortage: the factory peaks near 67 and then orders nothing. Counting the whole pipeline — the only difference in Pipeline-Aware — peaks near 18 and finishes near 8. A continuous weight does not give a ±5–10% cliff: raising it from 0 toward 1 shrinks the wave smoothly (a weight of 0.5 still peaks near 28), so the shipped knob stays the boolean.
- Why do the swings get bigger the further up the chain you go?
- Two amplifiers stack at every tier. Each tier forecasts from the orders it receives, which are already its neighbour's reaction, so the signal gets louder at each hop. And with the supply line ignored, a tier re-orders a stock gap that inbound trucks will already fill. Across four tiers a customer step of 4 cases (4 → 8) becomes a factory order of about 67, then a stall at 0. The chain manufactured the bullwhip internally. Order peaks climb retailer → wholesaler → distributor → factory.
- Why does counting the pipeline calm the chain down?
- It stops the double-ordering. The rule becomes order = forecast + stock gap + pipeline gap, an order-up-to policy. A demand step triggers one catch-up instead of the same catch-up every week until the first truck arrives. Once the supply line is full the correction backs off, and factory orders settle near the new demand of 8 instead of sitting at 0. The bullwhip ratio — variance of factory orders divided by variance of customer demand — falls from about 350 to about 12.
- Why does the pipeline-aware chain still overshoot once?
- Counting the pipeline removes the double-order. It does not let every tier see true customer demand. Each tier still forecasts off its neighbour's orders, which are jumpier than the customer's, and the stock correction is not infinitely gentle, so factory orders hump to about 18 before damping. Sharing the customer's actual orders, or shortening the delays, is what flattens that last hump. Local pipeline-awareness tames most of the bullwhip, not all of it.
- What is the real-world analog, and how is this different from Inventory Oscillation?
- This is the MIT Beer Distribution Game: the same four roles, played by smart people, reliably produce this boom and bust, because the structure drives the behaviour. Inventory Oscillation (/examples/inventory-oscillation) is the one-store version of the same mistake — reorder against the shelf, ignore the truck. The Beer Game stacks four of those decisions so you can watch the swing grow. The same pattern shows up in semiconductor and auto supply chains, in the 2020 run on household goods, in hiring against a backlog, and in autoscaling servers against queue depth.