Weighted quantile calculation is incorrect
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened?
DataArrayWeighted.quantile returns incorrect numbers for weighted quantiles.
As an example, passing a quantile of 1 should return the maximum value by definition, but when there are weights, it will oftentimes return a different number.
What did you expect to happen?
A quantile of 1 should return the maximum value. Note that the calculation is also incorrect for lower quantiles, not just for q=1.
Minimal Complete Verifiable Example
import numpy as np
import xarray
(
xarray.DataArray(
np.array([1,10,100])
)
.weighted(
xarray.DataArray(
np.array([10,10,5])
)
)
.quantile(
1.
)
.to_numpy()
)
array(60.)
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
No response
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.11.0 (main, Mar 1 2023, 18:26:19) [GCC 11.2.0]
python-bits: 64
OS: Linux
OS-release: 6.1.0-23-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.1
libnetcdf: None
xarray: 2023.6.0
pandas: 2.2.2
numpy: 1.26.4
scipy: 1.12.0
netCDF4: None
pydap: None
h5netcdf: None
h5py: 3.9.0
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: 1.3.8
dask: 2023.6.0
distributed: 2023.6.0
matplotlib: 3.7.2
cartopy: None
seaborn: 0.12.2
numbagg: None
fsspec: 2023.4.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 68.0.0
pip: 23.2.1
conda: 23.7.3
pytest: 7.4.0
mypy: 1.6.1
IPython: 8.12.2
sphinx: 5.0.2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the DataArrayWeighted.quantile entry point and reproduce the issue with the minimal Python example using the provided values and weights. Check the weighted quantile behavior for q=1 and lower quantiles, then add or update coverage so q=1 returns the maximum and the other quantiles return correct values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100