code
stringlengths
66
870k
docstring
stringlengths
19
26.7k
func_name
stringlengths
1
138
language
stringclasses
1 value
repo
stringlengths
7
68
path
stringlengths
5
324
url
stringlengths
46
389
license
stringclasses
7 values
def minimize_lbfgs( fun: Callable, x0: ArrayLikeTree, maxiter: int = 30, maxcor: float = 10, gtol: float = 1e-08, ftol: float = 1e-05, maxls: int = 1000, **lbfgs_kwargs, ) -> tuple[OptStep, LBFGSHistory]: """ Minimize a function using L-BFGS Parameters ---------- fun...
Minimize a function using L-BFGS Parameters ---------- fun: function of the form f(x) where x is a pytree and returns a real scalar. The function should be composed of operations with vjp defined. x0: initial guess maxiter: maximum number of iterations maxco...
minimize_lbfgs
python
blackjax-devs/blackjax
blackjax/optimizers/lbfgs.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/optimizers/lbfgs.py
Apache-2.0
def lbfgs_recover_alpha(alpha_lm1, s_l, z_l, epsilon=1e-12): """ Compute diagonal elements of the inverse Hessian approximation from optimation path. It implements the inner loop body of Algorithm 3 in :cite:p:`zhang2022pathfinder`. Parameters ---------- alpha_lm1 The diagonal element o...
Compute diagonal elements of the inverse Hessian approximation from optimation path. It implements the inner loop body of Algorithm 3 in :cite:p:`zhang2022pathfinder`. Parameters ---------- alpha_lm1 The diagonal element of the inverse Hessian approximation of the previous iteration s_...
lbfgs_recover_alpha
python
blackjax-devs/blackjax
blackjax/optimizers/lbfgs.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/optimizers/lbfgs.py
Apache-2.0
def lbfgs_inverse_hessian_factors(S, Z, alpha): """ Calculates factors for inverse hessian factored representation. It implements formula II.2 of: Pathfinder: Parallel quasi-newton variational inference, Lu Zhang et al., arXiv:2108.03782 """ param_dims = S.shape[-1] StZ = S.T @ Z R = j...
Calculates factors for inverse hessian factored representation. It implements formula II.2 of: Pathfinder: Parallel quasi-newton variational inference, Lu Zhang et al., arXiv:2108.03782
lbfgs_inverse_hessian_factors
python
blackjax-devs/blackjax
blackjax/optimizers/lbfgs.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/optimizers/lbfgs.py
Apache-2.0
def lbfgs_inverse_hessian_formula_2(alpha, beta, gamma): """ Calculates inverse hessian from factors as in formula II.3 of: Pathfinder: Parallel quasi-newton variational inference, Lu Zhang et al., arXiv:2108.03782 """ param_dims = alpha.shape[0] dsqrt_alpha = jnp.diag(jnp.sqrt(alpha)) ids...
Calculates inverse hessian from factors as in formula II.3 of: Pathfinder: Parallel quasi-newton variational inference, Lu Zhang et al., arXiv:2108.03782
lbfgs_inverse_hessian_formula_2
python
blackjax-devs/blackjax
blackjax/optimizers/lbfgs.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/optimizers/lbfgs.py
Apache-2.0
def bfgs_sample(rng_key, num_samples, position, grad_position, alpha, beta, gamma): """ Draws approximate samples of target distribution. It implements Algorithm 4 in: Pathfinder: Parallel quasi-newton variational inference, Lu Zhang et al., arXiv:2108.03782 """ if not isinstance(num_samples, ...
Draws approximate samples of target distribution. It implements Algorithm 4 in: Pathfinder: Parallel quasi-newton variational inference, Lu Zhang et al., arXiv:2108.03782
bfgs_sample
python
blackjax-devs/blackjax
blackjax/optimizers/lbfgs.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/optimizers/lbfgs.py
Apache-2.0
def as_top_level_api( logdensity_estimator: Callable, gradient_estimator: Callable, zeta: float = 1, num_partitions: int = 512, energy_gap: float = 100, min_energy: float = 0, ) -> SamplingAlgorithm: r"""Implements the (basic) user interface for the Contour SGLD kernel. Parameters -...
Implements the (basic) user interface for the Contour SGLD kernel. Parameters ---------- logdensity_estimator A function that returns an estimation of the model's logdensity given a position and a batch of data. gradient_estimator A function that takes a position, a batch of dat...
as_top_level_api
python
blackjax-devs/blackjax
blackjax/sgmcmc/csgld.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/csgld.py
Apache-2.0
def overdamped_langevin(): """Euler solver for overdamped Langevin diffusion. This algorithm was ported from :cite:p:`coullon2022sgmcmcjax`. """ def one_step( rng_key: PRNGKey, position: ArrayLikeTree, logdensity_grad: ArrayLikeTree, step_size: float, temperatu...
Euler solver for overdamped Langevin diffusion. This algorithm was ported from :cite:p:`coullon2022sgmcmcjax`.
overdamped_langevin
python
blackjax-devs/blackjax
blackjax/sgmcmc/diffusions.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/diffusions.py
Apache-2.0
def sghmc(alpha: float = 0.01, beta: float = 0): """Euler solver for the diffusion equation of the SGHMC algorithm :cite:p:`chen2014stochastic`, with parameters alpha and beta scaled according to :cite:p:`ma2015complete`. This algorithm was ported from :cite:p:`coullon2022sgmcmcjax`. """ def one_...
Euler solver for the diffusion equation of the SGHMC algorithm :cite:p:`chen2014stochastic`, with parameters alpha and beta scaled according to :cite:p:`ma2015complete`. This algorithm was ported from :cite:p:`coullon2022sgmcmcjax`.
sghmc
python
blackjax-devs/blackjax
blackjax/sgmcmc/diffusions.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/diffusions.py
Apache-2.0
def sgnht(alpha: float = 0.01, beta: float = 0): """Euler solver for the diffusion equation of the SGNHT algorithm :cite:p:`ding2014bayesian`. This algorithm was ported from :cite:p:`coullon2022sgmcmcjax`. """ def one_step( rng_key: PRNGKey, position: ArrayLikeTree, momentum: ...
Euler solver for the diffusion equation of the SGNHT algorithm :cite:p:`ding2014bayesian`. This algorithm was ported from :cite:p:`coullon2022sgmcmcjax`.
sgnht
python
blackjax-devs/blackjax
blackjax/sgmcmc/diffusions.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/diffusions.py
Apache-2.0
def logdensity_estimator( logprior_fn: Callable, loglikelihood_fn: Callable, data_size: int ) -> Callable: """Builds a simple estimator for the log-density. This estimator first appeared in :cite:p:`robbins1951stochastic`. The `logprior_fn` function has a single argument: the current position (value o...
Builds a simple estimator for the log-density. This estimator first appeared in :cite:p:`robbins1951stochastic`. The `logprior_fn` function has a single argument: the current position (value of parameters). The `loglikelihood_fn` takes two arguments: the current position and a batch of data; if there ...
logdensity_estimator
python
blackjax-devs/blackjax
blackjax/sgmcmc/gradients.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/gradients.py
Apache-2.0
def logdensity_estimator_fn( position: ArrayLikeTree, minibatch: ArrayLikeTree ) -> ArrayTree: """Return an approximation of the log-posterior density. Parameters ---------- position The current value of the random variables. batch The current...
Return an approximation of the log-posterior density. Parameters ---------- position The current value of the random variables. batch The current batch of data Returns ------- An approximation of the value of the log-posterior density fun...
logdensity_estimator_fn
python
blackjax-devs/blackjax
blackjax/sgmcmc/gradients.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/gradients.py
Apache-2.0
def grad_estimator( logprior_fn: Callable, loglikelihood_fn: Callable, data_size: int ) -> Callable: """Build a simple estimator for the gradient of the log-density.""" logdensity_estimator_fn = logdensity_estimator( logprior_fn, loglikelihood_fn, data_size ) return jax.grad(logdensity_esti...
Build a simple estimator for the gradient of the log-density.
grad_estimator
python
blackjax-devs/blackjax
blackjax/sgmcmc/gradients.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/gradients.py
Apache-2.0
def control_variates( logdensity_grad_estimator: Callable, centering_position: ArrayLikeTree, data: ArrayLikeTree, ) -> Callable: """Builds a control variate gradient estimator :cite:p:`baker2019control`. This algorithm was ported from :cite:p:`coullon2022sgmcmcjax`. Parameters ---------- ...
Builds a control variate gradient estimator :cite:p:`baker2019control`. This algorithm was ported from :cite:p:`coullon2022sgmcmcjax`. Parameters ---------- logdensity_grad_estimator A function that approximates the target's gradient function. data The full dataset. centering_p...
control_variates
python
blackjax-devs/blackjax
blackjax/sgmcmc/gradients.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/gradients.py
Apache-2.0
def cv_grad_estimator_fn( position: ArrayLikeTree, minibatch: ArrayLikeTree ) -> ArrayTree: """Return an approximation of the log-posterior density. Parameters ---------- position The current value of the random variables. batch The current ba...
Return an approximation of the log-posterior density. Parameters ---------- position The current value of the random variables. batch The current batch of data. The first dimension is assumed to be the batch dimension. Returns -------...
cv_grad_estimator_fn
python
blackjax-devs/blackjax
blackjax/sgmcmc/gradients.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/gradients.py
Apache-2.0
def build_kernel(alpha: float = 0.01, beta: float = 0) -> Callable: """Stochastic gradient Hamiltonian Monte Carlo (SgHMC) algorithm.""" integrator = diffusions.sghmc(alpha, beta) def kernel( rng_key: PRNGKey, position: ArrayLikeTree, grad_estimator: Callable, minibatch: Arr...
Stochastic gradient Hamiltonian Monte Carlo (SgHMC) algorithm.
build_kernel
python
blackjax-devs/blackjax
blackjax/sgmcmc/sghmc.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/sghmc.py
Apache-2.0
def as_top_level_api( grad_estimator: Callable, num_integration_steps: int = 10, alpha: float = 0.01, beta: float = 0, ) -> SamplingAlgorithm: """Implements the (basic) user interface for the SGHMC kernel. The general sghmc kernel builder (:meth:`blackjax.sgmcmc.sghmc.build_kernel`, alias `...
Implements the (basic) user interface for the SGHMC kernel. The general sghmc kernel builder (:meth:`blackjax.sgmcmc.sghmc.build_kernel`, alias `blackjax.sghmc.build_kernel`) can be cumbersome to manipulate. Since most users only need to specify the kernel parameters at initialization time, we provide ...
as_top_level_api
python
blackjax-devs/blackjax
blackjax/sgmcmc/sghmc.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/sghmc.py
Apache-2.0
def build_kernel() -> Callable: """Stochastic gradient Langevin Dynamics (SgLD) algorithm.""" integrator = diffusions.overdamped_langevin() def kernel( rng_key: PRNGKey, position: ArrayLikeTree, grad_estimator: Callable, minibatch: ArrayLikeTree, step_size: float, ...
Stochastic gradient Langevin Dynamics (SgLD) algorithm.
build_kernel
python
blackjax-devs/blackjax
blackjax/sgmcmc/sgld.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/sgld.py
Apache-2.0
def as_top_level_api( grad_estimator: Callable, ) -> SamplingAlgorithm: """Implements the (basic) user interface for the SGLD kernel. The general sgld kernel builder (:meth:`blackjax.sgmcmc.sgld.build_kernel`, alias `blackjax.sgld.build_kernel`) can be cumbersome to manipulate. Since most users onl...
Implements the (basic) user interface for the SGLD kernel. The general sgld kernel builder (:meth:`blackjax.sgmcmc.sgld.build_kernel`, alias `blackjax.sgld.build_kernel`) can be cumbersome to manipulate. Since most users only need to specify the kernel parameters at initialization time, we provide a he...
as_top_level_api
python
blackjax-devs/blackjax
blackjax/sgmcmc/sgld.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/sgld.py
Apache-2.0
def as_top_level_api( grad_estimator: Callable, alpha: float = 0.01, beta: float = 0.0, ) -> SamplingAlgorithm: """Implements the (basic) user interface for the SGNHT kernel. The general sgnht kernel (:meth:`blackjax.sgmcmc.sgnht.build_kernel`, alias `blackjax.sgnht.build_kernel`) can be cumber...
Implements the (basic) user interface for the SGNHT kernel. The general sgnht kernel (:meth:`blackjax.sgmcmc.sgnht.build_kernel`, alias `blackjax.sgnht.build_kernel`) can be cumbersome to manipulate. Since most users only need to specify the kernel parameters at initialization time, we provide a helper...
as_top_level_api
python
blackjax-devs/blackjax
blackjax/sgmcmc/sgnht.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/sgmcmc/sgnht.py
Apache-2.0
def build_kernel( logprior_fn: Callable, loglikelihood_fn: Callable, mcmc_step_fn: Callable, mcmc_init_fn: Callable, resampling_fn: Callable, target_ess: float, root_solver: Callable = solver.dichotomy, **extra_parameters, ) -> Callable: r"""Build a Tempered SMC step using an adaptiv...
Build a Tempered SMC step using an adaptive schedule. Parameters ---------- logprior_fn: Callable A function that computes the log-prior density. loglikelihood_fn: Callable A function that returns the log-likelihood density. mcmc_kernel_factory: Callable A callable function ...
build_kernel
python
blackjax-devs/blackjax
blackjax/smc/adaptive_tempered.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/adaptive_tempered.py
Apache-2.0
def as_top_level_api( logprior_fn: Callable, loglikelihood_fn: Callable, mcmc_step_fn: Callable, mcmc_init_fn: Callable, mcmc_parameters: dict, resampling_fn: Callable, target_ess: float, root_solver: Callable = solver.dichotomy, num_mcmc_steps: int = 10, **extra_parameters, ) ->...
Implements the (basic) user interface for the Adaptive Tempered SMC kernel. Parameters ---------- logprior_fn The log-prior function of the model we wish to draw samples from. loglikelihood_fn The log-likelihood function of the model we wish to draw samples from. mcmc_step_fn ...
as_top_level_api
python
blackjax-devs/blackjax
blackjax/smc/adaptive_tempered.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/adaptive_tempered.py
Apache-2.0
def step( rng_key: PRNGKey, state: SMCState, update_fn: Callable, weight_fn: Callable, resample_fn: Callable, num_resampled: Optional[int] = None, ) -> tuple[SMCState, SMCInfo]: """General SMC sampling step. `update_fn` here corresponds to the Markov kernel $M_{t+1}$, and `weight_fn` ...
General SMC sampling step. `update_fn` here corresponds to the Markov kernel $M_{t+1}$, and `weight_fn` corresponds to the potential function $G_t$. We first use `update_fn` to generate new particles from the current ones, weigh these particles using `weight_fn` and resample them with `resample_fn`. ...
step
python
blackjax-devs/blackjax
blackjax/smc/base.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/base.py
Apache-2.0
def update_and_take_last( mcmc_init_fn, tempered_logposterior_fn, shared_mcmc_step_fn, num_mcmc_steps, n_particles, ): """Given N particles, runs num_mcmc_steps of a kernel starting at each particle, and returns the last values, waisting the previous num_mcmc_steps-1 samples per chain. ...
Given N particles, runs num_mcmc_steps of a kernel starting at each particle, and returns the last values, waisting the previous num_mcmc_steps-1 samples per chain.
update_and_take_last
python
blackjax-devs/blackjax
blackjax/smc/base.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/base.py
Apache-2.0
def log_ess(log_weights: Array) -> float: """Compute the effective sample size. Parameters ---------- log_weights: 1D Array log-weights of the sample Returns ------- log_ess: float The logarithm of the effective sample size """ return 2 * jsp.special.logsumexp(log_...
Compute the effective sample size. Parameters ---------- log_weights: 1D Array log-weights of the sample Returns ------- log_ess: float The logarithm of the effective sample size
log_ess
python
blackjax-devs/blackjax
blackjax/smc/ess.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/ess.py
Apache-2.0
def ess_solver( logdensity_fn: Callable, particles: ArrayLikeTree, target_ess: float, max_delta: float, root_solver: Callable, ): """ESS solver for computing the next increment of SMC tempering. Parameters ---------- logdensity_fn: Callable The log probability function we wi...
ESS solver for computing the next increment of SMC tempering. Parameters ---------- logdensity_fn: Callable The log probability function we wish to sample from. particles: SMCState Current state of the tempered SMC algorithm target_ess: float The relative ESS targeted for th...
ess_solver
python
blackjax-devs/blackjax
blackjax/smc/ess.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/ess.py
Apache-2.0
def unshared_parameters_and_step_fn(mcmc_parameters, mcmc_step_fn): """Splits MCMC parameters into two dictionaries. The shared dictionary represents the parameters common to all chains, and the unshared are different per chain. Binds the step fn using the shared parameters. """ shared_mcmc_para...
Splits MCMC parameters into two dictionaries. The shared dictionary represents the parameters common to all chains, and the unshared are different per chain. Binds the step fn using the shared parameters.
unshared_parameters_and_step_fn
python
blackjax-devs/blackjax
blackjax/smc/from_mcmc.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/from_mcmc.py
Apache-2.0
def build_kernel( mcmc_step_fn: Callable, mcmc_init_fn: Callable, resampling_fn: Callable, update_strategy: Callable = update_and_take_last, ): """SMC step from MCMC kernels. Builds MCMC kernels from the input parameters, which may change across iterations. Moreover, it defines the way such ...
SMC step from MCMC kernels. Builds MCMC kernels from the input parameters, which may change across iterations. Moreover, it defines the way such kernels are used to update the particles. This layer adapts an API defined in terms of kernels (mcmc_step_fn and mcmc_init_fn) into an API that depends on an u...
build_kernel
python
blackjax-devs/blackjax
blackjax/smc/from_mcmc.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/from_mcmc.py
Apache-2.0
def build_kernel( smc_algorithm, logprior_fn: Callable, loglikelihood_fn: Callable, mcmc_step_fn: Callable, mcmc_init_fn: Callable, resampling_fn: Callable, mcmc_parameter_update_fn: Callable[ [PRNGKey, SMCState, SMCInfo], Dict[str, ArrayTree] ], num_mcmc_steps: int = 10, ...
In the context of an SMC sampler (whose step_fn returning state has a .particles attribute), there's an inner MCMC that is used to perturbate/update each of the particles. This adaptation tunes some parameter of that MCMC, based on particles. The parameter type must be a valid JAX type. Parameters ----...
build_kernel
python
blackjax-devs/blackjax
blackjax/smc/inner_kernel_tuning.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/inner_kernel_tuning.py
Apache-2.0
def as_top_level_api( smc_algorithm, logprior_fn: Callable, loglikelihood_fn: Callable, mcmc_step_fn: Callable, mcmc_init_fn: Callable, resampling_fn: Callable, mcmc_parameter_update_fn: Callable[ [PRNGKey, SMCState, SMCInfo], Dict[str, ArrayTree] ], initial_parameter_value, ...
In the context of an SMC sampler (whose step_fn returning state has a .particles attribute), there's an inner MCMC that is used to perturbate/update each of the particles. This adaptation tunes some parameter of that MCMC, based on particles. The parameter type must be a valid JAX type. Parameters ...
as_top_level_api
python
blackjax-devs/blackjax
blackjax/smc/inner_kernel_tuning.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/inner_kernel_tuning.py
Apache-2.0
def init(particles: ArrayLikeTree, num_datapoints: int) -> PartialPosteriorsSMCState: """num_datapoints are the number of observations that could potentially be used in a partial posterior. Since the initial data_mask is all 0s, it means that no likelihood term will be added (only prior). """ num_pa...
num_datapoints are the number of observations that could potentially be used in a partial posterior. Since the initial data_mask is all 0s, it means that no likelihood term will be added (only prior).
init
python
blackjax-devs/blackjax
blackjax/smc/partial_posteriors_path.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/partial_posteriors_path.py
Apache-2.0
def build_kernel( mcmc_step_fn: Callable, mcmc_init_fn: Callable, resampling_fn: Callable, num_mcmc_steps: Optional[int], mcmc_parameters: ArrayTree, partial_logposterior_factory: Callable[[Array], Callable], update_strategy=update_and_take_last, ) -> Callable: """Build the Partial Poste...
Build the Partial Posteriors (data tempering) SMC kernel. The distribution's trajectory includes increasingly adding more datapoints to the likelihood. See Section 2.2 of https://arxiv.org/pdf/2007.11936 Parameters ---------- mcmc_step_fn A function that computes the log density of the prior...
build_kernel
python
blackjax-devs/blackjax
blackjax/smc/partial_posteriors_path.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/partial_posteriors_path.py
Apache-2.0
def as_top_level_api( mcmc_step_fn: Callable, mcmc_init_fn: Callable, mcmc_parameters: dict, resampling_fn: Callable, num_mcmc_steps, partial_logposterior_factory: Callable, update_strategy=update_and_take_last, ) -> SamplingAlgorithm: """A factory that wraps the kernel into a SamplingAl...
A factory that wraps the kernel into a SamplingAlgorithm object. See build_kernel for full documentation on the parameters.
as_top_level_api
python
blackjax-devs/blackjax
blackjax/smc/partial_posteriors_path.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/partial_posteriors_path.py
Apache-2.0
def esjd(m): """Implements ESJD (expected squared jumping distance). Inner Mahalanobis distance is computed using the Cholesky decomposition of M=LLt, and then inverting L. Whenever M is symmetrical definite positive then it must exist a Cholesky Decomposition. For example, if M is the Covariance Matrix...
Implements ESJD (expected squared jumping distance). Inner Mahalanobis distance is computed using the Cholesky decomposition of M=LLt, and then inverting L. Whenever M is symmetrical definite positive then it must exist a Cholesky Decomposition. For example, if M is the Covariance Matrix of Metropolis-Hasti...
esjd
python
blackjax-devs/blackjax
blackjax/smc/pretuning.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/pretuning.py
Apache-2.0
def update_parameter_distribution( key: PRNGKey, previous_param_samples: ArrayLikeTree, previous_particles: ArrayLikeTree, latest_particles: ArrayLikeTree, measure_of_chain_mixing: Callable, alpha: float, sigma_parameters: ArrayLikeTree, acceptance_probability: Array, ): """Given an ...
Given an existing parameter distribution that was used to mutate previous_particles into latest_particles, updates that parameter distribution by resampling from previous_param_samples after adding noise to those samples. The weights used are a linear function of the measure of chain mixing. Only works with...
update_parameter_distribution
python
blackjax-devs/blackjax
blackjax/smc/pretuning.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/pretuning.py
Apache-2.0
def build_pretune( mcmc_init_fn: Callable, mcmc_step_fn: Callable, alpha: float, sigma_parameters: ArrayLikeTree, n_particles: int, performance_of_chain_measure_factory: Callable = default_measure_factory, natural_parameters: Optional[List[str]] = None, positive_parameters: Optional[List...
Implements Buchholz et al https://arxiv.org/pdf/1808.07730 pretuning procedure. The goal is to maintain a probability distribution of parameters, in order to assign different values to each inner MCMC chain. To have performant parameters for the distribution at step t, it takes a single step, measures t...
build_pretune
python
blackjax-devs/blackjax
blackjax/smc/pretuning.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/pretuning.py
Apache-2.0
def pretune_and_update(key, state: StateWithParameterOverride, logposterior): """ Updates the parameters that need to be pretuned and returns the rest. """ new_parameter_distribution, chain_mixing_measurement = pretune( key, state, logposterior ) old_parameter...
Updates the parameters that need to be pretuned and returns the rest.
pretune_and_update
python
blackjax-devs/blackjax
blackjax/smc/pretuning.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/pretuning.py
Apache-2.0
def build_kernel( smc_algorithm, logprior_fn: Callable, loglikelihood_fn: Callable, mcmc_step_fn: Callable, mcmc_init_fn: Callable, resampling_fn: Callable, pretune_fn: Callable, num_mcmc_steps: int = 10, update_strategy=update_and_take_last, **extra_parameters, ) -> Callable: ...
In the context of an SMC sampler (whose step_fn returning state has a .particles attribute), there's an inner MCMC that is used to perturbate/update each of the particles. This adaptation tunes some parameter of that MCMC, based on particles. The parameter type must be a valid JAX type. Parameters ----...
build_kernel
python
blackjax-devs/blackjax
blackjax/smc/pretuning.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/pretuning.py
Apache-2.0
def pretuned_step( rng_key: PRNGKey, state, num_mcmc_steps: int, mcmc_parameters: dict, logposterior_fn: Callable, log_weights_fn: Callable, ) -> tuple[smc.base.SMCState, SMCInfoWithParameterDistribution]: """Wraps the output of smc.from_mcmc.build_kernel into...
Wraps the output of smc.from_mcmc.build_kernel into a pretuning + step method. This one should be a subtype of the former, in the sense that a usage of the former can be replaced with an instance of this one.
pretuned_step
python
blackjax-devs/blackjax
blackjax/smc/pretuning.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/pretuning.py
Apache-2.0
def as_top_level_api( smc_algorithm, logprior_fn: Callable, loglikelihood_fn: Callable, mcmc_step_fn: Callable, mcmc_init_fn: Callable, resampling_fn: Callable, num_mcmc_steps: int, initial_parameter_value: ArrayLikeTree, pretune_fn: Callable, **extra_parameters, ): """In the...
In the context of an SMC sampler (whose step_fn returning state has a .particles attribute), there's an inner MCMC that is used to perturbate/update each of the particles. This adaptation tunes some parameter of that MCMC, based on particles. The parameter type must be a valid JAX type. Parameters ----...
as_top_level_api
python
blackjax-devs/blackjax
blackjax/smc/pretuning.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/pretuning.py
Apache-2.0
def dichotomy(fun, min_delta, max_delta, eps=1e-4, max_iter=100): """Solves for delta by dichotomy. If max_delta is such that fun(max_delta) > 0, then we assume that max_delta can be used as an increment in the tempering. Parameters ---------- fun: Callable The decreasing function to s...
Solves for delta by dichotomy. If max_delta is such that fun(max_delta) > 0, then we assume that max_delta can be used as an increment in the tempering. Parameters ---------- fun: Callable The decreasing function to solve, we must have fun(min_delta) > 0, fun(max_delta) < 0 min_delta: ...
dichotomy
python
blackjax-devs/blackjax
blackjax/smc/solver.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/solver.py
Apache-2.0
def build_kernel( logprior_fn: Callable, loglikelihood_fn: Callable, mcmc_step_fn: Callable, mcmc_init_fn: Callable, resampling_fn: Callable, update_strategy: Callable = update_and_take_last, update_particles_fn: Optional[Callable] = None, ) -> Callable: """Build the base Tempered SMC ke...
Build the base Tempered SMC kernel. Tempered SMC uses tempering to sample from a distribution given by .. math:: p(x) \propto p_0(x) \exp(-V(x)) \mathrm{d}x where :math:`p_0` is the prior distribution, typically easy to sample from and for which the density is easy to compute, and :math:`\exp...
build_kernel
python
blackjax-devs/blackjax
blackjax/smc/tempered.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/tempered.py
Apache-2.0
def kernel( rng_key: PRNGKey, state: TemperedSMCState, num_mcmc_steps: int, lmbda: float, mcmc_parameters: dict, ) -> tuple[TemperedSMCState, smc.base.SMCInfo]: """Move the particles one step using the Tempered SMC algorithm. Parameters ---------- ...
Move the particles one step using the Tempered SMC algorithm. Parameters ---------- rng_key JAX PRNGKey for randomness state Current state of the tempered SMC algorithm lmbda Current value of the tempering parameter mcmc_parameters ...
kernel
python
blackjax-devs/blackjax
blackjax/smc/tempered.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/tempered.py
Apache-2.0
def as_top_level_api( logprior_fn: Callable, loglikelihood_fn: Callable, mcmc_step_fn: Callable, mcmc_init_fn: Callable, mcmc_parameters: dict, resampling_fn: Callable, num_mcmc_steps: Optional[int] = 10, update_strategy=update_and_take_last, update_particles_fn=None, ) -> SamplingAl...
Implements the (basic) user interface for the Adaptive Tempered SMC kernel. Parameters ---------- logprior_fn The log-prior function of the model we wish to draw samples from. loglikelihood_fn The log-likelihood function of the model we wish to draw samples from. mcmc_step_fn ...
as_top_level_api
python
blackjax-devs/blackjax
blackjax/smc/tempered.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/tempered.py
Apache-2.0
def update_waste_free( mcmc_init_fn, logposterior_fn, mcmc_step_fn, n_particles: int, p: int, num_resampled, num_mcmc_steps=None, ): """ Given M particles, mutates them using p-1 steps. Returns M*P-1 particles, consistent of the initial plus all the intermediate steps, thus imple...
Given M particles, mutates them using p-1 steps. Returns M*P-1 particles, consistent of the initial plus all the intermediate steps, thus implementing a waste-free update function See Algorithm 2: https://arxiv.org/abs/2011.02328
update_waste_free
python
blackjax-devs/blackjax
blackjax/smc/waste_free.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/waste_free.py
Apache-2.0
def update(rng_key, position, step_parameters): """ Given the initial particles, runs a chain starting at each. The combines the initial particles with all the particles generated at each step of each chain. """ states, infos = jax.vmap(mcmc_kernel)(rng_key, position, ste...
Given the initial particles, runs a chain starting at each. The combines the initial particles with all the particles generated at each step of each chain.
update
python
blackjax-devs/blackjax
blackjax/smc/waste_free.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/waste_free.py
Apache-2.0
def update_scale_from_acceptance_rate( scales: jax.Array, acceptance_rates: jax.Array, target_acceptance_rate: float = 0.234, ) -> jax.Array: """ Given N chains from some MCMC algorithm like Random Walk Metropolis and N scale factors, each associated to a different chain. Updates the scale f...
Given N chains from some MCMC algorithm like Random Walk Metropolis and N scale factors, each associated to a different chain. Updates the scale factors taking into account acceptance rates and the average acceptance rate. Under certain assumptions it is known that the optimal acceptance rate ...
update_scale_from_acceptance_rate
python
blackjax-devs/blackjax
blackjax/smc/tuning/from_kernel_info.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/smc/tuning/from_kernel_info.py
Apache-2.0
def init( position: ArrayLikeTree, optimizer: GradientTransformation, *optimizer_args, **optimizer_kwargs, ) -> MFVIState: """Initialize the mean-field VI state.""" mu = jax.tree.map(jnp.zeros_like, position) rho = jax.tree.map(lambda x: -2.0 * jnp.ones_like(x), position) opt_state = opt...
Initialize the mean-field VI state.
init
python
blackjax-devs/blackjax
blackjax/vi/meanfield_vi.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/vi/meanfield_vi.py
Apache-2.0
def step( rng_key: PRNGKey, state: MFVIState, logdensity_fn: Callable, optimizer: GradientTransformation, num_samples: int = 5, stl_estimator: bool = True, ) -> tuple[MFVIState, MFVIInfo]: """Approximate the target density using the mean-field approximation. Parameters ---------- ...
Approximate the target density using the mean-field approximation. Parameters ---------- rng_key Key for JAX's pseudo-random number generator. init_state Initial state of the mean-field approximation. logdensity_fn Function that represents the target log-density to approxima...
step
python
blackjax-devs/blackjax
blackjax/vi/meanfield_vi.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/vi/meanfield_vi.py
Apache-2.0
def as_top_level_api( logdensity_fn: Callable, optimizer: GradientTransformation, num_samples: int = 100, ): """High-level implementation of Mean-Field Variational Inference. Parameters ---------- logdensity_fn A function that represents the log-density function associated with ...
High-level implementation of Mean-Field Variational Inference. Parameters ---------- logdensity_fn A function that represents the log-density function associated with the distribution we want to sample from. optimizer Optax optimizer to use to optimize the ELBO. num_samples ...
as_top_level_api
python
blackjax-devs/blackjax
blackjax/vi/meanfield_vi.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/vi/meanfield_vi.py
Apache-2.0
def approximate( rng_key: PRNGKey, logdensity_fn: Callable, initial_position: ArrayLikeTree, num_samples: int = 200, *, # lgbfs parameters maxiter=30, maxcor=10, maxls=1000, gtol=1e-08, ftol=1e-05, **lbfgs_kwargs, ) -> tuple[PathfinderState, PathfinderInfo]: """Pathfinde...
Pathfinder variational inference algorithm. Pathfinder locates normal approximations to the target density along a quasi-Newton optimization path, with local covariance estimated using the inverse Hessian estimates produced by the L-BFGS optimizer. Function implements the algorithm 3 in :cite:p:`zhang...
approximate
python
blackjax-devs/blackjax
blackjax/vi/pathfinder.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/vi/pathfinder.py
Apache-2.0
def path_finder_body_fn(rng_key, S, Z, alpha_l, theta, theta_grad): """The for loop body in Algorithm 1 of the Pathfinder paper.""" beta, gamma = lbfgs_inverse_hessian_factors(S.T, Z.T, alpha_l) phi, logq = bfgs_sample( rng_key=rng_key, num_samples=num_samples, ...
The for loop body in Algorithm 1 of the Pathfinder paper.
path_finder_body_fn
python
blackjax-devs/blackjax
blackjax/vi/pathfinder.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/vi/pathfinder.py
Apache-2.0
def sample( rng_key: PRNGKey, state: PathfinderState, num_samples: Union[int, tuple[()], tuple[int]] = (), ) -> ArrayTree: """Draw from the Pathfinder approximation of the target distribution. Parameters ---------- rng_key PRNG key state PathfinderState containing inform...
Draw from the Pathfinder approximation of the target distribution. Parameters ---------- rng_key PRNG key state PathfinderState containing information for sampling num_samples Number of samples to draw Returns ------- Samples drawn from the approximate Pathfinde...
sample
python
blackjax-devs/blackjax
blackjax/vi/pathfinder.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/vi/pathfinder.py
Apache-2.0
def as_top_level_api(logdensity_fn: Callable) -> PathFinderAlgorithm: """Implements the (basic) user interface for the pathfinder kernel. Pathfinder locates normal approximations to the target density along a quasi-Newton optimization path, with local covariance estimated using the inverse Hessian esti...
Implements the (basic) user interface for the pathfinder kernel. Pathfinder locates normal approximations to the target density along a quasi-Newton optimization path, with local covariance estimated using the inverse Hessian estimates produced by the L-BFGS optimizer. Pathfinder returns draws from the...
as_top_level_api
python
blackjax-devs/blackjax
blackjax/vi/pathfinder.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/vi/pathfinder.py
Apache-2.0
def init( initial_particles: ArrayLikeTree, kernel_parameters: dict[str, Any], optimizer: optax.GradientTransformation, ) -> SVGDState: """ Initializes Stein Variational Gradient Descent Algorithm. Parameters ---------- initial_particles Initial set of particles to start the opt...
Initializes Stein Variational Gradient Descent Algorithm. Parameters ---------- initial_particles Initial set of particles to start the optimization kernel_paremeters Arguments to the kernel function optimizer Optax compatible optimizer, which conforms to the `optax.Gra...
init
python
blackjax-devs/blackjax
blackjax/vi/svgd.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/vi/svgd.py
Apache-2.0
def kernel( state: SVGDState, grad_logdensity_fn: Callable, kernel: Callable, **grad_params, ) -> SVGDState: """ Performs one step of Stein Variational Gradient Descent. See Algorithm 1 of :cite:p:`liu2016stein`. Parameters ---------- ...
Performs one step of Stein Variational Gradient Descent. See Algorithm 1 of :cite:p:`liu2016stein`. Parameters ---------- state SVGDState object containing information about previous iteration grad_logdensity_fn gradient, or an estimate, of the ...
kernel
python
blackjax-devs/blackjax
blackjax/vi/svgd.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/vi/svgd.py
Apache-2.0
def update_median_heuristic(state: SVGDState) -> SVGDState: """Median heuristic for setting the bandwidth of RBF kernels. A reasonable middle-ground for choosing the `length_scale` of the RBF kernel is to pick the empirical median of the squared distance between particles. This strategy is called the m...
Median heuristic for setting the bandwidth of RBF kernels. A reasonable middle-ground for choosing the `length_scale` of the RBF kernel is to pick the empirical median of the squared distance between particles. This strategy is called the median heuristic.
update_median_heuristic
python
blackjax-devs/blackjax
blackjax/vi/svgd.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/vi/svgd.py
Apache-2.0
def as_top_level_api( grad_logdensity_fn: Callable, optimizer, kernel: Callable = rbf_kernel, update_kernel_parameters: Callable = update_median_heuristic, ): """Implements the (basic) user interface for the svgd algorithm :cite:p:`liu2016stein`. Parameters ---------- grad_logdensity_fn...
Implements the (basic) user interface for the svgd algorithm :cite:p:`liu2016stein`. Parameters ---------- grad_logdensity_fn gradient, or an estimate, of the target log density function to samples approximately from optimizer Optax compatible optimizer, which conforms to the `optax.Gra...
as_top_level_api
python
blackjax-devs/blackjax
blackjax/vi/svgd.py
https://github.com/blackjax-devs/blackjax/blob/master/blackjax/vi/svgd.py
Apache-2.0
def test_hmc(self): """Count the number of times the logdensity is compiled when using HMC. The logdensity is compiled twice: when initializing the state and when compiling the kernel. """ @chex.assert_max_traces(n=2) def logdensity_fn(x): return jscipy.sta...
Count the number of times the logdensity is compiled when using HMC. The logdensity is compiled twice: when initializing the state and when compiling the kernel.
test_hmc
python
blackjax-devs/blackjax
tests/test_compilation.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/test_compilation.py
Apache-2.0
def test_nuts(self): """Count the number of times the logdensity is compiled when using NUTS. The logdensity is compiled twice: when initializing the state and when compiling the kernel. """ @chex.assert_max_traces(n=2) def logdensity_fn(x): return jscipy.s...
Count the number of times the logdensity is compiled when using NUTS. The logdensity is compiled twice: when initializing the state and when compiling the kernel.
test_nuts
python
blackjax-devs/blackjax
tests/test_compilation.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/test_compilation.py
Apache-2.0
def test_hmc_warmup(self): """Count the number of times the logdensity is compiled when using window adaptation to adapt the value of the step size and the inverse mass matrix for the HMC algorithm. """ @chex.assert_max_traces(n=3) def logdensity_fn(x): retu...
Count the number of times the logdensity is compiled when using window adaptation to adapt the value of the step size and the inverse mass matrix for the HMC algorithm.
test_hmc_warmup
python
blackjax-devs/blackjax
tests/test_compilation.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/test_compilation.py
Apache-2.0
def test_nuts_warmup(self): """Count the number of times the logdensity is compiled when using window adaptation to adapt the value of the step size and the inverse mass matrix for the NUTS algorithm. """ @chex.assert_max_traces(n=3) def logdensity_fn(x): re...
Count the number of times the logdensity is compiled when using window adaptation to adapt the value of the step size and the inverse mass matrix for the NUTS algorithm.
test_nuts_warmup
python
blackjax-devs/blackjax
tests/test_compilation.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/test_compilation.py
Apache-2.0
def check_compatible(self, initial_state, progress_bar): """ Runs 10 steps with `run_inference_algorithm` starting with `initial_state` and potentially a progress bar. """ _ = run_inference_algorithm( rng_key=self.key, initial_state=initial_state, ...
Runs 10 steps with `run_inference_algorithm` starting with `initial_state` and potentially a progress bar.
check_compatible
python
blackjax-devs/blackjax
tests/test_util.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/test_util.py
Apache-2.0
def test_preconditioning_matrix(self, seed): """Test two different ways of using pre-conditioning matrix has exactly same effect. We follow the discussion in Appendix G of the Barker 2020 paper. """ key = jax.random.key(seed) init_key, inference_key = jax.random.split(key, 2) ...
Test two different ways of using pre-conditioning matrix has exactly same effect. We follow the discussion in Appendix G of the Barker 2020 paper.
test_preconditioning_matrix
python
blackjax-devs/blackjax
tests/mcmc/test_barker.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_barker.py
Apache-2.0
def HarmonicOscillator(inv_mass_matrix, k=1.0, m=1.0): """Potential and Kinetic energy of an harmonic oscillator.""" def neg_potential_energy(q): return -jnp.sum(0.5 * k * jnp.square(q["x"])) def kinetic_energy(p, position=None): del position v = jnp.multiply(inv_mass_matrix, p["x"...
Potential and Kinetic energy of an harmonic oscillator.
HarmonicOscillator
python
blackjax-devs/blackjax
tests/mcmc/test_integrators.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_integrators.py
Apache-2.0
def FreeFall(inv_mass_matrix, g=1.0): """Potential and kinetic energy of a free-falling object.""" def neg_potential_energy(q): return -jnp.sum(g * q["x"]) def kinetic_energy(p, position=None): del position v = jnp.multiply(inv_mass_matrix, p["x"]) return jnp.sum(0.5 * jnp....
Potential and kinetic energy of a free-falling object.
FreeFall
python
blackjax-devs/blackjax
tests/mcmc/test_integrators.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_integrators.py
Apache-2.0
def PlanetaryMotion(inv_mass_matrix): """Potential and kinetic energy for planar planetary motion.""" def neg_potential_energy(q): return 1.0 / jnp.power(q["x"] ** 2 + q["y"] ** 2, 0.5) def kinetic_energy(p, position=None): del position z = jnp.stack([p["x"], p["y"]], axis=-1) ...
Potential and kinetic energy for planar planetary motion.
PlanetaryMotion
python
blackjax-devs/blackjax
tests/mcmc/test_integrators.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_integrators.py
Apache-2.0
def MultivariateNormal(inv_mass_matrix): """Potential and kinetic energy for a multivariate normal distribution.""" def log_density(q): q, _ = ravel_pytree(q) return stats.multivariate_normal.logpdf(q, jnp.zeros_like(q), inv_mass_matrix) def kinetic_energy(p, position=None): del po...
Potential and kinetic energy for a multivariate normal distribution.
MultivariateNormal
python
blackjax-devs/blackjax
tests/mcmc/test_integrators.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_integrators.py
Apache-2.0
def test_esh_momentum_update(self, dims): """ Test the numerically efficient version of the momentum update currently implemented match the naive implementation according to the Equation 16 in :cite:p:`robnik2023microcanonical` """ step_size = 1e-3 key0, key1 = ja...
Test the numerically efficient version of the momentum update currently implemented match the naive implementation according to the Equation 16 in :cite:p:`robnik2023microcanonical`
test_esh_momentum_update
python
blackjax-devs/blackjax
tests/mcmc/test_integrators.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_integrators.py
Apache-2.0
def test_non_separable(self): """Test the integration of a non-separable Hamiltonian with a known closed-form solution, as defined in https://arxiv.org/abs/1609.02212. """ def neg_potential(q): return -0.5 * (q**2 + 1) def kinetic_energy(p, position=None): ...
Test the integration of a non-separable Hamiltonian with a known closed-form solution, as defined in https://arxiv.org/abs/1609.02212.
test_non_separable
python
blackjax-devs/blackjax
tests/mcmc/test_integrators.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_integrators.py
Apache-2.0
def test_invalid(self, shape, is_inv): """Test formatting raises error for invalid shapes""" mass_matrix = jnp.zeros(shape=shape) with self.assertRaisesRegex( ValueError, "The mass matrix has the wrong number of dimensions" ): metrics._format_covariance(mass_matri...
Test formatting raises error for invalid shapes
test_invalid
python
blackjax-devs/blackjax
tests/mcmc/test_metrics.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_metrics.py
Apache-2.0
def test_gaussian_euclidean_ndim_invalid(self, shape): """Test Gaussian Euclidean Function returns correct function invalid ndim""" x = jnp.ones(shape=shape) with self.assertRaisesRegex( ValueError, "The mass matrix has the wrong number of dimensions" ): _ = metri...
Test Gaussian Euclidean Function returns correct function invalid ndim
test_gaussian_euclidean_ndim_invalid
python
blackjax-devs/blackjax
tests/mcmc/test_metrics.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_metrics.py
Apache-2.0
def test_gaussian_euclidean_dim_1(self): """Test Gaussian Euclidean Function with ndim 1""" inverse_mass_matrix = jnp.asarray([1 / 4], dtype=self.dtype) momentum, kinetic_energy, _, scale = metrics.gaussian_euclidean( inverse_mass_matrix ) arbitrary_position = jnp.as...
Test Gaussian Euclidean Function with ndim 1
test_gaussian_euclidean_dim_1
python
blackjax-devs/blackjax
tests/mcmc/test_metrics.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_metrics.py
Apache-2.0
def test_gaussian_euclidean_dim_2(self): """Test Gaussian Euclidean Function with ndim 2""" inverse_mass_matrix = jnp.asarray( [[2 / 3, 0.5], [0.5, 3 / 4]], dtype=self.dtype ) momentum, kinetic_energy, _, scale = metrics.gaussian_euclidean( inverse_mass_matrix ...
Test Gaussian Euclidean Function with ndim 2
test_gaussian_euclidean_dim_2
python
blackjax-devs/blackjax
tests/mcmc/test_metrics.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_metrics.py
Apache-2.0
def test_normal_univariate(self, initial_position): """ Move samples are generated in the univariate case, with std following sigma, and independently of the position. """ keys = jax.random.split(self.key, 200) proposal = normal(sigma=jnp.array([1.0])) samples = [...
Move samples are generated in the univariate case, with std following sigma, and independently of the position.
test_normal_univariate
python
blackjax-devs/blackjax
tests/mcmc/test_proposal.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_proposal.py
Apache-2.0
def test_one_step_addition(self): """New position is an addition to previous position. Since the density == 1, the proposal is accepted. The random step may depend on the previous position """ rng_key = jax.random.key(0) initial_position = jnp.array([50.0]) def r...
New position is an addition to previous position. Since the density == 1, the proposal is accepted. The random step may depend on the previous position
test_one_step_addition
python
blackjax-devs/blackjax
tests/mcmc/test_random_walk_without_chex.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_random_walk_without_chex.py
Apache-2.0
def test_proposal_is_independent_of_position(self): """New position does not depend on previous position""" rng_key = jax.random.key(0) initial_position = jnp.array([50.0]) other_position = jnp.array([15000.0]) step = build_irmh() for previous_position in [initial_posit...
New position does not depend on previous position
test_proposal_is_independent_of_position
python
blackjax-devs/blackjax
tests/mcmc/test_random_walk_without_chex.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_random_walk_without_chex.py
Apache-2.0
def test_non_symmetric_proposal(self): """ Given that proposal_logdensity_fn is included, thus the proposal is non-symmetric. When computing the acceptance of the proposed state Then proposal_logdensity_fn value is taken into account """ rng_key = jax.random.key(0...
Given that proposal_logdensity_fn is included, thus the proposal is non-symmetric. When computing the acceptance of the proposed state Then proposal_logdensity_fn value is taken into account
test_non_symmetric_proposal
python
blackjax-devs/blackjax
tests/mcmc/test_random_walk_without_chex.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_random_walk_without_chex.py
Apache-2.0
def test_generate_reject(self): """ Steps from previous state, Builds a proposal from the new state and given that the sampling rule rejects, the prev_state is proposed again """ rng_key = jax.random.key(0) prev_state = RWState(jnp.array([30.0]), 15.0) ...
Steps from previous state, Builds a proposal from the new state and given that the sampling rule rejects, the prev_state is proposed again
test_generate_reject
python
blackjax-devs/blackjax
tests/mcmc/test_random_walk_without_chex.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_random_walk_without_chex.py
Apache-2.0
def test_window_adaptation( self, case, is_mass_matrix_diagonal, window_adapt_config ): """Test the HMC kernel and the Stan warmup.""" rng_key, init_key0, init_key1 = jax.random.split(self.key, 3) x_data = jax.random.normal(init_key0, shape=(1000, 1)) y_data = 3 * x_data + ja...
Test the HMC kernel and the Stan warmup.
test_window_adaptation
python
blackjax-devs/blackjax
tests/mcmc/test_sampling.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_sampling.py
Apache-2.0
def __init__(self, d, condition_number): """numpy_seed is used to generate a random rotation for the covariance matrix. If None, the covariance matrix is diagonal.""" self.ndims = d self.name = "IllConditionedGaussian" self.condition_numbe...
numpy_seed is used to generate a random rotation for the covariance matrix. If None, the covariance matrix is diagonal.
__init__
python
blackjax-devs/blackjax
tests/mcmc/test_sampling.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_sampling.py
Apache-2.0
def test_pathfinder_adaptation( self, algorithm, num_warmup_steps, initial_position, num_sampling_steps, parameters, ): """Test the HMC kernel and the Stan warmup.""" rng_key, init_key0, init_key1 = jax.random.split(self.key, 3) x_data = jax.ra...
Test the HMC kernel and the Stan warmup.
test_pathfinder_adaptation
python
blackjax-devs/blackjax
tests/mcmc/test_sampling.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_sampling.py
Apache-2.0
def test_meads(self): """Test the MEADS adaptation w/ GHMC kernel.""" rng_key, init_key0, init_key1 = jax.random.split(self.key, 3) x_data = jax.random.normal(init_key0, shape=(1000, 1)) y_data = 3 * x_data + jax.random.normal(init_key1, shape=x_data.shape) logposterior_fn_ = fu...
Test the MEADS adaptation w/ GHMC kernel.
test_meads
python
blackjax-devs/blackjax
tests/mcmc/test_sampling.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_sampling.py
Apache-2.0
def test_chees(self, jitter_generator): """Test the ChEES adaptation w/ HMC kernel.""" rng_key, init_key0, init_key1 = jax.random.split(self.key, 3) x_data = jax.random.normal(init_key0, shape=(1000, 1)) y_data = 3 * x_data + jax.random.normal(init_key1, shape=x_data.shape) logp...
Test the ChEES adaptation w/ HMC kernel.
test_chees
python
blackjax-devs/blackjax
tests/mcmc/test_sampling.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_sampling.py
Apache-2.0
def generate_multivariate_target(self, rng=None): """Genrate a Multivariate Normal distribution as target.""" if rng is None: loc = jnp.array([0.0, 3]) scale = jnp.array([1.0, 2.0]) rho = jnp.array(0.75) else: loc_rng, scale_rng, rho_rng = jax.rand...
Genrate a Multivariate Normal distribution as target.
generate_multivariate_target
python
blackjax-devs/blackjax
tests/mcmc/test_sampling.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_sampling.py
Apache-2.0
def test_mcse(self, algorithm, parameters, is_mass_matrix_diagonal): """Test convergence using Monte Carlo CLT across multiple chains.""" pos_init_key, sample_key = jax.random.split(self.key) ( logdensity_fn, true_loc, true_scale, true_rho, ...
Test convergence using Monte Carlo CLT across multiple chains.
test_mcse
python
blackjax-devs/blackjax
tests/mcmc/test_sampling.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/mcmc/test_sampling.py
Apache-2.0
def test_dual_averaging(self): """We test the dual averaging algorithm by searching for the point that minimizes the gradient of a simple function. """ # we need to wrap the gradient in a namedtuple as we optimize for a target # acceptance probability in the context of HMC. ...
We test the dual averaging algorithm by searching for the point that minimizes the gradient of a simple function.
test_dual_averaging
python
blackjax-devs/blackjax
tests/optimizers/test_optimizers.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/optimizers/test_optimizers.py
Apache-2.0
def test_minimize_lbfgs(self, maxiter, maxcor): """Test if dot product between approximate inverse hessian and gradient is the same between two loop recursion algorthm of LBFGS and formulas of the pathfinder paper""" def regression_logprob(log_scale, coefs, preds, x): """Lin...
Test if dot product between approximate inverse hessian and gradient is the same between two loop recursion algorthm of LBFGS and formulas of the pathfinder paper
test_minimize_lbfgs
python
blackjax-devs/blackjax
tests/optimizers/test_optimizers.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/optimizers/test_optimizers.py
Apache-2.0
def test_recover_diag_inv_hess(self): "Compare inverse Hessian estimation from LBFGS with known groundtruth." nd = 5 mean = np.linspace(3.0, 50.0, nd) cov = np.diag(np.linspace(1.0, 10.0, nd)) def loss_fn(x): return -stats.multivariate_normal.logpdf(x, mean, cov) ...
Compare inverse Hessian estimation from LBFGS with known groundtruth.
test_recover_diag_inv_hess
python
blackjax-devs/blackjax
tests/optimizers/test_optimizers.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/optimizers/test_optimizers.py
Apache-2.0
def test_recover_posterior(self, ndim): """Test if pathfinder is able to estimate well enough the posterior of a normal-normal conjugate model""" def logp_posterior_conjugate_normal_model( x, observed, prior_mu, prior_prec, true_prec ): n = observed.shape[0] ...
Test if pathfinder is able to estimate well enough the posterior of a normal-normal conjugate model
test_recover_posterior
python
blackjax-devs/blackjax
tests/optimizers/test_pathfinder.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/optimizers/test_pathfinder.py
Apache-2.0
def test_scale_when_aceptance_below_optimal(self): """ Given that the acceptance rate is below optimal, the scale gets reduced. """ np.testing.assert_allclose( update_scale_from_acceptance_rate( scales=jnp.array([0.5]), acceptance_rates=jnp.array([0.2]...
Given that the acceptance rate is below optimal, the scale gets reduced.
test_scale_when_aceptance_below_optimal
python
blackjax-devs/blackjax
tests/smc/test_inner_kernel_tuning.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/smc/test_inner_kernel_tuning.py
Apache-2.0
def test_scale_when_aceptance_above_optimal(self): """ Given that the acceptance rate is above optimal the scale increases ------- """ np.testing.assert_allclose( update_scale_from_acceptance_rate( scales=jnp.array([0.5]), acceptance_rates=jnp....
Given that the acceptance rate is above optimal the scale increases -------
test_scale_when_aceptance_above_optimal
python
blackjax-devs/blackjax
tests/smc/test_inner_kernel_tuning.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/smc/test_inner_kernel_tuning.py
Apache-2.0
def test_scale_mean_smoothes(self): """ The end result depends on the mean acceptance rate, smoothing the results """ np.testing.assert_allclose( update_scale_from_acceptance_rate( scales=jnp.array([0.5, 0.5]), acceptance_rates=jnp.array([0.3, 0.2]) ...
The end result depends on the mean acceptance rate, smoothing the results
test_scale_mean_smoothes
python
blackjax-devs/blackjax
tests/smc/test_inner_kernel_tuning.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/smc/test_inner_kernel_tuning.py
Apache-2.0
def test_tuning_pretuning(self): """ Tests that we can apply tuning on some parameters and pretuning in some others at the same time. """ ( init_particles, logprior_fn, loglikelihood_fn, ) = self.particles_prior_loglikelihood() ...
Tests that we can apply tuning on some parameters and pretuning in some others at the same time.
test_tuning_pretuning
python
blackjax-devs/blackjax
tests/smc/test_inner_kernel_tuning.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/smc/test_inner_kernel_tuning.py
Apache-2.0
def test_measure_of_chain_mixing_identity(self): """ Given identity matrix and 1. acceptance probability then the mixing is the square of norm 2. """ m = np.eye(2) acceptance_probabilities = np.array([1.0, 1.0]) chain_mixing = esjd(m)( self.previous_p...
Given identity matrix and 1. acceptance probability then the mixing is the square of norm 2.
test_measure_of_chain_mixing_identity
python
blackjax-devs/blackjax
tests/smc/test_pretuning.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/smc/test_pretuning.py
Apache-2.0
def test_measure_of_chain_mixing_with_non_1_acceptance_rate(self): """ Given identity matrix then the mixing is the square of norm 2. multiplied by the acceptance rate """ m = np.eye(2) acceptance_probabilities = np.array([0.5, 0.2]) chain_mixing = esjd(m)( ...
Given identity matrix then the mixing is the square of norm 2. multiplied by the acceptance rate
test_measure_of_chain_mixing_with_non_1_acceptance_rate
python
blackjax-devs/blackjax
tests/smc/test_pretuning.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/smc/test_pretuning.py
Apache-2.0
def test_update_param_distribution(self): """ Given an extremely good mixing on one chain, and that the alpha parameter is 0, then the parameters of that chain with a slight mutation due to noise are reused. """ ( new_parameter_distribution, chain...
Given an extremely good mixing on one chain, and that the alpha parameter is 0, then the parameters of that chain with a slight mutation due to noise are reused.
test_update_param_distribution
python
blackjax-devs/blackjax
tests/smc/test_pretuning.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/smc/test_pretuning.py
Apache-2.0
def test_update_multi_sigmas(self): """ When we have multiple parameters, the performance is attached to its combination so sampling must work accordingly. """ ( new_parameter_distribution, chain_mixing_measurement, ) = update_parameter_distributio...
When we have multiple parameters, the performance is attached to its combination so sampling must work accordingly.
test_update_multi_sigmas
python
blackjax-devs/blackjax
tests/smc/test_pretuning.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/smc/test_pretuning.py
Apache-2.0
def test_ess_solver_multivariate(self, target_ess): """ Posterior with more than one variable. Let's assume we want to sample from P(x) x ~ N(mean, cov) x in R^{2} """ num_particles = 1000 mean = jnp.zeros((1, 2)) cov = jnp.diag(jnp.array([1, 1])) _logdens...
Posterior with more than one variable. Let's assume we want to sample from P(x) x ~ N(mean, cov) x in R^{2}
test_ess_solver_multivariate
python
blackjax-devs/blackjax
tests/smc/test_smc_ess.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/smc/test_smc_ess.py
Apache-2.0
def test_ess_solver_posterior_signature(self, target_ess): """ Posterior with more than one variable. Let's assume we want to sample from P(x,y) x ~ N(mean, cov) y ~ N(mean, cov) """ num_particles = 1000 mean = jnp.zeros((1, 2)) cov = jnp.diag(jnp.array([1, 1])) ...
Posterior with more than one variable. Let's assume we want to sample from P(x,y) x ~ N(mean, cov) y ~ N(mean, cov)
test_ess_solver_posterior_signature
python
blackjax-devs/blackjax
tests/smc/test_smc_ess.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/smc/test_smc_ess.py
Apache-2.0
def normal_logdensity_fn(x, chol_cov): """minus log-density of a centered multivariate normal distribution""" dim = chol_cov.shape[0] y = jax.scipy.linalg.solve_triangular(chol_cov, x, lower=True) normalizing_constant = ( np.sum(np.log(np.abs(np.diag(chol_cov)))) + dim * np.log(2 * np.pi) / 2.0 ...
minus log-density of a centered multivariate normal distribution
normal_logdensity_fn
python
blackjax-devs/blackjax
tests/smc/test_tempered_smc.py
https://github.com/blackjax-devs/blackjax/blob/master/tests/smc/test_tempered_smc.py
Apache-2.0