or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bins.mdindex.mditerables.mdsearch.mdsequences.mdsets.mdstatistics.mdtransformations.md
tile.json

statistics.mddocs/

Statistics

Core statistical functions for computing summary statistics, distributions, and mathematical operations on arrays of numbers. These functions gracefully handle undefined, null, and NaN values by ignoring them, making them ideal for real-world data with missing values.

Capabilities

Basic Statistics

min

Returns the minimum value in the given iterable using natural order.

/**
 * Returns the minimum value in the given iterable using natural order
 * @param iterable - The iterable to search
 * @param accessor - Optional function to extract values from elements
 * @returns The minimum value or undefined if no comparable values exist
 */
function min(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => any): any;

Usage Examples:

import { min } from "d3-array";

// Basic usage
min([3, 1, 4, 1, 5]); // 1
min(["20", "3"]); // "20" (string comparison)
min([20, 3]); // 3 (numeric comparison)

// With accessor
min([{value: 3}, {value: 1}, {value: 4}], d => d.value); // 1

// Handles missing data
min([3, null, 1, undefined, 4, NaN]); // 1

minIndex

Returns the index of the minimum value in the given iterable.

/**
 * Returns the index of the minimum value in the given iterable
 * @param iterable - The iterable to search
 * @param accessor - Optional function to extract values from elements
 * @returns The index of minimum value or -1 if no comparable values exist
 */
function minIndex(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => any): number;

max

Returns the maximum value in the given iterable using natural order.

/**
 * Returns the maximum value in the given iterable using natural order
 * @param iterable - The iterable to search
 * @param accessor - Optional function to extract values from elements
 * @returns The maximum value or undefined if no comparable values exist
 */
function max(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => any): any;

maxIndex

Returns the index of the maximum value in the given iterable.

/**
 * Returns the index of the maximum value in the given iterable
 * @param iterable - The iterable to search
 * @param accessor - Optional function to extract values from elements
 * @returns The index of maximum value or -1 if no comparable values exist
 */
function maxIndex(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => any): number;

extent

Returns both the minimum and maximum values as a two-element array.

/**
 * Returns both the minimum and maximum values as a two-element array
 * @param iterable - The iterable to analyze
 * @param accessor - Optional function to extract values from elements
 * @returns [min, max] array or [undefined, undefined] if no comparable values
 */
function extent(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => any): [any, any];

Usage Examples:

import { extent } from "d3-array";

extent([3, 1, 4, 1, 5]); // [1, 5]
extent([{score: 95}, {score: 87}], d => d.score); // [87, 95]

Central Tendency

mean

Returns the arithmetic mean of the given iterable of numbers.

/**
 * Returns the arithmetic mean of the given iterable of numbers
 * @param iterable - The iterable of numbers
 * @param accessor - Optional function to extract numeric values
 * @returns The mean or undefined if no numbers exist
 */
function mean(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => number): number | undefined;

median

Returns the median of the given iterable using the R-7 method.

/**
 * Returns the median of the given iterable using the R-7 method
 * @param iterable - The iterable of numbers
 * @param accessor - Optional function to extract numeric values
 * @returns The median or undefined if no numbers exist
 */
function median(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => number): number | undefined;

medianIndex

Returns the index of the element to the left of the median.

/**
 * Returns the index of the element to the left of the median
 * @param array - The array of values
 * @param accessor - Optional function to extract numeric values
 * @returns The index of the median element
 */
function medianIndex(array: any[], accessor?: (d: any, i: number, data: any[]) => number): number;

mode

Returns the mode (most frequently occurring value) of the given iterable.

/**
 * Returns the mode (most frequently occurring value) of the given iterable
 * @param iterable - The iterable to analyze
 * @param accessor - Optional function to extract values from elements
 * @returns The mode or undefined if no comparable values exist
 */
function mode(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => any): any;

Summation

sum

Returns the sum of the given iterable of numbers.

/**
 * Returns the sum of the given iterable of numbers
 * @param iterable - The iterable of numbers
 * @param accessor - Optional function to extract numeric values
 * @returns The sum (0 if no numbers exist)
 */
function sum(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => number): number;

cumsum

Returns the cumulative sum as a Float64Array.

/**
 * Returns the cumulative sum as a Float64Array
 * @param iterable - The iterable of numbers
 * @param accessor - Optional function to extract numeric values
 * @returns Float64Array of cumulative sums
 */
function cumsum(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => number): Float64Array;

Usage Examples:

import { sum, cumsum } from "d3-array";

sum([1, 2, 3, 4]); // 10
cumsum([1, 2, 3, 4]); // Float64Array [1, 3, 6, 10]

Variance and Deviation

variance

Returns an unbiased estimator of the population variance using Welford's algorithm.

/**
 * Returns an unbiased estimator of the population variance using Welford's algorithm
 * @param iterable - The iterable of numbers
 * @param accessor - Optional function to extract numeric values
 * @returns The variance or undefined if fewer than two numbers exist
 */
