-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wrapper for fine distributions (#115)
- Loading branch information
1 parent
136eb7d
commit c8035ef
Showing
7 changed files
with
83 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 @@ | ||
"""A wrapper from TFP distributions to BMI samplers.""" | ||
from typing import Optional, Union | ||
|
||
import jax | ||
|
||
from bmi.samplers._tfp._core import JointDistribution, monte_carlo_mi_estimate | ||
from bmi.samplers.base import BaseSampler, KeyArray, cast_to_rng | ||
|
||
|
||
class FineSampler(BaseSampler): | ||
"""Wrapper around a fine distribution.""" | ||
|
||
def __init__( | ||
self, | ||
dist: JointDistribution, | ||
mi: Optional[float] = None, | ||
mi_estimate_seed: Union[KeyArray, int] = 0, | ||
mi_estimate_sample: int = 200_000, | ||
) -> None: | ||
""" | ||
Args: | ||
dist: fine distribution to be wrapped | ||
mi: mutual information of the fine distribution, if already calculated. | ||
If not provided, it will be estimated via Monte Carlo sampling. | ||
mi_estimate_seed: seed for the Monte Carlo sampling | ||
mi_estimate_sample: number of samples for the Monte Carlo sampling | ||
""" | ||
super().__init__(dim_x=dist.dim_x, dim_y=dist.dim_y) | ||
self._dist = dist | ||
|
||
if mi is None: | ||
rng = cast_to_rng(mi_estimate_seed) | ||
self._mi, self._mi_stderr = monte_carlo_mi_estimate( | ||
key=rng, dist=self._dist, n=mi_estimate_sample | ||
) | ||
else: | ||
self._mi = mi | ||
self._mi_stderr = None | ||
|
||
def sample( | ||
self, n_points: int, rng: Union[int, KeyArray] | ||
) -> tuple[jax.numpy.ndarray, jax.numpy.ndarray]: | ||
key = cast_to_rng(rng) | ||
return self._dist.sample(n_points=n_points, key=key) | ||
|
||
def mutual_information(self) -> float: | ||
return self._mi |
This file contains 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 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 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,19 @@ | ||
import jax.numpy as jnp | ||
import pytest | ||
|
||
from bmi.samplers import fine | ||
|
||
|
||
def test_can_create_sampler() -> None: | ||
dist = fine.MultivariateNormalDistribution( | ||
dim_x=1, dim_y=1, covariance=jnp.asarray([[1, 0.5], [0.5, 1]]) | ||
) | ||
mi = -0.5 * jnp.log(1 - 0.5**2) | ||
|
||
sampler = fine.FineSampler(dist=dist, mi_estimate_seed=0, mi_estimate_sample=1_000) | ||
|
||
x_sample, y_sample = sampler.sample(n_points=10, rng=0) | ||
assert x_sample.shape == (10, 1) | ||
assert y_sample.shape == (10, 1) | ||
|
||
assert sampler.mutual_information() == pytest.approx(mi, abs=0.01) |