-
Notifications
You must be signed in to change notification settings - Fork 5
Refactor resampling functions #207
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f844c05
Refactoring resamplings to allow for more flexibility in the outputs.
AdrienCorenflos 7a3744d
run examples
AdrienCorenflos 689bdf0
Validate sampled indices count in adaptive resampling
AdrienCorenflos 171f444
remove redundant deps
AdrienCorenflos a07d980
Fix pyright
SamDuffield 7678739
Type hint output of _wrapped
SamDuffield 8310044
Tuple -> tuple
SamDuffield 309c235
Mention adaptive in README
SamDuffield 47d0dae
Merge branch 'main' into resampling-refactor
AdrienCorenflos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| from cuthbertlib.resampling import killing, multinomial, systematic | ||
| from cuthbertlib.resampling import adaptive, killing, multinomial, systematic | ||
| from cuthbertlib.resampling.adaptive import ess_decorator | ||
| from cuthbertlib.resampling.protocols import ConditionalResampling, Resampling | ||
| from cuthbertlib.resampling.utils import inverse_cdf |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """Adaptive resampling decorator. | ||
|
|
||
| Provides a decorator to turn any Resampling function into an adaptive resampling | ||
| function which performs resampling only when the effective sample size (ESS) | ||
| falls below a threshold. | ||
| """ | ||
|
|
||
| from functools import wraps | ||
|
|
||
| import jax | ||
| import jax.numpy as jnp | ||
|
|
||
| from cuthbertlib.resampling.protocols import Resampling | ||
| from cuthbertlib.smc.ess import log_ess | ||
| from cuthbertlib.types import Array, ArrayLike, ArrayTree, ArrayTreeLike | ||
|
|
||
|
|
||
| def ess_decorator(func: Resampling, threshold: float) -> Resampling: | ||
| """Wrap a Resampling function so that it only resamples when ESS < threshold. | ||
|
|
||
| The returned function is jitted and has `n` as a static argument. The | ||
| original resampler's docstring is appended to this wrapper's docstring so | ||
| IDEs and users can see the underlying algorithm documentation. | ||
|
|
||
| Args: | ||
| func: A resampling function with signature | ||
| (key, logits, positions, n) -> (indices, logits_out, positions_out). | ||
| threshold: Fraction of particle count specifying when to resample. | ||
| Resampling is triggered when ESS < ess_threshold * n. | ||
|
|
||
| Returns: | ||
| A Resampling function implementing adaptive resampling. | ||
| """ | ||
| # Build a descriptive docstring that includes the wrapped function doc | ||
| wrapped_doc = func.__doc__ or "" | ||
| doc = f""" | ||
| Adaptive resampling decorator (threshold={threshold}). | ||
|
|
||
| This wrapper will call the provided resampling function only when the | ||
| effective sample size (ESS) is below `ess_threshold * n`. | ||
|
|
||
| Wrapped resampler documentation: | ||
| {wrapped_doc} | ||
| """ | ||
|
|
||
| @wraps(func) | ||
| def _wrapped( | ||
| key: Array, logits: ArrayLike, positions: ArrayTreeLike, n: int | ||
| ) -> tuple[Array, Array, ArrayTree]: | ||
| logits_arr = jnp.asarray(logits) | ||
| N = logits_arr.shape[0] | ||
| if n != N: | ||
| raise AssertionError( | ||
| "The number of sampled indices must be equal to the number of " | ||
| f"particles for `adaptive` resampling. Got {n} instead of {N}." | ||
| ) | ||
|
|
||
| def _do_resample(): | ||
| return func(key, logits_arr, positions, n) | ||
|
|
||
| def _no_resample(): | ||
| return jnp.arange(n), logits_arr, positions | ||
|
|
||
| return jax.lax.cond( | ||
| log_ess(logits_arr) < jnp.log(threshold * n), | ||
| _do_resample, | ||
| _no_resample, | ||
| ) | ||
|
|
||
| # Attach the composed docstring and return a jitted version | ||
| _wrapped.__doc__ = doc | ||
| return jax.jit(_wrapped, static_argnames=("n",)) | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.