How to fit polynomial to data with error bars

后端 未结 3 2417
旧时难觅i
旧时难觅i 2021-02-20 16:17

I am currently using numpy.polyfit(x,y,deg) to fit a polynomial to experimental data. I would however like to fit a polynomial that uses weighting based on the errors of the poi

3条回答
  •  眼角桃花
    2021-02-20 16:43

    For weighted polynomial fitting you can use:

    numpy.polynomial.polynomial.polyfit(x, y, deg, rcond=None, full=False, w=weights)
    

    see http://docs.scipy.org/doc/numpy/reference/generated/numpy.polynomial.polynomial.polyfit.html

    Important to note that in this function the weights should not be supplied as 1/variance (which is the usual form in many weighted applications), but as 1/sigma

    Although curve_fit and leastsq are much more general and powerful optimization tools than polyfit (in that they can fit just any function), polyfit has the advantage that it yields an (exact) analytical solution and is therefore probably much faster than iterative approximation methods like curve_fit and leastsq - especially in the case of fitting polynomials to multiple sets of y-data (obtained at the same x-vector)

    Update: As of numpy version 1.7, numpy.polyfit also takes weights as an input (which ideally should be supplied as 1/sigma, not 1/variance)

提交回复
热议问题