Topic

Scenarios

A scenario is a few accounts, the money that moves between them, and the rates they earn or cost. Projecting it is bookkeeping, done exactly. Two scenarios can then be compared to the cent, and the effect of each input measured without guessing a step size.

const household = scenario({
  start: "2027-01", periods: 120,
  accounts: [
    { name: "bank", balance: 3000 },
    { name: "savings", balance: 10000, rate: "4%" },
    { name: "loan", balance: -20000, rate: "7%" },
  ],
  flows: [
    { name: "salary", amount: 4500, to: "bank", growth: "3%" },
    { name: "living", amount: 3200, from: "bank", growth: "2.5%" },
    { name: "loan payment", amount: 400, from: "bank", to: "loan", until: "paid", then: "savings" },
    { name: "car", amount: 9000, from: "savings", at: "2029-06" },
  ],
});
const p = household.project();
p.rows[11].balances;     // after the first year
p.netWorth;              // exact
p.firstNegative;         // the first time an account goes below zero, or null

The model

Time runs in equal periods (months by default). Each period, in this order:

  1. every account earns or is charged interest on its opening balance at rate/periodsPerYear. A negative balance is a debt and is charged the same way.
  2. the flows happen in the order listed. A flow with only to is money arriving from outside, with only from money leaving, with both a transfer.

A flow can start and end (start, end, as period numbers or months), repeat every so many periods (every), happen once (at), and grow by a rate once a year (growth). until: "paid" stops a payment into a debt when the debt is cleared, and then sends the money somewhere else from that moment, which is how "once the loan is gone, save the payment" is written. An account's rate can change with rateChanges.

Nothing stops a balance going negative: that is information. firstNegative and minimum report it.

By default nothing is rounded, so the projection is the exact consequence of the inputs: a savings account with level deposits reproduces the annuity formula to the last digit. With rounding: 2 interest and flows are rounded to the cent each period, as a bank would, and a debt account then reproduces a loan schedule.

What if

compare({ a: …, b: … }) projects several scenarios and sets them side by side: each one's outcome (final net worth unless you give another measure), its difference from the first, the lowest any account fell, and crossovers, the periods at which one scenario's net worth overtakes another's. Paying a loan down against investing, renting against buying and stopping work at one date against another are all of this shape.

Which input matters

The outcome of an unrounded projection is a rational function of its inputs, built from additions and multiplications. Its derivative with respect to an input can therefore be carried through the same arithmetic step by step (forward-mode differentiation), and the result is the exact derivative, a fraction, not a difference quotient with a step you had to choose.

sensitivity(build, inputs) does this for each input in turn. build is a function from the inputs to a scenario. It reports the derivative, perPercent (the change in the outcome when that input rises by 1% of itself) and the elasticity, sorted so the input that moves the outcome most comes first. Inside build, arithmetic on an input uses .add, .subtract, .multiply, .divide.

A derivative describes small changes. Where a flow is capped (a debt being paid off in a particular month) the outcome has a kink, and the derivative is the one on the side you are on.

callreturns
scenario({ periods, periodsPerYear, start, accounts, flows, rounding })Scenario
.project(){ opening, rows: [{ period, date, balances, interest, flows, netWorth }], final, netWorth, minimum, firstNegative, totals }
compare(scenarios, { measure }){ rows: [{ name, outcome, difference, netWorth, lowest, firstNegative }], best, series, crossovers }
sensitivity(build, inputs, { measure }){ base, rows: [{ input, value, derivative, perPercent, elasticity }], most }
Try itEdit the code and run it with @ratmath/finance and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs