Top Description Fields Constructors Methods
org.apache.commons.math3.analysis.interpolation

public Class LoessInterpolator

extends Object
implements UnivariateInterpolator, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator
Imports
java.io.Serializable, java.util.Arrays, org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction, org.apache.commons.math3.exception.DimensionMismatchException, .NoDataException, .NonMonotonicSequenceException, .NotFiniteNumberException, .NotPositiveException, .NumberIsTooSmallException, .OutOfRangeException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.util.FastMath, .MathArrays, .MathUtils

Implements the Local Regression Algorithm (also Loess, Lowess) for interpolation of real univariate functions.

For reference, see William S. Cleveland - Robust Locally Weighted Regression and Smoothing Scatterplots

This class implements both the loess method and serves as an interpolation adapter to it, allowing one to build a spline on the obtained loess fit.
Since
2.0

Field Summary

Modifier and TypeField and Description
private final double
accuracy

If the median residual at a certain robustness iteration is less than this amount, no more iterations are done.

private final double
bandwidth

The bandwidth parameter: when computing the loess fit at a particular point, this fraction of source points closest to the current point is taken into account for computing a least-squares regression.

public static final double
DEFAULT_ACCURACY

Default value for accuracy.

public static final double
DEFAULT_BANDWIDTH

Default value of the bandwidth parameter.

public static final int
DEFAULT_ROBUSTNESS_ITERS

Default value of the number of robustness iterations.

private final int
robustnessIters

The number of robustness iterations parameter: this many robustness iterations are done.

private static final long
serialVersionUID

serializable version identifier.

Constructor Summary

AccessConstructor and Description
public
LoessInterpolator()

