Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AverageLearner: implement min_npoints #274

Merged
merged 7 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions adaptive/learner/average_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class AverageLearner(BaseLearner):
Desired absolute tolerance.
rtol : float
Desired relative tolerance.
min_npoints : int
Minimum number of points to sample.

Attributes
----------
Expand All @@ -30,7 +32,7 @@ class AverageLearner(BaseLearner):
Number of evaluated points.
"""

def __init__(self, function, atol=None, rtol=None):
def __init__(self, function, atol=None, rtol=None, min_npoints=2):
if atol is None and rtol is None:
raise Exception("At least one of `atol` and `rtol` should be set.")
if atol is None:
Expand All @@ -44,6 +46,8 @@ def __init__(self, function, atol=None, rtol=None):
self.atol = atol
self.rtol = rtol
self.npoints = 0
# Cannot estimate standard deviation with fewer than 2 points.
self.min_npoints = max(min_npoints, 2)
self.sum_f = 0
self.sum_f_sq = 0

Expand Down Expand Up @@ -92,7 +96,7 @@ def std(self):
"""The corrected sample standard deviation of the values
in `data`."""
n = self.npoints
if n < 2:
if n < self.min_npoints:
return np.inf
numerator = self.sum_f_sq - n * self.mean ** 2
if numerator < 0:
Expand All @@ -106,7 +110,7 @@ def loss(self, real=True, *, n=None):
n = self.npoints if real else self.n_requested
else:
n = n
if n < 2:
if n < self.min_npoints:
return np.inf
standard_error = self.std / sqrt(n)
return max(
Expand Down Expand Up @@ -150,10 +154,11 @@ def __getstate__(self):
self.function,
self.atol,
self.rtol,
self.min_npoints,
self._get_data(),
)

def __setstate__(self, state):
function, atol, rtol, data = state
self.__init__(function, atol, rtol)
function, atol, rtol, min_npoints, data = state
self.__init__(function, atol, rtol, min_npoints)
self._set_data(data)
13 changes: 13 additions & 0 deletions adaptive/tests/test_average_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np

from adaptive.learner import AverageLearner
from adaptive.runner import simple


def test_only_returns_new_points():
Expand Down Expand Up @@ -46,3 +47,15 @@ def test_avg_std_and_npoints():
assert learner.npoints == len(learner.data)
assert abs(learner.sum_f - values.sum()) < 1e-13
assert abs(learner.std - std) < 1e-13


def test_min_npoints():
basnijholt marked this conversation as resolved.
Show resolved Hide resolved
def constant_function(seed):
return 0.1

for min_npoints in [1, 2, 3]:
learner = AverageLearner(
constant_function, atol=0.01, rtol=0.01, min_npoints=min_npoints
)
simple(learner, lambda l: l.loss() < 1)
assert learner.npoints >= max(2, min_npoints)