Search code examples
pythonscipyminimizationlmfit

Custom interpolation / minimization function (aka curve fitting) any library


I usually work with ROOT and its powerful MINUIT interpolation classes in a C++ environment. For reason related to plotting and ease of data handling, I switched recently to Python 3.8. The problem is that I can't find a library that allows me to set my custom interpolation function (1D by now, multivariate in the future)! Scipy and Pandas have preset methods, and I am not aware of other libraries for interpolation. It seems to me quite impossible, therefore I ask you!

What I need to reproduce is something that sounds like this in ROOT:

DEFINITION OF THE INTERPOLATING FUNCTION

TF1* f3 = new TF1("PECD6", "([0]*x + 0.5*[2]*(5*x*x*x-3*x) +0.125*[4]*(63*x*x*x*x*x-70*x*x*x+15*x)) / (1 + 0.5*[1]*(3*x*x-1) + 0.125*[3]*(35*x*x*x*x-30*x*x+3) + 0.0625*[5]*(231*x*x*x*x*x*x-315*x*x*x*x+105*x*x-5)) +1", -0.9, 0.9);
    f3->SetParameters(0, 0, 0, 0, 0, 0);
    f3->SetParLimits(0, -1, 1);
    f3->SetParLimits(1, -1, 1);
    f3->SetParLimits(2, -1, 1);
    f3->SetParLimits(3, -1, 1);
    f3->SetParLimits(4, -1, 1);
    f3->SetParLimits(5, -1, 1);
    f3->SetParNames("b1", "b2", "b3", "b4", "b5", "b6");

as you can see, I am using Legendre polynomials up to the 6th order and I'd like to receive the coefficient, possibly with propagations of error.

USE OF THE FUNCTION

ratio_genarray[rcounter]->Fit("PECD6", "QR");
fit1[k] = ratio_genarray[rcounter]->GetFunction("PECD6");

Does anybody have a suggestion?

Thank you!


Solution

  • I would suggest using iminuit. You'd need to rearrange your C++ syntax to iminuit's Python syntax, but from there on, it's a minimal change because it is MINUIT.

    (LMFIT also looks promising, but since you're already using MINUIT, it would be a smoother transition to iminuit.)