Constructs a new LoessInterpolator with a bandwidth of DEFAULT_BANDWIDTH, DEFAULT_ROBUSTNESS_ITERS robustness iterations and an accuracy of {#link #DEFAULT_ACCURACY}.

public
LoessInterpolator(double
when computing the loess fit at a particular point, this fraction of source points closest to the current point is taken into account for computing a least-squares regression. A sensible value is usually 0.25 to 0.5, the default value is DEFAULT_BANDWIDTH.
bandwidth
,
int
This many robustness iterations are done. A sensible value is usually 0 (just the initial fit without any robustness iterations) to 4, the default value is DEFAULT_ROBUSTNESS_ITERS.
robustnessIters
)

Construct a new LoessInterpolator with given bandwidth and number of robustness iterations.

public
LoessInterpolator(double
when computing the loess fit at a particular point, this fraction of source points closest to the current point is taken into account for computing a least-squares regression. A sensible value is usually 0.25 to 0.5, the default value is DEFAULT_BANDWIDTH.
bandwidth
,
int
This many robustness iterations are done. A sensible value is usually 0 (just the initial fit without any robustness iterations) to 4, the default value is DEFAULT_ROBUSTNESS_ITERS.
robustnessIters
,
double
If the median residual at a certain robustness iteration is less than this amount, no more iterations are done.
accuracy
)

Construct a new LoessInterpolator with given bandwidth, number of robustness iterations and accuracy.

Method Summary

Modifier and TypeMethod and Description
private static void
checkAllFiniteReal(final double[]
Values array.
values
)

Check that all elements of an array are finite real numbers.

public final PolynomialSplineFunction

Returns:

A cubic spline built upon a loess fit to the data at the original abscissae
interpolate
(final double[]
the arguments for the interpolation points
xval
,
final double[]
the values for the interpolation points
yval
)

Implements org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator.interpolate.

Deprecated as a consequence of PolynomialSplineFunction being deprecated.

Compute an interpolating function by performing a loess fit on the data at the original abscissae and then building a cubic spline with a org.apache.commons.math3.analysis.interpolation.SplineInterpolator on the resulting fit.

private static int

Returns:

the smallest compliant index.
nextNonzero
(final double[]
Weights array.
weights
,
final int
Index from which to start search.
i
)

Return the smallest index j such that j > i && (j == weights.length || weights[j] != 0).

public final double[]

Returns:

the values of the loess fit at corresponding original abscissae.
smooth
(final double[]
Arguments for the interpolation points.
xval
,
final double[]
Values for the interpolation points.
yval
,
final double[]
point weights: coefficients by which the robustness weight of a point is multiplied.
weights
)

Compute a weighted loess fit on the data at the original abscissae.

public final double[]

Returns:

values of the loess fit at corresponding original abscissae
smooth
(final double[]
the arguments for the interpolation points
xval
,
final double[]
the values for the interpolation points
yval
)

Compute a loess fit on the data at the original abscissae.

private static double

Returns:

(1 - |x|3)3 for |x| < 1, 0 otherwise.
tricube
(final double
Argument.
x
)

Compute the tricube weight function

private static void
updateBandwidthInterval(final double[]
Arguments array.
xval
,
final double[]
Weights array.
weights
,
final int
Index around which the new interval should be computed.
i
,
final int[]
a two-element array {left, right} such that: (left==0 or xval[i] - xval[left-1] > xval[right] - xval[i]) and (right==xval.length-1 or xval[right+1] - xval[i] > xval[i] - xval[left]). The array will be updated.
bandwidthInterval
)

Given an index interval into xval that embraces a certain number of points closest to xval[i-1], update the interval so that it embraces the same number of points closest to xval[i], ignoring zero weights.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

accuracyback to summary
private final double accuracy

If the median residual at a certain robustness iteration is less than this amount, no more iterations are done.

bandwidthback to summary
private final double bandwidth

The bandwidth parameter: when computing the loess fit at a particular point, this fraction of source points closest to the current point is taken into account for computing a least-squares regression.

A sensible value is usually 0.25 to 0.5.

DEFAULT_ACCURACYback to summary
public static final double DEFAULT_ACCURACY

Default value for accuracy.

Since
2.1
DEFAULT_BANDWIDTHback to summary
public static final double DEFAULT_BANDWIDTH

Default value of the bandwidth parameter.

DEFAULT_ROBUSTNESS_ITERSback to summary
public static final int DEFAULT_ROBUSTNESS_ITERS

Default value of the number of robustness iterations.

robustnessItersback to summary
private final int robustnessIters

The number of robustness iterations parameter: this many robustness iterations are done.

A sensible value is usually 0 (just the initial fit without any robustness iterations) to 4.

serialVersionUIDback to summary
private static final long serialVersionUID

serializable version identifier.

Constructor Detail

LoessInterpolatorback to summary
public LoessInterpolator()

Constructs a new LoessInterpolator with a bandwidth of DEFAULT_BANDWIDTH, DEFAULT_ROBUSTNESS_ITERS robustness iterations and an accuracy of {#link #DEFAULT_ACCURACY}. See LoessInterpolator(double, int, double) for an explanation of the parameters.

LoessInterpolatorback to summary
public LoessInterpolator(double bandwidth, int robustnessIters)

Construct a new LoessInterpolator with given bandwidth and number of robustness iterations.

Calling this constructor is equivalent to calling {link LoessInterpolator(bandwidth, robustnessIters, LoessInterpolator.DEFAULT_ACCURACY)

Parameters
bandwidth:double

when computing the loess fit at a particular point, this fraction of source points closest to the current point is taken into account for computing a least-squares regression. A sensible value is usually 0.25 to 0.5, the default value is DEFAULT_BANDWIDTH.

robustnessIters:int

This many robustness iterations are done. A sensible value is usually 0 (just the initial fit without any robustness iterations) to 4, the default value is DEFAULT_ROBUSTNESS_ITERS.

See Also
LoessInterpolator(double, int, double)
LoessInterpolatorback to summary
public LoessInterpolator(double bandwidth, int robustnessIters, double accuracy) throws OutOfRangeException, NotPositiveException

Construct a new LoessInterpolator with given bandwidth, number of robustness iterations and accuracy.

Parameters
bandwidth:double

when computing the loess fit at a particular point, this fraction of source points closest to the current point is taken into account for computing a least-squares regression. A sensible value is usually 0.25 to 0.5, the default value is DEFAULT_BANDWIDTH.

robustnessIters:int

This many robustness iterations are done. A sensible value is usually 0 (just the initial fit without any robustness iterations) to 4, the default value is DEFAULT_ROBUSTNESS_ITERS.

accuracy:double

If the median residual at a certain robustness iteration is less than this amount, no more iterations are done.

Exceptions
OutOfRangeException:
if bandwidth does not lie in the interval [0,1].
NotPositiveException:
if robustnessIters is negative.
Since
2.1
See Also
LoessInterpolator(double, int)

Method Detail

checkAllFiniteRealback to summary
private static void checkAllFiniteReal(final double[] values)

Check that all elements of an array are finite real numbers.

Parameters
values:double[]

Values array.

Exceptions
NotFiniteNumberException:
if one of the values is not a finite real number.
interpolateback to summary
public final PolynomialSplineFunction interpolate(final double[] xval, final double[] yval) throws NonMonotonicSequenceException, DimensionMismatchException, NoDataException, NotFiniteNumberException, NumberIsTooSmallException

Implements org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator.interpolate.

Deprecated

as a consequence of PolynomialSplineFunction being deprecated. See corresponding docs for further information.

Compute an interpolating function by performing a loess fit on the data at the original abscissae and then building a cubic spline with a org.apache.commons.math3.analysis.interpolation.SplineInterpolator on the resulting fit.

Parameters
xval:double[]

the arguments for the interpolation points

yval:double[]

the values for the interpolation points

Returns:PolynomialSplineFunction

A cubic spline built upon a loess fit to the data at the original abscissae

Exceptions
NonMonotonicSequenceException:
if xval not sorted in strictly increasing order.
DimensionMismatchException:
if xval and yval have different sizes.
NoDataException:
if xval or yval has zero size.
NotFiniteNumberException:
if any of the arguments and values are not finite real numbers.
NumberIsTooSmallException:
if the bandwidth is too small to accomodate the size of the input data (i.e. the bandwidth must be larger than 2/n).
nextNonzeroback to summary
private static int nextNonzero(final double[] weights, final int i)

Return the smallest index j such that j > i && (j == weights.length || weights[j] != 0).

Parameters
weights:double[]

Weights array.

i:int

Index from which to start search.

Returns:int

the smallest compliant index.

smoothback to summary
public final double[] smooth(final double[] xval, final double[] yval, final double[] weights) throws NonMonotonicSequenceException, DimensionMismatchException, NoDataException, NotFiniteNumberException, NumberIsTooSmallException

Compute a weighted loess fit on the data at the original abscissae.

Parameters
xval:double[]

Arguments for the interpolation points.

yval:double[]

Values for the interpolation points.

weights:double[]

point weights: coefficients by which the robustness weight of a point is multiplied.

Returns:double[]

the values of the loess fit at corresponding original abscissae.

Exceptions
NonMonotonicSequenceException:
if xval not sorted in strictly increasing order.
DimensionMismatchException:
if xval and yval have different sizes.
NoDataException:
if xval or yval has zero size.
NotFiniteNumberException:
if any of the arguments and values are not finite real numbers.
NumberIsTooSmallException:
if the bandwidth is too small to accomodate the size of the input data (i.e. the bandwidth must be larger than 2/n).
Since
2.1
smoothback to summary
public final double[] smooth(final double[] xval, final double[] yval) throws NonMonotonicSequenceException, DimensionMismatchException, NoDataException, NotFiniteNumberException, NumberIsTooSmallException

Compute a loess fit on the data at the original abscissae.

Parameters
xval:double[]

the arguments for the interpolation points

yval:double[]

the values for the interpolation points

Returns:double[]

values of the loess fit at corresponding original abscissae

Exceptions
NonMonotonicSequenceException:
if xval not sorted in strictly increasing order.
DimensionMismatchException:
if xval and yval have different sizes.
NoDataException:
if xval or yval has zero size.
NotFiniteNumberException:
if any of the arguments and values are not finite real numbers.
NumberIsTooSmallException:
if the bandwidth is too small to accomodate the size of the input data (i.e. the bandwidth must be larger than 2/n).
tricubeback to summary
private static double tricube(final double x)

Compute the tricube weight function

Parameters
x:double

Argument.

Returns:double

(1 - |x|3)3 for |x| < 1, 0 otherwise.

updateBandwidthIntervalback to summary
private static void updateBandwidthInterval(final double[] xval, final double[] weights, final int i, final int[] bandwidthInterval)

Given an index interval into xval that embraces a certain number of points closest to xval[i-1], update the interval so that it embraces the same number of points closest to xval[i], ignoring zero weights.

Parameters
xval:double[]

Arguments array.

weights:double[]

Weights array.

i:int

Index around which the new interval should be computed.

bandwidthInterval:int[]

a two-element array {left, right} such that: (left==0 or xval[i] - xval[left-1] > xval[right] - xval[i]) and (right==xval.length-1 or xval[right+1] - xval[i] > xval[i] - xval[left]). The array will be updated.