forked from blackjax-devs/blackjax
-
Notifications
You must be signed in to change notification settings - Fork 2
Make slice sampling work standalone #43
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
Closed
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ | |
| """ | ||
|
|
||
| from functools import partial | ||
| from typing import Callable, NamedTuple | ||
| from typing import Callable, NamedTuple, Optional | ||
|
|
||
| import jax | ||
| import jax.numpy as jnp | ||
|
|
@@ -87,7 +87,9 @@ class SliceInfo(NamedTuple): | |
|
|
||
|
|
||
| def init( | ||
| position: ArrayTree, logdensity_fn: Callable, constraint_fn: Callable | ||
| position: ArrayTree, | ||
| logdensity_fn: Callable, | ||
| constraint_fn: Optional[Callable] = None, | ||
| ) -> SliceState: | ||
| """Initialize the Slice Sampler state. | ||
|
|
||
|
|
@@ -103,7 +105,9 @@ def init( | |
| SliceState | ||
| The initial state of the Slice Sampler. | ||
| """ | ||
| return SliceState(position, logdensity_fn(position), constraint_fn(position)) | ||
| logp = logdensity_fn(position) | ||
| constraint_val = (constraint_fn or (lambda _: jnp.array([])))(position) | ||
| return SliceState(position, logp, constraint_val) | ||
|
|
||
|
|
||
| def build_kernel( | ||
|
|
@@ -266,6 +270,7 @@ def build_hrss_kernel( | |
| generate_slice_direction_fn: Callable, | ||
| stepper_fn: Callable, | ||
| max_steps: int = 10, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Defaults are important -- is max_steps 10 well motivated (and not e.g. 16, or 1024)? |
||
| max_shrinkage: int = 100, | ||
| ) -> Callable: | ||
| """Build a Hit-and-Run Slice Sampling kernel. | ||
|
|
||
|
|
@@ -292,7 +297,7 @@ def build_hrss_kernel( | |
| A kernel function that takes a PRNG key, the current `SliceState`, and | ||
| the log-density function, and returns a new `SliceState` and `SliceInfo`. | ||
| """ | ||
| slice_kernel = build_kernel(stepper_fn, max_steps) | ||
| slice_kernel = build_kernel(stepper_fn, max_steps, max_shrinkage) | ||
|
|
||
| def kernel( | ||
| rng_key: PRNGKey, state: SliceState, logdensity_fn: Callable | ||
|
|
@@ -359,8 +364,7 @@ def sample_direction_from_covariance(rng_key: PRNGKey, cov: Array) -> Array: | |
|
|
||
|
|
||
| def hrss_as_top_level_api( | ||
| logdensity_fn: Callable, | ||
| cov: Array, | ||
| logdensity_fn: Callable, cov: Array, max_steps: int = 10, max_shrinkage: int = 100 | ||
| ) -> SamplingAlgorithm: | ||
| """Creates a Hit-and-Run Slice Sampling algorithm. | ||
|
|
||
|
|
@@ -384,7 +388,9 @@ def hrss_as_top_level_api( | |
| the configured Hit-and-Run Slice Sampler. | ||
| """ | ||
| generate_slice_direction_fn = partial(sample_direction_from_covariance, cov=cov) | ||
| kernel = build_hrss_kernel(generate_slice_direction_fn, default_stepper_fn) | ||
| kernel = build_hrss_kernel( | ||
| generate_slice_direction_fn, default_stepper_fn, max_steps, max_shrinkage | ||
| ) | ||
| init_fn = partial(init, logdensity_fn=logdensity_fn) | ||
| step_fn = partial(kernel, logdensity_fn=logdensity_fn) | ||
| return SamplingAlgorithm(init_fn, step_fn) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the blackjaxy way to do it, or should this default go at line 92?