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 optimization, signal and simulation checks.
Standard library only; no network or learner records. Exact arithmetic verifies
the stipulated model. Seeded trial error is not uncertainty about real inputs.
"""
from fractions import Fraction as F
from itertools import product,combinations
import random,math,statistics,json
results={}
def eq(n,v,t,tol=0.000002):
assert abs(float(v)-t)<tol,(n,float(v),t)
results[n]=float(v)
def slot(x,d):return 30*min(x,d)-10*x
assert (slot(4,10)+slot(4,4))/2==80
assert (slot(8,10)+slot(8,4))/2==100
assert slot(8,4)==40 and slot(4,4)==80
for p,v in [(F(3,10),76),(F(1,2),100),(F(7,10),124)]:assert p*slot(8,10)+(1-p)*slot(8,4)==v
assert max((F(slot(x,10)+slot(x,4),2),x) for x in range(11))==(110,10)
assert F(200+80,2)-110==30
assert F(slot(4,10)+slot(4,2),2)==50
assert F(slot(8,10)+slot(8,2),2)==70
def vertices(H=F(10),cap2=F(10),cx=30,cy=50):
lines=[(F(2),F(1),F(H)),(F(1),F(2),F(cap2)),(F(1),F(0),F(0)),(F(0),F(1),F(0))];out=[]
for (a,b,c),(d,e,f) in combinations(lines,2):
det=a*e-b*d
if not det:continue
x=(c*e-b*f)/det;y=(a*f-c*d)/det
if x>=0 and y>=0 and 2*x+y<=H and x+2*y<=cap2:out.append((cx*x+cy*y,x,y))
return sorted(set(out),reverse=True)
def integer(H=10,setup=0):return max((30*x+50*y-(setup if y else 0),x,y) for x in range(11) for y in range(11) if 2*x+y<=H and x+2*y<=10)
assert vertices()[0]==(F(800,3),F(10,3),F(10,3))
u,v=F(10,3),F(70,3);assert 2*u+v==30 and u+2*v==50 and 10*(u+v)==F(800,3)
assert integer()==(260,2,4)
assert 30*3+50*3==240
assert integer(setup=120)==(150,5,0) and integer(setup=25)==(235,2,4)
assert integer(H=11)==(270,4,3) and vertices(11)[0][0]==270
def value(H):return 50*H if H<=5 else (10*H+700)/3 if H<=20 else F(300)
for H in map(F,range(31)):assert vertices(H)[0][0]==value(H)
eq('finite_capacity_package40',value(F(25))-value(F(10))-40,F(-20,3))
eq('finite_capacity_package25',value(F(25))-value(F(10))-25,F(25,3))
assert vertices(11)[0][0]-vertices()[0][0]==F(10,3)
assert integer(11)[0]-integer()[0]==10
def signal_values(prior,sensitivity,falsepositive,gain=12,loss=8):
positive=prior*sensitivity+(1-prior)*falsepositive
ph=prior*sensitivity/positive
nh=prior*(1-sensitivity)/(1-positive)
launch=lambda p:p*gain-(1-p)*loss
gross=positive*max(0,launch(ph))+(1-positive)*max(0,launch(nh))
return positive,ph,nh,gross
pos,ph,nh,gross=signal_values(F(1,5),F(4,5),F(1,10))
assert pos==F(6,25) and ph==F(2,3) and nh==F(1,19)
assert gross==F(32,25) and gross-F(1,2)==F(39,50)
rare=signal_values(F(1,20),F(4,5),F(1,10));assert rare[1]==F(8,27) and rare[3]==0
assert F(1,20)*F(4,5)+F(19,20)*F(9,10)==F(179,200)
assert signal_values(F(1,5),F(4,5),F(1,5))[3]==F(16,25)
assert F(7,10)*10+F(3,10)*-20==1
assert F(7,10)*8+F(3,10)*-8==F(16,5)
assert 12-20==-8 and 12-20+5==-3
mean=F(7,10)*2+F(3,10)*-12
variance=F(7,10)*4+F(3,10)*144-mean*mean
assert mean==F(-11,5) and variance==F(1029,25)
eq('paired_se10000',math.sqrt(float(variance)/10000),.0641560597)
eq('independent_se10000',math.sqrt(242.76/10000),.155807574)
# Funding-adjusted signal policy: action follows only information then available.
cost={'none':F(0),'A':F(8),'B':F(6)}
pay={'none':(F(0),F(0)),'A':(F(10),F(-6)),'B':(F(6),F(2))}
def conditional(opening,fee,posterior):
feasible=[a for a in cost if opening-fee-cost[a]>=2]
assert feasible
return max(feasible,key=lambda a:posterior*pay[a][0]+(1-posterior)*pay[a][1])
joint=[(True,True,F(2,5)),(True,False,F(1,10)),(False,True,F(1,10)),(False,False,F(2,5))]
def study(opening=F(10),fee=F(1,2)):
rows=[]
for high,positive,prob in joint:
posterior=F(4,5) if positive else F(1,5)
action=conditional(opening,fee,posterior)
initial=opening-fee-cost[action]
net=pay[action][0 if high else 1]-fee
final=opening+net
assert initial>=2 and final>=2
rows.append(dict(high=high,positive=positive,probability=prob,action=action,initialReserve=initial,net=net,closing=final))
return rows,sum(r['probability']*r['net'] for r in rows)
base,basevalue=study();assert basevalue==F(7,2) and all(r['action']=='B' for r in base)
enabled,enabledvalue=study(F(21,2));assert enabledvalue==F(43,10)
assert [r['net'] for r in enabled]==list(map(F,['9.5','5.5','-6.5','1.5']))
assert [r['closing'] for r in enabled]==list(map(F,['20','16','4','12']))
assert study(F(10),F(0))[1]==F(24,5)
assert F(1,2)*max(10,6,0)+F(1,2)*max(-6,2,0)-4==2
# Seeded trials independently sampled from the same explicit joint distribution.
N=20000;rng=random.Random(20261028);sample=[];paired=[]
for _ in range(N):
high=rng.random()<.5;positive=rng.random()<(.8 if high else .2)
posterior=F(4,5) if positive else F(1,5)
action=conditional(F(21,2),F(1,2),posterior)
sample.append(float(pay[action][0 if high else 1]-F(1,2)))
paired.append(float(pay['A'][0 if high else 1]-pay['B'][0 if high else 1]))
simmean=statistics.mean(sample);se=statistics.stdev(sample)/math.sqrt(N)
assert abs(simmean-4.3)<6*se,(simmean,se)
assert abs(statistics.mean(paired)+2)<6*statistics.stdev(paired)/math.sqrt(N)
# Complete retained native and companion checks.
assert max((40*x+30*y,x,y) for x in range(7) for y in range(9) if x+y<=10)==(360,6,4)
assert 3000>2700 and 8000+6000>10000 and 8000+6000==14000
assert 3000+2700==5700
assert 50*4+40*4==360 and 30*4+40*4==280
assert 2*2+6==10 and 40*2+30*6==260
assert 2*3+6==12 and 40*3+30*6==300
assert 300-260-65==-25 and 300-260-35==5
assert 10*20+6*10==260
retained=signal_values(F(1,2),F(3,4),F(1,4),12,8)
assert retained[3]==F(7,2) and retained[3]-2-F(1)==F(1,2)
assert retained[3]-2-F(18,10)==F(-3,10)
retainedclinic=signal_values(F(1,2),F(4,5),F(1,5),10,6)
assert retainedclinic[3]==F(17,5) and retainedclinic[3]-2-1==F(2,5)
assert retainedclinic[3]-2-F(3,2)==F(-1,10)
assert 8000-2000-1000==5000 and 8000-2000==6000
assert 6000-1200-800==4000 and 6000-500-800==4700
assert 6000-500-1200==4300
assert 2*40+20==100 and 40+2*20==80 and 40*40+30*20==2200
assert 2*F(50,3)+F(20,3)==40 and F(50,3)+2*F(20,3)==30
assert 100*F(50,3)+80*F(20,3)==2200
assert 2*80==160 and 80<=80 and 40*80==3200
def plain(v):
if isinstance(v,F):return float(v)
if isinstance(v,dict):return {k:plain(x) for k,x in v.items()}
if isinstance(v,list):return [plain(x) for x in v]
return v
print(json.dumps(plain(dict(passed=True,scope='Synthetic exact and Monte Carlo implementation checks; not real input, causal, learner or academic validation',results=results,fundingConstrainedPolicy=base,enabledPolicy=enabled,exactEnabledValue=enabledvalue,simulation=dict(seed=20261028,trials=N,mean=simmean,standardError=se,exactTarget=4.3,inputUncertaintyValidated=False))),indent=2))
Related chapters
- Choose actions with only the information actually available
- Verify linear optima and distinguish executable integer decisions
- Derive a resource value function before buying a capacity package
- Evaluate a signal using base rates and feasible conditional actions
- Verify simulation against exact states and measure computational error
- Deliver an analytical recommendation that can be checked and revised
- Cumulative clinic: information, feasibility and model validation