A time-value-of-money toolkit from first principles in pure Python + NumPy/SciPy. Single-sum and m-periodic compounding, the continuous limit, rate-convention algebra, annuities/perpetuities (level and growing) in closed form, NPV/IRR with the two return measures, and the full money-market yield web (BDY / HPY / EAY / MMY / BEY) — every formula derived from its definition and matched to the CFA Level I curriculum.
66/66 tests verifying algebraic identities — closed form against brute-force sum, every round-trip, every limit — no finance library underneath.
$ pytest tests/
=========================== 66 passed in 0.47s ============================
Every identity in this repo was adversarially verified by a multi-agent workflow before a line of code was written — 41 claims were independently checked and 14 had subtle errors corrected (e.g. the continuous- compounding limit is O(1/m) with relative error r_s²t/(2m) and cannot be rate-tested at m=10⁶ where roundoff dominates; NPV at its own root must use a scale-aware absolute tolerance, never
== 0; money-weighted return must be the same IRR solve, checked against the closed-form root 264/(59+√29881)−1; the two R_MM formulas are equal but the property test needs rtol 1e-9 because 360−t·r_BD cancels when price ≪ face). See "How this was built" below.
Time value of money is the first thing every finance course teaches and the
last thing anyone implements honestly — most code reaches for a spreadsheet
function or numpy_financial and never sees the geometric series underneath.
This repo is the opposite: every annuity is its closed-form geometric sum, and
every test pins that closed form against an independent term-by-term sum, a
round-trip, or an analytic limit — never against another library's output.
It is the foundation the rest of the CFA-derived portfolio sits on: the
discounting here is what
corpfin-lab and
eqval-lab value cash flows with,
the EAR/continuous-rate algebra feeds
fixedalt-lab, and the
money-market yield conventions are the ones quoted throughout.
| Module | Topic | Headline functions |
|---|---|---|
core.py |
Single-sum TVM + EAR | future_value, present_value, future_value_periodic, future_value_continuous, ear, ear_continuous, stated_from_ear, n_periods, rate_from_pv_fv |
rates.py |
Rate-convention algebra | RateQuote, convert_compounding, discrete_to_continuous, continuous_to_discrete, effective_per_period |
annuities.py |
Annuities/perpetuities (level + growing) | pv_ordinary_annuity, fv_ordinary_annuity, pv_annuity_due, pv_perpetuity, pv_growing_annuity, pv_growing_perpetuity, annuity_payment |
dcf.py |
NPV / IRR / return measures | npv, npv_derivative, irr, money_weighted_return, time_weighted_return |
yields.py |
Money-market yields | bank_discount_yield, holding_period_yield, effective_annual_yield, money_market_yield, money_market_yield_from_bdy, bond_equivalent_yield_from_eay |
from tvm import (
future_value, ear, ear_continuous,
pv_ordinary_annuity, pv_annuity_due, pv_perpetuity, annuity_payment,
npv, irr, money_weighted_return, time_weighted_return,
bank_discount_yield, holding_period_yield,
money_market_yield, effective_annual_yield,
)
future_value(1000, 0.07, 30) # 7612.26 (single sum)
ear(0.12, 12) # 0.126825 (12% comp. monthly)
pv_ordinary_annuity(100, 0.08, 5) # 399.27 == sum_{k=1..5} 100/1.08**k
pv_annuity_due(100, 0.08, 5) # 431.21 == ordinary * 1.08
annuity_payment(200_000, 0.005, 360) # 1199.10 (30-yr monthly mortgage)
npv(0.10, [-10, 3, 4.8, 5.5]) # 0.826446
irr([-10, 3, 4.8, 5.5]) # 0.142020 (NPV == 0 at this rate)
money_weighted_return([-50, -59, 132]) # 0.138612 (the IRR of the flows)
hpy = holding_period_yield(98_000, 100_000) # 2.0408% on price
money_market_yield(hpy, 95) # 7.7336% (360, simple)
effective_annual_yield(hpy, 95) # 8.0713% (365, compound)66 tests, every one an identity the formula must satisfy. Highlights:
- PV/FV round-trip:
present_value(future_value(pv,r,n),r,n) == pv(rtol 1e-12; ~1 ulp, not bit-exact) - m-periodic == single-sum with rescaled rate/exponent:
future_value_periodic(pv,r_s,m,N) == future_value(pv,r_s/m,m·N) - Continuous limit: relative error of the m-periodic FV vs
pv·e^(r_s t)isr_s²t/(2m)— tested as a rate at moderate m - EAR
(1+r_s/m)^m−1viaexpm1/log1p; strictly increasing in m, bounded bye^(r_s)−1 stated_from_earinvertsear;rate_from_pv_fv/n_periodsinvertfuture_value
discrete_to_continuous⇄continuous_to_discreteare exact inverses;e^(r_c)−1 == EARconvert_compoundingpreserves the EAR across any frequencies; identity (bit-exact) whenm_from == m_to(1 + effective_per_period(r_s,m,ppy))^ppy − 1 == ear(r_s,m)
- Closed form == brute-force geometric sum for PV and FV (the central test)
- Annuity-due / ordinary
== 1+rexactly;FV == PV·(1+r)^N - r→0 limit → N·A (guarded); small-r sweep bounded by the analytic
r·(N+1) - Perpetuity is the N→∞ limit of the ordinary annuity; growing perpetuity is the N→∞ growing annuity
- g→r removable singularity → N·A/(1+r) (guarded); continuity verified on a scale-aware band
annuity_paymentis the exact inverse ofpv_ordinary_annuity(mortgage round-trip)
npv== explicit discounted sum; reduces toc0 + c1/(1+r)for two flows- NPV strictly decreasing in the rate; analytic
npv_derivative < 0 - IRR is the NPV root, residual-checked with a scale-aware absolute tolerance (never
== 0) - Money-weighted return == IRR (one solve); golden pinned to
264/(59+√29881)−1 - Time-weighted:
(1+TWR)^k == ∏(1+HPYᵢ); equals MWR with no interim flows, diverges with them
- The two R_MM formulas agree:
HPY·(360/t) == 360·r_BD/(360−t·r_BD)for a zero-coupon bill - EAY⇄HPY exact inverses on the 365 basis; MMY⇄HPY on the 360 basis
- Discount-instrument ordering:
r_BD < R_MM < EAY - Canonical BEY inverts the semiannual-bond basis:
(1 + BEY/2)² − 1 == EAY - CFA worked examples pinned as golden vectors (BDY 6.00%/7.58%, EAY 3.52%, R_MM 2.01%/5.17%, BEY 6.88%, TWR 15.84%, MWR 13.86%, NPV $826,446, IRR 14.20%)
PYTHONPATH=. python examples/ear_frequency_ladder.py # EAR -> continuous limit
PYTHONPATH=. python examples/annuity_identities.py # closed form == brute sum
PYTHONPATH=. python examples/money_market_yields.py # the BDY/HPY/EAY/MMY/BEY webear_frequency_ladder.py output (textbook-exact):
m (periods/yr) EAR gap to cont.
1 0.12000000 7.50e-03
2 0.12360000 3.90e-03
12 0.12682503 6.72e-04
365 0.12747462 2.22e-05
1,000,000 0.12749684 8.12e-09
continuous 0.12749685 0.00e+00
money_market_yields.py output (T-bill, face 100k, price 98k, 95 days):
bank discount yield (face, 360, simple) r_BD = 7.578947%
money-market yield (price, 360, simple) R_MM = 7.733620%
effective annual yield (price, 365, comp.) EAY = 8.071291%
ordering for a discount instrument: r_BD < R_MM < EAY -> True
R_MM via HPY vs via BDY agree: 0.00e+00
This repo was built with an adversarial multi-agent workflow before implementation:
- Design panel — three independent agents proposed the module layout and the algebraic identities from different angles (numerical-analysis-first, CFA-faithful, testing-first).
- Synthesis — merged into one canonical spec with 41 identities, precise signatures, and 20 CFA worked-example golden vectors.
- Adversarial verification — one agent per identity tried to refute it.
14 of 41 were corrected before any code: the O(1/m) continuous-limit
rate (untestable at m=10⁶), the r→0 and g→r small-offset error scales (flat
1e-6 sweep bounds were wrong), NPV-at-its-root needing a scale-aware
absolute tolerance, the money-weighted-return golden (closed-form root, not
a bit-pinned literal), the R_MM property-test cancellation (rtol 1e-9), a
misattributed annuity-due CFA anchor (1197.81 belongs to A=300,N=5), and the
BEY-from-HPY value that is not exactly 0.1218 (it carries a trailing
floating-point tail, so it is pinned with an absolute tolerance, not
==). - Implementation against the verified spec, using
expm1/log1pwhere it buys accuracy. - Adversarial code review — reviewers per dimension (math, numerical, API, tests), findings verified before applying.
The result: identities that are right because they were proven right, not because they happened to pass.
- v0.2.0 — XIRR/XNPV on actual calendar dates (day-count fractions)
- v0.2.0 alt — bond pricing (price ⇄ YTM) built on the annuity + single-sum spine
- v0.3.0 — amortization schedules (principal/interest split per period)
- v0.3.0 alt — modified IRR (MIRR) and the NPV-vs-IRR ranking-conflict demo
- v0.4.0 — non-conventional-flow IRR (all real roots via the companion matrix)
corpfin-lab,eqval-lab— capital budgeting & equity valuation; they discount with this spinefixedalt-lab— fixed income; consumes the EAR / yield-convention algebramonte-carlo-lab,var-lab— the simulation/risk siblings built the same way (identity tests, adversarial workflow)port-lab— portfolio math; the time-weighted vs money-weighted return live here
- CFA Institute (2014). CFA Program Curriculum, Level I, Vol. 1: Quantitative Methods (Readings 5–6: Time Value of Money, Discounted Cash Flow Applications).
- Bodie, Z., Kane, A., & Marcus, A. (2014). Investments. McGraw-Hill.
- Brealey, R., Myers, S., & Allen, F. (2016). Principles of Corporate Finance. McGraw-Hill.
MIT.