Skip to content

Commit 315b57a

Browse files
committed
unumpy.average: init
Ref: #38
1 parent 9a4c275 commit 315b57a

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

CHANGES.rst

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Unreleased
77
Fixes:
88

99
- fix `readthedocs` configuration so that the build passes (#254)
10+
- Add ``unumpy.average`` to calculate uncertainties aware average (#38)
1011

1112
3.2.2 2024-July-08
1213
-----------------------

doc/numpy_guide.rst

+16
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ functions is available in the documentation for :mod:`uncertainties.umath`.
144144
.. index::
145145
pair: testing and operations (in arrays); NaN
146146

147+
Uncertainties aware average
148+
---------------------------
149+
150+
If you have measured a certain value multiple times, with a different uncertainty every measurement. Averaging over the results in a manner aware of the different uncertainties, is not trivial. The function ``unumpy.average()`` does that:
151+
152+
>>> measurements = numpy.array([2.1, 2.0, 2.05, 2.08, 2.02])
153+
>>> stds = numpy.array([0.05, 0.03, 0.04, 0.06, 0.05])
154+
>>> arr = unumpy.uarray(measurements, stds)
155+
>>> unumpy.average(arr)
156+
2.03606+/-0.00018
157+
158+
Note how that function gives a value different from numpy's ``mean`` function:
159+
160+
>>> numpy.mean(arr)
161+
2.050+/-0.021
162+
147163
NaN testing and NaN-aware operations
148164
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
149165

tests/test_unumpy.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
try:
24
import numpy
35
except ImportError:
@@ -300,3 +302,9 @@ def test_array_comparisons():
300302
# For matrices, 1D arrays are converted to 2D arrays:
301303
mat = unumpy.umatrix([1, 2], [1, 4])
302304
assert numpy.all((mat == [mat[0, 0], 4]) == [True, False])
305+
306+
307+
def test_average_type_check():
308+
arr = numpy.array(["bla"])
309+
with pytest.raises(ValueError):
310+
unumpy.average(arr)

uncertainties/unumpy/core.py

+20
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,30 @@
3030
# Utilities:
3131
"nominal_values",
3232
"std_devs",
33+
"average",
3334
# Classes:
3435
"matrix",
3536
]
3637

38+
39+
def average(arr, axis=None):
40+
"""
41+
Return a weighted averaged along with a weighted mean over a certain axis.
42+
43+
By default, operates on all axes of the given array.
44+
"""
45+
if arr.dtype != numpy.dtype("object") or (
46+
not isinstance(arr.flat[0], uncert_core.Variable)
47+
):
48+
raise ValueError(
49+
"unumpy.average is meant to operate only upon numpy arrays of ufloats."
50+
)
51+
weights = std_devs(arr) ** -2
52+
weights_sum = weights.sum(axis=axis)
53+
weighted_mean = (nominal_values(arr) * weights).sum(axis=axis) / weights_sum
54+
return uarray(weighted_mean, weights_sum**-1 / 2)
55+
56+
3757
###############################################################################
3858
# Utilities:
3959

0 commit comments

Comments
 (0)