Skip to content
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

MCMCSamplingAlgorithm -> SamplingAlgorithm #542

Merged
merged 2 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion blackjax/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __call__(self, rng_key: PRNGKey, state: State) -> Tuple[State, Info]:
"""


class MCMCSamplingAlgorithm(NamedTuple):
class SamplingAlgorithm(NamedTuple):
"""A pair of functions that represents a MCMC sampling algorithm.

Blackjax sampling algorithms are implemented as a pair of pure functions: a
Expand Down
8 changes: 4 additions & 4 deletions blackjax/mcmc/elliptical_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import jax
import jax.numpy as jnp

from blackjax.base import MCMCSamplingAlgorithm
from blackjax.base import SamplingAlgorithm
from blackjax.types import Array, ArrayLikeTree, ArrayTree, PRNGKey
from blackjax.util import generate_gaussian_noise

Expand Down Expand Up @@ -149,7 +149,7 @@ class elliptical_slice:

Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.
"""

init = staticmethod(init)
Expand All @@ -161,7 +161,7 @@ def __new__( # type: ignore[misc]
*,
mean: Array,
cov: Array,
) -> MCMCSamplingAlgorithm:
) -> SamplingAlgorithm:
kernel = cls.build_kernel(cov, mean)

def init_fn(position: ArrayLikeTree):
Expand All @@ -174,7 +174,7 @@ def step_fn(rng_key: PRNGKey, state):
loglikelihood_fn,
)

return MCMCSamplingAlgorithm(init_fn, step_fn)
return SamplingAlgorithm(init_fn, step_fn)


def elliptical_proposal(
Expand Down
8 changes: 4 additions & 4 deletions blackjax/mcmc/ghmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import blackjax.mcmc.integrators as integrators
import blackjax.mcmc.metrics as metrics
import blackjax.mcmc.proposal as proposal
from blackjax.base import MCMCSamplingAlgorithm
from blackjax.base import SamplingAlgorithm
from blackjax.types import ArrayLikeTree, ArrayTree, PRNGKey
from blackjax.util import generate_gaussian_noise, pytree_size

Expand Down Expand Up @@ -253,7 +253,7 @@ class ghmc:

Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.
"""

init = staticmethod(init)
Expand All @@ -269,7 +269,7 @@ def __new__( # type: ignore[misc]
*,
divergence_threshold: int = 1000,
noise_gn: Callable = lambda _: 0.0,
) -> MCMCSamplingAlgorithm:
) -> SamplingAlgorithm:
kernel = cls.build_kernel(noise_gn, divergence_threshold)

def init_fn(position: ArrayLikeTree, rng_key: PRNGKey):
Expand All @@ -286,4 +286,4 @@ def step_fn(rng_key: PRNGKey, state):
delta,
)

return MCMCSamplingAlgorithm(init_fn, step_fn) # type: ignore[arg-type]
return SamplingAlgorithm(init_fn, step_fn) # type: ignore[arg-type]
8 changes: 4 additions & 4 deletions blackjax/mcmc/hmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import blackjax.mcmc.metrics as metrics
import blackjax.mcmc.proposal as proposal
import blackjax.mcmc.trajectory as trajectory
from blackjax.base import MCMCSamplingAlgorithm
from blackjax.base import SamplingAlgorithm
from blackjax.mcmc.trajectory import hmc_energy
from blackjax.types import Array, ArrayLikeTree, ArrayTree, PRNGKey

Expand Down Expand Up @@ -205,7 +205,7 @@ class hmc:

Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.
"""

init = staticmethod(init)
Expand All @@ -220,7 +220,7 @@ def __new__( # type: ignore[misc]
*,
divergence_threshold: int = 1000,
integrator: Callable = integrators.velocity_verlet,
) -> MCMCSamplingAlgorithm:
) -> SamplingAlgorithm:
kernel = cls.build_kernel(integrator, divergence_threshold)

def init_fn(position: ArrayLikeTree):
Expand All @@ -236,7 +236,7 @@ def step_fn(rng_key: PRNGKey, state):
num_integration_steps,
)

return MCMCSamplingAlgorithm(init_fn, step_fn)
return SamplingAlgorithm(init_fn, step_fn)


def hmc_proposal(
Expand Down
8 changes: 4 additions & 4 deletions blackjax/mcmc/mala.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import blackjax.mcmc.diffusions as diffusions
import blackjax.mcmc.proposal as proposal
from blackjax.base import MCMCSamplingAlgorithm
from blackjax.base import SamplingAlgorithm
from blackjax.types import ArrayLikeTree, ArrayTree, PRNGKey

__all__ = ["MALAState", "MALAInfo", "init", "build_kernel", "mala"]
Expand Down Expand Up @@ -165,7 +165,7 @@ class mala:

Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.

"""

