-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implement compute_log_prior utility
#7149
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 1 commit
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
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,159 @@ | ||
| # Copyright 2024 The PyMC Developers | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from collections.abc import Sequence | ||
| from typing import Optional, cast | ||
|
|
||
| from arviz import InferenceData, dict_to_dataset | ||
| from fastprogress import progress_bar | ||
|
|
||
| import pymc | ||
|
|
||
| from pymc.backends.arviz import _DefaultTrace, coords_and_dims_for_inferencedata | ||
| from pymc.model import Model, modelcontext | ||
| from pymc.pytensorf import PointFunc | ||
| from pymc.util import dataset_to_point_list | ||
|
|
||
|
|
||
| def compute_log_prior( | ||
| idata: InferenceData, | ||
| var_names: Optional[Sequence[str]] = None, | ||
| extend_inferencedata: bool = True, | ||
| model: Optional[Model] = None, | ||
| sample_dims: Sequence[str] = ("chain", "draw"), | ||
| progressbar=True, | ||
| ): | ||
| """Compute elemwise log_prior of model given InferenceData with posterior group | ||
|
|
||
| Parameters | ||
| ---------- | ||
| idata : InferenceData | ||
| InferenceData with posterior group | ||
| var_names : sequence of str, optional | ||
| List of Observed variable names for which to compute log_prior. | ||
| Defaults to all all free variables. | ||
| extend_inferencedata : bool, default True | ||
| Whether to extend the original InferenceData or return a new one | ||
| model : Model, optional | ||
| sample_dims : sequence of str, default ("chain", "draw") | ||
| progressbar : bool, default True | ||
|
|
||
| Returns | ||
| ------- | ||
| idata : InferenceData | ||
| InferenceData with log_prior group | ||
| """ | ||
| return compute_log_density( | ||
| idata=idata, | ||
| var_names=var_names, | ||
| extend_inferencedata=extend_inferencedata, | ||
| model=model, | ||
| kind="prior", | ||
| sample_dims=sample_dims, | ||
| progressbar=progressbar, | ||
| ) | ||
|
|
||
|
|
||
| def compute_log_density( | ||
| idata: InferenceData, | ||
| *, | ||
| var_names: Optional[Sequence[str]] = None, | ||
| extend_inferencedata: bool = True, | ||
| model: Optional[Model] = None, | ||
| kind="likelihood", | ||
| sample_dims: Sequence[str] = ("chain", "draw"), | ||
| progressbar=True, | ||
| ): | ||
| """ | ||
| Compute elemwise log_likelihood or log_prior of model given InferenceData with posterior group | ||
| """ | ||
|
|
||
| posterior = idata["posterior"] | ||
|
|
||
| model = modelcontext(model) | ||
|
|
||
| if kind not in ("likelihood", "prior"): | ||
| raise ValueError("kind must be either 'likelihood' or 'prior'") | ||
|
|
||
| if kind == "likelihood": | ||
| target_rvs = model.observed_RVs | ||
| target_str = "observed_RVs" | ||
| else: | ||
| target_rvs = model.unobserved_RVs | ||
| target_str = "free_RVs" | ||
|
|
||
| if var_names is None: | ||
| vars = target_rvs | ||
| var_names = tuple(rv.name for rv in vars) | ||
| else: | ||
| vars = [model.named_vars[name] for name in var_names] | ||
| if not set(vars).issubset(target_rvs): | ||
| raise ValueError(f"var_names must refer to {target_str} in the model. Got: {var_names}") | ||
|
|
||
| # We need to temporarily disable transforms, because the InferenceData only keeps the untransformed values | ||
| try: | ||
| original_rvs_to_values = model.rvs_to_values | ||
| original_rvs_to_transforms = model.rvs_to_transforms | ||
|
|
||
| model.rvs_to_values = { | ||
| rv: rv.clone() if rv not in model.observed_RVs else value | ||
| for rv, value in model.rvs_to_values.items() | ||
| } | ||
| model.rvs_to_transforms = {rv: None for rv in model.basic_RVs} | ||
|
|
||
| elemwise_logdens_fn = model.compile_fn( | ||
| inputs=model.value_vars, | ||
| outs=model.logp(vars=vars, sum=False), | ||
| on_unused_input="ignore", | ||
| ) | ||
| elemwise_logdens_fn = cast(PointFunc, elemwise_logdens_fn) | ||
| finally: | ||
| model.rvs_to_values = original_rvs_to_values | ||
| model.rvs_to_transforms = original_rvs_to_transforms | ||
|
|
||
| # Ignore Deterministics | ||
| posterior_values = posterior[[rv.name for rv in model.free_RVs]] | ||
| posterior_pts, stacked_dims = dataset_to_point_list(posterior_values, sample_dims) | ||
|
|
||
| n_pts = len(posterior_pts) | ||
| logdens_dict = _DefaultTrace(n_pts) | ||
| indices = range(n_pts) | ||
| if progressbar: | ||
| indices = progress_bar(indices, total=n_pts, display=progressbar) | ||
|
|
||
| for idx in indices: | ||
| logdenss_pts = elemwise_logdens_fn(posterior_pts[idx]) | ||
| for rv_name, rv_logdens in zip(var_names, logdenss_pts): | ||
| logdens_dict.insert(rv_name, rv_logdens, idx) | ||
|
|
||
| logdens_trace = logdens_dict.trace_dict | ||
| for key, array in logdens_trace.items(): | ||
| logdens_trace[key] = array.reshape( | ||
| (*[len(coord) for coord in stacked_dims.values()], *array.shape[1:]) | ||
| ) | ||
|
|
||
| coords, dims = coords_and_dims_for_inferencedata(model) | ||
| logdens_dataset = dict_to_dataset( | ||
| logdens_trace, | ||
| library=pymc, | ||
| dims=dims, | ||
| coords=coords, | ||
| default_dims=list(sample_dims), | ||
| skip_event_dims=True, | ||
| ) | ||
|
|
||
| if extend_inferencedata: | ||
| idata.add_groups({f"log_{kind}": logdens_dataset}) | ||
| return idata | ||
| else: | ||
| return logdens_dataset | ||
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 |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Copyright 2024 The PyMC Developers | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from collections.abc import Sequence | ||
| from typing import Optional | ||
|
|
||
| from arviz import InferenceData | ||
|
|
||
| from pymc.model import Model | ||
| from pymc.stats.log_density import compute_log_density | ||
|
|
||
| __all__ = "compute_log_prior" | ||
|
|
||
|
|
||
| def compute_log_prior( | ||
| idata: InferenceData, | ||
| var_names: Optional[Sequence[str]] = None, | ||
| extend_inferencedata: bool = True, | ||
| model: Optional[Model] = None, | ||
| sample_dims: Sequence[str] = ("chain", "draw"), | ||
| progressbar=True, | ||
| ): | ||
| """Compute elemwise log_prior of model given InferenceData with posterior group | ||
|
|
||
| Parameters | ||
| ---------- | ||
| idata : InferenceData | ||
| InferenceData with posterior group | ||
| var_names : sequence of str, optional | ||
| List of Observed variable names for which to compute log_prior. | ||
| Defaults to all all free variables. | ||
| extend_inferencedata : bool, default True | ||
| Whether to extend the original InferenceData or return a new one | ||
| model : Model, optional | ||
| sample_dims : sequence of str, default ("chain", "draw") | ||
| progressbar : bool, default True | ||
|
|
||
| Returns | ||
| ------- | ||
| idata : InferenceData | ||
| InferenceData with log_prior group | ||
| """ | ||
| return compute_log_density( | ||
| idata=idata, | ||
| var_names=var_names, | ||
| extend_inferencedata=extend_inferencedata, | ||
| model=model, | ||
| kind="prior", | ||
| sample_dims=sample_dims, | ||
| progressbar=progressbar, | ||
| ) |
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,48 @@ | ||
| # Copyright 2024 The PyMC Developers | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import numpy as np | ||
| import pytest | ||
| import scipy.stats as st | ||
|
|
||
| from arviz import InferenceData, dict_to_dataset | ||
|
|
||
| from pymc.distributions import Normal | ||
| from pymc.distributions.transforms import log | ||
| from pymc.model import Model | ||
| from pymc.stats.log_prior import compute_log_prior | ||
|
|
||
|
|
||
| class TestComputeLogPrior: | ||
| @pytest.mark.parametrize("transform", (False, True)) | ||
| def test_basic(self, transform): | ||
| transform = log if transform else None | ||
| with Model() as m: | ||
| x = Normal("x", transform=transform) | ||
| x_value_var = m.rvs_to_values[x] | ||
| Normal("y", x, observed=[0, 1, 2]) | ||
|
|
||
| idata = InferenceData(posterior=dict_to_dataset({"x": np.arange(100).reshape(4, 25)})) | ||
| res = compute_log_prior(idata) | ||
|
|
||
| # Check we didn't erase the original mappings | ||
| assert m.rvs_to_values[x] is x_value_var | ||
| assert m.rvs_to_transforms[x] is transform | ||
|
|
||
| assert res is idata | ||
| assert res.log_prior.dims == {"chain": 4, "draw": 25} | ||
|
|
||
| np.testing.assert_allclose( | ||
| res.log_prior["x"].values, | ||
| st.norm(0, 1).logpdf(idata.posterior["x"].values), | ||
| ) |
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.