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 fictional strategic laboratory. No network, accounts or learner data.
Enumerates exact finite policies and state-specific cash gates. Does not verify
real demand, signal accuracy, qualified capacity, legal authority or competence.
"""
from fractions import Fraction as F
from itertools import product
import json
out={}
def eq(n,v,t,tol=0.000002):
 assert abs(float(v)-t)<tol,(n,float(v),t)
 out[n]=float(v)
assert F(min(80,50)+min(20,50),2)*40==1400
assert min(F(80+20,2),50)*40==2000
assert F(3,4)*10000+F(1,4)*-4000==6500
assert F(3,4)*6000+F(1,4)*2000==5000
assert F(1,2)*(10000-4000)==3000
assert F(1,2)*(6000+2000)==4000
assert 15*80-10*60==600 and 600-800==-200
assert 15*80-5*60-800==100
def mixes(setup=0):
 return sorted([(60*b+100*p-(setup if p else 0),b,p) for b in range(31) for p in range(21) if b+2*p<=40],reverse=True)
assert mixes()[0]==(2300,30,5)
assert mixes(600)[0]==(1800,30,0)
assert mixes(300)[0]==(2000,30,5)
def value(p,a=160,c=40,k=50):return (p-c)*min(max(0,a-p),k)
assert value(100)==3000 and value(110)==3500
assert max((value(F(p,10)),F(p,10)) for p in range(400,1601))==(3500,F(110))
assert value(100,k=80)==3600 and value(110,k=80)==3500
assert value(90,a=140)==2500
assert max((value(F(p,10)),F(p,10)) for p in range(1200,1601))==(3200,F(120))
assert value(110,c=60,k=80)==2500
# Information: stipulated equal prior and symmetric signal, exact Bayes updates.
prior=F(1,2);accuracy=F(4,5)
prob_high=prior*accuracy+(1-prior)*(1-accuracy)
posterior_high=prior*accuracy/prob_high
posterior_low=prior*(1-accuracy)/(1-prob_high)
def payA(p):return p*12+(1-p)*-4
def payB(p):return p*6+(1-p)*4
assert max(payA(prior),payB(prior))==5
assert prior*12+(1-prior)*4==8
signal=prob_high*max(payA(posterior_high),payB(posterior_high))+(1-prob_high)*max(payA(posterior_low),payB(posterior_low))
eq('signal_value_before_cost',signal,F(33,5))
eq('imperfect_information_value',signal-5,1.6)
eq('test_cost_one_net',signal-1,5.6)
assert payA(F(3,4))==8 and payB(F(3,4))==F(11,2)
assert max(0,8)==8 and max(6,0)==6
# Retained 50/60-job operating cases and ordered reserve/repayment.
assert 50*(300-180)-4000==2000
assert 50*300*F(1,2)-50*180-4000==-5500
assert 60*(300-180)-4000==3200
assert 60*300*F(1,2)-60*180-4000==-5800
assert 8000+7000-9000-4000==2000
assert 2000+7500-7300==2200
assert 2200-750==1450
assert 10800+4000-8000+2000==8800
assert 8000-5800-300==1900
assert 8800-1000==7800
def state(price,promote,capacity,high,limit=9000):
 demand=({400:(50,35),380:(60,45)}[price])[0 if high else 1]+((5 if high else 2) if promote else 0)
 jobs=min(demand,capacity);fixed=2000+600*promote+(700 if capacity==65 else 0)
 pre=220*jobs+fixed;draw=max(0,pre-6000);fee=300 if draw else 0
 revenue=price*jobs;profit=revenue-pre;collection=F(revenue,2)
 earliest=8000+draw-pre;after=earliest+collection;closing=after-draw-fee
 return dict(demand=demand,jobs=jobs,lost=demand-jobs,profit=profit,draw=draw,earliestCash=earliest,closingCash=closing,fee=fee,afterFee=profit-fee,feasible=draw<=limit and earliest>=2000 and closing>=2000)
def policies(p=F(1,2),limit=9000):
 rows=[]
 for price,promote,capacity in product((400,380),(0,1),(55,65)):
  h=state(price,promote,capacity,True,limit);l=state(price,promote,capacity,False,limit)
  rows.append(dict(price=price,promotion=bool(promote),capacity=capacity,high=h,low=l,expectedProfit=p*h['profit']+(1-p)*l['profit'],expectedAfterFee=p*h['afterFee']+(1-p)*l['afterFee'],admissible=h['feasible'] and l['feasible']))
 return rows
def winner(p=F(1,2),limit=9000):return max((x for x in policies(p,limit) if x['admissible']),key=lambda x:x['expectedAfterFee'])
base=winner();assert (base['price'],base['promotion'],base['capacity'])==(380,False,55)
assert base['expectedProfit']==6000 and base['expectedAfterFee']==5700
assert base['high']['draw']==8100 and base['high']['closingCash']==4050
assert base['low']['draw']==5900 and base['low']['closingCash']==4350
assert not state(380,0,65,True)['feasible'] and state(380,0,65,True)['draw']==9900
assert state(400,1,55,True)['draw']==8700
assert state(400,1,55,False)['draw']==4740
highbelief=winner(F(4,5));assert (highbelief['price'],highbelief['promotion'],highbelief['capacity'])==(400,True,55)
assert highbelief['expectedProfit']==6652
eq('belief_crossing',F(57,82),0.69512195122)
lowlimit=winner(limit=8000);assert (lowlimit['price'],lowlimit['promotion'],lowlimit['capacity'])==(400,False,55)
assert lowlimit['expectedProfit']==5650 and lowlimit['expectedAfterFee']==5350
assert winner(limit=10000)['expectedProfit']==6000
# Reconstruct all other numerical retained native/companion cases.
assert 100*80==8000
assert min(60,50)*40==2000 and min(40,50)*40==1600
assert (60-20)*50==2000 and (70-20)*50==2500 and (80-20)*40==2400
assert (60-20)*40==1600 and (70-20)*40==2000
assert (26-20)*50-400==-100 and (26-20)*50-200==100
assert 40*200-2000==6000 and 30*250-2500==5000
assert -2000+1500-500==-1000 and 5000-5*300==3500
assert 50*(400-220)==9000
assert 55*(400-220)-600==9300
assert 55*(380-220)==8800 and 55*(380-220)-600==8200
assert 52*(400-220)-600==8760
assert 3*180<=600<4*180
assert F(3,10)*80+F(7,10)*20==38
assert F(3,10)*60+F(7,10)*40==46
assert F(8,10)*80+F(2,10)*20==68
assert F(8,10)*60+F(2,10)*40==56
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='Original synthetic state/price/signal/cash checks, not actual market evidence or academic acceptance',results=out,allEightPolicies=policies(),initialWinner=base,changedBeliefWinner=highbelief,changedFundingWinner=lowlimit)),indent=2))

Related chapters