/wellmet/welford.py (d829bff1dd721bdb8bbbed9a53db73efac471dac) (2465 bytes) (mode 100644) (type blob)
#!/usr/bin/env python
# coding: utf-8
# One does not simply calculate the sample variance.
# https://github.com/numpy/numpy/issues/6231
# Only one citation from:
# "Online calculations happen all the time (due to resource limitations),
# and a np.welford function would be convenient so that not everyone
# has to implement welford on its own over and over again."
# There are actually some code can be found in internet,
# but those I've seen supposed one-by-one data addition.
"""
This is implementation of generalized (for arbitrary sample sizes)
Welford's updating (online) algorithm, given by Chan et al. in two papers:
"Algorithms for computing the sample variance: Analysis and recommendations"
https://doi.org/10.1080%2F00031305.1983.10483115
and
"Updating Formulae and a Pairwise Algorithm for Computing Sample Variances."
http://i.stanford.edu/pub/cstr/reports/cs/tr/79/773/CS-TR-79-773.pdf
"""
import numpy as np
class Welford:
def __init__(self):
self.n = 0
def add(self, data):
n = len(data)
T = np.sum(data)
#č takhle je to numericky nejstabilnější
S = np.sum(np.square(data - T/n))
if self.n == 0:
self.n = n
self.T = T
self.S = S
else:
m = self.n #č to, co bylo
self.S += S + m /n /(m+n) * (n/m * self.T - T)**2
self.n += n
self.T += T
#č Pro řídká data, speciálita pro IS
#č uděláme explicitnou funkci s povinným sample size
def add_sparse(self, data, n):
"""Method processes sparse data, assumes
only non-zero values of entire sample (of size n) need to be given"""
sample_size = len(data)
assert n >= sample_size
T = np.sum(data)
mean = T/n
#č takhle je to numericky nejstabilnější
S = np.sum(np.square(data - mean)) + (n - sample_size) * mean**2
if self.n == 0:
self.n = n
self.T = T
self.S = S
else:
m = self.n #č to, co bylo
self.S += S + m /n /(m+n) * (n/m * self.T - T)**2
self.n += n
self.T += T
@property
def mean(self):
return self.T / self.n
@property
def var(self):
return self.S / self.n
@property
def s2(self):
return self.S / (self.n - 1)
Mode |
Type |
Size |
Ref |
File |
100644 |
blob |
26 |
aed04ad7c97da717e759111aa8dd7cd48768647f |
.gitignore |
100644 |
blob |
1093 |
263306d87c51114b1320be2ee3277ea0bff99b1f |
LICENSE |
100644 |
blob |
5165 |
c9a2ecc2110771d29b800aee6152fd3a3d239e80 |
README.md |
100644 |
blob |
2084 |
6cd17c9e68ac9734b1881157c553856bd2e034de |
cli_example.py |
100644 |
blob |
1257 |
52ad8257fd62a3dc12f8d08eaf73a7cfb5d392b8 |
gui_example.py |
100644 |
blob |
81 |
fed528d4a7a148fd0bf0b0198a6461f8c91b87e9 |
pyproject.toml |
100644 |
blob |
795 |
7f9286ab2094e7dddfb6e1c5e49396fa7d79e67c |
setup.cfg |
100644 |
blob |
54 |
ee2a480d94ead7579fdddabda39a672e31b90ced |
setup.py |
040000 |
tree |
- |
c71333f92448098d44613a5de568eb3f3788abbe |
wellmet |
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"
Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/iam-git/WellMet
Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/iam-git/WellMet
Clone this repository using git:
git clone git://git.rocketgit.com/user/iam-git/WellMet
You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a
merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main