Skip to main content

Python (NumPy) implementation of Welford's algorithm with the ability to remove data points.

Project description

Welford-Remove

This library is a Python (Numpy) implementation of a modified Welford's algorithm, which is online and parallel algorithm for calculating variances. Typically, Welford's algorithm only allows for adding data points. This modification allows for removing data points.

Welford's algorithm is described in the following:

The modification for removing data points is described here:

Welford's original method is more numerically stable than the standard method as described in the following blog:

However, There has been no formal analysis on whether the modified version of the algorithm provided here is numerically stable, but based on the testing done in test_welford.test_remove, I have reason to believe it is.

This library is inspired by the jvf's implementation, which is implemented without using numpy library. In particular, this implementation is a fork of the implementation by a-mitani, * Implementation done by jvf: github.com/jvf/welford * Implementation done by a-mitani: github.com/a-mitani/welford

Install

Download package via PyPI repository

$ pip install welford-remove

Example

For Online Calculation

import numpy as np
from welford import Welford

# Initialize Welford object
w = Welford()

# Input data samples sequentially
w.add(np.array([0, 100]))
w.add(np.array([1, 110]))
w.add(np.array([2, 120]))

# output
print(w.mean)  # mean --> [1. 110.]
print(w.var_s)  # sample variance --> [1. 100.]
print(w.var_p)  # population variance --> [0.6666. 66.66.]

# You can add other samples after calculating variances.
w.add(np.array([3, 130]))
w.add(np.array([4, 140]))

# output with added samples
print(w.mean)  # mean --> [2. 120.]
print(w.var_s)  # sample variance --> [2.5. 250.]
print(w.var_p)  # population variance --> [2. 200.]

# You can remove samples after calculating variances.
w.remove(np.array([3, 130]))
w.remove(np.array([4, 140]))
print(w.mean)  # mean --> [1. 110.]
print(w.var_s)  # sample variance --> [1. 100.]
print(w.var_p)  # population variance --> [0.6666. 66.66.]

# You can also get the standard deviation
print(w.std_s)  # sample standard deviation --> [1. 10.]
print(w.std_p)  # population standard deviation --> [0.81649658. 8.16496581.]

Welford object supports initialization with data samples and batch addition of samples.

import numpy as np
from welford import Welford

# Initialize Welford object with samples.
ini = np.array([[0, 100], [1, 110], [2, 120]])
w = Welford(ini)

# output
print(w.mean)  # mean --> [1. 110.]
print(w.var_s)  # sample variance --> [1. 100.]
print(w.var_p)  # population variance --> [0.66666667. 66.66666667.]

# add other samples through batch method
other_samples = np.array([[3, 130], [4, 140]])
w.add_all(other_samples)

# output with added samples
print(w.mean)  # mean --> [2. 120.]
print(w.var_s)  # sample variance --> [2.5 250.]
print(w.var_p)  # population variance --> [2. 200.]

For Parallel Calculation

Welford also offers parallel calculation method for variance.

import numpy as np
from welford import Welford

# Initialize two Welford objects
w_1 = Welford()
w_2 = Welford()

# Each object will calculate variance of each samples in parallel.
# On w_1
w_1.add(np.array([0, 100]))
w_1.add(np.array([1, 110]))
w_1.add(np.array([2, 120]))
print(w_1.var_s)  # sample variance --> [1. 100.]
print(w_1.var_p)  # population variance --> [0.66666667. 66.66666667.]

# On w_2
w_2.add(np.array([3, 130]))
w_2.add(np.array([4, 140]))
print(w_2.var_s)  # sample variance --> [0.5 50.]
print(w_2.var_p)  # sample variance --> [0.25 25.]

# You can Merge objects to get variance of WHOLE samples
w_1.merge(w_2)
print(w.var_s)  # sample variance --> [2.5. 250.]
print(w_1.var_p)  # sample variance --> [2. 200.]

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

welford_remove-0.2.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

welford_remove-0.2.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file welford_remove-0.2.1.tar.gz.

File metadata

  • Download URL: welford_remove-0.2.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for welford_remove-0.2.1.tar.gz
Algorithm Hash digest
SHA256 aad2f045aca0bc487ba9f4e2e1e609a23a279f07deb73bee4cff2115822469d6
MD5 7fae257ffebe60297bedc7e33840834f
BLAKE2b-256 7954c01cada9245ccecb9015ac465796b59abaed69d0a3c97ebd65feaaa85e7b

See more details on using hashes here.

File details

Details for the file welford_remove-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: welford_remove-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for welford_remove-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 27b85150c420bbe6575e6ad886e1a1be33f4945b3f2daebb128f027134f9dc79
MD5 30913d3284149ba0c4d2b77248d50e5d
BLAKE2b-256 21df3181e6a4c6b3a79b7aef76e2f4a380b2614aaa7efab29e292a52ab330946

See more details on using hashes here.

Supported by

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