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

Add xclim.indices.stats.fit method: MSE (maximum product of spacings) #2077

Merged
merged 16 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ New features and enhancements
* Missing values method "pct" and "at_least_n" now accept a new "subfreq" option that allows to compute the missing mask in two steps. When given, the algorithm is applied at this "subfreq" resampling frequency first and then the result is resampled at the target indicator frequency. In the output, a period is invalid if any of its subgroup where flagged as invalid by the chosen method. (:pull:`2058`, :issue:`1820`).
* ``scipy.stats.rv_continuous`` instances can now be given directly as the ``dist`` argument in ``standardized_precipitation_index`` and ``standardized_precipitation_evapotranspiration_index`` indicators. This includes `lmoments3` distributions when specifying ``method="PWM"``. (:issue:`2043`, :pull:`2045`).
* Time selection in ``xclim.core.calendar.select_time`` and the ``**indexer`` argument of indicators now supports day-of-year bounds given as DataArrays with spatial and/or temporal dimensions. (:issue:`1987`, :pull:`2055`).
* Maximum Spacing Estimation method for distribution fitting has been added to `xclim.indices.stats.fit` (:issue:`2078`, :pull:`2077`)

Bug fixes
^^^^^^^^^
Expand Down
22 changes: 19 additions & 3 deletions src/xclim/indices/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ def _fitfunc_1d(arr, *, dist, nparams, method, **fitkwargs):
params = dist.fit(x, *args, method="mle", **kwargs, **fitkwargs)
elif method == "MM":
params = dist.fit(x, method="mm", **fitkwargs)
elif method in ["MSE", "MPS"]:
args, guess = _fit_start(x, dist.name, **fitkwargs)
param_info = dist.shapes
for i, arg in enumerate(args):
guess[param_info[i]] = arg

fitresult = scipy.stats.fit(
dist=dist, data=x, method="mse", guess=guess, **fitkwargs
)
params = fitresult.params
elif method == "PWM":
# lmoments3 will raise an error if only dist.numargs + 2 values are provided
if len(x) <= dist.numargs + 2:
Expand Down Expand Up @@ -107,10 +117,14 @@ def fit(
dist : str or rv_continuous distribution object
Name of the univariate distribution, such as beta, expon, genextreme, gamma, gumbel_r, lognorm, norm
(see :py:mod:scipy.stats for full list) or the distribution object itself.
method : {"ML", "MLE", "MM", "PWM", "APP"}
Fitting method, either maximum likelihood (ML or MLE), method of moments (MM) or approximate method (APP).
method : {"ML", "MLE", "MM", "PWM", "APP", "MSE", "MPS"}
Fitting method, either maximum likelihood (ML or MLE), method of moments (MM), maximum product of spacings (MSE or MPS)
or approximate method (APP).
Can also be the probability weighted moments (PWM), also called L-Moments, if a compatible `dist` object is passed.
The PWM method is usually more robust to outliers.
The PWM method is usually more robust to outliers. The MSE method is more consistent than the MLE method, although
it can be more sensitive to repeated data.
For the MSE method, each variable parameter must be given finite bounds
(provided with keyword argument bounds={'param_name':(min,max),...}).
dim : str
The dimension upon which to perform the indexing (default: "time").
**fitkwargs : dict
Expand All @@ -131,6 +145,8 @@ def fit(
"ML": "maximum likelihood",
"MM": "method of moments",
"MLE": "maximum likelihood",
"MSE": "maximum product of spacings",
"MPS": "maximum product of spacings",
"PWM": "probability weighted moments",
"APP": "approximative method",
}
Expand Down
21 changes: 21 additions & 0 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

from __future__ import annotations

from functools import partial

import numpy as np
import pytest
import xarray as xr
from scipy.optimize import differential_evolution
from scipy.stats import lognorm, norm

from xclim.indices import stats
Expand Down Expand Up @@ -157,6 +160,24 @@ def test_genextreme_fit(genextreme):
np.testing.assert_allclose(p, (0.20949, 297.954091, 75.7911863), 1e-5)


def test_mse_fit(genextreme):
"""Check MSE fit with a series that leads to poor values without good initial conditions."""
# Use fixed-seed rng to remove randomness of differential_evolution
# (alternative: change optimizer to non-stochastic gradient-based)
# TODO: change when minimum scipy exceeds 1.15.0 to rng=0
optimizer = partial(differential_evolution, seed=0)
p = stats.fit(
genextreme,
"genextreme",
"MSE",
bounds=dict(c=(0, 1), scale=(0, 100), loc=(200, 400)),
optimizer=optimizer,
)
np.testing.assert_allclose(
p, (0.18435517630019815, 293.61049928703073, 86.70937297745427), 1e-3
)


def test_fa(fitda):
T = 10
q = stats.fa(fitda, T, "lognorm")
Expand Down