Regression#

Weight functions used by the moving least squares approximator.

class sigmaepsilon.math.approx.functions.ConstantWeightFunction(*, value: Number = 1.0, **kwargs)[source]#

A constant weight function for the moving least squares method.

G(x: Iterable[Number]) ndarray#

Return the Hessian of the weight function at x.

Hessian(x: Iterable[Number]) ndarray[source]#

Return the Hessian of the weight function at x.

f(x: Iterable[Number]) float#

Return the value of the weight function at x.

g(x: Iterable[Number]) ndarray#

Return the gradient of the weight function at x.

gradient(x: Iterable[Number]) ndarray[source]#

Return the gradient of the weight function at x.

value(x: Iterable[Number]) float[source]#

Return the value of the weight function at x.

class sigmaepsilon.math.approx.functions.CubicWeightFunction(*, core: int | Iterable[Number] | ndarray | None = None, supportdomain: Iterable[Number] | None = None, sd: Iterable[Number] | None = None, **kwargs)[source]#

A cubic weight function for the moving least squares method.

Example

>>> from sigmaepsilon.math.approx import CubicWeightFunction
>>> w = CubicWeightFunction(core=[0.0, 0.0], sd=[0.5, 0.5])
>>> w([0.0, 0.0])
0.4444444444444444
G(x: Iterable[Number]) ndarray#

Return the Hessian of the weight function at x.

Hessian(x: Iterable[Number]) ndarray[source]#

Return the Hessian of the weight function at x.

evaluate(x: Iterable[Number]) Tuple[float, ndarray, ndarray][source]#

Return the value, gradient and Hessian of the weight function at x.

f(x: Iterable[Number]) float#

Return the value of the weight function at x.

g(x: Iterable[Number]) ndarray#

Return the gradient of the weight function at x.

gradient(x: Iterable[Number]) ndarray[source]#

Return the gradient of the weight function at x.

value(x: Iterable[Number]) float[source]#

Return the value of the weight function at x.

class sigmaepsilon.math.approx.functions.MLSWeightFunction(*, core: int | Iterable[Number] | ndarray | None = None, supportdomain: Iterable[Number] | None = None, sd: Iterable[Number] | None = None, **kwargs)[source]#

Base class for weight functions for the moving least squares method.

property core: ndarray | Iterable[Number] | None#

Return the core (center) of the weight function.

f(x: Iterable[Number]) float#

Evaluate the function.

preproc_evaluation(x: Iterable[Number])[source]#

Validate and coerce the evaluation point x into a NumPy array.

property supportdomain: ndarray | Iterable[Number] | None#

Return the support domain of the weight function.

value(x: Iterable[Number]) float[source]#

Evaluate the function.

class sigmaepsilon.math.approx.functions.SingularWeightFunction(*, eps: Number = 1e-05, **kwargs)[source]#

A singular weight function for the moving least squares method.

G(x: Iterable[Number]) ndarray#

Return the Hessian of the weight function at x.

Hessian(x: Iterable[Number]) ndarray[source]#

Return the Hessian of the weight function at x.

f(x: Iterable[Number])#

Return the value of the weight function at x.

g(x: Iterable[Number]) ndarray#

Return the gradient of the weight function at x.

gradient(x: Iterable[Number]) ndarray[source]#

Return the gradient of the weight function at x.

value(x: Iterable[Number])[source]#

Return the value of the weight function at x.

sigmaepsilon.math.approx.functions.isMLSWeightFunction(f: Any) bool[source]#

Return True if the argument is a valid weight function for the moving least squares method.

Parameters:

f (Any) – The object to check.

Numba-jitted kernels for moving least squares approximation and its derivatives.

Polynomial basis functions and their derivatives for least squares approximation.

Assembly of the moment matrices used by moving least squares approximation.

Least squares and moving least squares approximation.

sigmaepsilon.math.approx.ls.least_squares(points: ndarray | Iterable, values: ndarray | Iterable, *, deg: int = 1, order: int = 2) Callable[source]#

Fit a polynomial to the given points and values in the least-squares sense.

Given \(N\) points located at \(\mathbf{x}_i\) in \(\mathbb{R}^d\) where \(i \in [1 \dots N]\). The returned fit function approximates the given values \(f_i\) at \(\mathbf{x}_i\) in the least-squares sence with the error functional

\[\begin{split}\sum_{i} \\biggr[ || f \left( \mathbf{x}_i \\right) - f_i || \\biggr] ^2\end{split}\]

where \(f\) is taken from \(\Pi_{m}^d\), the space of polynomials of total degree \(m\) in \(d\) spatial dimensions.

