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 Managerial Economics laboratory: Python 3 standard library only.
All input demand, costs, payoffs, probabilities and policy quantities are synthetic.
No current market rates, actual legal duties, production systems or learner records.
Each file is distinct; the different market structures are not merged.
"""
from fractions import Fraction as F
import json
checks={}
def check(name,actual,expected):
assert actual==expected,(name,actual,expected)
checks[name]=str(actual)
# Demand, contribution and finite midpoint elasticity.
check('original revenue',50*100,5000)
check('higher-price revenue',55*80,4400)
check('contribution cost20 original',(50-20)*100,3000)
check('contribution cost20 higher price',(55-20)*80,2800)
elasticity=F(80-100,90)/F(55-50,F(105,2))
check('midpoint elasticity',elasticity,F(-7,3))
reverse=F(100-80,90)/F(50-55,F(105,2))
check('reversed midpoint elasticity',reverse,elasticity)
check('contribution cost45 original',(50-45)*100,500)
check('contribution cost45 higher price',(55-45)*80,800)
# Cost functions, discrete increments, special orders and capacity.
def tc(q):return 600+20*q+F(q*q,2)
check('TC20',tc(20),1200)
check('AVC20',(tc(20)-600)/20,30)
check('ATC20',tc(20)/20,60)
check('derivative MC20',20+20,40)
check('discrete increment20to21',tc(21)-tc(20),F(81,2))
order=10*(28-22)-40
check('idle special order',order,20)
check('displaced special order',order-50,-30)
check('dispatch20jobs',20*25-300,200)
check('dispatch10jobs',10*25-300,-50)
check('dispatch break even',F(300,25),12)
check('dispatch changed break even',F(300,20),15)
def price(q):return 100-q
def profit(q):return price(q)*q-20*q-500
check('unconstrained optimal q',max(range(101),key=profit),40)
check('unconstrained price',price(40),60)
check('unconstrained profit',profit(40),1100)
check('constrained optimal q',max(range(31),key=profit),30)
check('constrained price',price(30),70)
check('constrained profit',profit(30),1000)
for q in range(101):assert profit(q)==1100-(q-40)**2
check('expansion benefit',profit(40)-profit(30),100)
check('expansion120 profit',profit(40)-120,980)
check('expansion80 profit',profit(40)-80,1020)
check('committed no-output profit',profit(0),-500)
check('incremental operation vs committed shutdown',profit(30)-profit(0),1500)
# Simultaneous strategic game. The exercise performs no competitor action.
game={('Hold','Hold'):(80,80),('Discount','Hold'):(100,30),('Hold','Discount'):(30,100),('Discount','Discount'):(50,50)}
actions=['Hold','Discount']
equilibria=[(a,b) for a in actions for b in actions if all(game[a,b][0]>=game[x,b][0] for x in actions) and all(game[a,b][1]>=game[a,y][1] for y in actions)]
assert equilibria==[('Discount','Discount')];checks['pure equilibria']=equilibria
for other in actions:
assert game['Discount',other][0]>game['Hold',other][0]
assert game[other,'Discount'][1]>game[other,'Hold'][1]
check('mutual hold joint payoff',sum(game['Hold','Hold']),160)
check('equilibrium joint payoff',sum(game['Discount','Discount']),100)
entry={'Accommodate':(30,40),'Fight':(-20,10)}
response=max(entry,key=lambda k:entry[k][1]);assert response=='Accommodate' and entry[response][0]>0
changed={'Accommodate':(30,40),'Fight':(-20,50)}
response2=max(changed,key=lambda k:changed[k][1]);assert response2=='Fight' and changed[response2][0]<0
checks['original entry outcome']='Enter then Accommodate';checks['changed entry outcome']='Stay out'
# Expected value and Bayesian signal policy.
prior=F(2,5);launch={'High':120,'Low':-60};defer=20
ev=prior*120+(1-prior)*(-60)
check('launch uninformed EV',ev,12)
baseline=max(ev,defer);check('best uninformed value',baseline,20)
perfect=prior*max(120,defer)+(1-prior)*max(-60,defer)
check('perfect-information gross value',perfect,60)
check('EVPI',perfect-baseline,40)
joint={('High','Positive'):prior*F(4,5),('Low','Positive'):(1-prior)*F(1,5),('High','Negative'):prior*F(1,5),('Low','Negative'):(1-prior)*F(4,5)}
assert sum(joint.values())==1
signal={s:sum(joint[k,s] for k in ['High','Low']) for s in ['Positive','Negative']}
check('positive probability',signal['Positive'],F(11,25))
check('positive High posterior',joint['High','Positive']/signal['Positive'],F(8,11))
check('negative High posterior',joint['High','Negative']/signal['Negative'],F(1,7))
conditional={s:sum(joint[k,s]*launch[k] for k in ['High','Low'])/signal[s] for s in signal}
check('positive launch expected profit',conditional['Positive'],F(780,11))
check('negative launch expected profit',conditional['Negative'],F(-240,7))
gross=sum(signal[s]*max(conditional[s],defer) for s in signal)
check('sample-information gross value',gross,F(212,5))
check('EVSI',gross-baseline,F(112,5))
check('net information strategy at cost10',gross-10,F(162,5))
check('net improvement at cost10',gross-10-baseline,F(62,5))
check('net value at cost25',gross-25,F(87,5))
assert gross-25<baseline and gross-baseline<=perfect-baseline
changed_prior=F(1,10);positive=changed_prior*F(4,5)+(1-changed_prior)*F(1,5)
check('changed prior positive probability',positive,F(13,50))
check('changed prior positive High posterior',changed_prior*F(4,5)/positive,F(4,13))
# Separate competitive policy market, not the single-price profit problem.
q0=F(100-20,2);p0=100-q0
tax=20;q1=F(100-20-tax,2);buyer=100-q1;seller=20+q1
check('pretax quantity',q0,40);check('pretax price',p0,60)
check('posttax quantity',q1,30);check('buyer price',buyer,70);check('seller receipt',seller,50)
check('tax wedge',buyer-seller,tax)
cs0=(100-p0)*q0/2;ps0=(p0-20)*q0/2
cs1=(100-buyer)*q1/2;ps1=(seller-20)*q1/2;revenue=tax*q1
check('pretax consumer surplus',cs0,800);check('pretax producer surplus',ps0,800)
check('posttax consumer surplus',cs1,450);check('posttax producer surplus',ps1,450)
check('tax revenue',revenue,600)
check('no-externality deadweight loss',cs0+ps0-cs1-ps1-revenue,100)
def gross_benefit(q):return 100*q-q*q/2
def resource_cost(q):return 20*q+q*q/2
def social(q):return gross_benefit(q)-resource_cost(q)-20*q
check('external-cost efficient quantity',max(range(81),key=social),30)
check('gross benefit at30',gross_benefit(q1),2550)
check('resource cost at30',resource_cost(q1),1050)
check('external damage at30',20*q1,600)
check('net social surplus at private quantity',social(q0),800)
check('net social surplus at efficient quantity',social(q1),900)
check('external-cost social improvement',social(q1)-social(q0),100)
print(json.dumps({'passed':True,'checks':checks,'scope':'Exact rational synthetic files; arithmetic and model implications only, not empirical validation, actual policy or legal advice.'},indent=2))
Related chapters
- Choose a price using demand evidence and incremental contribution
- Separate marginal cost, committed capacity and scale choices
- Find a feasible profit maximum and test the alternatives
- Analyze rival responses without confusing equilibrium with approval
- Value information through the decisions it can change
- Trace policy incidence and external costs before recommending a response
- Decision clinic: defend the model, the evidence and the feasible action