Skip to main content

Personal Finance: Saving and Investing

Charts for a retirement planner or an investing app: savings growing, what a fee costs, and how a balance sheet and its goals move over the years. Each example builds its numbers from a few lines of arithmetic, so change the rate, the amount or the years in its code and the chart redraws. Budgeting and Debt has the other half of the genre.

note

The numbers on this page are made up to show the charts. They are not financial advice.


Growing Savings​

Compound Growth​

$500 a month, earning 7% a year. A stacked area shows the balance as its two parts, what went in and what the returns added, so you can see the growth catch up with the contributions and then pass them.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });
  const monthly = 500;
  const rate = 0.07 / 12;
  const years = Array.from({ length: 31 }, (_, year) => {
    const months = year * 12;
    const balance = (monthly * ((1 + rate) ** months - 1)) / rate;
    const contributed = monthly * months;
    return { year, contributed, growth: Math.round(balance - contributed) };
  });

  return (
    <AreaChart
      label="$500 a month at 7% for 30 years"
      type="stacked"
      data={years}
      dataKey="year"
      series={[
        { name: 'contributed', label: 'Contributions', color: 'info' },
        { name: 'growth', label: 'Growth', color: 'success' },
      ]}
      hasGradient={false}
      fillOpacity={0.6}
      valueFormatter={value => dollars.format(value)}
      xAxisLabel="Years"
      hasLegend
    />
  );
}

Fee Drag​

The same $500 a month in two made-up funds that earn 7% before fees, one charging 0.1% a year and the other 1%. Two lines on one axis show the gap the fee opens, and that it compounds: what the higher fee takes each year stops earning too, so after 30 years the gap is about a sixth of the balance.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });
  const balance = (fee: number, years: number) => {
    const rate = (0.07 - fee) / 12;
    return Math.round((500 * ((1 + rate) ** (years * 12) - 1)) / rate);
  };
  const years = Array.from({ length: 31 }, (_, year) => ({
    year,
    index: balance(0.001, year),
    managed: balance(0.01, year),
  }));

  return (
    <LineChart
      label="The same savings at a 0.1% and a 1% yearly fee"
      data={years}
      dataKey="year"
      series={[
        {
          name: 'index',
          label: 'Larkspur Index Fund, 0.1% fee',
          color: 'success',
        },
        {
          name: 'managed',
          label: 'Brambleton Managed Fund, 1% fee',
          color: 'danger',
          strokeDasharray: '5 5',
        },
      ]}
      valueFormatter={value => dollars.format(value)}
      xAxisLabel="Years"
      hasDots={false}
      hasLegend
    />
  );
}


Planning for Retirement​

Target-Date Glide Path​

How a target-date fund moves from stocks to bonds and cash as its owner nears retirement. type="percent" draws each age's mix as shares of the whole, so the bands always fill the chart and only their boundaries move.

function example() {
  const mix = Array.from({ length: 46 }, (_, i) => {
    const age = 25 + i;
    const stocks = Math.min(90, Math.max(40, 90 - (age - 40) * 2));
    const cash = Math.min(10, Math.max(0, age - 55));
    return { age, stocks, bonds: 100 - stocks - cash, cash };
  });

  return (
    <AreaChart
      label="A target-date fund's mix by age"
      type="percent"
      data={mix}
      dataKey="age"
      series={[
        { name: 'stocks', label: 'Stocks', color: 'primary' },
        { name: 'bonds', label: 'Bonds', color: 'info' },
        { name: 'cash', label: 'Cash', color: 'grey-light' },
      ]}
      hasGradient={false}
      fillOpacity={0.6}
      unit="%"
      xAxisLabel="Age"
      hasLegend
    />
  );
}

Retirement Monte Carlo Fan​

A made-up saver's balance from 35 to 90 across 1,000 simulated markets: $12,000 a year in until 65, then $55,000 a year out, with each year's return drawn at random around 5%. A line chart's bands shade the spread around the median balance, the middle half of the paths and the middle 90%, so the fan shows how wide the outcomes run, and where its lower edges reach zero, that some paths run out of money before 90. The paths are drawn once, in useState's initializer, rather than again on every render.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    notation: 'compact',
    maximumFractionDigits: 1,
  });
  const [ages] = useState(() => {
    // mulberry32, by Tommy Ettinger (2017), dedicated to the public domain
    // under CC0 1.0:
    // https://gist.github.com/tommyettinger/46a874533244883189143505d203312c
    // A seeded random number generator: the same seed gives the same paths
    // every time.
    function mulberry32(seed: number) {
      return () => {
        seed = (seed + 0x6d2b79f5) | 0;
        let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
        t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
        return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
      };
    }
    const random = mulberry32(2512);
    // A year's return: 5% on average, give or take 10%, from a normal
    // distribution (the Box-Muller transform of two uniform draws).
    const yearlyReturn = () =>
      0.05 +
      0.1 *
        Math.sqrt(-2 * Math.log(1 - random())) *
        Math.cos(2 * Math.PI * random());
    const paths = 1000;
    const first = 35;
    const last = 90;
    // Every path's balance at each age.
    const balances = Array.from({ length: last - first + 1 }, () =>
      new Array<number>(paths).fill(0)
    );
    for (let path = 0; path < paths; path += 1) {
      let balance = 50000;
      for (let age = first; age <= last; age += 1) {
        balances[age - first][path] = balance;
        const flow = age < 65 ? 12000 : -55000;
        balance = Math.max(0, balance * (1 + yearlyReturn()) + flow);
      }
    }
    // The balance a given share of the paths fall below, at each age.
    return balances.map((atAge, index) => {
      const sorted = [...atAge].sort((a, b) => a - b);
      const below = (share: number) =>
        Math.round(sorted[Math.floor(share * (paths - 1))]);
      return {
        age: first + index,
        p5: below(0.05),
        p25: below(0.25),
        median: below(0.5),
        p75: below(0.75),
        p95: below(0.95),
      };
    });
  });

  return (
    <LineChart
      label="Retirement balance by age across 1,000 simulated markets"
      data={ages}
      dataKey="age"
      series={[{ name: 'median', label: 'Median balance', color: 'primary' }]}
      bands={[
        {
          series: 'median',
          lower: 'p25',
          upper: 'p75',
          label: 'Middle 50% of paths',
          lowerLabel: '25th percentile',
          upperLabel: '75th percentile',
        },
        {
          series: 'median',
          lower: 'p5',
          upper: 'p95',
          label: 'Middle 90% of paths',
          lowerLabel: '5th percentile',
          upperLabel: '95th percentile',
        },
      ]}
      referenceLines={[{ x: 65, label: 'Retires' }]}
      valueFormatter={value => dollars.format(value)}
      xAxisLabel="Age"
      hasDots={false}
      height={320}
    />
  );
}

Social Security Claiming Age​

What a made-up retiree has collected by each age if they claim at 62, 67 or 70, when waiting pays more each month. Cumulative lines cross where waiting has caught up. The reference lines mark the first age at which each claiming age has collected at least as much as the one before it, worked out from the same rows, and are left out if that never happens by 90.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });
  // $2,000 a month at 67: 70% of it from 62, or 124% of it from 70.
  const collected = (claimAge: number, monthly: number, age: number) =>
    Math.max(0, age - claimAge) * 12 * monthly;
  const ages = Array.from({ length: 29 }, (_, i) => {
    const age = 62 + i;
    return {
      age,
      at62: collected(62, 1400, age),
      at67: collected(67, 2000, age),
      at70: collected(70, 2480, age),
    };
  });
  // The first age at which claiming later has paid at least as much.
  const breakEven = (later: 'at67' | 'at70', earlier: 'at62' | 'at67') =>
    ages.find(row => row[later] > 0 && row[later] >= row[earlier])?.age;

  return (
    <LineChart
      label="Benefits collected by age, by claiming age"
      data={ages}
      dataKey="age"
      series={[
        { name: 'at62', label: 'Claim at 62', color: 'warning' },
        { name: 'at67', label: 'Claim at 67', color: 'info' },
        { name: 'at70', label: 'Claim at 70', color: 'success' },
      ]}
      referenceLines={[
        { x: breakEven('at67', 'at62'), label: '67 passes 62' },
        { x: breakEven('at70', 'at67'), label: '70 passes 67' },
      ].filter(line => line.x !== undefined)}
      valueFormatter={value => dollars.format(value)}
      xAxisLabel="Age"
      hasDots={false}
      hasLegend
      height={320}
    />
  );
}


