Skip to content

Commit a72d4a0

Browse files
authored
Merge pull request #276 from python-adaptive/bug/zero-mean
AverageLearner: fix zero mean
2 parents 4e34aba + 9a3f8b9 commit a72d4a0

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

adaptive/learner/average_learner.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ def loss(self, real=True, *, n=None):
118118
if n < self.min_npoints:
119119
return np.inf
120120
standard_error = self.std / sqrt(n)
121-
return max(
122-
standard_error / self.atol, standard_error / abs(self.mean) / self.rtol
123-
)
121+
aloss = standard_error / self.atol
122+
rloss = standard_error / self.rtol
123+
if self.mean != 0:
124+
rloss /= abs(self.mean)
125+
return max(aloss, rloss)
124126

125127
def _loss_improvement(self, n):
126128
loss = self.loss()

adaptive/tests/test_average_learner.py

+8
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@ def constant_function(seed):
5959
)
6060
simple(learner, lambda l: l.loss() < 1)
6161
assert learner.npoints >= max(2, min_npoints)
62+
63+
64+
def test_zero_mean():
65+
# see https://github.com/python-adaptive/adaptive/issues/275
66+
learner = AverageLearner(None, rtol=0.01)
67+
learner.tell(0, -1)
68+
learner.tell(1, 1)
69+
learner.loss()

0 commit comments

Comments
 (0)