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 synthetic probability, scoring and feasible-policy calculations, not independent academic acceptance or production migration safety.

"""Original MBA690 synthetic reasoning/model checks. Standard library, no records or network."""
from fractions import Fraction as F
from math import sqrt,log,isclose,comb
import json
checks=[]
def eq(name,x,y):
 assert isclose(float(x),float(y),rel_tol=1e-10,abs_tol=1e-8),(name,x,y)
 checks.append(dict(case=name,value=float(x),expected=float(y)))
eq('expected monetary gamble',F(1,2)*50+F(1,2)*(-40),5)
eq('expected final wealth',F(1,2)*150+F(1,2)*60,105)
ce=sqrt(150*60);eq('log certainty equivalent',ce,94.86832980505137)
assert .5*log(150)+.5*log(60)<log(100)<log(102)
def product_wealth(f):return (100+50*f)*(100-40*f)
grid=[F(i,1000) for i in range(1001)]
best=max(grid,key=product_wealth);assert best==F(1,4)
eq('fractional certainty equivalent',sqrt(product_wealth(best)),100.62305898749054)
assert product_wealth(best)>100**2 and product_wealth(best)<102**2
assert 40*F(3,4)==30
eq('high prevalence posterior',F(140,220),F(7,11))
eq('low prevalence posterior',F(14,112),F(1,8))
eq('changed false-positive posterior',F(14,14+14),.5)
eq('initial independent estimates',F(75+80+85,3),80)
eq('revised estimates',F(160+170+180,3),170)
eq('estimate shift',170-80,90)
def brier(ps,ys):return sum((p-y)**2 for p,y in zip(ps,ys))/len(ps)
ys=[1]*16+[0]*4+[1]*4+[0]*16
ps=[F(8,10)]*20+[F(2,10)]*20
eq('registered sample Brier',brier(ps,ys),.16)
eq('constant rival Brier',brier([F(1,2)]*40,ys),.25)
eq('sharper sample Brier',brier([F(95,100)]*20+[F(5,100)]*20,ys),.1825)
# Independent expected-score identity, not a claim of observed long-run calibration.
q=F(7,10)
for p in [F(i,10) for i in range(11)]:assert q*(1-p)**2+(1-q)*p**2==(p-q)**2+q*(1-q)
eq('continue future net',22000-18000,4000)
eq('stop future net',5000-3000,2000)
eq('changed stop advantage',8000-4000,4000)
eq('baseline useful incentive outcome',100*50-10*40,4600)
eq('high-callback policy',120*50-30*40-500,4300)
eq('lower-callback policy',120*50-12*40-500,5020)
eq('callback economic threshold',F(6000-500-4600,40),22.5)
assert 6000-22*40-500>4600 and 6000-23*40-500<4600
eq('higher bonus at12callbacks',6000-480-1000,4520)
eq('A recorded retention',F(270,300),.9)
eq('B recorded retention',F(380,400),.95)
eq('B intent-adjusted retention',F(400-20-60,400),.8)
eq('risky project mean',F(1,2)*20+F(1,2)*(-4),8)
assert 4>3 # Actual loss-absorbing constraint excludes the risky project.
eq('review avoided expected loss',F(1,10)*30000,3000)
eq('review incremental before delay',3000-2000,1000)
eq('review incremental after delay',3000-2000-2000,-1000)
eq('review overlap-corrected increment',F(5,100)*30000-2000,-500)
# Derive all no-test and signal-contingent maintenance policies independently.
prior=F(2,100);sensitivity=F(7,10);fp=F(1,10)
states=[(True,True,prior*sensitivity),(True,False,prior*(1-sensitivity)),(False,True,(1-prior)*fp),(False,False,(1-prior)*(1-fp))]
assert sum(p for _,_,p in states)==1
def expected_cost(repair_positive,repair_negative,test_price=50,effectiveness=1):
 result=F(test_price)
 for faulty,positive,p in states:
  repair=repair_positive if positive else repair_negative
  loss=(20000*(1-effectiveness) if repair else 20000) if faulty else 0
  result+=p*((2000 if repair else 0)+loss)
 return result
