Repository navigation
Respect num_samples and normalize weights in weighted extract - #237
Open
parth-patel010 wants to merge 1 commit into
Open
parth-patel010 wants to merge 1 commit into
parth-patel010 wants to merge 1 commit into
Conversation
Stratified resampling always returned len(weights) draws and compared the raw cumulative weights against uniforms in [0, 1), so unnormalized weights collapsed the resample onto the first draws. Normalize the weights once in extract, reject negative/non-finite/all-zero weights and draw num_samples indices in _stratified_resample. Fixes arviz-devs#236
Documentation build overview
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #236
extract(..., weights=...)with the default stratified resampling ignorednum_samplesand used the weights as given, so weights that didn't sum to 1 collapsed the resample onto the first few draws (or hit anIndexError).Changes:
extractcasts the weights to float, raises aValueErrorfor negative, non-finite or all-zero weights, and normalizes them once. That covers both the stratified and multinomial paths. Multinomial used to raise "Probabilities do not sum to 1" for unnormalized weights._stratified_resampletakesnum_samplesand draws that many stratified uniforms. The Pythonwhileloop is replaced withnp.searchsorted(..., side="right"), which picks the same "first index whose cumulative weight is above u" and is clipped to guard against rounding at the end of the cumulative sum.weightssays they are normalized internally.reorg.pyiwas regenerated withdocstub run src/arviz_base.With normalized weights and
num_samples == len(weights)the selected indices are identical to before. I checked 300 random weight vectors (sizes 5 to 3000, some leading zero weights) against the old loop with the same seed: 0 mismatches. Sopower_scale_datasetin arviz-stats gives the same results.tests/test_psense.pyandtests/test_metrics.pyin arviz-stats pass against this branch (80 passed).Repro from the issue
Before (main, b87afe3):
After (this branch):
Tests
New tests in
tests/test_reorg.py::TestExtract:num_samplesis respected (both methods), unnormalized weights give the same draws as the normalized ones (both methods), stratified counts match the weights exactly (weights 1:2:1 on three draws with 40 samples gives 10/20/10), and invalid weights raise.On main (src reverted, new tests kept):
On this branch:
Full suite:
python -m pytest testsgives259 passed.pre-commit run(mypy, ruff, pylint) passes on the changed files.