A common rule for an emergency fund is "three to six months of expenses". It ignores how much the income actually moves. A rule that uses the volatility instead:
months = clamp(ceil(3 + 12 × CV), 3, 12), where CV = standard deviation / mean of the last months of net income.
Here is a small implementation with money in integer cents, plus the two details that bit me: which standard deviation, and which percentile.
function bufferMonths(centsArr, { sample = true } = {}) { const xs = centsArr.map(Number); // cents are safe in Number for statistics const n = xs.length; if (n < 2) return { months: 3, cv: 0 }; const mean = xs.reduce((a, b) => a + b, 0) / n; const ss = xs.reduce((a, x) => a + (x - mean) ** 2, 0); const sd = Math.sqrt(ss / (sample ? n - 1 : n)); const cv = sd / mean; return { months: Math.min(12, Math.max(3, Math.ceil(3 + 12 * cv))), cv }; } Detail 1: sample or population standard deviation
With twelve months of a freelancer's income ($4,260, $890, $2,740, $3,980, $1,150, $2,360, $5,120, $640, $2,980, $3,410, $1,820, $4,470):
- sample SD (n − 1): CV = 0.5273 → 3 + 6.33 → 10 months
- population SD (n): CV = 0.5048 → 3 + 6.06 → 10 months
Same answer here. But with only four months logged ($2,000, $1,000, $3,000, $1,500) it splits: sample CV 0.4554 gives 9 months, population CV 0.3944 gives 8. With short histories the choice matters, so pick one, document it and test it. I use the sample SD, because the months you logged are a sample of the months you will have.
Detail 2: the "safe paycheck" percentile
The monthly amount the buffer has to cover is a self-paycheck (25th percentile of the months) plus fixed costs. Percentiles have several definitions; I use linear interpolation (the same as Excel PERCENTILE.INC and NumPy's default), computed on cents:
function percentileLinear(sortedCents, p) { const h = (sortedCents.length - 1) * p; const lo = Math.floor(h), hi = Math.ceil(h); if (lo === hi) return BigInt(sortedCents[lo]); const w = BigInt(Math.round((h - lo) * 1e6)); const num = BigInt(sortedCents[lo]) * (1000000n - w) + BigInt(sortedCents[hi]) * w; return (num + 500000n) / 1000000n; // round half up to whole cents } For the twelve months above: h = 11 × 0.25 = 2.75, between $1,150 and $1,820 → $1,652.50. With fixed costs of $1,850 one month costs $3,502.50, the target is 10 × $3,502.50 = $35,025.00, and $14,200 saved lasts 4.1 months at zero income.
Tests worth having
console.assert(bufferMonths([300000, 300000, 300000]).months === 3); // steady income -> floor const inc = [426000, 89000, 274000, 398000, 115000, 236000, 512000, 64000, 298000, 341000, 182000, 447000]; console.assert(bufferMonths(inc.map(x => x * 3)).months === bufferMonths(inc).months); // scale-free The second one is a metamorphic test: CV does not depend on the currency unit or on scaling, so multiplying every month by 3 must not change the months of cover. If it does, something is dividing by the wrong thing.
I use this rule in a free calculator (no sign-up): https://gettidycents.com/tools/emergency-fund-calculator