Overview

Purpose

Compute the variance of x, supporting both the unweighted and weighted cases.

Parameters

  • x: numeric data.

  • w: optional weights.

  • ddof: correction applied to the denominator. In the unweighted case the denominator is len(x) - ddof; in the weighted case it is sum(w) - ddof.

Returns

A float with the variance.

Errors and exceptions

  • ValueError if x is empty or not finite.

  • ValueError if w is invalid.

  • ValueError if the denominator becomes less than or equal to zero.

Example

from mespy import variance

x = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]

print(variance(x))          # 4.0
print(variance(x, ddof=1))  # correzione di Bessel

Notes

  • The default ddof=0 is consistent with the descriptive convention used throughout the package.

  • The weighted version uses the internal weighted mean, not numpy.var.