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.
"""Exact synthetic economic-model laboratory. No empirical or learner data."""
from fractions import Fraction as F
import json
checks={}
def check(n,a,b):
assert a==b,(n,a,b)
checks[n]=str(a)
def revenue(q):return 100*q-q*q
def profit(q,setup=200):return 80*q-q*q-setup
check('revenue20',revenue(20),1600)
check('revenue80',revenue(80),1600)
check('next unit revenue',revenue(21)-revenue(20),59)
check('two unit revenue',revenue(22)-revenue(20),116)
check('ten unit revenue',revenue(30)-revenue(20),500)
for h in [F(1),F(1,10),F(1,100),F(1,1000)]:
check('difference quotient h='+str(h),(revenue(20+h)-revenue(20))/h,60-h)
check('elasticity at60',-F(60,40),F(-3,2))
check('base stationary quantity',F(80,2),40)
check('base global maximum',profit(40),1400)
check('capacity maximum',max((profit(q),q) for q in range(31)),(1300,30))
check('opening setup1500',profit(40,1500),100)
check('opening setup1700',profit(40,1700),-100)
def benefit(x,y):return 12*x-x*x+8*y-y*y
check('allocation benefit',benefit(4,2),44)
check('left allocation corner',benefit(0,6),12)
check('right allocation corner',benefit(6,0),36)
check('marginal x',12-2*4,4)
check('marginal y',8-2*2,4)
check('expanded allocation benefit',benefit(F(9,2),F(5,2)),F(95,2))
check('finite extra hour benefit',F(95,2)-44,F(7,2))
check('slack resource optimum benefit',benefit(6,4),52)
assert 6+4<11
def cost(q):return 200+20*q+F(1,5)*q*q
check('cost10',cost(10),420)
check('cost20',cost(20),680)
check('cost accumulated10to20',cost(20)-cost(10),260)
check('consumer surplus60',40*40-F(40*40,2),800)
check('consumer surplus70',30*30-F(30*30,2),450)
def value(n):return 72*n-10*n*n
check('smooth integer vehicle maximum',max((value(n),n) for n in range(9)),(128,4))
check('threshold integer vehicle maximum',max((value(n)-(15 if n>=4 else 0),n) for n in range(9)),(126,3))
check('Newton step',F(10)-(80-2*10)/F(-2),40)
def newprofit(q):return revenue(q)-cost(q)
check('new continuous optimum',F(80)/F(12,5),F(100,3))
check('new capacity30 profit',newprofit(30),1120)
check('new integer33 profit',newprofit(33),F(5666,5))
check('new integer34 profit',newprofit(34),F(5664,5))
check('new integer global maximum',max((newprofit(q),q) for q in range(41)),(F(5666,5),33))
check('new continuous peak',newprofit(F(100,3)),F(3400,3))
check('relaxation gap',newprofit(F(100,3))-newprofit(33),F(2,15))
print(json.dumps(dict(passed=True,checks=checks,limits='Exact calculations validate only the supplied synthetic models. Empirical demand, resource availability, causal and welfare assumptions, legal authority, independent subject review and observed learner testing are not established.'),indent=2))
Related chapters
- Specify the economic model before solving its equation
- Derive marginal change and measure its approximation error
- Prove the relevant maximum rather than stopping at a stationary point
- Solve constrained choice and interpret a multiplier within its limits
- Recover accumulated change without losing the initial condition
- Turn a continuous solution into an auditable feasible decision
- Optimization clinic: defend the model, calculation and feasible recommendation