-
Notifications
You must be signed in to change notification settings - Fork 18
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
harmonic model: only fit orders up to local minimum and use coeffs from previous order as first guess #443
harmonic model: only fit orders up to local minimum and use coeffs from previous order as first guess #443
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #443 +/- ##
==========================================
- Coverage 87.90% 83.77% -4.14%
==========================================
Files 40 44 +4
Lines 1745 1954 +209
==========================================
+ Hits 1534 1637 +103
- Misses 211 317 +106
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
We could also write a function for finding the local minimum similar to What do you think @mathause, leave it like this, factor it out to a separate minimize function, rewrite the original |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I first thought to allow
first_guess=None
but as (if) this function is internal I think it is fine. - the way it is currently set up (returning the loss and not the residuals),
loss="cauchy"
has no effect - can you confirm? (also cauchy does not seem to be recommended)
|
(I should maybe not have reviewed this PR, but thought it would be an easy win...) |
Actually, no, it does have an influence... could it be that this is actually wrong? The documentation says func should return the residuals. Could it be that in this way the loss is interpreted as a residual? |
I agree and think returning the residuals is the correct thing..., however, this sentence in the docstring had me assume returning the loss is fine as well. https://github.com/scipy/scipy/blob/7dcd8c59933524986923cde8e9126f5fc2e6b30b/scipy/optimize/_lsq/least_squares.py#L265
I assumed |
Empirically - yes, mathematically - 😬 No?. But logically, since we assume that underlying our data is a "pretty" function, i.e. an oscillating curve with a period of 12, I think it is fair to assume that each additional order brings us closer to the actual curve, and that the residuals decrease monotonically. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok let's add the two TODO
comments and merge.
Hm the first derivative is not monotonic so maybe. I am not exactly sure why it comes to different results. I made a few observations:
What it does in the background when we return residuals is apply the loss function to the residuals before computing MSE, and cauchy dampens outliers. I'm not sure though what it does when However I found it nice to have the MSE of the chosen parameters returned by the optimize result, which we lose when we do it like that.
I'll add the TODOs (including some from the stuff above) but I want to wait with merging after we merged the tests. |
for more information, see https://pre-commit.ci
…SMER-group#444) * rename i and j to gridcell and year for readability and some commentary --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>
* Adjust bounds for lambda optimization * update comment and change to numpy array
implement nansum in generate_harmonic model
* fix axis in nansum
* add tests for harmonic model * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * flaking * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * generalise tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add test for fit to bic xr, rearrange and remove order in generate fs * pandas datetime version compliance * Revert "pandas datetime version compliance" This reverts commit 40a1f45. * actual pandas datetime compliance * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * make coefficients a numpy array * extend tests on generate fourier series * write out tolerance in test_fit_to_bic * write out max_order * use time.dt.month for months * fix for the nan and zero wrangling * verbose 0.1 in xr test * add tests for dataarray in xr func * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add test for numerical stability * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * test with non constant predictors * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove comment about close residuals Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com> * add Note Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>
* adjust bounds of power transformer fit * add comments
…group#447) * remove rosender for pt jacobian * remove rosender import
* align equality check in yeo johnson transform with scikit learn yeo-johnson transform * add NOTE * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* deprecate mask_3D_frac_approx * changelog * use public func again * ignore warnings on the module level * forgotten rename
* deprecate mask_3D_frac_approx * changelog * use public func again * ignore warnings on the module level * forgotten rename * use `mask_3D_frac_approx` from regionmask * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * bugfix * deprecate mask_3D_frac_approx * use public func again * ignore warnings on the module level * use `mask_3D_frac_approx` from regionmask * bugfix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix linting * flip names again * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* clean up dependencies * add nc-time-axis * changelog * add plotting dependencies as extras * update min-all-deps.yml * rename extra to complete
* RTD: remove transitive deps from docs env * add comment
for more information, see https://pre-commit.ci
order - 1
as first guess #422CHANGELOG.rst
As already mentioned in the issue above, we can use the fit from the previous order as the first guess for the next order. Moreover, we can only fit orders until we reach a local minimum instead of fitting for all orders and then selecting the one with the smallest loss.
This brings down the time needed to fit the harmonic model from 40.8 seconds to 8.9 seconds for a 250 years x 118 gridpoint dataset.