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.
from fractions import Fraction as F
from math import isclose
import json
checks=[]
def check(name,condition):
assert condition,name
checks.append(name)
def production(q):
sold=800;price=100;variable=40;fixed=30000;period=10000;selling=5
unit=F(variable)+F(fixed,q);inventory=(q-sold)*unit
absorption=sold*price-sold*unit-sold*selling-period
variableProfit=sold*(price-variable-selling)-fixed-period
cash=sold*price-q*variable-fixed-period-sold*selling
return dict(produced=q,sold=sold,unitCost=unit,inventory=inventory,absorptionProfit=absorption,variableProfit=variableProfit,cashChange=cash)
p1=production(1000);p2=production(1200)
check('base absorption/variable/cash',p1['absorptionProfit']==10000 and p1['variableProfit']==4000 and p1['inventory']==14000 and p1['cashChange']==-4000)
check('base reconciliation',p1['absorptionProfit']-p1['inventory']==p1['variableProfit']-200*40==p1['cashChange'])
check('overproduction stated actual-output model',p2['absorptionProfit']==14000 and p2['variableProfit']==4000 and p2['inventory']==26000 and p2['cashChange']==-12000)
check('overproduction cash reconciliation',p2['absorptionProfit']-p2['inventory']==p2['cashChange'] and p2['cashChange']-p1['cashChange']==-8000)
check('overproduction income versus inventory',p2['absorptionProfit']-p1['absorptionProfit']==4000 and p2['inventory']-p1['inventory']==12000)
check('activity practical rates',36000/1800==20 and 24000/120==200)
check('traditional allocation',60000/1500==40 and 1000*40==40000 and 500*40==20000)
check('practical allocation and unused capacity',1000*20+20*200==24000 and 500*20+80*200==26000 and 300*20+20*200==10000 and 24000+26000+10000==60000)
check('setup reduction allocation not automatic cash saving',500*20+60*200==22000 and 24000+22000+14000==60000)
def plans(setup=0):
return [dict(a=a,b=b,hours=3*a+2*b,value=17*a+11*b-(setup if b else 0)) for a in range(4) for b in range(6) if 3*a+2*b<=10]
optima={}
for setup in [0,5,6]:
ps=plans(setup);best=max(p['value'] for p in ps);optima[str(setup)]=[p for p in ps if p['value']==best]
check('integer optimalmix',optima['0']==[dict(a=2,b=2,hours=10,value=56)])
check('greedy ratio counterexample',F(17,3)>F(11,2) and 3*17==51<56)
check('setup5 tie',{(p['a'],p['b'],p['value']) for p in optima['5']}=={(2,2,51),(3,0,51)})
check('setup6 changes optimum',optima['6']==[dict(a=3,b=0,hours=9,value=51)])
multi=[(20*a+18*b,a,b) for a in range(7) for b in range(7) if 2*a+3*b<=12 and 3*a+b<=12]
check('two-resource optimum',max(multi)==(96,3,2))
check('idle relevant make cost',30-8==22 and 25-22==3)
ceiling=F(22)+6-F(2,100)*80-F(1000,1000)
check('supplier price ceiling with quality and transition',ceiling==F(127,5))
check('supplier base advantage',1000*(28-25-F(2,100)*80)-1000==400)
check('supplier defect stress reverses',1000*(28-25-F(4,100)*80)-1000==-1200)
check('supplier infeasible opportunity removed',1000*(22-25-F(2,100)*80)-1000==-5600)
mpv=2100*(F(24,5)-5);muv=5*(2100-2000);lrv=520*(21-20);lev=20*(520-500)
check('material price/usage bridge',mpv==-420 and muv==500 and mpv+muv==10080-10000==80)
check('labor rate/efficiency bridge',lrv==520 and lev==400 and lrv+lev==10920-10000==920)
check('fixed and flexible cost',8300-8000==300 and 10080+10920+8300==29300)
check('plan-volume-spending bridge',26000+2000+80+920+300==29300 and 29300-28000==1300)
check('good-output changed denominator',990*2==1980 and 990*F(1,2)==495 and 29300-(990*20+8000)==1500)
check('good-output material/labor usage',5*(2100-1980)==600 and 20*(520-495)==500)
check('good-output resource decomposition',-420+600+520+500+300==1500)
factor=sum(1/1.1**t for t in range(1,6));npv=-100000+35000*factor
check('project cash and accounting profit',35000-100000/5==15000 and isclose(npv,32677.53692929563,abs_tol=.01))
check('ROI dilution and residual increment',100000/500000==.2 and isclose(115000/600000,23/120) and (115000-.1*600000)-(100000-.1*500000)==5000)
check('one-year actual economic loss',15000-100000==-85000 and -100000+15000/1.1<0)
check('idle transfer interval',20<28<32 and 28-20==8 and 32-28==4 and 32-20==12)
check('full-capacity transfer no interval',20+15==35>32 and 35-32==3)
# Preserved companion numerical records, independently reconstructed.
check('retained closure base',-35000+8000+18000-3000==-12000)
check('retained closure changed alternative',-35000+8000+35000-3000==5000)
check('retained equal unit versus setup driver',60000/600==100 and 100*100==10000 and 500*100==50000)
check('retained ABC hour/setup allocation',30000/1500*1000+30000/30*10==30000 and 30000/1500*500+30000/30*20==30000)
check('retained fixed setup pool after reduction',30000/20*10==15000)
check('retained constrained special order',18000-10000-1000-40*250==-3000 and 18000-10000-1000-30*250==-500)
check('retained regular/special choices',10*200==2000 and 7*200+2*180==1760 and 6*200+2*180==1560)
check('retained profit variance bridge',100*(20-10)-500==500 and 120*(20-10)-500==700 and 500+200-120-120-50==410)
check('retained flexible cost bridge',5000+600+240-100==5740 and 120*30+2000==5600)
check('retained deferred maintenance consequence',300-100==200)
check('retained ROI/RI',200000/800000==.25 and 230000/1000000==.23 and 230000-100000==130000)
check('retained changed project RI',215000/1000000==.215 and 215000-100000==115000)
check('retained makebuy threshold',10000*6+8000==68000 and 10000*7-68000==2000)
check('retained outsourcing tie and ceiling',-42000+38000+6000-2000==0 and (38000+6000-2000)/1000==42)
check('retained infeasible opportunity removed',-42000+38000-2000==-6000)
check('retained cumulative product closure',-30000+5000+12000==-13000 and -30000+5000+30000==5000 and 30000-5000==25000)
if __name__=='__main__':print(json.dumps(dict(passed=True,checks=checks,calculated=dict(productionBase=p1,productionHigher=p2,integerOptima=optima,twoResourceOptimum=max(multi),supplierPriceCeiling=ceiling,materialPriceVariance=mpv,materialUsageVariance=muv,laborRateVariance=lrv,laborEfficiencyVariance=lev,projectNPV=npv)),default=str,indent=2))
Related chapters
- Reconcile the cost measure to the question being asked
- Show resource consumption and unused capacity separately
- Compare actual alternatives, including displaced contribution
- Reconcile the variance before explaining or rewarding it
- Align local measures with the enterprise decision
- Recommend a feasible operating response and test its reversal
- Cumulative clinic: a reconciled cost, decision and control dossier