js/ml/metrics/shared
js/ml/metrics/shared.ts
Shared validation, ordering, and summation helpers for fino:ml/metrics.
Types
type Label = string | number | boolean
A class label. Metrics compare labels by value, so any primitive works.
Classes
class MetricError extends Error {
Error thrown when metric inputs are malformed.
Covers length mismatches, empty inputs, non-finite values, probabilities
outside [0, 1], unknown labels, and averaging modes that cannot be
resolved from the data.
Constructors
constructor(message: string)
class CompensatedSum {
Neumaier compensated summation.
Streaming metrics accumulate over arbitrarily many batches, where naive addition drifts once the running total dwarfs individual terms.
Methods
add(value: number): void
reset(): void
Getters
get value(): number
Functions
function requireSameLength(
a: ArrayLike<unknown>,
b: ArrayLike<unknown>,
aName: string,
bName: string,
): void
function requireNonEmpty(values: ArrayLike<unknown>, name: string): void
function requireFinite(values: ArrayLike<number>, name: string): void
function requireProbabilities(values: ArrayLike<number>, name: string): void
function requirePositiveInteger(value: number, name: string): void
function compareLabels(a: Label, b: Label): number
Total ordering over labels: numbers numerically, booleans false-first, strings lexicographically, and mixed types grouped by type name so the ordering stays stable regardless of insertion order.
function sortedLabelUnion(...sources: ReadonlyArray<ArrayLike<Label>>): Label[]
function resolvePositiveLabel(labels: readonly Label[], explicit?: Label): Label
Resolve which label counts as positive for a binary metric.
An explicit choice always wins. Otherwise {0, 1} resolves to 1 and
{false, true} to true; anything else is ambiguous and must be stated.
function resolveScoreLabel(labels: readonly Label[], explicit?: Label): Label
Resolve the positive label for a probability- or score-based metric.
Unlike resolvePositiveLabel this does not require the positive class to
actually appear: a batch can legitimately be all negatives, and refusing to
score it would make streaming evaluation fail on unlucky shard boundaries.
function formatLabels(labels: readonly Label[]): string
function ratio(numerator: number, denominator: number): number
numerator / denominator, or 0 when the denominator is zero.
Undefined metric values are reported as zero throughout this module rather
than as NaN, matching the convention established by scikit-learn.
function descendingOrder(scores: ArrayLike<number>): number[]
Ranks values best-first, breaking ties by original position so that equal scores keep input order and every derived curve is deterministic.