Parameters:
  • points (Iterable) – [[X11, X12, …, X1d], …, [Xn1, Xn2, …, Xnd]]

  • values (Iterable) – [[f11, f12, …, f1r], …, [fn1, fn2, …, fnr]]

  • deg (int, Optional) – The degree of the fit function. Default is 1.

  • order (int, Optional.) – The order of the approximation. Default is 2.

Returns:

Fit function r(x) -> f(x), fdx(x), fdy(x), fdxx(x), fdyy(x), fdxy(x) fi([X1, X2, …, Xd]) = [fi1, fi2,…, fir]

Return type:

Callable

Note

The resulting fit function can have an approximation or regression behaviour, depending on the dataset and the degree of the polynomial.

sigmaepsilon.math.approx.ls.moving_least_squares(points: ndarray | Iterable, values: ndarray | Iterable, *, w: Callable | None = None, **kwargs) Callable[source]#

Perform moving least squares approximation.

The usage is the same as for the weighted_least_squares() function.

sigmaepsilon.math.approx.ls.weighted_least_squares(points: ndarray | Iterable, values: ndarray | Iterable, *, deg: int = 1, order: int = 2, w: Callable | None = None) Callable[source]#

Return a Callable that can be used to approximate over datasets.

Parameters:
  • points (Iterable) – [[X11, X12, …, X1d], …, [Xn1, Xn2, …, Xnd]]

  • values (Iterable) – [[f11, f12, …, f1r], …, [fn1, fn2, …, fnr]]

  • deg (int, Optional) – The degree of the fit function. Default is 1.

  • w (MLSWeightFunction, Optional) – A proper weight function. Default is a ConstantWeightFunction.

  • order (int, Optional.) – The order of the approximation. Default is 2.

Returns:

Fit function r(x) -> f(x), fdx(x), fdy(x), fdxx(x), fdyy(x), fdxy(x) fi([X1, X2, …, Xd]) = [fi1, fi2,…, fir]

Return type:

Callable

Note

The resulting fit function can have an approximation or regression behaviour, depending on the dataset and the degree of the polynomial.

High performance, object oriented moving least squares approximation.

class sigmaepsilon.math.approx.mls.MLSApproximator(X_S: ndarray, Y_S: ndarray, knn_backend: str | None = None, k: int | None = None, max_distance: Number | None = None)[source]#

Object oriented, high performance implementation of moving least squares.

This implementation is less flexible than the others, but performes well for extremely large datasets too. If you want to experiment with the hyperparameters of the MLS as a method, it is suggested to use the other solutions offered by the library.

Parameters:
  • X_S (ndarray) – The source points.

  • Y_S (ndarray, Optional) – The source data.

  • knn_backend ({"scipy", "sklearn"}, Optional) – The backend to use for the KNN calculation. If None, the default backend of the library is used. Default is None.

  • k (int, Optional) – The number of neighbours to consider. Default is None.

  • max_distance (Number, Optional) – The maximum distance to consider for the neighbours. Default is None.

Notes

1) There is a KNN calculation involved in the process, which is done using either scipy or sklearn. 2) The target points can be provided when calling the fit() method to precalculate the neighbours and factors. If not provided, the neighbours and factors are calculated when the approximate() method is called. 3) We use Numba to speed up the approximation, hence the first call might be slower.

Examples

>>> import numpy as np
>>> from sigmaepsilon.math.approx import MLSApproximator
>>>
>>> # prepare the source points
>>> nx, ny, nz = (10, 10, 10)
>>> x = np.linspace(0, 1, nx)
>>> y = np.linspace(0, 1, ny)
>>> z = np.linspace(0, 1, nz)
>>> xv, yv, zv = np.meshgrid(x, y, z)
>>> source_points = np.stack([xv.flatten(), yv.flatten(), zv.flatten()], axis=-1)
>>>
>>> # prepare the source values
>>> source_values = np.ones((nx*ny*nz, 3))  # could be anything with a shape of (nx*ny*nz, ...)
>>>
>>> # instantiate the approximator
>>> approximator = MLSApproximator(source_points, source_values, knn_backend="scipy")
>>>
>>> # approximate to the target points
>>> target_points = source_points[:10]  # could be anything with a shape of (..., source_points.shape[-1])
>>> target_values = approximator.approximate(target_points)
>>>
>>> # check the results
>>> assert np.allclose(target_values, np.ones_like(target_values))
approximate(X_T: ndarray) ndarray[source]#

Estimates the value of the function at the given points.

Parameters:

X_T (ndarray) – The target points.

property factors: ndarray | None#

Return the factors of the target points.

If the factors are not calculated yet, it returns None.

property neighbours: ndarray | None#

Return the neighbours of the target points.

If the neighbours are not calculated yet, it returns None.