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 sqrt
import json,itertools
checks=[]
def eq(name,actual,expected):
 assert actual==expected,(name,actual,expected)
 checks.append(dict(name=name,value=str(actual)))
def near(name,actual,expected,tol=1e-7):
 assert abs(actual-expected)<tol,(name,actual,expected)
 checks.append(dict(name=name,value=actual))
eq('feature loss',200*(40-35-10)-3000,-4000)
eq('redesign tie',200*(40-20-5)-3000,0)
eq('capacity opportunity cost',180-3*80,-60)
eq('partial scarce-hours comparison',250-2*60,130)
def allocate(a_value,b_value,a_time,b_time,hours,a_cap,b_cap):
 rows=[(a*a_value+b*b_value,a,b) for a,b in itertools.product(range(a_cap+1),range(b_cap+1)) if a*a_time+b*b_time<=hours]
 return max(rows)
eq('segment integer optimum',allocate(120,150,2,3,20,6,4),(1080,4,4))
eq('segment continuous bound',6*120+F(8,3)*150,1120)
eq('changed cap optimum',allocate(120,150,2,3,20,3,4),(960,3,4))
eq('mixed budget net',2000+1200-2000,1200)
eq('allA budget net',2000+500-2000,500)
eq('switching release conditional gain',10*100-5*(150-100),750)
eq('observed test net',20*60-1500,-300)
eq('cheaper test net',20*60-900,300)
eq('test threshold',F(1500,60),25)
se=sqrt(.08*.92/1000+.10*.90/1000)
near('independent binomial standard error',se,sqrt(.0001636))
near('rough interval lower',.02-1.96*se,-.0050696055022828,1e-5)
near('rough interval upper',.02+1.96*se,.0450696055022828,1e-5)
eq('perfect information value',F(2,5)*12000,4800)
eq('launch expected value tie',F(2,5)*12000-F(3,5)*8000,0)
eq('brand20switchers',80*40+40*70-1000-4000,1000)
eq('brand35switchers',65*40+40*70-1000-4000,400)
eq('brand40switchers',1800-40*40,200)
eq('changed support32switchers',1300-40*32,20)
eq('changed support33switchers',1300-40*33,-20)
eq('newtier30switchers',50*80-30*50-1800,700)
eq('newtier45switchers',50*80-45*50-1800,-50)
def pricing(p,capacity=60,fee=0,fixed=0):return (p*(1-fee)-12)*min(180-3*p,capacity)-fixed
eq('unconstrained quantity',180-3*36,72)
eq('unconstrained value',pricing(36,80),1728)
eq('capacity boundary value',pricing(40),1680)
eq('finite quarter-price max',max((pricing(F(i,4)),F(i,4)) for i in range(241)),(1680,F(40)))
eq('expensive capacity',pricing(36,80,fixed=100),1628)
eq('cheaper capacity',pricing(36,80,fixed=30),1698)
eq('20pct gross fee',pricing(40,fee=F(1,5)),1200)
eq('25pct gross fee',pricing(40,fee=F(1,4)),1080)
eq('flat fee',pricing(40,fixed=500),1180)
eq('original synthetic channel',54*100-3500-3000,-1100)
eq('retained companion unconstrained price',60-F(1,2)*50,35)
eq('retained companion unconstrained value',(35-10)*50,1250)
eq('retained companion constrained value',(40-10)*40,1200)
eq('retained companion capacity cost',(35-10)*50-100,1150)
eq('retained companion changed segment cap',allocate(150,100,3,1,20,10,5),(1250,5,5))
eq('refundcost channel',54*100-3500-6*40-3000,-1340)
eq('quality channel',60*100-3500-300-3000,-800)
eq('new channel capacity80',72*50-2000-2500,-900)
eq('new channel capacity100',90*50-2000-700-2500,-700)
eq('hold closing cash',1000+20000*F(7,10)-14000,1000)
eq('hold funding floor',3000-1000,2000)
base=allocate(150,100,3,1,20,10,10);campaign=allocate(150,100,3,1,20,10,14)
eq('cumulative baseline',base,(1450,3,10));eq('cumulative campaign',campaign,(1700,2,14))
eq('campaign400net',campaign[0]-400,1300)
eq('campaign200net',campaign[0]-200,1500)
eq('campaign tie threshold',campaign[0]-base[0],250)
def cash(a,b,cost,collection=F(1,2),hold=0):
 revenue=a*300+b*250;variable=(a+b)*150;profit=revenue-variable-cost
 receipts=revenue*collection;held=receipts*hold;usable=receipts-held
 closing=300+usable-variable-cost;funding=max(0,500-closing)
 assert closing-300==profit-(revenue-receipts)-held
 return profit,closing,funding
eq('baseline cash',cash(3,10,0),(1450,50,450))
eq('original campaign cash',cash(2,14,400),(1300,-450,950))
eq('lower campaign cash',cash(2,14,200),(1500,-250,750))
eq('earlier collection',cash(2,14,200,F(7,10)),(1500,570,0))
eq('held collection',cash(2,14,200,F(7,10),F(1,10)),(1500,283,217))
eq('changed nonrefundable fee',1500-287,1213)
eq('rubric total',4+6+6+4+6+4,30)
print(json.dumps({'passed':True,'checks':checks,'scope':'Synthetic arithmetic, finite allocation and consistency checks only; not market response, legal approval or observed learner testing'},indent=2))

Related chapters