0

I'm trying to implement an Extrapolation function using the Apache Commons Math lib and the PolynomialSplineFunction & LinearInterpolator functions.

 public double[] linearInterp(double[] x, double[] y, double[] xi) {
   LinearInterpolator li = new LinearInterpolator(); // or other interpolator
   PolynomialSplineFunction psf = li.interpolate(x, y);

   double[] yi = new double[xi.length];
   for (int i = 0; i < xi.length; i++) {
       yi[i] = psf.value(xi[i]);
   }
   return yi;
}

x = [0, 60, 120,180,240];

y = [196, 232, 250, 157, 300];

xi = [300, 360, 420];

the problem is that if I use a value outside of the range of x I get a OutOfRangeException is there any way to Extrapolate using this method? how do I get around this error.

JTK
  • 1,469
  • 2
  • 22
  • 39
  • http://stackoverflow.com/questions/32076041/extrapolation-in-java – martinez314 Apr 15 '16 at 21:14
  • Yeah couldn't get that to work properly: https://stackoverflow.com/questions/36655244/extrapolation-using-apache-commons-math-where-x-date There was no fluctuation in the results, they either grew or shrank Thought maybe there was something different in my input that would necessitate a different solution. – JTK Apr 15 '16 at 21:37

1 Answers1

2

If you look at the documentation PolynomialSimlineFunction Doucumentaition you can read for the value method "OutOfRangeException - if v is outside of the domain of the spline function (smaller than the smallest knot point or larger than the largest knot point)."

This is quite reasonable since the interpolation out of the knots is not an aproximation of your funcition. What your are attempting simply doesn't make sense mathematically speaking.

Nevado
  • 162
  • 7
  • What part of what I'm trying to do doesn't make sense, is my goal unachievable in general, or just the way I'm trying to do it? From reading the wiki page ( I know not a great ref) it states that "the formula for linear interpolation is identical to linear extrapolation" So assuming this is true, why can't it be achieved this way? also the link to a solution to a very similar problem, which is posted in the comments would suggest it is possible? is the other solution incorrect? – JTK Apr 15 '16 at 21:52
  • Extrapolation is possible if the function you are trying to extrapolate is a polynomial function of known degree, or if you know the "shape" of the function (log, sin, ...) The problem here is you are doing a **Linear** extrapolation, and you cant make a linear aproximation betwen a knot an infinity (with only one knot basically). – Nevado Apr 15 '16 at 21:56
  • Is there anyway I can Extrapolate my data? I'd hate to have spent so much time on this with nothing to show for it, if you could point in the right direction, if I used `SplineInterpolator`, `LoessInterpolator` or `NevilleInterpolator` to Extrapolate maybe? if needs be put me out of my misery and I'l reluctantly walk away from this endeavor. – JTK Apr 15 '16 at 22:17
  • 1
    I will investigate further and post an answer as soon as I can :) – Nevado Apr 15 '16 at 22:19
  • The NevilleInterpolator will do the job. But you have to consider that, for the exprapolation to work (have a reasonable error), your data must be from a polynomial of degree n-1 (or less), being n the number of knots. You won't get the `OutOfRangeException` because this time you wont evaluate a `PolynomialSplineFunction` but a `PolynomialFunctionLagrangeForm` that can be evalutated at all points – Nevado Apr 17 '16 at 23:28
  • I've tried your solution and the `OutOfRangeException` is gone and the results are fluctuating, but the values are not in the range of the data set and are all minus values, possibly I have misunderstood something you have explained, returned values are: `-1.9789738863701387E43`, `-7.958942185870982E48` I feel like I'm close, could you go into a bit more detail what `data must be from a polynomial of degree n-1 (or less)` means, maybe this is where I'm falling down. – JTK Apr 18 '16 at 19:31
  • This method will get your a polynomial that passes through all the points, the degree of this polynomial will be n-1 or less, with n being the number of points. What i think is happening is that you want a linear or polynomial **adjustment** of your data. Which is the function they are suposed to follow (linear, quadratic, ...)? – Nevado Apr 18 '16 at 20:05
  • The values are going to be in the same range constantly, they go up and down, I've been trying to figure out the function they are supposed to follow (I'm a student) it's not quadratic, maybe linear straight line (Piecewise linear function)? I've added some images of my graphs to the question. – JTK Apr 18 '16 at 21:02
  • If this are market values or something along those lines, this data may no necessarily follow a polynomial function. Making predictions of this sort is something way more difficult than extrapolating a function, you need advanced behavioural models for this – Nevado Apr 20 '16 at 15:48
  • Cheers for the help @Nevado, I've found an alternative solution (only plotting the expected function) using a different library. – JTK Apr 24 '16 at 23:59