Loans: let the stated rate compound half-yearly or yearly

RA
5d ago
+523-32

Step 1 of #5207. It does not close the issue: steps 2 to 6 follow.

Problem

Sure charges a loan's annual rate divided by twelve each month: monthly compounding. A contract can state the rate compounded half-yearly or yearly instead. Canadian fixed-rate mortgages are the common case: the Interest Act (s. 6) requires the rate to be stated "calculated yearly or half-yearly, not in advance", and lenders use half-yearly. A month then charges (1 + r/2)^(1/6) − 1, not r / 12, and the lender sizes the payment at that rate.

At 4.40% on CAD 100,000 over 20 years, the lender's monthly payment is CAD 625.13; Sure computes CAD 627.26 and charges CAD 366.67 of interest in the first month instead of CAD 363.35. The schedule, the payoff projection and the #4470 replay all drift from the lender's statements.

What changes

  • The loan form gets a "Compounding" select, after "Interest basis": each payment (the default), half-yearly or yearly. The hint says that most loans compound once per payment, including loans where interest is calculated daily and charged monthly, as in Australia and the UK, and that Canadian fixed-rate mortgages compound half-yearly.
  • Loan::AmortizationMath takes a compounding:. A period of t years charges (1 + r/m)^(m × t) − 1, with m compoundings a year and t from the loan's day-count convention (#3639). The day count measures the period; the compounding turns the measure into a charge.
  • The payment is sized at the rate for a twelfth of a year under the loan's compounding. Sizing still ignores the day count, so the payment stays level, as it does today.
  • The schedule, the payoff projection and the #4470 replay all read the loan's compounding, so a borrower who pays the schedule replays onto the schedule's balance. Changing the setting queues a balance sync, as the other schedule inputs do.
  • Compounded factors are computed once per process for each rate, compounding and length of period, so the logarithm runs once per factor rather than twice a period.
  • The family export and import carry the setting.
  • A loan that does not choose a compounding is unchanged: the default takes the existing code path, and default_convention_baseline_test.rb passes untouched.

Known limits

  • Payments are still monthly. Biweekly and weekly payments are step 3 of #5207; with them, the same formula charges a 14-day period, and a test here pins that figure (CAD 167.08 on CAD 100,000 at 4.40% over 14 days on actual/365).
  • The setting belongs to the loan, not to a term. Per-term settings are step 2 of #5207, after #5130 and #5131.
  • A compounded factor is not additive across parts of a period: two windows charged on the same balance come to slightly less than the whole period. Nothing splits a scheduled period today; accrual_factor's comment states the rule for code that will (#5130's term changes): compound across the windows, which on an unchanged balance gives exactly the whole period's factor.
  • The replay carries a missed payment's interest beside the principal, as before, so that interest does not compound. The setting is about how the lender states the rate, not about arrears.

Migration

Adds loans.interest_compounding, a string defaulting to per_period, with the check constraint chk_loans_interest_compounding, as day_count_convention does. No backfill.

Validation

  • New: test/models/loan/interest_compounding_test.rb, with figures computed by hand from the formula: the default on every day count, half-yearly and yearly monthly rates, a 14-day period, a leap February, a part period on 30/360, the payment sizing, a level schedule that ends at zero, a variable loan re-sized once across a rate change, the cache, the stored setting reaching the schedule, and the #4470 replay landing on the schedule's balance. The form, the balance sync and the export round trip are covered in loans_controller_test.rb and data_importer_test.rb.
  • bin/rails test test/models/loan test/models/loan_test.rb test/controllers/loans_controller_test.rb test/helpers/loans_helper_test.rb test/models/family test/i18n_test.rb test/migrations: 777 runs, 0 failures. The 4 skips are the i18n tests skipped on main.
  • bin/rails test on the first commit: 16,852 runs. 13 failures and errors in PDF extraction, provider generators, LLM pricing, recurring transactions, AI routing and the Monarch import; they fail the same way on main in the same container, so they come from the environment. One more, security/health_checker_test.rb:86, passed on three reruns.
  • The migration rolls back and forward; the dumped loans table matches db/schema.rb.
  • RuboCop and ERB lint clean; Brakeman clean on the first commit. The six locale files parse.

Checked against a lender

Before writing this, I checked the half-yearly formula, with each period's days over the year's length, against a Canadian lender's annual statements for a mortgage with biweekly payments: all 79 interest lines matched within a cent. Those figures are private, so the tests use round numbers computed by hand from the formula.

Screenshots

From a development instance of this branch: the form with the new select and its hint, and the first rows of the Schedule tab for CAD 100,000 at 4.40% compounded half-yearly over 20 years.

Loan form: Compounding select and hint

Schedule tab, half-yearly compounding

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Loan settings now let you choose how interest compounds: per payment, semi-annually, or annually. Loan schedules and payoff projections reflect the selected option.
    • The loan form includes guidance on compounding frequency and supports the new options in multiple languages.
    • Loan imports and exports preserve compounding settings.