weightedMean

Calculates a weighted arithmetic mean without requiring weights to sum to 1.

import { Arith } from "@teakit/arith";

Arith.weightedMean(["10", "20", "30"], ["0.2", "0.3", "0.5"]).toString(); // "23"
Arith.weightedMean(["100", "120", "80"], ["5", "3", "2"]).toString(); // "102"

// Zero weights are allowed if at least one weight is positive.
Arith.weightedMean(["10", "20", "30"], [0, 1, 0]).toString(); // "20"

const Short = Arith.clone({ DECIMAL_PLACES: 4 });
Short.weightedMean(["1", "2"], ["1", "2"]).toString(); // "1.6667"

API Reference

Signature

Arith.weightedMean(
  values: readonly ArithValue[],
  weights: readonly ArithValue[],
): ArithInstance;

Parameters

ParameterTypeRequiredNotes
valuesreadonly ArithValue[]YesNon-empty array of finite values.
weightsreadonly ArithValue[]YesNon-empty array of finite non-negative weights with the same length as values.

Returns

Returns sum(value[i] * weight[i]) / sum(weights) as a new Arith instance.

Throws

  • Throws if values or weights is not an array.
  • Throws if their lengths differ.
  • Throws if any value is non-finite.
  • Throws if any weight is non-finite or negative.
  • Throws if all weights are zero.

Agent Contract

FieldValue
Kindstatic method
Canonical nameweightedMean
AliasesNone
Mutates receiverNo
ReturnsArithInstance
Accepts (string, base) overloadNo
Configuration dependenciesDECIMAL_PLACES, ROUNDING_MODE, STRICT
Related methodsmean, sum

Agent Notes

  • Weights do not need to be normalized.
  • Use weightedMean for weighted cost, portfolio weight, or score aggregation.
  • Do not generate average or other aliases.