Skip to content

About

Lean 4 + Mathlib proof of an explicit formula for the constant loan payment (PMT) when the interest rate changes each period, with an optional balloon payment.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

PmtVariableRate

An explicit formula for the constant payment (PMT) of a loan whose interest rate changes from period to period, with a machine-checked proof in Lean 4 and Mathlib. Published by ALTA Software.

Purpose

The textbook annuity formula assumes one interest rate for the whole loan, and a monthly rate of annual rate / 12. When interest accrues daily, at annual rate / day-count basis times the actual days in each month, every month has its own rate and the textbook formula no longer repays the loan exactly. An explicit formula for this case, with an optional balloon balance at the end, was derived at ALTA Software around 2006. It holds for any sequence of non-negative per-period rates, not only day-count rates.

The full derivation is in the technical note docs/pmt_variable.md. Because the formula is used to compute real payments, it has also been verified by a proof assistant: Lean checks every step of the proof, so the formula is correct for every number of periods and every choice of rates that meets the stated assumptions, not just for the cases someone tested.

The formula and its original handwritten proof are by Niko Pachuashvili (c. 2006). The Lean 4 formalisation, both the theorem statements and the proofs, was generated by Claude Code, starting from the original summary of the derivation (pmt_ne, Appendix A.1 of the technical note) and the hint that the proof was by induction. Lean's kernel checks every proof, so their correctness does not depend on who wrote them.

Scans of the original handwritten proof from around 2006 are in docs/original/, and Appendix A of the technical note explains them.

The formula

Setting

  • n ≥ 1 periods,
  • L: loan amount,
  • b: balloon, the balance that remains after period n,
  • r_1, …, r_n: the interest rate of each period,
  • c: the constant payment (the PMT),
  • x_1, …, x_n: the principal part of each payment (payment minus interest).

The schedule equations say that each payment is interest on the outstanding balance plus a principal part, and that the principal parts repay everything except the balloon:

L · r_1                          + x_1 = c
(L − x_1) · r_2                  + x_2 = c
…
(L − x_1 − … − x_{n−1}) · r_n    + x_n = c
x_1 + x_2 + … + x_n = L − b

Result

        L · ∏_{i=1}^{n} (1 + r_i)  −  b
c = ─────────────────────────────────────────
     1 + ∑_{i=2}^{n} ∏_{j=i}^{n} (1 + r_j)

Below, P_n = ∏_{i=1}^{n} (1 + r_i) is the numerator's product and S_n = 1 + ∑_{i=2}^{n} ∏_{j=i}^{n} (1 + r_j) the denominator.

With all rates equal, the formula reduces to the textbook annuity payment.

Using the formula in practice

For an implementation:

  1. Compute c with the formula above. Rates must be non-negative (r_i ≥ 0); then S_n ≥ 1 and the division is always defined.
  2. Build the schedule period by period: start with balance B_0 = L; in period i the interest is B_{i−1} · r_i, the principal part is x_i = c − B_{i−1} · r_i, and the new balance is B_i = B_{i−1} − x_i. In exact arithmetic the balance after the last period is exactly b, and this is the only schedule with a constant payment.
  3. Check the inputs:
    • Positive payment: c > 0 exactly when b < L · P_n. A balloon smaller than the loan (b < L) is always enough.
    • No negative amortisation: a principal part is negative when that period's interest exceeds the payment, which happens when one period is much longer than the others. A simple check that rules it out for every period is c ≥ L · max r_i. The exact per-period condition is in Section 7 of the technical note.

Rounding to currency units is outside the scope of the proof (see Scope).

The theorems

All names live in the PMT namespace.

