Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solves bug related to fitting using __curve_fit with x errors #63

Merged
merged 1 commit into from
Feb 19, 2024

Conversation

MBerman9
Copy link
Contributor

When fitting to custom functions through the __curve_fit method, if the data includes x errors and the function is decreasing, the errors on the parameters will decrease as the x errors increase until the method returns the following error:
RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 600.

The following code was used to test this as well as my fix;

import numpy as np
import qexpy as q

def fit(x, *p):
    return p[0] + p[1]*x

y = q.MeasurementArray(np.array([100, 80, 60, 40, 20, 0])+np.random.normal(loc=0,scale=5,size=6), error = 5)

x1 = q.MeasurementArray([0, 1, 2, 3, 4, 5], error= 0)
x2 = q.MeasurementArray([0, 1, 2, 3, 4, 5], error= 5)

print("x1: ", x1)
results1 = q.fit(xdata=x1, ydata = y, model=fit, parguess=[100, -20])
print(results1)
print()
print("x2: ", x2)
results2 = q.fit(xdata=x2, ydata = y, model=fit, parguess=[100, -20])
print(results2)

It may be a good idea to use scipy.odr instead when there are x errors.

Copy link
Contributor

@astralcai astralcai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@astralcai astralcai merged commit 12509fc into Queens-Physics:master Feb 19, 2024
12 checks passed
@MBerman9 MBerman9 deleted the fitting_yerrCorr branch February 19, 2024 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants