function moment
moment(
x: array,
k: number,
dim?: 0 | 1,
): number

Computes the central moment of a dataset.

Computes the k-th central moment of a dataset around the mean. The first moment is always zero, the second moment equals variance, and higher moments describe shape.

Examples

Second moment (variance)

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

assertEquals(moment([1, 2, 3, 4, 5], 2), 2);

First moment (always zero)

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

assertEquals(moment([1, 2, 3], 1), 0);

Matrix moments

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

assertEquals(moment([[1, 2], [3, 4]], 2), [0.25, 0.25]);

Parameters

Input array or matrix

k: number

Order of the moment to compute

optional
dim: 0 | 1

Dimension to compute along (0 for rows, 1 for columns). Default is 0

Return Type

number

Central moment values

Throws

When input is invalid

moment(
x: matrix,
k: number,
dim?: 0 | 1,
): array

Computes the central moment of a dataset.

Computes the k-th central moment of a dataset around the mean. The first moment is always zero, the second moment equals variance, and higher moments describe shape.

Examples

Second moment (variance)

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

assertEquals(moment([1, 2, 3, 4, 5], 2), 2);

First moment (always zero)

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

assertEquals(moment([1, 2, 3], 1), 0);

Matrix moments

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

assertEquals(moment([[1, 2], [3, 4]], 2), [0.25, 0.25]);

Parameters

Input array or matrix

k: number

Order of the moment to compute

optional
dim: 0 | 1

Dimension to compute along (0 for rows, 1 for columns). Default is 0

Return Type

Central moment values

Throws

When input is invalid