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,isclose
import json
def near(a,b,tol=.015):assert abs(float(a)-float(b))<tol,(a,b)
def npv(rate,flows):return sum(F(v)/(1+F(rate))**t for t,v in enumerate(flows))
def ann(rate,n):return sum((1+F(rate))**-t for t in range(1,n+1))
r=F(12,100);base=npv(r,[-100,35,35,35,45]);shield=F(4,5)*ann(F(8,100),4);apv=base+shield-2
near(base,12.662407916,.000001);near(apv,13.312109388,.000001)
threshold=(100-10/(1+r)**4)/ann(r,4);near(threshold,30.831099268,.000001);assert npv(r,[-100,30,30,30,40])<0
recovery_loss=4/(1+r)**4;near(recovery_loss,2.542072314,.000001)
assert npv(r,[-100,35,35,35,41])==base-recovery_loss
real=F(112,103)-1;assert 103/F(112,100)==100/(1+real);near(real,.08737864078,.00000001)
asset=F(3,2)/(1+F(3,4)*F(1,2));beta=asset*(1+F(3,4));ke=F(3,100)+beta*F(5,100);wacc=ke/2+F(6,100)*F(3,4)/2
assert asset==F(12,11) and beta==F(21,11);near(ke,.12545454545,.00000001);near(wacc,.085227272727,.00000001)
immediate=-60+70/F(11,10)**2
def waiting(cost):return (max(F(0),-cost+120/F(11,10))+max(F(0),-cost+20/F(11,10)))/2/F(11,10)
near(immediate,-2.14876033,.000001);near(waiting(60),22.314049587,.000001);assert waiting(110)==0
delta=F(30,140-80);borrow=-delta*80/F(105,100);call=delta*100+borrow
assert delta*140+borrow*F(105,100)==30 and delta*80+borrow*F(105,100)==0
q=(F(105)-80)/(140-80);assert q==F(5,12) and call==q*30/F(105,100);near(call,11.904761905,.000001)
assert F(7,10)*30/F(105,100)==20 and call!=20
def hedge(quantity,spot):return quantity*spot-1000*(spot-5)
assert [hedge(1000,s) for s in [4,7]]==[5000,5000];assert [hedge(600,s) for s in [4,7]]==[3400,2200]
cash=800;ladder=[]
for flow in [-1000,-4000,6000]:cash+=flow;ladder.append(cash)
assert ladder==[-200,-4200,1800] and -min(ladder)==4200
cash=F(70)+40-90-10-2;project_cash=[cash]
for flow in [F(326,10)]*3+[F(26,10)]:cash+=flow;project_cash.append(cash)
assert project_cash==[8,F(406,10),F(732,10),F(1058,10),F(1084,10)]
assert 10-project_cash[0]==2 and F(26,10)-4==F(-14,10)
# Reconstruct the retained native and complete companion examples independently.
cedar=npv(F(1,10),[-110000,40000,40000,70000]);down=npv(F(1,10),[-110000,25000,25000,45000])
near(cedar,12013.52);near(cedar-6000*ann(F(1,10),3),-2907.59);near(down,-32802.40);near(F(3,5)*cedar+F(2,5)*down,-5912.85)
near(sqrt(.02),.141421356,.00000001);near(sqrt(.01),.1,.00000001)
assert F(4,100)+F(12,10)*F(5,100)==F(10,100)
assert F(6,10)*F(10,100)+F(4,10)*F(6,100)*F(3,4)==F(78,1000)
assert F(6,10)*F(10,100)+F(4,10)*F(6,100)==F(84,1000)
for s,pay,profit in [(120,20,12),(105,5,-3),(90,0,-8),(100,0,-8),(108,8,0)]:assert max(s-100,0)==pay and pay-8==profit
assert 4+100-95==9 and 7+100-95==12
assert 20000+60000+10000-65000-15000==10000 and 15000-10000==5000
assert 20000+40000+10000-65000-15000==-10000 and 15000-(-10000)==25000
ocf=(35000-15000-10000)*F(3,4)+10000;assert ocf==17500
near(npv(F(1,10),[-48000,17500,17500,25500]),1530.43)
near(npv(F(1,10),[-48000,14500,14500,22500]),-5930.13)
near(sqrt(.05),.223606798,.00000001);near(sqrt(.08),.282842712,.00000001)
assert F(4,100)+F(125,100)*F(6,100)==F(115,1000)
near(sqrt(.0325),.180277564,.00000001);near(sqrt(.0475),.217944947,.00000001)
near(npv(F(1,10),[-20000,9000,9000]),-4380.17)
near(npv(F(1,10),[-20000,16500,16500]),8636.36)
near(F(1,2)*max(F(0),-20000+30000/F(11,10))/F(11,10),3305.79)
assert 500000-100000+20000==420000
assert 50000+30000-40000==40000 and 200000+30000-40000==190000
assert 80000+30000+120000-40000==190000 and 30000*F(1,10)==3000
assert 45000-40000==5000
assert [max(s-50,0)-6 for s in [40,55,70]]==[-6,-1,14]
assert [max(s-50,0)-9 for s in [40,55,70]]==[-9,-4,11]
assert 12000+35000-25000-15000==7000 and 12000+5000-25000-15000==-23000
assert -23000+10000==-13000 and 12000+5000-25000+10000==2000
near(npv(F(1,10),[-35000,14000,14000,19000]),3572.50)
near(npv(F(1,10),[-35000,14000,14000,14000]),-184.07)
near(npv(F(1,10),[-35000,10800,10800,15800]),-4385.42)
print(json.dumps(dict(passed=True,unleveredNPV=float(base),annualCashThreshold=float(threshold),shieldPV=float(shield),APV=float(apv),recoveryStressLoss=float(recovery_loss),assetBeta=float(asset),targetEquityBeta=float(beta),targetWACC=float(wacc),immediateScenarioNPV=float(immediate),feasibleWaitingValue=float(waiting(60)),replicatedCallValue=float(call),hedgeDatedCash=ladder,projectCashLadder=[float(x) for x in project_cash],retainedNativeAndCompanionModelsPassed=True,networkAccess=False,learnerAccess=False),indent=2))