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 synthetic Business Finance laboratory. Standard library only; no network or learner data."""
from fractions import Fraction as F
from math import sqrt,isclose
import json
out={}
def same(a,b):assert isclose(float(a),float(b),rel_tol=1e-10,abs_tol=1e-8),(a,b)
def pv(flows,rate):return sum(F(v)/(1+rate)**t for t,v in enumerate(flows))
assert 27000-20000-10000==-3000 and 5000-(-3000)==8000
out['value_and_funding']={'pre_financing_cash':-3000,'funding_to_floor':8000}
monthly=F(1,100);ear=(1+monthly)**12-1
same(ear,0.1268250301319697)
ordinary=sum(F(5000)/(1+F(1,10))**t for t in [1,2,3]);due=sum(F(5000)/(1+F(1,10))**t for t in [0,1,2]);late=sum(F(5000)/(1+F(1,10))**t for t in [2,3,4])
assert due==ordinary*F(11,10) and late==ordinary/F(11,10)
same(ordinary,F(5000)*(1-(1+F(1,10))**-3)/F(1,10))
payment=10000*monthly/(1-(1+monthly)**-12);balance=F(10000);rows=[];principal_sum=F(0)
for month in range(1,13):
interest=balance*monthly;principal=payment-interest;balance-=principal;principal_sum+=principal
rows.append({'month':month,'payment':float(payment),'interest':float(interest),'principal':float(principal),'ending_balance':float(balance)})
assert balance==0 and principal_sum==10000 and rows[0]['interest']==100
same(sum(payment/(1+monthly)**t for t in range(1,13)),10000)
fee_rate_lo=0.01;fee_rate_hi=0.02
for _ in range(100):
mid=(fee_rate_lo+fee_rate_hi)/2;value=sum(float(payment)/(1+mid)**t for t in range(1,13))
if value>9800:fee_rate_lo=mid
else:fee_rate_hi=mid
fee_rate=(fee_rate_lo+fee_rate_hi)/2;assert fee_rate>float(monthly);same(sum(float(payment)/(1+fee_rate)**t for t in range(1,13)),9800)
out['rates_and_amortization']={'effective_annual':float(ear),'ordinary_annuity':float(ordinary),'annuity_due':float(due),'delayed_annuity':float(late),'amortization':rows,'fee_adjusted_monthly_cost':fee_rate}
bond8=sum(F(60)/F(108,100)**t for t in [1,2,3])+F(1000)/F(108,100)**3
bond5=sum(F(60)/F(105,100)**t for t in [1,2,3])+F(1000)/F(105,100)**3
assert bond8<1000<bond5
same(bond8,948.4580602550423);same(bond5,1027.2324802937046)
d1=2*F(104,100);dividend_value=d1/(F(1,10)-F(4,100));assert dividend_value==F(104,3)
premoney=200000-50000+10000;postmoney=premoney+40000;assert premoney==160000 and postmoney==200000 and F(40000,postmoney)==F(1,5)
assert F(40000,F(1,4))-40000==120000
out['claims_and_valuation']={'bond_at_8pct':float(bond8),'bond_at_5pct':float(bond5),'dividend_value':float(dividend_value),'premoney_equity':premoney,'postmoney_equity':postmoney,'new_ownership':0.2}
def variance(rho,w=F(1,2)):
return w*w*F(1,10)**2+(1-w)**2*F(1,5)**2+2*w*(1-w)*rho*F(1,10)*F(1,5)
assert variance(F(1,4))==F(15,1000) and variance(1)==F(225,10000) and variance(-1)==F(25,10000)
assert variance(-1,F(2,3))==0
expected=F(1,2)*F(8,100)+F(1,2)*F(12,100);capm=F(3,100)+F(12,10)*F(5,100)
assert expected==F(1,10) and capm==F(9,100)
out['risk']={'expected_portfolio_return':float(expected),'variance':float(variance(F(1,4))),'standard_deviation':sqrt(variance(F(1,4))),'perfect_positive_sd':sqrt(variance(1)),'perfect_negative_sd':sqrt(variance(-1)),'stipulated_capm_return':float(capm)}
rate=F(1,10);base=pv([-39000,15000,15000,23000],rate);no_recovery=pv([-39000,15000,15000,18000],rate);downside=pv([-39000,12000,12000,20000],rate)
assert round(float(base),2)==4313.30 and round(float(no_recovery),2)==556.72 and round(float(downside),2)==-3147.26
assert base-no_recovery==5000/F(11,10)**3
a=pv([-10000,13000],rate);b=pv([-50000,62000],rate);assert b>a>0 and F(13000,10000)-1>F(62000,50000)-1
assert F(4,10)*F(6,100)+F(6,10)*F(11,100)==F(9,100)
out['capital_choices']={'base_npv':float(base),'no_working_capital_recovery_npv':float(no_recovery),'operating_downside_npv':float(downside),'A_npv':float(a),'B_npv':float(b),'A_irr':0.3,'B_irr':0.24,'separate_no_tax_wacc':0.09}
sales_day=F(365000,365);cost_day=F(219000,365);inventory=45*cost_day;ar=30*sales_day;ap=25*cost_day;nwc=inventory+ar-ap
assert [inventory,ar,ap,nwc]==[27000,30000,15000,42000]
assert inventory+45*sales_day-ap==57000 and 45+30-25==50 and 45+45-25==65
trade_period=F(2,98);simple=trade_period*F(365,20);effective=(1+float(trade_period))**(365/20)-1
eoq=sqrt(F(2*1200*30,8));ordering=1200*30/eoq;holding=8*eoq/2;same(ordering,holding)
assert 1200*30/100+8*100/2==760 and F(1200,360)*6+10==30
cash=20000;debt=0;draws=0;peak=0;timeline=[];unfinanced=20000;unfinanced_balances=[]
for day,flow in [(1,-18000),(10,25000),(12,-15000),(20,-10000),(30,10000)]:
unfinanced+=flow;unfinanced_balances.append(unfinanced);cash+=flow
draw=max(0,5000-cash);repay=min(debt,max(0,cash-5000));cash+=draw-repay;debt+=draw-repay;draws+=draw;peak=max(peak,debt)
assert cash>=5000
timeline.append(dict(day=day,net_event=flow,draw=draw,repay=repay,cash=cash,debt=debt))
assert unfinanced_balances==[2000,27000,12000,2000,12000]
assert cash==12000 and debt==0 and draws==6000 and peak==3000
out['working_capital']={'inventory':int(inventory),'receivables':int(ar),'payables':int(ap),'operating_nwc':int(nwc),'slow_collection_nwc':57000,'trade_period_cost':float(trade_period),'simple_annualized_trade_cost':float(simple),'compounded_trade_cost':effective,'eoq':eoq,'ordering_plus_holding_at_eoq':ordering+holding,'rounded_Q100_cost':760,'reorder_point':30,'cash_timeline':timeline,'total_draws':draws,'peak_debt':peak}
pre=37000-35000-8000;gap=5000-pre;net_proceeds=15000-500;interest=F(7,100)*15000;service=6000+interest
assert pre==-6000 and gap==11000 and gap-9000==2000 and net_proceeds-gap==3500 and service==7050 and 15000-service==7950
out['cumulative_dossier']={'economic_initial_cost':39000,'actual_initial_cheques':35000,'pre_financing_cash':pre,'gap_to_floor':gap,'facility_9000_shortfall':2000,'loan_net_proceeds':net_proceeds,'year1_debt_service':int(service),'project_cash_after_specified_service':7950}
# Preserve independently reproduced earlier native-course examples rather than replacing them.
old_two=pv([-10000,6000,6000],rate);old_delayed=pv([-10000,0,6000,6000],rate)
assert round(float(old_two),2)==413.22 and round(float(old_delayed),2)==-533.43
old_project=pv([-24000,10000,10000,16000],rate);old_no_recovery=pv([-24000,10000,10000,12000],rate)
assert round(float(old_project),2)==5376.41 and round(float(old_no_recovery),2)==2371.15
assert F(1,2)*12000+F(3,10)*2000-F(1,5)*15000==3600
assert F(4,10)*12000+F(3,10)*2000-F(3,10)*15000==900
assert 4000+(F(4,10)*19800+6000)-10800-4000-1500-3000-5000==-6380
assert -6380+5000+F(3,10)*19800==4560
out['preserved_native_examples']={'two_year_npv':float(old_two),'delayed_npv':float(old_delayed),'working_capital_project_npv':float(old_project),'unrecoverable_working_capital_npv':float(old_no_recovery),'scenario_expectations':[3600,900],'original_cash_gap':11380,'improved_cash_gap':440}
print(json.dumps({'passed':True,'cases':out},indent=2))
Related chapters
- Separate value, liquidity and the authority to commit
- Draw the cash-flow timeline before choosing a formula
- Value the claim being bought and reconcile the ownership
- Diversify the exposure and qualify the required return
- Value incremental project cash and compare feasible alternatives
- Fund the operating cycle and test the lowest cash date
- Finance clinic: reconcile value, funding, risk and the recommendation