Python Forum
scipy.optimize.curve_fit uncertainty calculation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
scipy.optimize.curve_fit uncertainty calculation
#1
Our team is using scipy.optimize.curve_fit to return least-squares fitting and I am unable to return the "textbook uncertainties" that I expect. In fact, the uncertainty on the slope seems to be incorrect by exactly a factor of 1.5.

My toy example is three data points with only uncertainty on the y values:
(0,0+-1)
(0,1+-1)
(1,1+-2)

The best fit returned is 0+0.2x, which is correct based on my by-hand and excel calculations (attached). The uncertainty on the y-intercept is 1.0, which is also what I expect. The uncertainty on the slope is 0.8944271909999176 (python) rather than 1.341640786 (excel/hand calculations) which is a factor of exactly 1.5 to 10 digits.

I'm referencing the formulas in problem 8.19 in John Taylor's 'An Introduction to Error Analysis'.

This is a toy problem to make sure we trust the package. We will be using more points and higher-order polynomials.

Any suggestions? Thanks in advance for your time and help.

Attached Files

.py   Test_Least_squares.py (Size: 1.37 KB / Downloads: 1)
.xlsx   LinearLeastSquaresExample.xlsx (Size: 205.19 KB / Downloads: 1)
Reply
#2
Following up on my own thread - we have found that setting the method to Trust Region Reflective algorithm ('trf') or dogleg algorithm ('dogbox') fixes this issue.

curve_fit(linear_function, x_data, y_data, sigma=y_error, absolute_sigma=True, method='trf')

The documentation does warn against using the Levenberg-Marquardt algorithm ('lm') with a sparse number of observations:
Quote:"The method ‘lm’ won’t work when the number of observations is less than the number of variables, use ‘trf’ or ‘dogbox’ in this case."

In this toy example we are fitting two parameters with three points, so I don't believe we are violating this rule above. Regardless, it hints that these other two algorithms are better for sparse data.

https://docs.scipy.org/doc/scipy/referen...e_fit.html
https://docs.scipy.org/doc/scipy/referen...st_squares
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using scipy.optimize: curve_fit ju21878436312 0 3,401 Sep-24-2022, 01:15 PM
Last Post: ju21878436312
  SOLVED: scipy.optimize.least_squares problem Skytter13 2 7,376 Mar-06-2022, 10:17 AM
Last Post: Skytter13
  Help with Scipy optimize for numerical problem Jbjbjb1 0 2,360 Jun-22-2021, 05:03 AM
Last Post: Jbjbjb1
  Round float according to uncertainty CatorCanulis 2 3,397 Jun-08-2021, 09:52 PM
Last Post: Gribouillis
  How to get coefficient of determination R2 after scipy.curve_fit? AlekseyPython 0 2,876 Feb-12-2021, 09:03 AM
Last Post: AlekseyPython
  scipy.optimize.basinhopping generates unstable output bb19x11 0 2,646 Mar-09-2020, 04:07 PM
Last Post: bb19x11
  scipy curve_fit for multiple independent variables Jay_Nerella 1 9,812 May-08-2019, 02:08 AM
Last Post: scidam
  Solve a system of non-linear equations in Python (scipy.optimize.fsolve) drudox 7 26,867 Aug-18-2018, 02:27 AM
Last Post: scidam
  minimize with scipy.optimize tobenmoben 0 3,804 Feb-17-2018, 01:47 PM
Last Post: tobenmoben
  curve_fit rizal 3 5,827 Dec-20-2017, 04:53 PM
Last Post: hshivaraj

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020