Expand All @@ -176,7 +176,7 @@ def __new__( # type: ignore[misc]
cls,
logdensity_fn: Callable,
step_size: float,
) -> MCMCSamplingAlgorithm:
) -> SamplingAlgorithm:
kernel = cls.build_kernel()

def init_fn(position: ArrayLikeTree):
Expand All @@ -185,4 +185,4 @@ def init_fn(position: ArrayLikeTree):
def step_fn(rng_key: PRNGKey, state):
return kernel(rng_key, state, logdensity_fn, step_size)

return MCMCSamplingAlgorithm(init_fn, step_fn)
return SamplingAlgorithm(init_fn, step_fn)
8 changes: 4 additions & 4 deletions blackjax/mcmc/marginal_latent_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import jax.numpy as jnp
import jax.scipy.linalg as linalg

from blackjax.base import MCMCSamplingAlgorithm
from blackjax.base import SamplingAlgorithm
from blackjax.types import Array, PRNGKey

__all__ = ["MarginalState", "MarginalInfo", "init_and_kernel", "mgrad_gaussian"]
Expand Down Expand Up @@ -173,7 +173,7 @@ class mgrad_gaussian:

Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.

"""

Expand All @@ -182,7 +182,7 @@ def __new__( # type: ignore[misc]
logdensity_fn: Callable,
covariance: Array,
mean: Optional[Array] = None,
) -> MCMCSamplingAlgorithm:
) -> SamplingAlgorithm:
init, kernel = init_and_kernel(logdensity_fn, covariance, mean)

def init_fn(position: Array):
Expand All @@ -195,4 +195,4 @@ def step_fn(rng_key: PRNGKey, state, delta: float):
delta,
)

return MCMCSamplingAlgorithm(init_fn, step_fn) # type: ignore[arg-type]
return SamplingAlgorithm(init_fn, step_fn) # type: ignore[arg-type]
8 changes: 4 additions & 4 deletions blackjax/mcmc/nuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import blackjax.mcmc.proposal as proposal
import blackjax.mcmc.termination as termination
import blackjax.mcmc.trajectory as trajectory
from blackjax.base import MCMCSamplingAlgorithm
from blackjax.base import SamplingAlgorithm
from blackjax.types import Array, ArrayLikeTree, ArrayTree, PRNGKey

__all__ = ["NUTSInfo", "init", "build_kernel", "nuts"]
Expand Down Expand Up @@ -207,7 +207,7 @@ class nuts:

Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.

"""

Expand All @@ -223,7 +223,7 @@ def __new__( # type: ignore[misc]
max_num_doublings: int = 10,
divergence_threshold: int = 1000,
integrator: Callable = integrators.velocity_verlet,
) -> MCMCSamplingAlgorithm:
) -> SamplingAlgorithm:
kernel = cls.build_kernel(integrator, divergence_threshold, max_num_doublings)

def init_fn(position: ArrayLikeTree):
Expand All @@ -238,7 +238,7 @@ def step_fn(rng_key: PRNGKey, state):
inverse_mass_matrix,
)

return MCMCSamplingAlgorithm(init_fn, step_fn)
return SamplingAlgorithm(init_fn, step_fn)


def iterative_nuts_proposal(
Expand Down
8 changes: 4 additions & 4 deletions blackjax/mcmc/periodic_orbital.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import blackjax.mcmc.integrators as integrators
import blackjax.mcmc.metrics as metrics
from blackjax.base import MCMCSamplingAlgorithm
from blackjax.base import SamplingAlgorithm
from blackjax.types import Array, ArrayLikeTree, ArrayTree, PRNGKey

__all__ = ["PeriodicOrbitalState", "init", "build_kernel", "orbital_hmc"]
Expand Down Expand Up @@ -259,7 +259,7 @@ class orbital_hmc:

Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.
"""

init = staticmethod(init)
Expand All @@ -273,7 +273,7 @@ def __new__( # type: ignore[misc]
period: int,
*,
bijection: Callable = integrators.velocity_verlet,
) -> MCMCSamplingAlgorithm:
) -> SamplingAlgorithm:
kernel = cls.build_kernel(bijection)

def init_fn(position: ArrayLikeTree):
Expand All @@ -289,7 +289,7 @@ def step_fn(rng_key: PRNGKey, state):
period,
)

return MCMCSamplingAlgorithm(init_fn, step_fn)
return SamplingAlgorithm(init_fn, step_fn)


def periodic_orbital_proposal(
Expand Down
22 changes: 11 additions & 11 deletions blackjax/mcmc/random_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
import numpy as np
from jax import numpy as jnp

from blackjax.base import MCMCSamplingAlgorithm
from blackjax.base import SamplingAlgorithm
from blackjax.mcmc import proposal
from blackjax.types import Array, ArrayLikeTree, ArrayTree, PRNGKey
from blackjax.util import generate_gaussian_noise
Expand Down Expand Up @@ -210,7 +210,7 @@ class additive_step_random_walk:

Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.
"""

init = staticmethod(init)
Expand All @@ -227,13 +227,13 @@ def normal_random_walk(cls, logdensity_fn: Callable, sigma):
The value of the covariance matrix of the gaussian proposal distribution.
Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.
"""
return cls(logdensity_fn, normal(sigma))

def __new__( # type: ignore[misc]
cls, logdensity_fn: Callable, random_step: Callable
) -> MCMCSamplingAlgorithm:
) -> SamplingAlgorithm:
kernel = cls.build_kernel()

def init_fn(position: ArrayLikeTree):
Expand All @@ -242,7 +242,7 @@ def init_fn(position: ArrayLikeTree):
def step_fn(rng_key: PRNGKey, state):
return kernel(rng_key, state, logdensity_fn, random_step)

return MCMCSamplingAlgorithm(init_fn, step_fn)
return SamplingAlgorithm(init_fn, step_fn)


def build_irmh() -> Callable:
Expand Down Expand Up @@ -313,7 +313,7 @@ class irmh:

Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.

"""

Expand All @@ -324,7 +324,7 @@ def __new__( # type: ignore[misc]
cls,
logdensity_fn: Callable,
proposal_distribution: Callable,
) -> MCMCSamplingAlgorithm:
) -> SamplingAlgorithm:
kernel = cls.build_kernel()

def init_fn(position: ArrayLikeTree):
Expand All @@ -333,7 +333,7 @@ def init_fn(position: ArrayLikeTree):
def step_fn(rng_key: PRNGKey, state):
return kernel(rng_key, state, logdensity_fn, proposal_distribution)

return MCMCSamplingAlgorithm(init_fn, step_fn)
return SamplingAlgorithm(init_fn, step_fn)


def build_rmh():
Expand Down Expand Up @@ -428,7 +428,7 @@ class rmh:

Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.
"""

init = staticmethod(init)
Expand All @@ -439,7 +439,7 @@ def __new__( # type: ignore[misc]
logdensity_fn: Callable,
proposal_generator: Callable[[PRNGKey, ArrayLikeTree], ArrayTree],
proposal_logdensity_fn: Optional[Callable[[ArrayLikeTree], ArrayTree]] = None,
) -> MCMCSamplingAlgorithm:
) -> SamplingAlgorithm:
kernel = cls.build_kernel()

def init_fn(position: ArrayLikeTree):
Expand All @@ -454,7 +454,7 @@ def step_fn(rng_key: PRNGKey, state):
proposal_logdensity_fn,
)

return MCMCSamplingAlgorithm(init_fn, step_fn)
return SamplingAlgorithm(init_fn, step_fn)


def build_rmh_transition_energy(proposal_logdensity_fn: Optional[Callable]) -> Callable:
Expand Down
8 changes: 4 additions & 4 deletions blackjax/sgmcmc/csgld.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import jax
import jax.numpy as jnp

from blackjax.base import MCMCSamplingAlgorithm
from blackjax.base import SamplingAlgorithm
from blackjax.sgmcmc.diffusions import overdamped_langevin
from blackjax.types import Array, ArrayLikeTree, ArrayTree, PRNGKey

Expand Down Expand Up @@ -206,7 +206,7 @@ class csgld:

Returns
-------
A ``MCMCSamplingAlgorithm``.
A ``SamplingAlgorithm``.

"""
init = staticmethod(init)
Expand All @@ -220,7 +220,7 @@ def __new__( # type: ignore[misc]
num_partitions: int = 512,
energy_gap: float = 100,
min_energy: float = 0,
) -> MCMCSamplingAlgorithm:
) -> SamplingAlgorithm:
kernel = cls.build_kernel(num_partitions, energy_gap, min_energy)

def init_fn(position: ArrayLikeTree):
Expand All @@ -246,4 +246,4 @@ def step_fn(
temperature,
)

return MCMCSamplingAlgorithm(init_fn, step_fn) # type: ignore[arg-type]
return SamplingAlgorithm(init_fn, step_fn) # type: ignore[arg-type]
Loading