Skip to content

adaptive.AverageLearner1D

AverageLearner1D

AverageLearner1D(function: Callable[[tuple[int, Real]], Real], bounds: tuple[Real, Real], loss_per_interval: None | Callable[[Sequence[Real], Sequence[Real]], float] = None, delta: float = 0.2, alpha: float = 0.005, neighbor_sampling: float = 0.3, min_samples: int = 50, max_samples: int = maxsize, min_error: float = 0)

Bases: Learner1D

Learns and predicts a noisy function 'f:ℝ → ℝ'.

Parameters:

Name Type Description Default
function callable

The function to learn. Must take a tuple of (seed, x) and return a real number.

required
bounds pair of reals

The bounds of the interval on which to learn 'function'.

required
loss_per_interval None | Callable[[Sequence[Real], Sequence[Real]], float]

A function that returns the loss for a single interval of the domain. If not provided, then a default is used, which uses the scaled distance in the x-y plane as the loss. See the notes for more details of adaptive.Learner1D for more details.

None
delta float

This parameter controls the resampling condition. A point is resampled if its uncertainty is larger than delta times the smallest neighboring interval. We strongly recommend 0 < delta <= 1.

0.2
alpha float(0 < alpha < 1)

The true value of the function at x is within the confidence interval [self.data[x] - self.error[x], self.data[x] + self.error[x]] with probability 1-2*alpha. We recommend to keep alpha=0.005.

0.005
neighbor_sampling float(0 < neighbor_sampling <= 1)

Each new point is initially sampled at least a (neighbor_sampling*100)% of the average number of samples of its neighbors.

0.3
min_samples int(min_samples > 0)

Minimum number of samples at each point x. Each new point is initially sampled at least min_samples times.

50
max_samples int(min_samples < max_samples)

Maximum number of samples at each point x.

np.inf
min_error float(min_error >= 0)

Minimum size of the confidence intervals. The true value of the function at x is within the confidence interval [self.data[x] - self.error[x], self.data[x] + self.error[x]] with probability 1-2*alpha. If self.error[x] < min_error, then x will not be resampled anymore, i.e., the smallest confidence interval at x is [self.data[x] - min_error, self.data[x] + min_error].

0
Source code in adaptive/learner/average_learner1D.py
def __init__(
    self,
    function: Callable[[tuple[int, Real]], Real],
    bounds: tuple[Real, Real],
    loss_per_interval: None
    | (Callable[[Sequence[Real], Sequence[Real]], float]) = None,
    delta: float = 0.2,
    alpha: float = 0.005,
    neighbor_sampling: float = 0.3,
    min_samples: int = 50,
    max_samples: int = sys.maxsize,
    min_error: float = 0,
):
    if not (0 < delta <= 1):
        raise ValueError("Learner requires 0 < delta <= 1.")
    if not (0 < alpha <= 1):
        raise ValueError("Learner requires 0 < alpha <= 1.")
    if not (0 < neighbor_sampling <= 1):
        raise ValueError("Learner requires 0 < neighbor_sampling <= 1.")
    if min_samples < 0:
        raise ValueError("min_samples should be positive.")
    if min_samples > max_samples:
        raise ValueError("max_samples should be larger than min_samples.")

    super().__init__(function, bounds, loss_per_interval)  # type: ignore[arg-type]

    self.delta = delta
    self.alpha = alpha
    self.min_samples = min_samples
    self.min_error = min_error
    self.max_samples = max_samples
    self.neighbor_sampling = neighbor_sampling

    # Contains all samples f(x) for each
    # point x in the form {x0: {0: f_0(x0), 1: f_1(x0), ...}, ...}
    self._data_samples: SortedDict[float, dict[int, Real]] = SortedDict()
    # Contains the number of samples taken
    # at each point x in the form {x0: n0, x1: n1, ...}
    self._number_samples = SortedDict()
    # This set contains the points x that have less than min_samples
    # samples or less than a (neighbor_sampling*100)% of their neighbors
    self._undersampled_points: set[Real] = set()
    # Contains the error in the estimate of the
    # mean at each point x in the form {x0: error(x0), ...}
    self.error: dict[Real, float] = decreasing_dict()
    #  Distance between two neighboring points in the
    # form {xi: ((xii-xi)^2 + (yii-yi)^2)^0.5, ...}
    self._distances: dict[Real, float] = decreasing_dict()
    # {xii: error[xii]/min(_distances[xi], _distances[xii], ...}
    self.rescaled_error: ItemSortedDict[Real, float] = decreasing_dict()

delta instance-attribute

delta = delta

alpha instance-attribute

alpha = alpha

min_samples instance-attribute

min_samples = min_samples

min_error instance-attribute

min_error = min_error

max_samples instance-attribute

max_samples = max_samples

neighbor_sampling instance-attribute

neighbor_sampling = neighbor_sampling

error instance-attribute

error: dict[Real, float] = decreasing_dict()

rescaled_error instance-attribute

rescaled_error: ItemSortedDict[Real, float] = decreasing_dict()

nsamples property

nsamples: int

Returns the total number of samples

min_samples_per_point property

min_samples_per_point: int

new

Create a copy of AverageLearner1D without the data.

Source code in adaptive/learner/average_learner1D.py
def new(self) -> AverageLearner1D:
    """Create a copy of `~adaptive.AverageLearner1D` without the data."""
    return AverageLearner1D(
        self.function,
        self.bounds,
        self.loss_per_interval,  # type: ignore[arg-type]
        self.delta,
        self.alpha,
        self.neighbor_sampling,
        self.min_samples,
        self.max_samples,
        self.min_error,
    )

to_numpy

to_numpy(mean: bool = False) -> ndarray
Source code in adaptive/learner/average_learner1D.py
def to_numpy(self, mean: bool = False) -> np.ndarray:
    if mean:
        return super().to_numpy()
    else:
        return np.array(
            [
                (seed, x, *np.atleast_1d(y))
                for x, seed_y in self._data_samples.items()
                for seed, y in seed_y.items()
            ]
        )

to_dataframe

to_dataframe(mean: bool = False, with_default_function_args: bool = True, function_prefix: str = 'function.', seed_name: str = 'seed', x_name: str = 'x', y_name: str = 'y') -> DataFrame

Return the data as a pandas.DataFrame.

Parameters:

Name Type Description Default
with_default_function_args bool

Include the learner.function's default arguments as a column, by default True

True
function_prefix str

Prefix to the learner.function's default arguments' names, by default "function."

'function.'
seed_name str

Name of the seed parameter, by default "seed"

'seed'
x_name str

Name of the x parameter, by default "x"

'x'
y_name str

Name of the output value, by default "y"

'y'

Returns:

Type Description
DataFrame

Raises:

Type Description
ImportError

If pandas is not installed.

Source code in adaptive/learner/average_learner1D.py
def to_dataframe(  # type: ignore[override]
    self,
    mean: bool = False,
    with_default_function_args: bool = True,
    function_prefix: str = "function.",
    seed_name: str = "seed",
    x_name: str = "x",
    y_name: str = "y",
) -> pandas.DataFrame:
    """Return the data as a `pandas.DataFrame`.

    Parameters
    ----------
    with_default_function_args : bool, optional
        Include the ``learner.function``'s default arguments as a
        column, by default True
    function_prefix : str, optional
        Prefix to the ``learner.function``'s default arguments' names,
        by default "function."
    seed_name : str, optional
        Name of the ``seed`` parameter, by default "seed"
    x_name : str, optional
        Name of the ``x`` parameter, by default "x"
    y_name : str, optional
        Name of the output value, by default "y"

    Returns
    -------
    pandas.DataFrame

    Raises
    ------
    ImportError
        If `pandas` is not installed.
    """
    if not with_pandas:
        raise ImportError("pandas is not installed.")
    if mean:
        data: list[tuple[Real, Real]] = sorted(self.data.items())
        columns = [x_name, y_name]
    else:
        data: list[tuple[int, Real, Real]] = [  # type: ignore[no-redef]
            (seed, x, y)
            for x, seed_y in sorted(self._data_samples.items())
            for seed, y in sorted(seed_y.items())
        ]
        columns = [seed_name, x_name, y_name]
    df = pandas.DataFrame(data, columns=columns)
    df.attrs["inputs"] = [seed_name, x_name]
    df.attrs["output"] = y_name
    if with_default_function_args:
        assign_defaults(self.function, df, function_prefix)
    return df

load_dataframe

load_dataframe(df: DataFrame, with_default_function_args: bool = True, function_prefix: str = 'function.', seed_name: str = 'seed', x_name: str = 'x', y_name: str = 'y')

Load data from a pandas.DataFrame.

If with_default_function_args is True, then learner.function's default arguments are set (using functools.partial) from the values in the pandas.DataFrame.

Parameters:

Name Type Description Default
df DataFrame

The data to load.

required
with_default_function_args bool

The with_default_function_args used in to_dataframe(), by default True

True
function_prefix str

The function_prefix used in to_dataframe, by default "function."

'function.'
seed_name str

The seed_name used in to_dataframe, by default "seed"

'seed'
x_name str

The x_name used in to_dataframe, by default "x"

'x'
y_name str

The y_name used in to_dataframe, by default "y"

'y'
Source code in adaptive/learner/average_learner1D.py
def load_dataframe(  # type: ignore[override]
    self,
    df: pandas.DataFrame,
    with_default_function_args: bool = True,
    function_prefix: str = "function.",
    seed_name: str = "seed",
    x_name: str = "x",
    y_name: str = "y",
):
    """Load data from a `pandas.DataFrame`.

    If ``with_default_function_args`` is True, then ``learner.function``'s
    default arguments are set (using `functools.partial`) from the values
    in the `pandas.DataFrame`.

    Parameters
    ----------
    df : pandas.DataFrame
        The data to load.
    with_default_function_args : bool, optional
        The ``with_default_function_args`` used in ``to_dataframe()``,
        by default True
    function_prefix : str, optional
        The ``function_prefix`` used in ``to_dataframe``, by default "function."
    seed_name : str, optional
        The ``seed_name`` used in ``to_dataframe``, by default "seed"
    x_name : str, optional
        The ``x_name`` used in ``to_dataframe``, by default "x"
    y_name : str, optional
        The ``y_name`` used in ``to_dataframe``, by default "y"
    """
    # Were using zip instead of df[[seed_name, x_name]].values because that will
    # make the seeds into floats
    seed_x = list(zip(df[seed_name].values.tolist(), df[x_name].values.tolist()))
    self.tell_many(seed_x, df[y_name].values)
    if with_default_function_args:
        self.function = partial_function_from_dataframe(
            self.function, df, function_prefix
        )

ask

ask(n: int, tell_pending: bool = True) -> tuple[Points, list[float]]

Return 'n' points that are expected to maximally reduce the loss.

Source code in adaptive/learner/average_learner1D.py
def ask(self, n: int, tell_pending: bool = True) -> tuple[Points, list[float]]:  # type: ignore[override]
    """Return 'n' points that are expected to maximally reduce the loss."""
    # If some point is undersampled, resample it
    if len(self._undersampled_points):
        x = next(iter(self._undersampled_points))
        points, loss_improvements = self._ask_for_more_samples(x, n)
    # If less than 2 points were sampled, sample a new one
    elif len(self.data) <= 1:
        # TODO: if `n` is very large, we should suggest a few different points.
        points, loss_improvements = self._ask_for_new_point(n)
    #  Else, check the resampling condition
    else:
        if len(self.rescaled_error):
            # This is in case rescaled_error is empty (e.g. when sigma=0)
            x, resc_error = self.rescaled_error.peekitem(0)
            # Resampling condition
            if resc_error > self.delta:
                points, loss_improvements = self._ask_for_more_samples(x, n)
            else:
                points, loss_improvements = self._ask_for_new_point(n)
        else:
            points, loss_improvements = self._ask_for_new_point(n)

    if tell_pending:
        for p in points:
            self.tell_pending(p)

    return points, loss_improvements

tell_pending

tell_pending(seed_x: Point) -> None
Source code in adaptive/learner/average_learner1D.py
def tell_pending(self, seed_x: Point) -> None:  # type: ignore[override]
    _, x = seed_x
    self.pending_points.add(seed_x)
    if x not in self.data:
        self._update_neighbors(x, self.neighbors_combined)
        self._update_losses(x, real=False)

tell

tell(seed_x: Point, y: Real) -> None
Source code in adaptive/learner/average_learner1D.py
def tell(self, seed_x: Point, y: Real) -> None:  # type: ignore[override]
    seed, x = seed_x
    if y is None:
        raise TypeError(
            "Y-value may not be None, use learner.tell_pending(x)"
            "to indicate that this value is currently being calculated"
        )

    if x not in self.data:
        self._update_data(x, y, "new")
        self._update_data_structures(seed_x, y, "new")
    elif seed not in self._data_samples[x]:  # check if the seed is new
        self._update_data(x, y, "resampled")
        self._update_data_structures(seed_x, y, "resampled")
    self.pending_points.discard(seed_x)

tell_many

tell_many(xs: Points | ndarray, ys: Sequence[Real] | ndarray) -> None
Source code in adaptive/learner/average_learner1D.py
def tell_many(  # type: ignore[override]
    self, xs: Points | np.ndarray, ys: Sequence[Real] | np.ndarray
) -> None:
    # Check that all x are within the bounds
    # TODO: remove this requirement, all other learners add the data
    # but ignore it going forward.
    if not np.prod([x >= self.bounds[0] and x <= self.bounds[1] for _, x in xs]):
        raise ValueError(
            "x value out of bounds, remove x or enlarge the bounds of the learner"
        )

    # Create a mapping of points to a list of samples
    mapping: defaultdict[Real, defaultdict[Int, Real]] = defaultdict(
        lambda: defaultdict(dict)
    )
    for (seed, x), y in zip(xs, ys):
        mapping[x][seed] = y

    for x, seed_y_mapping in mapping.items():
        if len(seed_y_mapping) == 1:
            seed, y = list(seed_y_mapping.items())[0]
            self.tell((seed, x), y)
        elif len(seed_y_mapping) > 1:
            # If we stored more than 1 y-value for the previous x,
            # use a more efficient routine to tell many samples
            # simultaneously, before we move on to a new x
            self.tell_many_at_point(x, seed_y_mapping)

