Last active
March 26, 2018 18:14
-
-
Save dmolesUC/a5163e99715f56c98e3253543ea36017 to your computer and use it in GitHub Desktop.
Python script implementing "Combining Multiple Averaged Data Points and Their Errors" by Ken Tatebe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Tuple, List | |
"""A 'bin' of observations in the form (average, error, # of observations)""" | |
Bin = Tuple[float, float, int] | |
"""Based on http://isi.ssl.berkeley.edu/~tatebe/whitepapers/Combining%20Errors.pdf""" | |
def combine(a: Bin, b: Bin) -> Bin: | |
a_avg, a_err, n_a = a | |
b_avg, b_err, n_b = b | |
n = n_a + n_b | |
s_avg = ((n_a / n) * a_avg) + ((n_b / n) * b_avg) | |
N = n ** 2 - n | |
N_a = n_a ** 2 - n_a | |
N_b = n_b ** 2 - n_b | |
s_err = ((N_a / N) * (a_err ** 2) | |
+ (N_b / N) * (b_err ** 2) | |
+ (n_a * n_b * (a_avg - b_avg) ** 2) / (n * N) | |
) ** .5 | |
return (s_avg, s_err, n) | |
def combine_all(x: List[Bin]) -> Bin: | |
if (len(x) == 1): | |
return x[0] | |
if (len(x) == 2): | |
return combine(x[0], x[1]) | |
if (len(x) > 2): | |
return combine(x[0], combine_all(x[1:])) | |
else: | |
raise ValueError("Can't combine without any observations") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment