JavaScript · Probability Metrics

Probability Metrics

import * as numkong from 'numkong'; 8.0.0-rc.1 · b1f866ab

import { kullbackleibler, jensenshannon } from "numkong";

const p = new Float32Array([0.2, 0.3, 0.5]);
const q = new Float32Array([0.1, 0.3, 0.6]);

console.log(kullbackleibler(p, q));
console.log(jensenshannon(p, q));

These call the underlying SIMD divergence kernels directly.

API

Functions

kullbackleiblerComputes the Kullback-Leibler divergence between two probability distributions.
numkong.ts:344 ↗
function kullbackleibler(
    a: Float64Array | Float32Array,
    b: Float64Array | Float32Array,
): number
function kullbackleibler(
    a: Float64Array | Float32Array | Uint16Array,
    b: Float64Array | Float32Array | Uint16Array,
    dtype: DType,
): number
function kullbackleibler(a: TensorBase, b: TensorBase): number

Both vectors must represent valid probability distributions, non-negative and summing to 1. Supports f64 and f32, auto-detected, and f16 and bf16 with an explicit dtype.

Parameters

aThe first probability distribution, as Float32Array, Float64Array, or TensorBase.
bThe second probability distribution, matching the type and length of a.
dtypeDTypeOptional dtype string for custom types, e.g. 'f16', 'bf16'.

Returns

The Kullback-Leibler divergence KL(a || b) = Σ a[i] · log(a[i] / b[i]).
jensenshannonComputes the Jensen-Shannon distance between two probability distributions.
numkong.ts:365 ↗
function jensenshannon(
    a: Float64Array | Float32Array,
    b: Float64Array | Float32Array,
): number
function jensenshannon(
    a: Float64Array | Float32Array | Uint16Array,
    b: Float64Array | Float32Array | Uint16Array,
    dtype: DType,
): number
function jensenshannon(a: TensorBase, b: TensorBase): number

Both vectors must represent valid probability distributions, non-negative and summing to 1. Supports f64 and f32, auto-detected, and f16 and bf16 with an explicit dtype. JSD is the square root of the symmetrized KL divergence: d_JS(a, b) = √(0.5 × (KL(a‖m) + KL(b‖m))), where m = (a + b) / 2.

Parameters

aThe first probability distribution, as Float32Array, Float64Array, or TensorBase.
bThe second probability distribution, matching the type and length of a.
dtypeDTypeOptional dtype string for custom types, e.g. 'f16', 'bf16'.

Returns

The Jensen-Shannon distance between a and b.