#!/usr/bin/env python3 """Price and roll forward Cedar Trail's fictional bond issue. Run with ``uv run python`` and this file's path. This dependency-free script recomputes supplied facts; it does not choose a yield, qualify issuance costs, interpret an indenture, or approve accounting and disclosure conclusions. """ def money(amount: float) -> str: """Format a number as accounting-style US dollars.""" return f"${amount:,.2f}" if amount >= 0 else f"(${abs(amount):,.2f})" def percent(rate: float) -> str: """Format a decimal rate as a percentage without hiding useful precision.""" return f"{rate:.10%}" def heading(title: str) -> None: print(f"\n{title}\n{'-' * len(title)}") def present_value(rate: float, coupon: float, face: float, periods: int) -> float: """Present value of equal coupons and principal paid at period-end.""" annuity_factor = (1 - (1 + rate) ** -periods) / rate return coupon * annuity_factor + face * (1 + rate) ** -periods def solve_yield(proceeds: float, coupon: float, face: float, periods: int) -> float: """Solve the positive periodic yield for conventional debt cash flows.""" low, high = 0.0, 1.0 for _ in range(200): midpoint = (low + high) / 2 if present_value(midpoint, coupon, face, periods) > proceeds: low = midpoint else: high = midpoint return (low + high) / 2 face_amount = 2_000_000.00 stated_annual_rate = 0.06 market_annual_rate = 0.08 payments_per_year = 2 periods = 8 issuance_costs = 25_000.00 stated_periodic_rate = stated_annual_rate / payments_per_year market_periodic_rate = market_annual_rate / payments_per_year cash_interest = face_amount * stated_periodic_rate heading("1. Contract cash flows") print(f"{'Face amount':<38} {money(face_amount):>20}") print(f"{'Stated rate each half-year':<38} {percent(stated_periodic_rate):>20}") print(f"{'Coupon cash each half-year':<38} {money(cash_interest):>20}") print(f"{'Coupon periods':<38} {periods:>20,}") heading("2. Issue price at the supplied market yield") coupon_present_value = cash_interest * ( 1 - (1 + market_periodic_rate) ** -periods ) / market_periodic_rate principal_present_value = face_amount * (1 + market_periodic_rate) ** -periods issue_price = coupon_present_value + principal_present_value discount = face_amount - issue_price print(f"{'Coupon present value':<38} {money(coupon_present_value):>20}") print(f"{'Principal present value':<38} {money(principal_present_value):>20}") print(f"{'Issue price':<38} {money(issue_price):>20}") print(f"{'Gross discount':<38} {money(discount):>20}") heading("3. Opening net carrying amount and effective yield") opening_carrying_amount = issue_price - issuance_costs effective_periodic_yield = solve_yield( opening_carrying_amount, cash_interest, face_amount, periods ) print(f"{'Issue price':<38} {money(issue_price):>20}") print(f"{'Qualifying issuance costs':<38} {money(-issuance_costs):>20}") print(f"{'Opening net carrying amount':<38} {money(opening_carrying_amount):>20}") print(f"{'Solved effective half-year yield':<38} {percent(effective_periodic_yield):>20}") heading("4. Full-precision effective-interest schedule") print( f"{'Per.':>4} {'Opening':>18} {'Cash':>14} {'Expense':>16} " f"{'Increase':>16} {'Ending':>18}" ) carrying_amount = opening_carrying_amount total_interest_expense = 0.0 for period in range(1, periods + 1): opening = carrying_amount interest_expense = opening * effective_periodic_yield carrying_increase = interest_expense - cash_interest carrying_amount = opening + carrying_increase total_interest_expense += interest_expense print( f"{period:>4} {money(opening):>18} {money(cash_interest):>14} " f"{money(interest_expense):>16} {money(carrying_increase):>16} " f"{money(carrying_amount):>18}" ) heading("5. Reconciliations") total_cash_interest = cash_interest * periods total_carrying_increase = carrying_amount - opening_carrying_amount print(f"{'Total cash interest':<38} {money(total_cash_interest):>20}") print(f"{'Total interest expense':<38} {money(total_interest_expense):>20}") print(f"{'Total carrying increase':<38} {money(total_carrying_increase):>20}") print(f"{'Ending carrying amount':<38} {money(carrying_amount):>20}") if abs(issue_price - 1_865_345.102500992) > 0.000001: raise SystemExit("FAIL: issue price does not match the controlled value") if abs(carrying_amount - face_amount) > 0.000001: raise SystemExit("FAIL: calculated schedule does not reach face amount") if abs(total_interest_expense - total_cash_interest - total_carrying_increase) > 0.000001: raise SystemExit("FAIL: interest, cash, and carrying amount do not reconcile") print("\nPASS: price, basis, effective interest, and maturity reconcile without a plug.") print("Boundary: PASS verifies arithmetic only; contract, yield, scope, and reporting need review.")