import numpy as np
def combine(averages, variances, counts, size=None):
"""
Combine averages and variances to one single average and variance.
# Arguments
averages: List of averages for each part.
variances: List of variances for each part.
counts: List of number of elements in each part.
size: Total number of elements in all of the parts.
# Returns
average: Average over all parts.
variance: Variance over all parts.
"""
average = np.average(averages, weights=counts)
# necessary for correct variance in case of multidimensional arrays
if size is not None:
counts = counts * size // np.sum(counts, dtype='int')
squares = (counts - 1) * variances + counts * (averages - average)**2
return average, np.sum(squares) / (size - 1)
import numpy as np
def combine(averages, variances, counts, size=None):
"""
Combine averages and variances to one single average and variance.
# Arguments
averages: List of averages for each part.
variances: List of variances for each part.
counts: List of number of elements in each part.
size: Total number of elements in all of the parts.
# Returns
average: Average over all parts.
variance: Variance over all parts.
"""
average = np.average(averages, weights=counts)
# necessary for correct variance in case of multidimensional arrays
if size is not None:
counts = counts * size // np.sum(counts, dtype='int')
squares = (counts - 1) * variances + counts * (averages - average)**2
return average, np.sum(squares) / (size - 1)
# sizes k_j and n
ks = np.random.poisson(10, 10)
n = np.sum(ks)
# create data
x = np.random.randn(n, 20)
parts = np.split(x, np.cumsum(ks[:-1]))
# compute statistics on parts
ms = [np.mean(p) for p in parts]
vs = [np.var(p, ddof=1) for p in parts]
# combine and compare
combined = combine(ms, vs, ks, x.size)
numpied = np.mean(x), np.var(x, ddof=1)
distance = np.abs(np.array(combined) - np.array(numpied))
print('combined --- mean:{: .9f} - var:{: .9f}'.format(*combined))
print('numpied --- mean:{: .9f} - var:{: .9f}'.format(*numpied))
print('distance --- mean:{: .5e} - var:{: .5e}'.format(*distance))
# sizes k_j and n
ks = np.random.poisson(10, 10)
n = np.sum(ks)
# create data
x = np.random.randn(n, 20)
parts = np.split(x, np.cumsum(ks[:-1]))
# compute statistics on parts
ms = [np.mean(p) for p in parts]
vs = [np.var(p, ddof=1) for p in parts]
# combine and compare
combined = combine(ms, vs, ks, x.size)
numpied = np.mean(x), np.var(x, ddof=1)
distance = np.abs(np.array(combined) - np.array(numpied))
print('combined --- mean:{: .9f} - var:{: .9f}'.format(*combined))
print('numpied --- mean:{: .9f} - var:{: .9f}'.format(*numpied))
print('distance --- mean:{: .5e} - var:{: .5e}'.format(*distance))