Skip to content

Var

Running variance using Welford's algorithm.

Parameters

  • ddof

    Default1

    Delta Degrees of Freedom. The divisor used in calculations is n - ddof, where n represents the number of seen elements.

Attributes

  • mean

    It is necessary to calculate the mean of the data in order to calculate its variance.

Examples

from river import stats

X = [3, 5, 4, 7, 10, 12]

var = stats.Var()
for x in X:
    var.update(x)
    print(var.get())
0.0
2.0
1.0
2.916666
7.7
12.56666

You can measure a rolling variance by using a utils.Rolling wrapper:

from river import utils

X = [1, 4, 2, -4, -8, 0]
rvar = utils.Rolling(stats.Var(ddof=1), window_size=3)
for x in X:
    rvar.update(x)
    print(rvar.get())
0.0
4.5
2.333333
17.333333
25.333333
16.0

Methods

get

Return the current value of the statistic.

revert
update

Update the called instance.

Parameters

  • xnumbers.Number
  • w — defaults to 1.0

update_many

Notes

The outcomes of the incremental and parallel updates are consistent with numpy's batch processing when \(\text{ddof} \le 1\).

References