File Theorem Statement
PMT/Basic.lean partial_sum Partial-sum identity: if the first k schedule equations hold, the first k principal parts sum to c · S_k + L · (1 − P_k).
PMT/Basic.lean pmt_mul The key identity c · S_n = L · P_n − b, i.e. the formula with the division multiplied out.
PMT/Basic.lean pmt_formula PMT formula. If the schedule equations hold and 1 + r_i > 0 for every period, then c = (L · P_n − b) / S_n.
PMT/Converse.lean pmt_converse Converse: if c · S_n = L · P_n − b, the explicit schedule (balance B_0 = L, B_k = B_{k−1} · (1 + r_k) − c, principal x_i = c − B_{i−1} · r_i) satisfies the schedule equations and ends with balance b.
PMT/Converse.lean pmt_iff Characterisation. For 1 + r_i > 0: a schedule with constant payment c exists if and only if c equals the formula.
PMT/Converse.lean schedule_unique For a given payment c, the schedule equations determine every principal part: x_i = c − B_{i−1} · r_i (over any commutative ring).
PMT/Converse.lean pmt_unique Uniqueness. For 1 + r_i > 0: two solutions of the n + 1 schedule equations in the n + 1 unknowns c, x_1, …, x_n have the same c and the same x_i. With pmt_iff, the system has exactly one solution.
PMT/Equal.lean pmt_equal Equal rates: if every r_i = ρ with ρ > −1 and ρ ≠ 0, the formula reduces to the textbook annuity payment c = ρ · (L · (1 + ρ)^n − b) / ((1 + ρ)^n − 1).
PMT/Equal.lean pmt_equal_zero Zero rate: if every r_i = 0, then c = (L − b) / n.
PMT/Positivity.lean pmt_pos_iff Positive payment. For 1 + r_i > 0: c > 0 if and only if b < L · P_n.
PMT/Positivity.lean pmt_pos_of_balloon_lt For L ≥ 0, r_i ≥ 0 and b < L, the payment is positive.
PMT/Positivity.lean principal_nonneg_iff, principal_nonneg_iff_one, principal_nonneg_iff_closed x_i ≥ 0 if and only if the payment covers the period's interest, B_{i−1} · r_i ≤ c; in terms of the data: L · r_1 ≤ c for i = 1, and L · P_{i−1} · r_i ≤ c · (1 + r_i · S_{i−1}) for i ≥ 2.
PMT/Positivity.lean principal_nonneg_of_le No negative amortisation. For r_i ≥ 0: if L · r_i ≤ c for every period, every x_i ≥ 0.

Notes on the assumptions:

  • Interest rates are non-negative, r_i ≥ 0 (zero allowed). The theorems assume only the weaker condition 1 + r_i > 0 (each period's growth factor is positive), which every non-negative rate satisfies. It is what guarantees the denominator S_n is positive, so the division is defined.
  • The identity c · S_n = L · P_n − b (pmt_mul, pmt_converse) needs no such assumption: it is proved over any commutative ring, not only the real numbers.

Prerequisites

  1. Install elan, the Lean toolchain manager: https://github.com/leanprover/elan#installation (on Windows, the PowerShell installer; on Linux/macOS, the curl … | sh one-liner). elan reads lean-toolchain and downloads the exact Lean version (v4.37.0-rc1) automatically on first use.
  2. A bash shell to run scripts/verify.sh (Git Bash works on Windows).
  3. Git and internet access to github.com and to Mathlib's cache server: the first lake exe cache get downloads Mathlib's prebuilt cache (several GB), so Mathlib does not have to be compiled locally.

No other Lean knowledge is needed to run scripts/verify.sh.

How to build and verify

lake exe cache get     # once: download prebuilt Mathlib
lake build             # build the library
scripts/verify.sh      # build + check the axioms of the main theorems

scripts/verify.sh prints the axioms each main theorem depends on and ends with

OK: all theorems depend only on propext, Classical.choice, Quot.sound.

Those three are Lean's standard axioms. The script fails if any theorem uses sorry (an unfinished proof) or any other axiom.

The build prints a warning that the files "import Mathlib ... designed for use with the module system"; it is expected and harmless.

Do not run lake update: lake-manifest.json and lean-toolchain pin the exact Mathlib revision and Lean toolchain the proofs were verified with.

Scope

The proof is about exact arithmetic over the real numbers. It does not cover:

  • rounding of payments, interest or balances to currency precision (e.g. cents), or the drift such rounding causes over a schedule;
  • the behaviour of System.Decimal or any other floating/fixed-point number type used by an implementation.

An implementation of the formula still needs its own tests for rounding.

License

Copyright 2026 ALTA Software.

Licensed under the Apache License, Version 2.0. This covers the Lean sources, the scripts and the documentation, including the technical note. See also NOTICE.

About

Lean 4 + Mathlib proof of an explicit formula for the constant loan payment (PMT) when the interest rate changes each period, with an optional balloon payment.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages