Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@uses_nth_neighbors(1)
def triangle_loss(simplex, values, value_scale, neighbors, neighbor_values):
"""
Computes the average of the volumes of the simplex combined with each
neighbouring point.
Parameters
----------
simplex : list of tuples
Each entry is one point of the simplex.
values : list of values
The scaled function values of each of the simplex points.
value_scale : float
The scale of values, where ``values = function_values * value_scale``.
neighbors : list of tuples
The neighboring points of the simplex, ordered such that simplex[0]
exacly opposes neighbors[0], etc.
@uses_nth_neighbors(1)
def curvature_loss(xs, ys):
xs_middle = xs[1:3]
ys_middle = ys[1:3]
triangle_loss_ = triangle_loss(xs, ys)
default_loss_ = default_loss(xs_middle, ys_middle)
dx = xs_middle[1] - xs_middle[0]
return (
area_factor * (triangle_loss_ ** 0.5)
+ euclid_factor * default_loss_
+ horizontal_factor * dx
)
@uses_nth_neighbors(0)
def uniform_loss(xs, ys):
"""Loss function that samples the domain uniformly.
Works with `~adaptive.Learner1D` only.
Examples
--------
>>> def f(x):
... return x**2
>>>
>>> learner = adaptive.Learner1D(f,
... bounds=(-1, 1),
... loss_per_interval=uniform_sampling_1d)
>>>
"""
dx = xs[1] - xs[0]
@uses_nth_neighbors(0)
def default_loss(xs, ys):
"""Calculate loss on a single interval.
Currently returns the rescaled length of the interval. If one of the
y-values is missing, returns 0 (so the intervals with missing data are
never touched. This behavior should be improved later.
"""
dx = xs[1] - xs[0]
if isinstance(ys[0], Iterable):
dy = [abs(a - b) for a, b in zip(*ys)]
return np.hypot(dx, dy).max()
else:
dy = ys[1] - ys[0]
return np.hypot(dx, dy)
@uses_nth_neighbors(1)
def triangle_loss(xs, ys):
xs = [x for x in xs if x is not None]
ys = [y for y in ys if y is not None]
if len(xs) == 2: # we do not have enough points for a triangle
return xs[1] - xs[0]
N = len(xs) - 2 # number of constructed triangles
if isinstance(ys[0], Iterable):
pts = [(x, *y) for x, y in zip(xs, ys)]
vol = simplex_volume_in_embedding
else:
pts = [(x, y) for x, y in zip(xs, ys)]
vol = volume
return sum(vol(pts[i : i + 3]) for i in range(N)) / N
@uses_nth_neighbors(1)
def curvature_loss(simplex, values, value_scale, neighbors, neighbor_values):
"""Compute the curvature loss of a simplex.
Parameters
----------
simplex : list of tuples
Each entry is one point of the simplex.
values : list of values
The scaled function values of each of the simplex points.
value_scale : float
The scale of values, where ``values = function_values * value_scale``.
neighbors : list of tuples
The neighboring points of the simplex, ordered such that simplex[0]
exacly opposes neighbors[0], etc.
neighbor_values : list of values
The scaled function values for each of the neighboring points.