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

Change lmoments3 behaviour with fitkwargs #2045

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Conversation

coxipi
Copy link
Contributor

@coxipi coxipi commented Jan 13, 2025

Pull Request Checklist:

  • This PR addresses an already opened issue (for bug fixes / features)
    • This PR fixes #xyz
  • Tests for the changes have been added (for bug fixes / features)
    • (If applicable) Documentation has been added / updated (for bug fixes / features)
  • CHANGELOG.rst has been updated (with summary of main changes)
    • Link to issue (:issue:number) and pull request (:pull:number) has been added

What kind of change does this PR introduce?

  • Add a warning if fitkwargs is given with lmoments method
  • Except if we use the gamma distribution is used with floc: This is accepted and used to shift the distribution

Does this PR introduce a breaking change?

No

Other information:

I realize that SPI/SPEI is still heavily constrained in what distribution can be used. For now, only gamma/fisk (log-logistic) are allowed. At some point we could revisit this. I think we reached a point where we could be more permissive, since we ironed out many of the potential problems in these functions. There is still potential that problems could arise if we allow more distributions, but we might have to accept that some problems might arise when working with these optimization procedures, and fix them on the fly (or not, if it's just a limitation of the method).

@coxipi
Copy link
Contributor Author

coxipi commented Jan 14, 2025

@aulemahal Sascha suggested an error instead of a warning, thoughts? Sometimes I'm not sure which is more appropriate

Copy link
Collaborator

@aulemahal aulemahal left a comment

Choose a reason for hiding this comment

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

I was thinking of a warning because I was thinking of a usage where one calls the function multiple time with different distributions but the same other arguments. I realize now that this does not make much sense, woups.

Thinking of it, an error might indeed be better. Sorry for the confusion!

src/xclim/indices/stats.py Outdated Show resolved Hide resolved
@github-actions github-actions bot added the approved Approved for additional tests label Jan 14, 2025
@github-actions github-actions bot added the indicators Climate indices and indicators label Jan 14, 2025
@coxipi
Copy link
Contributor Author

coxipi commented Jan 14, 2025

Now, we can use PWM in SPI/SPEI functions directly.

I added tests: The results with lmoments3 (PWM) / gamma is close to what we get with ML/gamma or APP/gamma, so I'm satisfied, things work well.

if method == "PWM":
lmom = pytest.importorskip("lmoments3.distr")
scipy2lmom = {"gamma": "gam"}
dist = getattr(lmom, scipy2lmom[dist])
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Zeitsperre are we sure that any *-lmoments3 tox test suite is running here ?

@coxipi Because I think the test should fail : dist is passed as a lmoments3 object in the test but in the SPI and SPEI functions there are tests like dist in dist_methods, which would fail. I'm not sure how to do it, but the function/indicator must now be able to accept scipy/lmoments3 distribution objects if the method=='PWM' is to be enabled!

Copy link
Contributor Author

@coxipi coxipi Jan 15, 2025

Choose a reason for hiding this comment

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

Ah you're right! in the tests I have called the low-level function, so it's really params that counts in the SPI call. The default distribution that is sent in the condition checker does not matter, so that's why things worked out. But the SPI/SPEI cannot truly accept PWM functions yet.

Perhaps I should add a dictionnary to convert scipy names to lmoments3 names.

Or, simply: Since we don't want to officially support lmoments3, we can just forget about the conditions if method="PWM" ? We allow all distributions by default with PWM

@coveralls
Copy link

Coverage Status

coverage: 89.942% (-0.02%) from 89.962%
when pulling f2e932b on fitkwargs_lmoments
into e38cdb6 on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Approved for additional tests indicators Climate indices and indicators
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants