function cagr
cagr(
x: array,
t: array,
dim?: 0 | 1,
): number

Compound Annual Growth Rate.

Calculates the Compound Annual Growth Rate (CAGR) which represents the mean annual growth rate of an investment over a specified time period longer than one year.

Examples

CAGR for a single asset over time

import { assertEquals } from "jsr:@std/assert";

const sp500 = [676.53, 902.40, 1378.43, 1769.30, 2206.45, 2643.85, 2679.63];
const dates = [1998, 2000, 2005, 2010, 2015, 2020, 2022];
assertEquals(cagr(sp500, dates), 0.05902892931364878);

CAGR for multiple assets with the same time series

import { assertEquals } from "jsr:@std/assert";

const dates = [1998, 2000, 2005, 2010, 2015, 2020, 2022];
const sp500 = [676.53, 902.40, 1378.43, 1769.30, 2206.45, 2643.85, 2679.63];

assertEquals(cagr([sp500, sp500], dates), [0.05902892931364878, 0.05902892931364878]);

CAGR with different time periods

import { assertEquals } from "jsr:@std/assert";

const portfolio = [1000, 1200, 1500, 1800];
const years = [2020, 2021, 2022, 2023];
assertEquals(cagr(portfolio, years), 0.21644039911467994);

Parameters

Array of asset values over time

Array of corresponding time periods

optional
dim: 0 | 1

Dimension to operate on (0: row-wise, 1: column-wise)

Return Type

number

Compound Annual Growth Rate

Throws

If input is a number (not supported)

cagr(
x: matrix,
t: array,
dim?: 0 | 1,
): array | matrix

Compound Annual Growth Rate.

Calculates the Compound Annual Growth Rate (CAGR) which represents the mean annual growth rate of an investment over a specified time period longer than one year.

Examples

CAGR for a single asset over time

import { assertEquals } from "jsr:@std/assert";

const sp500 = [676.53, 902.40, 1378.43, 1769.30, 2206.45, 2643.85, 2679.63];
const dates = [1998, 2000, 2005, 2010, 2015, 2020, 2022];
assertEquals(cagr(sp500, dates), 0.05902892931364878);

CAGR for multiple assets with the same time series

import { assertEquals } from "jsr:@std/assert";

const dates = [1998, 2000, 2005, 2010, 2015, 2020, 2022];
const sp500 = [676.53, 902.40, 1378.43, 1769.30, 2206.45, 2643.85, 2679.63];

assertEquals(cagr([sp500, sp500], dates), [0.05902892931364878, 0.05902892931364878]);

CAGR with different time periods

import { assertEquals } from "jsr:@std/assert";

const portfolio = [1000, 1200, 1500, 1800];
const years = [2020, 2021, 2022, 2023];
assertEquals(cagr(portfolio, years), 0.21644039911467994);

Parameters

Array of asset values over time

Array of corresponding time periods

optional
dim: 0 | 1

Dimension to operate on (0: row-wise, 1: column-wise)

Return Type

Compound Annual Growth Rate

Throws

If input is a number (not supported)