tell_many_at_point

tell_many_at_point(x: Real, seed_y_mapping: dict[int, Real]) -> None

Tell the learner about many samples at a certain location x.

Parameters:

Name Type Description Default
x float

Value from the function domain.

required
seed_y_mapping Dict[int, Real]

Dictionary of seed -> y at x.

required
Source code in adaptive/learner/average_learner1D.py
def tell_many_at_point(self, x: Real, seed_y_mapping: dict[int, Real]) -> None:
    """Tell the learner about many samples at a certain location x.

    Parameters
    ----------
    x : float
        Value from the function domain.
    seed_y_mapping : Dict[int, Real]
        Dictionary of ``seed`` -> ``y`` at ``x``.
    """
    # Check x is within the bounds
    if not np.prod(x >= self.bounds[0] and x <= self.bounds[1]):
        raise ValueError(
            "x value out of bounds, remove x or enlarge the bounds of the learner"
        )

    # If x is a new point:
    if x not in self.data:
        # we make a copy because we don't want to modify the original dict
        seed_y_mapping = seed_y_mapping.copy()
        seed = next(iter(seed_y_mapping))
        y = seed_y_mapping.pop(seed)
        self._update_data(x, y, "new")
        self._update_data_structures((seed, x), y, "new")

    ys = np.array(list(seed_y_mapping.values()))

    # If x is not a new point or if there were more than 1 sample in ys:
    if len(ys) > 0:
        self._data_samples[x].update(seed_y_mapping)
        n = len(ys) + self._number_samples[x]
        self.data[x] = (
            np.mean(ys) * len(ys) + self.data[x] * self._number_samples[x]
        ) / n
        self._number_samples[x] = n
        # `self._update_data(x, y, "new")` included the point
        # in _undersampled_points. We remove it if there are
        # more than min_samples samples, disregarding neighbor_sampling.
        if n > self.min_samples:
            self._undersampled_points.discard(x)
        self.error[x] = self._calc_error_in_mean(
            self._data_samples[x].values(), self.data[x], n
        )
        self._update_distances(x)
        self._update_rescaled_error_in_mean(x, "resampled")
        if self.error[x] <= self.min_error or n >= self.max_samples:
            self.rescaled_error.pop(x, None)
        self._update_scale(x, min(self._data_samples[x].values()))
        self._update_scale(x, max(self._data_samples[x].values()))
        self._update_losses_resampling(x, real=True)
        if self._scale[1] > self._recompute_losses_factor * self._oldscale[1]:
            for interval in reversed(self.losses):
                self._update_interpolated_loss_in_interval(*interval)
            self._oldscale = deepcopy(self._scale)

plot

plot()

Returns a plot of the evaluated data with error bars.

This is only implemented for scalar functions, i.e., it requires vdim=1.

Returns:

Name Type Description
plot `holoviews.element.Scatter * holoviews.element.ErrorBars *
holoviews.element.Path`

Plot of the evaluated data.

Source code in adaptive/learner/average_learner1D.py
def plot(self):
    """Returns a plot of the evaluated data with error bars.

    This is only implemented for scalar functions, i.e., it requires ``vdim=1``.

    Returns
    -------
    plot : `holoviews.element.Scatter * holoviews.element.ErrorBars *
            holoviews.element.Path`
        Plot of the evaluated data.
    """
    hv = ensure_holoviews()
    if not self.data:
        p = hv.Scatter([]) * hv.ErrorBars([]) * hv.Path([])
    elif not self.vdim > 1:
        xs, ys = zip(*sorted(self.data.items()))
        scatter = hv.Scatter(self.data)
        error = hv.ErrorBars([(x, self.data[x], self.error[x]) for x in self.data])
        line = hv.Path((xs, ys))
        p = scatter * error * line
    else:
        raise Exception("plot() not implemented for vector functions.")

    # Plot with 5% empty margins such that the boundary points are visible
    margin = 0.05 * (self.bounds[1] - self.bounds[0])
    plot_bounds = (self.bounds[0] - margin, self.bounds[1] + margin)

    return p.redim(x={"range": plot_bounds})