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

fitting (linear): fix indexing #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

drewrisinger
Copy link

The linear fitting function used 1-indexed array notation instead of the Python-default 0-indexing. We discovered this by ending up with a divide-by-zero error when our x-values were roughly set to [0, 1], so the two 1 values were subtracted from each other.

The linear fitting function used 1-indexed array notation instead of the Python-default 0-indexing. We discovered this by ending up with a divide-by-zero error when our x-values were roughly set to [0, 1], so the two 1 values were subtracted from each other.
Comment on lines +7 to +8
k = (y[-1] - y[0]) / (x[-1] - x[0])
p['a'] = y[0] - x[0] * k
Copy link
Author

Choose a reason for hiding this comment

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

I would also accept np.max(y)- np.min(y) / (np.max(x) - np.min(x). The existing code assumes that the x & y arrays are sorted, not necessarily true.

Copy link
Member

Choose a reason for hiding this comment

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

Good catch. None of the fitting routines should assume sorted data, so the max/min implementation is more appropriate.

Copy link
Member

@pathfinder49 pathfinder49 Jul 2, 2022

Choose a reason for hiding this comment

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

Another thought:

p['b'] = (np.max(y) -np.min(y)) / (np.max(x) - np.min(x))
idx = np.argmin(x)
p['a'] = y[idx] - x[idx] * p['b']

IIRC this estimates the offset with the user specified gradient (if supplied). Not that it should matter much for fitting a line...

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