:py:mod:`IMLCV.tools._rbf_interp`
=================================

.. py:module:: IMLCV.tools._rbf_interp

.. autoapi-nested-parse::

   Module for RBF interpolation.



Module Contents
---------------

Classes
~~~~~~~

.. autoapisummary::

   IMLCV.tools._rbf_interp.RBFInterpolator



Functions
~~~~~~~~~

.. autoapisummary::

   IMLCV.tools._rbf_interp.cv_vals



.. py:function:: cv_vals(x: IMLCV.base.CV.CV, powers, metric: IMLCV.base.CV.CvMetric)


.. py:class:: RBFInterpolator(y: IMLCV.base.CV.CV, metric: IMLCV.base.CV.CvMetric, d, neighbors=None, smoothing=0.0, kernel='thin_plate_spline', epsilon=None, degree=None)

   Radial basis function (RBF) interpolation in N dimensions.

   :param y: Data point coordinates.
   :type y: (P, N) array_like
   :param d: Data values at `y`.
   :type d: (P, ...) array_like
   :param neighbors: If specified, the value of the interpolant at each evaluation point
                     will be computed using only this many nearest data points. All the data
                     points are used by default.
   :type neighbors: int, optional
   :param smoothing: Smoothing parameter. The interpolant perfectly fits the data when this
                     is set to 0. For large values, the interpolant approaches a least
                     squares fit of a polynomial with the specified degree. Default is 0.
   :type smoothing: float or (P,) array_like, optional
   :param kernel:
                  Type of RBF. This should be one of

                      - 'linear'               : ``-r``
                      - 'thin_plate_spline'    : ``r**2 * log(r)``
                      - 'cubic'                : ``r**3``
                      - 'quintic'              : ``-r**5``
                      - 'multiquadric'         : ``-sqrt(1 + r**2)``
                      - 'inverse_multiquadric' : ``1/sqrt(1 + r**2)``
                      - 'inverse_quadratic'    : ``1/(1 + r**2)``
                      - 'gaussian'             : ``exp(-r**2)``

                  Default is 'thin_plate_spline'.
   :type kernel: str, optional
   :param epsilon: Shape parameter that scales the input to the RBF. If `kernel` is
                   'linear', 'thin_plate_spline', 'cubic', or 'quintic', this defaults to
                   1 and can be ignored because it has the same effect as scaling the
                   smoothing parameter. Otherwise, this must be specified.
   :type epsilon: float, optional
   :param degree: Degree of the added polynomial. For some RBFs the interpolant may not
                  be well-posed if the polynomial degree is too small. Those RBFs and
                  their corresponding minimum degrees are

                      - 'multiquadric'      : 0
                      - 'linear'            : 0
                      - 'thin_plate_spline' : 1
                      - 'cubic'             : 1
                      - 'quintic'           : 2

                  The default value is the minimum degree for `kernel` or 0 if there is
                  no minimum degree. Set this to -1 for no added polynomial.
   :type degree: int, optional

   .. rubric:: Notes

   An RBF is a scalar valued function in N-dimensional space whose value at
   :math:`x` can be expressed in terms of :math:`r=||x - c||`, where :math:`c`
   is the center of the RBF.

   An RBF interpolant for the vector of data values :math:`d`, which are from
   locations :math:`y`, is a linear combination of RBFs centered at :math:`y`
   plus a polynomial with a specified degree. The RBF interpolant is written
   as

   .. math::
       f(x) = K(x, y) a + P(x) b,

   where :math:`K(x, y)` is a matrix of RBFs with centers at :math:`y`
   evaluated at the points :math:`x`, and :math:`P(x)` is a matrix of
   monomials, which span polynomials with the specified degree, evaluated at
   :math:`x`. The coefficients :math:`a` and :math:`b` are the solution to the
   linear equations

   .. math::
       (K(y, y) + \lambda I) a + P(y) b = d

   and

   .. math::
       P(y)^T a = 0,

   where :math:`\lambda` is a non-negative smoothing parameter that controls
   how well we want to fit the data. The data are fit exactly when the
   smoothing parameter is 0.

   The above system is uniquely solvable if the following requirements are
   met:

       - :math:`P(y)` must have full column rank. :math:`P(y)` always has full
         column rank when `degree` is -1 or 0. When `degree` is 1,
         :math:`P(y)` has full column rank if the data point locations are not
         all collinear (N=2), coplanar (N=3), etc.
       - If `kernel` is 'multiquadric', 'linear', 'thin_plate_spline',
         'cubic', or 'quintic', then `degree` must not be lower than the
         minimum value listed above.
       - If `smoothing` is 0, then each data point location must be distinct.

   When using an RBF that is not scale invariant ('multiquadric',
   'inverse_multiquadric', 'inverse_quadratic', or 'gaussian'), an appropriate
   shape parameter must be chosen (e.g., through cross validation). Smaller
   values for the shape parameter correspond to wider RBFs. The problem can
   become ill-conditioned or singular when the shape parameter is too small.

   The memory required to solve for the RBF interpolation coefficients
   increases quadratically with the number of data points, which can become
   impractical when interpolating more than about a thousand data points.
   To overcome memory limitations for large interpolation problems, the
   `neighbors` argument can be specified to compute an RBF interpolant for
   each evaluation point using only the nearest data points.

   .. versionadded:: 1.7.0

   .. seealso:: :obj:`NearestNDInterpolator`, :obj:`LinearNDInterpolator`, :obj:`CloughTocher2DInterpolator`

   .. rubric:: References

   .. [1] Fasshauer, G., 2007. Meshfree Approximation Methods with Matlab.
       World Scientific Publishing Co.

   .. [2] http://amadeus.math.iit.edu/~fass/603_ch3.pdf

   .. [3] Wahba, G., 1990. Spline Models for Observational Data. SIAM.

   .. [4] http://pages.stat.wisc.edu/~wahba/stat860public/lect/lect8/lect8.pdf

   .. rubric:: Examples

   Demonstrate interpolating scattered data to a grid in 2-D.

   >>> import matplotlib.pyplot as plt
   >>> from scipy.interpolate import RBFInterpolator
   >>> from scipy.stats.qmc import Halton

   >>> rng = jnp.random.default_rng()
   >>> xobs = 2*Halton(2, seed=rng).random(100) - 1
   >>> yobs = jnp.sum(xobs, axis=1)*jnp.exp(-6*jnp.sum(xobs**2, axis=1))

   >>> xgrid = jnp.mgrid[-1:1:50j, -1:1:50j]
   >>> xflat = xgrid.reshape(2, -1).T
   >>> yflat = RBFInterpolator(xobs, yobs)(xflat)
   >>> ygrid = yflat.reshape(50, 50)

   >>> fig, ax = plt.subplots()
   >>> ax.pcolormesh(*xgrid, ygrid, vmin=-0.25, vmax=0.25, shading='gouraud')
   >>> p = ax.scatter(*xobs.T, c=yobs, s=50, ec='k', vmin=-0.25, vmax=0.25)
   >>> fig.colorbar(p)
   >>> plt.show()

   .. py:method:: _chunk_evaluator(x: IMLCV.base.CV.CV, y: IMLCV.base.CV.CV, coeffs, memory_budget=1000000)

      Evaluate the interpolation while controlling memory consumption.
      We chunk the input if we need more memory than specified.

      :param x: array of points on which to evaluate
      :type x: (Q, N) float ndarray
      :param y: array of points on which we know function values
      :type y: (P, N) float ndarray
      :param shift: Domain shift used to create the polynomial matrix.
      :type shift: (N, ) ndarray
      :param scale: Domain scaling used to create the polynomial matrix.
      :type scale: (N,) float ndarray
      :param coeffs: Coefficients in front of basis functions
      :type coeffs: (P+R, S) float ndarray
      :param memory_budget: Total amount of memory (in units of sizeof(float)) we wish
                            to devote for storing the array of coefficients for
                            interpolated points. If we need more memory than that, we
                            chunk the input.
      :type memory_budget: int

      :returns: * *(Q, S) float ndarray*
                * *Interpolated array*


   .. py:method:: __call__(x: IMLCV.base.CV.CV)

      Evaluate the interpolant at `x`.

      :param x: Evaluation point coordinates.
      :type x: (Q, N) array_like

      :returns: Values of the interpolant at `x`.
      :rtype: (Q, ...) ndarray



