Skip to content

cjrh/cjrh_math

Repository files navigation

image

image

image

image

image

image

image

cjrh_math

A collection of handy maths functions.

Streaming mean and variance

Sometimes it can be very useful to update an existing mean and variance with new data, rather than have to calculate it again over the whole set. The functions below implement one of the algorithms described in Wikipedia.

def update_mean(n: int, mean: float, value: float) -> float

Given a new value, calculate a new mean using the existing mean and the size of data, n, seen so far.

def update_pvariance(n: int, mean: float, var: float, value: float) -> float
def update_variance(n: int, mean: float, var: float, value: float) -> float

Given a new value, calculate a new variance using the existing mean, existing variance var, and the size of data, n, seen so far.

Note that update_variance() is for sample variance while update_pvariance() is for population variance.

All functions are pure and therefore thread-safe.

About

Handy math routines

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages