Skip to main content

Online algorithm for mean, variance, and covariance.

Project description

online-stats — Online algorithm for mean, variance, and covariance

The online-stats Python package contains a single function online_stats.add_sample() which updates a sample mean given a new sample, as well as optionally the sample variance and the sample covariance matrix.

The package has no dependencies, as the online_stats.add_sample() function works with any input data type that supports in-place addition and fancy slicing.

Usage

>>> import numpy as np
>>>
>>> # the package
>>> import online_stats
>>>
>>> # online algorithm for the mean
>>> # start from zero
>>> mu = np.zeros(4)
>>>
>>> # generate samples and compute their mean
>>> for i in range(1000):
...     x = np.random.normal([0.1, 0.3, 0.5, 0.7])
...     online_stats.add_sample(i, x, mu)
...
>>> # the mean is computed in place
>>> print(mu)
[0.08804402 0.25896929 0.44891264 0.73418769]
>>>
>>> # compute the variance
>>> mu = np.zeros(4)
>>> var = np.zeros(4)
>>> for i in range(1000):
...     x = np.random.normal([0.1, 0.3, 0.5, 0.7], [0.2, 0.4, 0.6, 0.8])
...     online_stats.add_sample(i, x, mu, var=var)
...
>>> print(mu)
[0.09854301 0.29509305 0.4777673  0.70008311]
>>> print(var**0.5)
[0.19900518 0.4012857  0.59267129 0.81856542]
>>>
>>> # compute the covariance matrix
>>> mu = np.zeros(4)
>>> cov = np.zeros((4, 4))
>>> for i in range(100_000):
...     x = np.random.multivariate_normal([0.1, 0.3, 0.5, 0.7],
...             [[0.2, 0.02, 0.04, 0.06],
...              [0.02, 0.4, 0.06, 0.08],
...              [0.04, 0.06, 0.6, 0.10],
...              [0.06, 0.08, 0.10, 0.8]])
...     online_stats.add_sample(i, x, mu, cov=cov)
...
>>> print(mu)
[0.10095607 0.30486108 0.50113141 0.69912377]
>>> print(cov)
[[0.20101406 0.02105503 0.0382198  0.06220174]
 [0.02105503 0.39909545 0.06192678 0.0791239 ]
 [0.0382198  0.06192678 0.59960537 0.1082596 ]
 [0.06220174 0.0791239  0.1082596  0.80071002]]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

online_stats-2023.6.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

online_stats-2023.6-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file online_stats-2023.6.tar.gz.

File metadata

  • Download URL: online_stats-2023.6.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for online_stats-2023.6.tar.gz
Algorithm Hash digest
SHA256 fe824878afc4f20a05895142e1c3e5169b0962f607b9a7891a3747077cae46fe
MD5 bdccb91c6f0d175af8769975585657e1
BLAKE2b-256 70ee6f3c3095a1712b8cc31ce64404c22302049fe6b13c3d6ccfa32724125d00

See more details on using hashes here.

File details

Details for the file online_stats-2023.6-py3-none-any.whl.

File metadata

File hashes

Hashes for online_stats-2023.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5cf15b049fd6d1c2c93dd7ec31501c06922d93f7b16914a0c61ff6cf9068593a
MD5 a1b607d41f83f5ca69b514145eb8a129
BLAKE2b-256 7b8d44f8ee8aaf48d052674fd4e96407bd453550be8c71808f1d8251ea33b8ee

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page