function variance(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => number): number | undefined;

deviation

Returns the standard deviation (square root of bias-corrected variance).

/**
 * Returns the standard deviation (square root of bias-corrected variance)
 * @param iterable - The iterable of numbers
 * @param accessor - Optional function to extract numeric values
 * @returns The standard deviation or undefined if fewer than two numbers exist
 */
function deviation(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => number): number | undefined;

Quantiles

quantile

Returns the p-quantile of the given iterable using the R-7 method.

/**
 * Returns the p-quantile of the given iterable using the R-7 method
 * @param iterable - The iterable of numbers
 * @param p - The quantile probability [0, 1]
 * @param accessor - Optional function to extract numeric values
 * @returns The p-quantile value
 */
function quantile(iterable: Iterable<any>, p: number, accessor?: (d: any, i: number, data: any[]) => number): number | undefined;

quantileIndex

Returns the index to the left of the p-quantile.

/**
 * Returns the index to the left of the p-quantile
 * @param array - The array of values
 * @param p - The quantile probability [0, 1]
 * @param accessor - Optional function to extract numeric values
 * @returns The index of the quantile element
 */
function quantileIndex(array: any[], p: number, accessor?: (d: any, i: number, data: any[]) => number): number;

quantileSorted

Returns the p-quantile for a pre-sorted array (more efficient).

/**
 * Returns the p-quantile for a pre-sorted array (more efficient)
 * @param array - The pre-sorted array of values
 * @param p - The quantile probability [0, 1]
 * @param accessor - Optional function to extract numeric values
 * @returns The p-quantile value
 */
function quantileSorted(array: any[], p: number, accessor?: (d: any, i: number, data: any[]) => number): number | undefined;

Usage Examples:

import { quantile } from "d3-array";

const data = [0, 10, 30];
quantile(data, 0);    // 0 (minimum)
quantile(data, 0.5);  // 10 (median)
quantile(data, 1);    // 30 (maximum)
quantile(data, 0.25); // 5 (first quartile)
quantile(data, 0.75); // 20 (third quartile)

Ranking

rank

Returns an array with the rank of each value in the iterable.

/**
 * Returns an array with the rank of each value in the iterable
 * @param iterable - The iterable to rank
 * @param comparator - Optional comparator function or accessor function
 * @returns Array of zero-based ranks
 */
function rank(iterable: Iterable<any>, comparator?: ((a: any, b: any) => number) | ((d: any) => any)): number[];

Usage Examples:

import { rank, descending } from "d3-array";

rank([1, 2, 3]); // [0, 1, 2]
rank(["b", "c", "b", "a"]); // [1, 3, 1, 0]
rank([1, 2, 3], descending); // [2, 1, 0]
rank([{x: 1}, {}, {x: 2}, {x: 0}], d => d.x); // [1, NaN, 2, 0]

Counting

count

Returns the number of valid numeric values (not null, NaN, or undefined).

/**
 * Returns the number of valid numeric values (not null, NaN, or undefined)
 * @param iterable - The iterable to count
 * @param accessor - Optional function to extract values from elements
 * @returns The count of valid values
 */
function count(iterable: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => any): number;

High-Precision Arithmetic

These functions provide IEEE 754 full precision arithmetic to avoid floating-point rounding errors.

fsum

Returns a full precision summation of the given values.

/**
 * Returns a full precision summation of the given values
 * @param values - The iterable of numbers
 * @param accessor - Optional function to extract numeric values
 * @returns Full precision sum
 */
function fsum(values?: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => number): number;

fcumsum

Returns a full precision cumulative sum of the given values.

/**
 * Returns a full precision cumulative sum of the given values
 * @param values - The iterable of numbers
 * @param accessor - Optional function to extract numeric values
 * @returns Array of full precision cumulative sums
 */
function fcumsum(values?: Iterable<any>, accessor?: (d: any, i: number, data: any[]) => number): number[];

Usage Examples:

import { sum, fsum, cumsum, fcumsum } from "d3-array";

// Demonstrating precision differences
const values = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1];

sum(values);    // 0.9999999999999999 (floating-point error)
fsum(values);   // 1 (full precision)

cumsum([1, 1e-14, -1]);  // [1, 1.00000000000001, 9.992e-15]
fcumsum([1, 1e-14, -1]); // [1, 1.00000000000001, 1e-14]

Adder

A class for incremental full precision addition.

/**
 * Creates a full precision adder for IEEE 754 floating point numbers
 */
class Adder {
  constructor();
  
  /**
   * Adds the specified number to the adder's current value
   * @param number - The number to add
   * @returns The adder instance for chaining
   */
  add(number: number): Adder;
  
  /**
   * Returns the IEEE 754 double precision representation of the current value
   * @returns The current sum value
   */
  valueOf(): number;
}

Usage Examples:

import { Adder } from "d3-array";

const adder = new Adder();
adder.add(0.1).add(0.2).add(0.3);
console.log(+adder); // 0.6 (full precision)
console.log(adder.valueOf()); // 0.6