all_policies={(rp,rn):expected_cost(rp,rn) for rp in [False,True] for rn in [False,True]}
assert min(all_policies,key=all_policies.get)==(True,False)
eq('no-test strongest baseline',min(prior*20000,F(2000))*1000,400000)
eq('optimal tested batch',expected_cost(True,False)*1000,394000)
eq('optimal tested batch gain',(prior*20000-expected_cost(True,False))*1000,6000)
eq('positive conditional avoided loss',F(14,112)*20000,2500)
eq('positive repair gain',F(14,112)*20000-2000,500)
eq('per-item gross signal gain',F(112,1000)*500,56)
eq('higher signal price',expected_cost(True,False,80)*1000,424000)
eq('higher signal loss against baseline',424000-400000,24000)
assert expected_cost(True,False,50,F(1,2))>expected_cost(False,False,50,F(1,2))
# Exact hypergeometric allocation among112positives with14faults, choosing100.
den=comb(112,100);alloc=[]
for x in range(15):
 if 0<=100-x<=98:
  p=F(comb(14,x)*comb(98,100-x),den);alloc.append((x,p))
assert sum(p for _,p in alloc)==1
repaired=sum(x*p for x,p in alloc)
eq('exact capacity-constrained expected repaired faults',repaired,12.5)
eq('constrained expected remaining faults',20-repaired,7.5)
eq('constrained full batch cost',100*2000+(20-repaired)*20000+50000,400000)
eq('qualified twelve-slot option',394000+3000,397000)
eq('costly twelve-slot option',394000+7000,401000)
checks.append(dict(case='exact hypergeometric follow-up capacity verification',passed=True,expectation=12.5,limits='Synthetic exact112positive/14fault batch with equal-risk random selection, not a universal ethical allocation rule.'))
checks.append(dict(case='all four signal-contingent policies independently evaluated',passed=True,policies=[dict(repairPositive=k[0],repairNegative=k[1],perItemCost=v) for k,v in all_policies.items()]))
# Higher prevalence changes the best no-test action: derive that rival rather than fix no-action.
high_prior=F(1,5)
high_states=[(True,True,high_prior*sensitivity),(True,False,high_prior*(1-sensitivity)),(False,True,(1-high_prior)*fp),(False,False,(1-high_prior)*(1-fp))]
high_test=50+sum(p*((2000 if positive else 0)+(20000 if faulty and not positive else 0)) for faulty,positive,p in high_states)
eq('high prevalence no-test best',min(high_prior*20000,F(2000)),2000)
eq('high prevalence test cost',high_test,1690)
eq('high prevalence information improvement',2000-high_test,310)
# Retained native and complete companion calculations, never altered by this script.
eq('retained positive gamble mean',F(7,10)*20000-F(3,10)*30000,5000)
eq('retained adverse funding gap',30000-10000,20000)
eq('retained protected gamble',F(7,10)*20000-F(3,10)*8000-2000,9600)
eq('retained flagged posterior',F(9,99),F(1,11))
eq('retained improved flags',F(9,18),.5)
eq('retained continue',15000-12000,3000)
eq('retained stop superiority',5000-3000,2000)
eq('retained changed continue superiority',3000-1000,2000)
eq('retained A recorded active',100-10,90)
eq('retained B recorded active',120-5,115)
eq('retained cumulative continue',10000-20000,-10000)
eq('retained cumulative stop',-15000,-15000)
eq('retained cumulative continue advantage',-10000-(-15000),5000)
eq('retained cumulative changed stop advantage',-5000-(-10000),5000)
native_ps=[F(8,10),F(8,10),F(2,10),F(2,10)]
eq('retained native Brier',brier(native_ps,[1,0,1,0]),.34)
eq('retained native rival',brier([F(1,2)]*4,[1,0,1,0]),.25)
eq('retained changed native Brier',brier(native_ps,[1,1,0,0]),.04)
def clean(v):
 if isinstance(v,F):return float(v)
 raise TypeError(type(v).__name__)
print(json.dumps(dict(passed=True,checks=checks,limits='Original synthetic reasoning/model checks only; actual probabilities, causal mechanisms, rights, authority, independent subject review and observed learner competence are not established.'),default=clean))

Related chapters