Search code examples
javaoptimizationcurve-fittingdata-fitting

Nonlinear (hyperbola) curve fitting in Java


I have algorithm that produces data during it's run time. It is guaranteed that the value has downward tend during the time and in ideal conditions it would be something like a hyperbola (or at least it has hyperbolic shape). Basic plotted graph (x axis is the time and y axis the value) looks like this:

value of algorithm over time

Now, I would like to predict, how will value look like after n time steps during the run time. I tried to use linear or polynomial regression from org.apache.commons.math3 but obviously, the predictions cannot be good since the curve is not linear nor polynomial. Also both predictions provide curves without downward tend.

To solve this I tried to use odinsbane's least squares, but I have troubles to provide correct first parameters assumption and therefore I can't fit the curve properly.

So my question is: Is there any kotlin/java library, which could be able to fit my data properly without me providing first parameters estimate?


Solution

  • My final solution was to use this and it's Levenberg–Marquardt solver. I decomposed generic hyperbola function a + b/(x + c) into a * c + a * x + b - y * c = xy - therefore:

    • setTargetValues received x*y data
    • setValues provided a * c + a * x + b - y * c values
    • setDerivatives provided [c + x, 1, a - y] values

    This allowed me to fit "hyperbolic like" data.