online_mean
===========
Online algorithm for the mean, variance, and covariance.
The `online_mean` package contains a single function `add_sample` which updates
the array for the sample mean, as well as optionally the sample variance and the
sample covariance matrix.
The package has no dependencies, as the `add_sample` function works with any
input data type that supports in-place addition and fancy slicing.
Usage
-----
```py
import numpy as np
# the only function in the package
from online_mean import add_sample
# 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])
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])
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]])
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]]
```
Raw data
{
"_id": null,
"home_page": "",
"name": "online-mean",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "online algorithm,online mean,online variance,online covariance,online statistics",
"author": "",
"author_email": "Nicolas Tessore <n.tessore@ucl.ac.uk>",
"download_url": "https://files.pythonhosted.org/packages/bd/6c/020de56c82a64264f9bbf7001570e37370e9649dec8c1affd381df605273/online_mean-2023.1.1.tar.gz",
"platform": null,
"description": "online_mean\n===========\n\nOnline algorithm for the mean, variance, and covariance.\n\nThe `online_mean` package contains a single function `add_sample` which updates\nthe array for the sample mean, as well as optionally the sample variance and the\nsample covariance matrix.\n\nThe package has no dependencies, as the `add_sample` function works with any\ninput data type that supports in-place addition and fancy slicing.\n\nUsage\n-----\n\n```py\nimport numpy as np\n\n# the only function in the package\nfrom online_mean import add_sample\n\n# online algorithm for the mean\n# start from zero\nmu = np.zeros(4)\n\n# generate samples and compute their mean\nfor i in range(1000):\n x = np.random.normal([0.1, 0.3, 0.5, 0.7])\n add_sample(i, x, mu)\n\n# the mean is computed in place\nprint(mu) # [0.08804402 0.25896929 0.44891264 0.73418769]\n\n# compute the variance\nmu = np.zeros(4)\nvar = np.zeros(4)\nfor i in range(1000):\n x = np.random.normal([0.1, 0.3, 0.5, 0.7], [0.2, 0.4, 0.6, 0.8])\n add_sample(i, x, mu, var=var)\n\nprint(mu) # [0.09854301 0.29509305 0.4777673 0.70008311]\nprint(var**0.5) # [0.19900518 0.4012857 0.59267129 0.81856542]\n\n# compute the covariance matrix\nmu = np.zeros(4)\ncov = np.zeros((4, 4))\nfor i in range(100_000):\n x = np.random.multivariate_normal([0.1, 0.3, 0.5, 0.7],\n [[0.2, 0.02, 0.04, 0.06],\n [0.02, 0.4, 0.06, 0.08],\n [0.04, 0.06, 0.6, 0.10],\n [0.06, 0.08, 0.10, 0.8]])\n add_sample(i, x, mu, cov=cov)\n\nprint(mu) # [0.10095607 0.30486108 0.50113141 0.69912377]\nprint(cov)\n# [[0.20101406 0.02105503 0.0382198 0.06220174]\n# [0.02105503 0.39909545 0.06192678 0.0791239 ]\n# [0.0382198 0.06192678 0.59960537 0.1082596 ]\n# [0.06220174 0.0791239 0.1082596 0.80071002]]\n\n```\n\n",
"bugtrack_url": null,
"license": "",
"summary": "online algorithm for mean, variance, and covariance",
"version": "2023.1.1",
"split_keywords": [
"online algorithm",
"online mean",
"online variance",
"online covariance",
"online statistics"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "31287c0f4d18a4c6e2e80e214ad3435b8f7742f1439240cd51ea09337c3efe60",
"md5": "6f5f5904043f1d9580bd75b661292b67",
"sha256": "3eb6c4bd8eded16ac4a70fc79c06a1e1c47fa3643b0db86882c99510c93e366f"
},
"downloads": -1,
"filename": "online_mean-2023.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6f5f5904043f1d9580bd75b661292b67",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 2937,
"upload_time": "2023-01-20T09:22:49",
"upload_time_iso_8601": "2023-01-20T09:22:49.797066Z",
"url": "https://files.pythonhosted.org/packages/31/28/7c0f4d18a4c6e2e80e214ad3435b8f7742f1439240cd51ea09337c3efe60/online_mean-2023.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "bd6c020de56c82a64264f9bbf7001570e37370e9649dec8c1affd381df605273",
"md5": "7b73ee6a26092ce16bcfee498eca5dc2",
"sha256": "ccd08ea336e0dc785ca5401e14d7ff18e06c294f77fc98c17564eabb196cdf9c"
},
"downloads": -1,
"filename": "online_mean-2023.1.1.tar.gz",
"has_sig": false,
"md5_digest": "7b73ee6a26092ce16bcfee498eca5dc2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 2393,
"upload_time": "2023-01-20T09:22:50",
"upload_time_iso_8601": "2023-01-20T09:22:50.808366Z",
"url": "https://files.pythonhosted.org/packages/bd/6c/020de56c82a64264f9bbf7001570e37370e9649dec8c1affd381df605273/online_mean-2023.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-20 09:22:50",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "online-mean"
}