Topic

Cash flows and rates of return

A project is a list of amounts at times. Its net present value at a rate is one number. Its internal rate of return is the solution of an equation, and equations can have several solutions or none.

npv("10%", [-1000, 300, 400, 500, 200]);      // exact
irr([-1000, 300, 400, 500, 200]).rate;         // an interval around 15.32%
irr([-1000, 2300, -1320]).rates;               // two: exactly 10% and 20%
irr([-1000, 2000, -1500]).count;               // 0: no rate works, proven
xirr([{ date: "2027-01-01", amount: -10000 }, { date: "2028-03-15", amount: 11500 }]).rate;
mirr([-1000, 300, 400, 500, 200], "8%", "6%"); // one answer, by assumption

Net present value

NPV(r)=∑tat(1+r)t. \mathrm{NPV}(r) = \sum_t \frac{a_t}{(1+r)^t}.

Money out is negative and money in positive. npv takes amounts at times 0, 1, 2, … and is exact; given { t, amount } with fractional times, or dates through xnpv, the powers are irrational and the answer is an interval.

Internal rates of return

A rate r with NPV(r) = 0. Multiplying through by (1+r)n(1+r)^n gives a polynomial of degree n in x=1+rx = 1 + r, and a polynomial of degree n can have up to n positive roots. Descartes' rule of signs bounds the number by the number of changes of sign in the flows: an investment followed only by returns has one change and exactly one rate, which is the textbook case. A project that ends with a cost (a mine that must be cleaned up, a lease with a final payment) has two changes, and may have two rates or none.

A spreadsheet's IRR starts from a guess and reports wherever Newton's method lands. Here the real roots are isolated by exact methods from @ratmath/algebraic, so the count is a theorem: rates lists every one, each exact when rational and otherwise in an interval that contains exactly one root; rate is set only when there is a single one; and note says in words what was found. A repeated root is reported with its multiplicity.

When there are several, none is "the" return. Use the net present value at your cost of capital, or the modified rate.

The modified rate

mirr(flows, financeRate, reinvestRate) discounts the money out at one rate, compounds the money in at another to the end, and reports the single rate that turns the first into the second over n periods: (FVin/−PVout)1/n−1(\mathrm{FV_{in}} / -\mathrm{PV_{out}})^{1/n} - 1. It always exists and is unique, at the price of two assumptions that you state.

Payback, inflation, depreciation, taxes

payback gives the first period at which the running total is no longer negative, and the same with the last period split in proportion; with a rate the flows are discounted first. realValue and nominalValue move an amount across inflation.

depreciation produces the schedule for straight-line, declining-balance (optionally switching to straight-line), sum-of-years-digits and units-of-production. The last period takes whatever is left, so the total written off is exactly cost minus salvage even after rounding.

taxSchedule(brackets) turns a list of { upTo, rate } into a piecewise-linear function: .on(income) gives the tax, the marginal and average rates and the part from each bracket. No jurisdiction's table is included; the brackets are yours.

callreturns
npv(rate, flows), xnpv(rate, datedFlows, { dayCount })exact, or an interval
irr(flows){ rates: [{ rate, exact, text, multiplicity }], count, signChanges, rate, note }
xirr(datedFlows, { dayCount, range })the same, rates as intervals
mirr(flows, financeRate, reinvestRate)exact, or an interval
payback(flows, { rate }){ period, fractional, cumulative }
realValue, nominalValue (amount, inflation, periods)exact for whole periods
depreciation({ cost, salvage, life, method, … })[{ period, depreciation, accumulated, bookValue }]
taxSchedule(brackets).on(income){ tax, marginalRate, averageRate, parts, afterTax }
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