Add stratified resampling; improve handling of -inf weights#39
Add stratified resampling; improve handling of -inf weights#39
Conversation
Codecov ReportAttention: Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull Request Overview
This PR introduces stratified resampling and improves handling of cases with -∞ weights in the sequential Monte Carlo (SMC) inference code. Key changes include updating test parameters to include stratified resampling, adding a new stratified_resample function in the inference module, and propagating the new resampling_method parameter to example scripts.
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/test_examples.py | Updated test cases to use fewer particles and added stratified tests |
| llamppl/util.py | Improved logsumexp by converting inputs to an array and handling -∞ |
| llamppl/inference/smc_standard.py | Added stratified_resample function and integrated the new resampling option; updated resampling condition |
| examples/hard_constraints.py | Updated run_example signature to accept and forward resampling_method |
| examples/haiku.py | Updated run_example signature to accept and forward resampling_method |
Comments suppressed due to low confidence (1)
llamppl/inference/smc_standard.py:116
- [nitpick] For better performance, consider using np.random.choice with the size parameter to generate ancestor_indices in one call, rather than using a list comprehension.
ancestor_indices = [np.random.choice(range(len(particles)), p=probs) for _ in range(n_particles)]
|
|
||
| N = len(weights) | ||
| # make N subdivisions, and chose a random position within each one | ||
| positions = (np.random.random(N) + range(N)) / N |
There was a problem hiding this comment.
[nitpick] Consider using np.arange(N) instead of range(N) for clarity and to ensure consistent behavior with numpy operations.
| positions = (np.random.random(N) + range(N)) / N | |
| positions = (np.random.random(N) + np.arange(N)) / N |
Add stratified, systematic, and residual resampling. Includes changes from #39
This PR adds stratified resampling to
smc_standard. The resamplingmethod can be specified by a new
resampling_methodargument tosmc_standardwhich defaults tomultinomial(and thus preserves thepast behaviour).
I have also improved the handling of cases in which all particles have
-infweight. Previously ourlogsumexpmethod would returnnanand raised a warning. I've added a case tologsumexpwhichreturns
-infwhen all input weights are-inf. We also skip theresampling step (and ess_threshold check) when all
particles are
-inf.