Regression#
- 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#
Returns the Hessian matrix if available.
For this operation the object must have an implementation of
- Hessian(self,*args,**kwargs):
<…> return <…>
- 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#
Returns the Hessian matrix if available.
For this operation the object must have an implementation of
- Hessian(self,*args,**kwargs):
<…> return <…>
- 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.
- 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#
Returns the Hessian matrix if available.
For this operation the object must have an implementation of
- Hessian(self,*args,**kwargs):
<…> return <…>
- sigmaepsilon.math.approx.functions.isMLSWeightFunction(f: Any) bool[source]#
Returns True if the argument is a valid weight function for the moving least squares method.
- sigmaepsilon.math.approx.ls.least_squares(points: ndarray | Iterable, values: ndarray | Iterable, *, deg: int = 1, order: int = 2) Callable[source]#
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
\[\sum_{i} \biggr[ || f \left( \mathbf{x}_i \right) - f_i || \biggr] ^2\]where \(f\) is taken from \(\Pi_{m}^d\), the space of polynomials of total degree \(m\) in \(d\) spatial dimensions.
- Parameters:
- 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]#
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]#
Returns 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.
- 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 a specific version of the moving least squares method. 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 theapproximate()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.