Lawfare Group → Knowledge Institute → Lawfare Academy
Reproduce the worked examples
Copy the complete code below into a UTF-8 file named verify.py and run python verify.py with Python 3. It uses only the standard library and performs exact or rational synthetic calculations. It does not access a network or learner records. Assertions fail if the expected synthetic results change.
These checks verify the stated arithmetic and query behavior, not independent academic acceptance or production migration safety.
"""Original synthetic analytics laboratory. Python 3 standard library only.
Distinct files have distinct units and assumptions. No production/learner access.
Exact rational checks precede an illustrative fixed-seed Monte Carlo run.
"""
from fractions import Fraction as F
from math import sqrt,isclose
from random import Random
import json
checks={}
def check(name,a,b):
assert a==b,(name,a,b)
checks[name]=str(a)
def close(name,a,b):
assert isclose(a,b,abs_tol=1e-9),(name,a,b)
checks[name]=a
def plans(hours,c=150):
return [(80*x+c*y,x,y,2*x+3*y) for x in range(5) for y in range(4) if 2*x+3*y<=hours]
check('bounded candidate pairs',5*4,20)
check('infeasible candidate score',80*4+150*3,770)
check('infeasible candidate hours',2*4+3*3,17)
check('integer optimum12',max(plans(12)),(540,3,2,12))
assert sum(p[0]==540 for p in plans(12))==1
check('continuous relaxation score',80*F(3,2)+150*3,570)
check('rounded-down score',80+150*3,530)
check('rounded-up hours',2*2+3*3,13)
check('integer relaxation gap',570-540,30)
check('integer optimum13',max(plans(13)),(610,2,3,13))
check('integer optimum14',max(plans(14)),(620,4,2,14))
check('one-hour benefit',610-540,70)
check('two-hour benefit',620-540,80)
check('second-hour benefit',620-610,10)
check('two-hour net at fee90',620-90,530)
check('one-hour net at fee50',610-50,560)
check('complex contribution switching threshold',F(240-80,3-2),160)
check('changed complex170 optimum',max(plans(12,170)),(590,1,3,11))
ties=[p[1:3] for p in plans(12,160) if p[0]==max(q[0] for q in plans(12,160))]
assert set(ties)=={(3,2),(1,3)};checks['threshold tied schedules']=ties
# Historical composition is descriptive, not automatically causal.
check('historical reminded aggregate',F(36+1,90+10),F(37,100))
check('historical comparison aggregate',F(5+18,10+90),F(23,100))
check('standardized reminded',F(1,2)*(F(36,90)+F(1,10)),F(1,4))
check('standardized comparison',F(1,2)*(F(5,10)+F(18,90)),F(7,20))
check('standardized difference',F(1,4)-F(7,20),F(-1,10))
p1=F(40,200);p0=F(60,200);difference=p1-p0
check('trial difference',difference,F(-1,10))
se=sqrt(float(p1*(1-p1)/200+p0*(1-p0)/200))
close('independent trial standard error',se,sqrt(.00185))
checks['approximate 95 percent difference interval']=[float(difference)-1.96*se,float(difference)+1.96*se]
check('point net benefit per account',-difference*60-1,5)
check('point net benefit200accounts',200*(-difference*60-1),1000)
check('changed cost8 net per account',-difference*60-8,-2)
actual=[12,18,9,21];baseline=[10,17,11,20];candidate=[12,23,8,17]
def metrics(pred):
e=[p-a for p,a in zip(pred,actual)]
return e,F(sum(e),len(e)),F(sum(abs(x) for x in e),len(e)),F(sum(x*x for x in e),len(e))
def loss(pred):return sum(100*max(a-p,0)+20*max(p-a,0) for p,a in zip(pred,actual))
be,bias,mae,mse=metrics(baseline)
assert be==[-2,-1,2,-1]
check('baseline bias',bias,F(-1,2));check('baseline MAE',mae,F(3,2));check('baseline MSE',mse,F(5,2))
ce,bias2,mae2,mse2=metrics(candidate)
assert ce==[0,5,-1,-4]
check('candidate bias',bias2,0);check('candidate MAE',mae2,F(5,2));check('candidate MSE',mse2,F(21,2))
check('baseline asymmetric loss',loss(baseline),440)
check('candidate asymmetric loss',loss(candidate),600)
a=[10,18,9,21];b=[13,19,10,22]
check('alternative A MAE',metrics(a)[2],F(1,2));check('alternative B MAE',metrics(b)[2],1)
check('alternative A loss',loss(a),200);check('alternative B loss',loss(b),80)
assert metrics(a)[2]<metrics(b)[2] and loss(a)>loss(b)
# Joint state: high demand, supplier delay, probability.
states=[(True,True,F(1,4)),(True,False,F(3,20)),(False,True,F(1,20)),(False,False,F(11,20))]
assert sum(p for h,d,p in states)==1
check('High marginal',sum(p for h,d,p in states if h),F(2,5))
check('Delay marginal',sum(p for h,d,p in states if d),F(3,10))
independent=[(h,d,(F(2,5) if h else F(3,5))*(F(3,10) if d else F(7,10))) for h,d,p in states]
def shortage(h,d,backup=False):return max((14 if h else 8)-(8 if d else 12)-(4 if backup else 0),0)
def mean_short(table,backup=False):return sum(p*shortage(h,d,backup) for h,d,p in table)
def probability(table,predicate):return sum(p for h,d,p in table if predicate(shortage(h,d)))
check('dependent expected shortage',mean_short(states),F(9,5))
check('independent expected shortage',mean_short(independent),F(32,25))
check('dependent any-shortage probability',probability(states,lambda n:n>0),F(2,5))
check('independent any-shortage probability',probability(independent,lambda n:n>0),F(2,5))
check('dependent six-unit tail',probability(states,lambda n:n>=6),F(1,4))
check('independent six-unit tail',probability(independent,lambda n:n>=6),F(3,25))
check('dependent no-backup loss',100*mean_short(states),180)
check('independent no-backup loss',100*mean_short(independent),128)
check('dependent backup shortage',mean_short(states,True),F(1,2))
check('independent backup shortage',mean_short(independent,True),F(6,25))
check('dependent backup total cost',80+100*mean_short(states,True),130)
check('independent backup total cost',80+100*mean_short(independent,True),104)
check('dependent backup improvement',180-130,50)
check('independent backup improvement',128-104,24)
# Reproducible illustrative draws; exact enumeration above is the reference.
rng=Random(2026);trials=20000;outcomes=[];backup_costs=[]
for _ in range(trials):
u=rng.random();cumulative=0
for h,d,p in states:
cumulative+=float(p)
if u<cumulative:
outcomes.append(shortage(h,d));backup_costs.append(80+100*shortage(h,d,True));break
assert len(outcomes)==trials
sim_mean=sum(outcomes)/trials
sim_tail=sum(n>=6 for n in outcomes)/trials
sim_backup=sum(backup_costs)/trials
assert abs(sim_mean-1.8)<.08 and abs(sim_tail-.25)<.015 and abs(sim_backup-130)<3
simulation=dict(seed=2026,trials=trials,meanShortage=sim_mean,sixUnitTailFrequency=sim_tail,meanBackupTotalCost=sim_backup,exactExpectedShortage=1.8,exactTailProbability=.25,exactBackupTotalCost=130,scope='Simulation checks numerical sampling of stipulated states, not empirical probability validity.')
print(json.dumps(dict(passed=True,checks=checks,simulation=simulation,scope='Original synthetic cases; independent-account interval is approximate; economic costs and distributions are stipulated, not actual legal or market facts.'),indent=2))
Related chapters
- Translate an operating decision into a model that can be checked
- Separate a useful predictor from an effective intervention
- Validate forecasts at the time and cost of the real decision
- Choose the best feasible schedule and price additional capacity
- Simulate dependent risks and compare a complete response policy
- Communicate a conditional decision and monitor what can break it
- Analytics clinic: defend the decision from data contract to reassessment