Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extrapolation in java

Tags:

java

math

I've been able to use Apache Math's interpolation using the LinearInterpolator().interpolate(x1, y1). Unfortunately, I could not find a way to extrapolate.

How can I do linear extrapolation in java?

x1 = [1, 2, 3, 4, 5];

y1 = [2, 4, 8, 16, 32];

I would like to know the values of any x2 not just the one in the range of the x1.

If I try to extract the value of 6 I get an: OutOfRangeException if {@code v} is outside of the domain of the * spline function (smaller than the smallest knot point or larger than the largest knot point).

Edit: Here is my simple interpolate function. I would like an option to enable the extrapolation just like in MathLab(interp2). Using x1 and y1 arrays an input for that function I get the Apache's OutOfRangeException because the value 6 is not contained in the x1 array.

public static List<Double> interpolateLinear(double[] x1, double[] y1, Double[] x2) {
    List<Double> resultList;
    final PolynomialSplineFunction function = new LinearInterpolator().interpolate(x1, y1);
    resultList = Arrays.stream(x2).map(aDouble -> function.value(aDouble)).collect(Collectors.toList());
    return resultList;
}

Edit2: Had to read a little bit on the .value method of the PolynomialSplineFunction object to get it right but there it goes (all the credit goes to user Joni) Thanks man:

public static double[] interpolateLinear(double[] x1, double[] y1, double[] x2) {
    final PolynomialSplineFunction function = new LinearInterpolator().interpolate(x1, y1);
    final PolynomialFunction[] splines = function.getPolynomials();
    final PolynomialFunction firstFunction = splines[0];
    final PolynomialFunction lastFunction = splines[splines.length - 1];

    final double[] knots = function.getKnots();
    final double firstKnot = knots[0];
    final double lastKnot = knots[knots.length - 1];

    double[] resultList = Arrays.stream(x2).map(aDouble -> {
        if (aDouble > lastKnot) {
            return lastFunction.value(aDouble - knots[knots.length - 2]);
        } else if (aDouble < firstKnot)
            return firstFunction.value(aDouble - knots[0]);
        return function.value(aDouble);
    }).toArray();
    return resultList;
}
like image 519
Jonathan Fortin Avatar asked Aug 18 '15 15:08

Jonathan Fortin


People also ask

What is extrapolation with example?

Extrapolation is a statistical method beamed at understanding the unknown data from the known data. It tries to predict future data based on historical data. For example, estimating the size of a population after a few years based on the current population size and its rate of growth.

What is the extrapolation formula?

Definition of Extrapolation Formula. The extrapolation formula is the formula used to estimate the value of the dependent variable concerning an independent variable that shall lie in a range outside of the given data set.

What is extrapolation explain?

Extrapolation refers to estimating an unknown value based on extending a known sequence of values or facts. To extrapolate is to infer something not explicitly stated from existing information. Interpolation is the act of estimating a value within two known values that exist within a sequence of values.

How do I extrapolate data?

To do this, the researcher plots out a linear equation on a graph and uses the sequence of the values to predict immediate future data points. You can draw a tangent line at the last point and extend this line beyond its limits.


1 Answers

You can get the first and last polynomial splines from the interpolator, and use those to extrapolate.

PolynomialSplineFunction function = new LinearInterpolator().interpolate(x1, y1);
PolynomialFunction[] splines = function.getPolynomials();
PolynomialFunction first = splines[0];
PolynomialFunction last = splines[splines.length-1];
// use first and last to extrapolate 

You won't get 64 from 6 though. You should expect 48 from a linear extrapolation. Which goes to show that extrapolation is bound to give you wrong answers.

like image 174
Joni Avatar answered Sep 29 '22 21:09

Joni