To compare two policies, evaluate both on the same simulated operating conditions and average their within-replication outcome differences.
Paired policy simulations with common random numbers
Define the contrast before sampling
A warehouse manager wants the expected reduction in unfinished orders from adding three flex workers to each shift. On a sampled shift, draw disruption, order count and base staffing once. Run the baseline capacity and flex capacity against that same shift. The difference in unfinished orders is the contribution from one replication. It does not require pretending the two policies were observed on different random days. The action unit is the shift.
Why pairing helps
A high-demand day makes both baseline and flex outcomes worse. Comparing them under identical demand cancels much of that irrelevant day-to-day variation from the policy difference. With positive covariance between policy outcomes, the variance of the within-shift difference can be lower than the variance of a difference of independently estimated means. This is a potential gain, not a universal promise; a policy with a very different mechanism can change the covariance pattern.
Keep the random draws aligned
Sample all exogenous inputs before applying either policy. A policy may change processing speed, but it should not silently cause the weather draw or incoming-order draw to move to another place in the random stream. Passing an immutable shift scenario into both policy functions makes the pairing visible. Record the seed and scenario generator version. Reproducible draws are necessary for debugging, not proof that the model represents reality.
Estimate the difference and its error
Average the per-shift shortfall reductions. The sample standard deviation of those reductions divided by the square root of the replication count is the Monte Carlo standard error of the paired mean, assuming independent simulated shifts. Do not compute this standard error from the two policy outcomes as though they were unrelated samples. A business decision should compare the reduction with incremental staffing cost and a range of plausible demand inputs.
Inspect edge cases
Flex capacity cannot increase unfinished orders if it only adds productive staff under the same shift scenario. The code asserts this monotonicity. If the output violates it after a code change, inspect the policy logic or state mutation before reporting results. Other policies may trade one loss for another, so a nonnegative difference is not always a valid invariant. The project adds cost units and a release decision.
Implementation
from math import sqrt
from random import Random
from statistics import stdev
def paired_shortfall_reduction(replications, seed):
if replications < 2:
raise ValueError("at least two shifts required")
draw_stream = Random(seed)
reductions = []
for _ in range(replications):
disruption = draw_stream.random() < 0.27
routine_orders = draw_stream.randint(34, 47)
shock_orders = draw_stream.randint(18, 26) if disruption else 0
orders = routine_orders + shock_orders
base_staff = 8 - (2 if disruption else 0)
base_shortfall = max(0, orders - 7 * base_staff)
flex_shortfall = max(0, orders - 7 * (base_staff + 3))
reductions.append(base_shortfall - flex_shortfall)
if any(reduction < 0 for reduction in reductions):
raise AssertionError("flex staff increased shortfall")
return sum(reductions) / replications, stdev(reductions) / sqrt(replications)
saved_orders, mc_error = paired_shortfall_reduction(24000, 907)
assert saved_orders > 0
assert mc_error > 0Performance and operating cost
For R shifts, the direct implementation is O(R) time and O(R) space because it retains differences for a sample standard deviation. A streaming variance algorithm can reduce space to O(1). Pairing can reduce the replication budget when policy outcomes are positively correlated.
Common Mistakes
- Do not simulate independent demand for policies meant to face the same shift.
- Do not calculate the difference error as if paired outcomes were independent.
- Do not assume common random numbers always reduce variance.