Tracking Progress​

Net Worth​

Ten years of a made-up household's balance sheet. What it owns stacks up from zero and what it owes stacks down, with net worth as a line through both. The debts are negative numbers, and chartProps passes Recharts' stackOffset="sign" through so they stack below zero rather than on top of the assets, and yAxisProps sizes the axis to fit the negative amounts.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });
  // A 30-year mortgage at 4.5%: its balance after `years`.
  const growth = 1 + 0.045 / 12;
  const mortgageLeft = (years: number) =>
    (272000 * (growth ** 360 - growth ** (years * 12))) / (growth ** 360 - 1);
  const years = Array.from({ length: 10 }, (_, i) => {
    const cash = 6000 + 1500 * i;
    const investments = Math.round(12000 * 1.07 ** i + 7000 * i);
    const home = Math.round(340000 * 1.035 ** i);
    const mortgage = -Math.round(mortgageLeft(i));
    const studentLoan = Math.min(0, 3500 * i - 24000);
    return {
      year: String(2016 + i),
      cash,
      investments,
      home,
      mortgage,
      studentLoan,
      netWorth: cash + investments + home + mortgage + studentLoan,
    };
  });

  return (
    <ComposedChart
      label="Assets, debts and net worth by year"
      data={years}
      dataKey="year"
      series={[
        {
          name: 'cash',
          label: 'Cash',
          type: 'bar',
          stackId: 'worth',
          color: 'success-60',
        },
        {
          name: 'investments',
          label: 'Investments',
          type: 'bar',
          stackId: 'worth',
          color: 'success-45',
        },
        {
          name: 'home',
          label: 'Home',
          type: 'bar',
          stackId: 'worth',
          color: 'success-30',
        },
        {
          name: 'mortgage',
          label: 'Mortgage',
          type: 'bar',
          stackId: 'worth',
          color: 'danger-45',
        },
        {
          name: 'studentLoan',
          label: 'Student loan',
          type: 'bar',
          stackId: 'worth',
          color: 'danger-60',
        },
        { name: 'netWorth', label: 'Net worth', type: 'line', color: 'link' },
      ]}
      chartProps={{ stackOffset: 'sign' }}
      yAxisProps={{ width: 'auto' }}
      valueFormatter={value => dollars.format(value)}
      hasLegend
      height={340}
    />
  );
}

Savings Goal Progress​

Three goals of different sizes, each as a ring filled as far as it has come. The goals don't share a target, so each value is its percentage of its own, against max={100}, and the names carry the dollars.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });
  const goals = [
    { name: 'Emergency fund', saved: 8200, target: 10000, color: 'success' },
    { name: 'House deposit', saved: 18000, target: 60000, color: 'link' },
    { name: 'Trip', saved: 2100, target: 3000, color: 'warning' },
  ];

  return (
    <RadialBarChart
      label="Savings goals, as a share of each target"
      data={goals.map(goal => ({
        name: `${goal.name}: ${dollars.format(goal.saved)} of ${dollars.format(goal.target)}`,
        value: Math.round((goal.saved / goal.target) * 100),
        color: goal.color,
      }))}
      max={100}
      unit="%"
      showLabels
      hasLegend
      height={300}
    />
  );
}