Moves
Proposal distributions for MCMC.
Move Parent Class
- class eryn.moves.Move(temperature_control=None, periodic=None, gibbs_sampling_setup=None, prevent_swaps=False, skip_supp_names_update=[], is_rj=False, use_gpu=False, random_seed=None, **kwargs)
Bases:
object
Parent class for proposals or “moves”
- Parameters:
temperature_control (
tempering.TemperatureControl
, optional) – This object controls the tempering. It is passed to the parent class to moves so that all proposals can share and use temperature settings. (default:None
)( (periodic) – class:eryn.utils.PeriodicContainer, optional): This object holds periodic information and methods for periodic parameters. It is passed to the parent class to moves so that all proposals can share and use periodic information. (default: ``None`)
gibbs_sampling_setup (str, tuple, dict, or list, optional) – This sets the Gibbs Sampling setup if desired. The Gibbs sampling setup is completely customizable down to the leaf and parameters. All of the separate Gibbs sampling splits will be run within 1 call to this proposal. If
None
, run all branches and all parameters. Ifstr
, run all parameters within the branch given as the string. To enter a branch with a specific set of parameters, you can provide a 2-tuple with the first entry as the branch name and the second entry as a 2D boolean array of shape(nleaves_max, ndim)
that indicates which leaves and/or parameters you want to run.None
can also be entered in the second entry if all parameters are to be run. A dictionary is also possible with keys as branch names and values as the same 2D boolean array of shape(nleaves_max, ndim)
that indicates which leaves and/or parameters you want to run.None
can also be entered in the value of the dictionary if all parameters are to be run. If multiple keys are provided in the dictionary, those branches will be run simultaneously in the proposal as one iteration of the proposing loop. The final option is a list. This is how you make sure to run all the Gibbs splits. Each entry of the list can be a string, 2-tuple, or dictionary as described above. The list controls the order in which all of these splits are run. (default:None
)prevent_swaps (bool, optional) – If
True
, do not perform temperature swaps in this move.skip_supp_names_update (list, optional) – List of names (str), that can be in any
eryn.state.BranchSupplemental
, to skip when updating states (Move.update()
). This is useful if a large amount of memory is stored in the branch supplementals.is_rj (bool, optional) – If using RJ, this should be
True
. (default:False
)use_gpu (bool, optional) – If
True
, useCuPy
for computations. UseNumPy
ifuse_gpu == False
. (default:False
)random_seed (int, optional) – Set the random seed in
CuPy/NumPy
if notNone
. (default:None
)
- Raises:
ValueError – Incorrect inputs.
- Note
All kwargs are stored as attributes.
- num_proposals
the number of times this move has been run. This is needed to compute the acceptance fraction.
- Type:
int
- gibbs_sampling_setup
All of the Gibbs sampling splits as described above.
- Type:
list
- xp
NumPy
orCuPy
.- Type:
obj
- use_gpu
Whether
Cupy
(True
) is used or not (False
).- Type:
bool
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- property accepted
Accepted counts for this move.
- property acceptance_fraction
Acceptance fraction for this move.
- property temperature_control
Temperature controller
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
Metropolis-Hastings Moves
MH Parent Class
- class eryn.moves.MHMove(**kwargs)
Bases:
Move
A general Metropolis-Hastings proposal
Concrete implementations can be made by providing a
get_proposal
method. For standard Gaussian Metropolis moves,moves.GaussianMove
can be used.- get_proposal(branches_coords, random, branches_inds=None, **kwargs)
Get proposal
- Parameters:
branches_coords (dict) – Keys are
branch_names
and values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim] representing coordinates for walkers.random (object) – Current random state object.
branches_inds (dict, optional) – Keys are
branch_names
and values are np.ndarray[ntemps, nwalkers, nleaves_max] representing which leaves are currently being used. (default:None
)**kwargs (ignored) – This is added for compatibility. It is ignored in this function.
- Returns:
(Proposed coordinates, factors) -> (dict, np.ndarray)
- Return type:
tuple
- Raises:
NotImplementedError – If proposal is not implemented in a subclass.
- setup(branches_coords)
Any setup for the proposal.
- Parameters:
branches_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.
- propose(model, state)
Use the move to generate a proposal and compute the acceptance
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
State of sampler after proposal is complete.
- Return type:
State
- property acceptance_fraction
Acceptance fraction for this move.
- property accepted
Accepted counts for this move.
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- property temperature_control
Temperature controller
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
Implemented MH Moves
- class eryn.moves.GaussianMove(cov_all, mode='vector', factor=None, **kwargs)
Bases:
MHMove
A Metropolis step with a Gaussian proposal function.
This class is heavily based on the same class in
emcee
.- Parameters:
cov (dict) – The covariance of the proposal function. The keys are branch names and the values are covariance information. This information can be provided as a scalar, vector, or matrix and the proposal will be assumed isotropic, axis-aligned, or general, respectively.
mode (str, optional) – Select the method used for updating parameters. This can be one of
"vector"
,"random"
, or"sequential"
. The"vector"
mode updates all dimensions simultaneously,"random"
randomly selects a dimension and only updates that one, and"sequential"
loops over dimensions and updates each one in turn. (default:"vector"
)factor (float, optional) – If provided the proposal will be made with a standard deviation uniformly selected from the range
exp(U(-log(factor), log(factor))) * cov
. This is invalid for the"vector"
mode. (default:None
)**kwargs (dict, optional) – Kwargs for parent classes. (default:
{}
)
- Raises:
ValueError – If the proposal dimensions are invalid or if any of any of the other arguments are inconsistent.
- get_proposal(branches_coords, random, branches_inds=None, **kwargs)
Get proposal from Gaussian distribution
- Parameters:
branches_coords (dict) – Keys are
branch_names
and values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim] representing coordinates for walkers.random (object) – Current random state object.
branches_inds (dict, optional) – Keys are
branch_names
and values are np.ndarray[ntemps, nwalkers, nleaves_max] representing which leaves are currently being used. (default:None
)**kwargs (ignored) – This is added for compatibility. It is ignored in this function.
- Returns:
(Proposed coordinates, factors) -> (dict, np.ndarray)
- Return type:
tuple
- property acceptance_fraction
Acceptance fraction for this move.
- property accepted
Accepted counts for this move.
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- propose(model, state)
Use the move to generate a proposal and compute the acceptance
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
State of sampler after proposal is complete.
- Return type:
State
- setup(branches_coords)
Any setup for the proposal.
- Parameters:
branches_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- property temperature_control
Temperature controller
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
- class eryn.moves.DistributionGenerate(generate_dist, *args, **kwargs)
Bases:
MHMove
Generate proposals from a distribution
- Parameters:
generate_dist (dict) – Dictionary with keys as branch names and items as
ProbDistContainer
objects that havelogpdf
andrvs
methods.
- get_proposal(branches_coords, random, branches_inds=None, **kwargs)
Make a proposal
- Parameters:
branches_coords (dict) – Keys are
branch_names
and values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim] representing coordinates for walkers.random (object) – Current random state object.
branches_inds (dict, optional) – Keys are
branch_names
and values are np.ndarray[ntemps, nwalkers, nleaves_max] representing which leaves are currently being used. (default:None
)**kwargs (ignored) – This is added for compatibility. It is ignored in this function.
- Returns:
- Tuple containing proposal information.
First entry is the new coordinates as a dictionary with keys as
branch_names
and values as ``double `` np.ndarray[ntemps, nwalkers, nleaves_max, ndim] containing proposed coordinates. Second entry is the factors associated with the proposal necessary for detailed balance. This is effectively any term in the detailed balance fraction. +log of factors if in the numerator. -log of factors if in the denominator.
- Return type:
tuple
- property acceptance_fraction
Acceptance fraction for this move.
- property accepted
Accepted counts for this move.
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- propose(model, state)
Use the move to generate a proposal and compute the acceptance
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
State of sampler after proposal is complete.
- Return type:
State
- setup(branches_coords)
Any setup for the proposal.
- Parameters:
branches_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- property temperature_control
Temperature controller
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
Red-Blue Moves
Red-Blue Parent Class
- class eryn.moves.RedBlueMove(nsplits=2, randomize_split=True, live_dangerously=False, **kwargs)
Bases:
Move
,ABC
An abstract red-blue ensemble move with parallelization as described in Foreman-Mackey et al. (2013).
This class is heavily based on the original from
emcee
.- Parameters:
nsplits (int, optional) – The number of sub-ensembles to use. Each sub-ensemble is updated in parallel using the other sets as the complementary ensemble. The default value is
2
and you probably won’t need to change that.randomize_split (bool, optional) – Randomly shuffle walkers between sub-ensembles. The same number of walkers will be assigned to each sub-ensemble on each iteration. (default:
True
)live_dangerously (bool, optional) – By default, an update will fail with a
RuntimeError
if the number of walkers is smaller than twice the dimension of the problem because the walkers would then be stuck on a low dimensional subspace. This can be avoided by switching between the stretch move and, for example, a Metropolis-Hastings step. If you want to do this and suppress the error, setlive_dangerously = True
. Thanks goes (once again) to @dstndstn for this wonderful terminology. (default:False
)**kwargs (dict, optional) – Kwargs for parent classes.
- setup(branches_coords)
Any setup for the proposal.
- Parameters:
branches_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.
- classmethod get_proposal(sample, complement, random, gibbs_ndim=None)
Make a proposal
- Parameters:
sample (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim].complement (dict) – Keys are
branch_names
. Values are lists of other other np.ndarray[ntemps, nwalkers - subset size, nleaves_max, ndim] from all other subsets. This is the compliment whosecoords
are used to form the proposal for thesample
subset.random (object) – Current random state of the sampler.
gibbs_ndim (int or np.ndarray, optional) – If Gibbs sampling, this indicates the true dimension. If given as an array, must have shape
(ntemps, nwalkers)
. See the tutorial for more information. (default:None
)
- Returns:
- Tuple contained proposal information.
First entry is the new coordinates as a dictionary with keys as
branch_names
and values as np.ndarray[ntemps, nwalkers, nleaves_max, ndim] of new coordinates. Second entry is the factors associated with the proposal necessary for detailed balance. This is effectively any term in the detailed balance fraction. +log of factors if in the numerator. -log of factors if in the denominator.
- Return type:
tuple
- propose(model, state)
Use the move to generate a proposal and compute the acceptance
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
- (state, accepted)
The first return is the state of the sampler after the move. The second return value is the accepted count array.
- Return type:
tuple
- property acceptance_fraction
Acceptance fraction for this move.
- property accepted
Accepted counts for this move.
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- property temperature_control
Temperature controller
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
Implemented Red-Blue Moves
- class eryn.moves.StretchMove(a=2.0, return_gpu=False, random_seed=None, **kwargs)
Bases:
RedBlueMove
Affine-Invariant Proposal
A Goodman & Weare (2010) “stretch move” with parallelization as described in Foreman-Mackey et al. (2013).
This class was originally implemented in
emcee
.- Parameters:
a (double, optional) – The stretch scale parameter. (default:
2.0
)return_gpu (bool, optional) – If
use_gpu == True and return_gpu == True
, the returned arrays will be returned asCuPy
arrays. (default:False
)kwargs (dict, optional) – Additional keyword arguments passed down through :class:`RedRedBlueMove`_.
- a
The stretch scale parameter.
- Type:
double
- return_gpu
Whether the array being returned is in
Cupy
(True
) orNumPy
(False
).- Type:
bool
- adjust_factors(factors, ndims_old, ndims_new)
Adjust the
factors
based on changing dimensions.factors
is adjusted in place.- Parameters:
factors (xp.ndarray) – Array of
factors
values. It is adjusted in place.ndims_old (int or xp.ndarray) – Old dimension. If given as an
xp.ndarray
, must be broadcastable withfactors
.ndims_new (int or xp.ndarray) – New dimension. If given as an
xp.ndarray
, must be broadcastable withfactors
.
- choose_c_vals(c, Nc, Ns, ntemps, random_number_generator, **kwargs)
Get the compliment array
The compliment represents the points that are used to move the actual points whose position is changing.
- Parameters:
c (np.ndarray) – Possible compliment values with shape
(ntemps, Nc, nleaves_max, ndim)
.Nc (int) – Length of the
...
: the subset of walkers proposed to move now (usually nwalkers/2).Ns (int) – Number of generation points.
ntemps (int) – Number of temperatures.
random_number_generator (object) – Random state object.
**kwargs (ignored) – Ignored here. For modularity.
- Returns:
Compliment values to use with shape
(ntemps, Ns, nleaves_max, ndim)
.- Return type:
np.ndarray
- get_new_points(name, s, c_temp, Ns, branch_shape, branch_i, random_number_generator)
Get mew points in stretch move.
Takes compliment and uses it to get new points for those being proposed.
- Parameters:
name (str) – Branch name.
s (np.ndarray) – Points to be moved with shape
(ntemps, Ns, nleaves_max, ndim)
.c (np.ndarray) – Compliment to move points with shape
(ntemps, Ns, nleaves_max, ndim)
.Ns (int) – Number to generate.
branch_shape (tuple) – Full branch shape.
branch_i (int) – Which branch in the order is being run now. This ensures that the randomly generated quantity per walker remains the same over branches.
random_number_generator (object) – Random state object.
- Returns:
New proposed points with shape
(ntemps, Ns, nleaves_max, ndim)
.- Return type:
np.ndarray
- get_proposal(s_all, c_all, random, gibbs_ndim=None, **kwargs)
Generate stretch proposal
- Parameters:
s_all (dict) – Keys are
branch_names
and values are coordinates for which a proposal is to be generated.c_all (dict) – Keys are
branch_names
and values are lists. These lists contain all the complement array values.random (object) – Random state object.
gibbs_ndim (int or np.ndarray, optional) – If Gibbs sampling, this indicates the true dimension. If given as an array, must have shape
(ntemps, nwalkers)
. See the tutorial for more information. (default:None
)
- Returns:
First entry is new positions. Second entry is detailed balance factors.
- Return type:
tuple
- Raises:
ValueError – Issues with dimensionality.
- property acceptance_fraction
Acceptance fraction for this move.
- property accepted
Accepted counts for this move.
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- propose(model, state)
Use the move to generate a proposal and compute the acceptance
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
- (state, accepted)
The first return is the state of the sampler after the move. The second return value is the accepted count array.
- Return type:
tuple
- setup(branches_coords)
Any setup for the proposal.
- Parameters:
branches_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- property temperature_control
Temperature controller
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
Group Moves
Group Parent Class
- class eryn.moves.GroupMove(nfriends=None, n_iter_update=100, live_dangerously=False, **kwargs)
Bases:
Move
,ABC
A “group” ensemble move based on the
eryn.moves.RedBlueMove
.In moves like the
eryn.moves.StretchMove
, the complimentary group for which the proposal is used is chosen from the current points in the ensemble. In “group” moves the complimentary group is a stationary group that is updated every n_iter_update iterations. This update is performed with the last set of coordinates to maintain detailed balance.- Parameters:
nfriends (int, optional) – The number of friends to draw from as the complimentary ensemble. This group is determined from the stationary group. If
None
, it will be set to the number of walkers. (default:None
)n_iter_update (int, optional) – Number of iterations to run before updating the stationary distribution. (default: 100).
live_dangerously (bool, optional) – If
True
, allow forn_iter_update == 1
. (deafault:False
)
kwargs
are passed toMove
class.- find_friends(name, s, s_inds=None, branch_supps=None)
Function for finding friends.
- Parameters:
name (str) – Branch name for proposal coordinates.
s (np.ndarray) – Coordinates array for the points to be moved.
s_inds (np.ndarray, optional) –
inds
arrays that represent which leaves are present. (default:None
)branch_supps (dict, optional) – Keys are
branch_names
and values areBranchSupplemental
objects. For group proposals,branch_supps
are the best device for passing and tracking useful information. (default:None
)
- Returns:
Complimentary values.
- Return type:
np.ndarray
- choose_c_vals(name, s, s_inds=None, branch_supps=None)
Get the complimentary values.
- setup(branches)
Any setup necessary for the proposal
- setup_friends(branches)
Setup anything for finding friends.
- Parameters:
branches (dict) – Dictionary with all the current branches in the sampler.
- fix_friends(branches)
Fix any friends that were born through RJ.
This function is not required. If not implemented, it will just return immediately.
- Parameters:
branches (dict) – Dictionary with all the current branches in the sampler.
- classmethod get_proposal(s_all, random, gibbs_ndim=None, s_inds_all=None, **kwargs)
Generate group stretch proposal coordinates
- Parameters:
s_all (dict) – Keys are
branch_names
and values are coordinates for which a proposal is to be generated.random (object) – Random state object.
gibbs_ndim (int or np.ndarray, optional) – If Gibbs sampling, this indicates the true dimension. If given as an array, must have shape
(ntemps, nwalkers)
. See the tutorial for more information. (default:None
)s_inds_all (dict, optional) – Keys are
branch_names
and values areinds
arrays indicating which leaves are currently used. (default:None
)
- Returns:
First entry is new positions. Second entry is detailed balance factors.
- Return type:
tuple
- Raises:
ValueError – Issues with dimensionality.
- propose(model, state)
Use the move to generate a proposal and compute the acceptance
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
- (state, accepted)
The first return is the state of the sampler after the move. The second return value is the accepted count array.
- Return type:
tuple
- property acceptance_fraction
Acceptance fraction for this move.
- property accepted
Accepted counts for this move.
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- property temperature_control
Temperature controller
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
Implemented Group Moves
- class eryn.moves.GroupStretchMove(**kwargs)
Bases:
GroupMove
,StretchMove
Proposal like stretch with stationary compliment.
This move uses the stretch proposal method and math, but the compliment of walkers used to propose a new point is chosen from a stationary group rather than the current walkers in the ensemble.
This move allows for “stretch”-like proposal to be used in Reversible Jump MCMC.
- Parameters:
**kwargs (dict, optional) – Keyword arguments passed to
GroupMove
andStretchMove
.
- get_proposal(s_all, random, gibbs_ndim=None, s_inds_all=None, branch_supps=None, **kwargs)
Generate group stretch proposal coordinates
- Parameters:
s_all (dict) – Keys are
branch_names
and values are coordinates for which a proposal is to be generated.random (object) – Random state object.
gibbs_ndim (int or np.ndarray, optional) – If Gibbs sampling, this indicates the true dimension. If given as an array, must have shape
(ntemps, nwalkers)
. See the tutorial for more information. (default:None
)s_inds_all (dict, optional) – Keys are
branch_names
and values areinds
arrays indicating which leaves are currently used. (default:None
)branch_supps (dict, optional) – Keys are
branch_names
and values areBranchSupplemental
objects. For the group stretch,branch_supps
are the best device for passing and tracking useful information. (default:None
)
- Returns:
First entry is new positions. Second entry is detailed balance factors.
- Return type:
tuple
- Raises:
ValueError – Issues with dimensionality.
- property acceptance_fraction
Acceptance fraction for this move.
- property accepted
Accepted counts for this move.
- adjust_factors(factors, ndims_old, ndims_new)
Adjust the
factors
based on changing dimensions.factors
is adjusted in place.- Parameters:
factors (xp.ndarray) – Array of
factors
values. It is adjusted in place.ndims_old (int or xp.ndarray) – Old dimension. If given as an
xp.ndarray
, must be broadcastable withfactors
.ndims_new (int or xp.ndarray) – New dimension. If given as an
xp.ndarray
, must be broadcastable withfactors
.
- choose_c_vals(name, s, s_inds=None, branch_supps=None)
Get the complimentary values.
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- find_friends(name, s, s_inds=None, branch_supps=None)
Function for finding friends.
- Parameters:
name (str) – Branch name for proposal coordinates.
s (np.ndarray) – Coordinates array for the points to be moved.
s_inds (np.ndarray, optional) –
inds
arrays that represent which leaves are present. (default:None
)branch_supps (dict, optional) – Keys are
branch_names
and values areBranchSupplemental
objects. For group proposals,branch_supps
are the best device for passing and tracking useful information. (default:None
)
- Returns:
Complimentary values.
- Return type:
np.ndarray
- fix_friends(branches)
Fix any friends that were born through RJ.
This function is not required. If not implemented, it will just return immediately.
- Parameters:
branches (dict) – Dictionary with all the current branches in the sampler.
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- get_new_points(name, s, c_temp, Ns, branch_shape, branch_i, random_number_generator)
Get mew points in stretch move.
Takes compliment and uses it to get new points for those being proposed.
- Parameters:
name (str) – Branch name.
s (np.ndarray) – Points to be moved with shape
(ntemps, Ns, nleaves_max, ndim)
.c (np.ndarray) – Compliment to move points with shape
(ntemps, Ns, nleaves_max, ndim)
.Ns (int) – Number to generate.
branch_shape (tuple) – Full branch shape.
branch_i (int) – Which branch in the order is being run now. This ensures that the randomly generated quantity per walker remains the same over branches.
random_number_generator (object) – Random state object.
- Returns:
New proposed points with shape
(ntemps, Ns, nleaves_max, ndim)
.- Return type:
np.ndarray
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- propose(model, state)
Use the move to generate a proposal and compute the acceptance
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
- (state, accepted)
The first return is the state of the sampler after the move. The second return value is the accepted count array.
- Return type:
tuple
- setup(branches)
Any setup necessary for the proposal
- setup_friends(branches)
Setup anything for finding friends.
- Parameters:
branches (dict) – Dictionary with all the current branches in the sampler.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- property temperature_control
Temperature controller
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
Reversible Jump Moves
Reversible Jump Parent Class
- class eryn.moves.ReversibleJumpMove(nleaves_max=None, nleaves_min=None, dr=None, dr_max_iter=5, tune=False, fix_change=None, **kwargs)
Bases:
Move
An abstract reversible jump move.
- Parameters:
nleaves_max (dict) – Maximum number(s) of leaves for each model. Keys are
branch_names
and values arenleaves_max
for each branch. This is a keyword argument, nut it is required.nleaves_min (dict) – Minimum number(s) of leaves for each model. Keys are
branch_names
and values arenleaves_min
for each branch. This is a keyword argument, nut it is required.tune (bool, optional) – If True, tune proposal. (Default:
False
)fix_change (int or None, optional) – Fix the change in the number of leaves. Make them all add a leaf or remove a leaf. This can be useful for some search functions. Options are
+1
or-1
. (default:None
)
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- setup(branches_coords)
Any setup for the proposal.
- Parameters:
branches_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.
- get_proposal(all_coords, all_inds, nleaves_min_all, nleaves_max_all, random, **kwargs)
Make a proposal
- Parameters:
all_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.all_inds (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max]. These are the boolean arrays marking which leaves are currently used within each walker.nleaves_min_all (dict) – Minimum values of leaf ount for each model. Must have same order as
all_cords
.nleaves_max_all (dict) – Maximum values of leaf ount for each model. Must have same order as
all_cords
.random (object) – Current random state of the sampler.
**kwargs (ignored) – For modularity.
- Returns:
- Tuple containing proposal information.
First entry is the new coordinates as a dictionary with keys as
branch_names
and values asdouble `` np.ndarray[ntemps, nwalkers, nleaves_max, ndim] containing proposed coordinates. Second entry is the new ``inds
array with boolean values flipped for added or removed sources. Third entry is the factors associated with the proposal necessary for detailed balance. This is effectively any term in the detailed balance fraction. +log of factors if in the numerator. -log of factors if in the denominator.
- Return type:
tuple
- Raises:
NotImplementedError – If this proposal is not implemented by a subclass.
- get_model_change_proposal(inds, random, nleaves_min, nleaves_max)
Helper function for changing the model count by 1.
This helper function works with nested models where you want to add or remove one leaf at a time.
- Parameters:
inds (np.ndarray) –
inds
values for this specific branch with shape(ntemps, nwalkers, nleaves_max)
.random (object) – Current random state of the sampler.
nleaves_min (int) – Minimum allowable leaf count for this branch.
nleaves_max (int) – Maximum allowable leaf count for this branch.
- Returns:
- Keys are
"+1"
and"-1"
. Values are indexing information. "+1"
and"-1"
indicate if a source is being added or removed, respectively. The indexing information is a 2D array with shape(number changing, 3)
. The length 3 is the index into each of the(ntemps, nwalkers, nleaves_max)
.
- Keys are
- Return type:
dict
- property acceptance_fraction
Acceptance fraction for this move.
- property accepted
Accepted counts for this move.
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- propose(model, state)
Use the move to generate a proposal and compute the acceptance
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
State of sampler after proposal is complete.
- Return type:
State
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- property temperature_control
Temperature controller
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
Implemented Reversible Jump Moves
- class eryn.moves.DistributionGenerateRJ(generate_dist, *args, **kwargs)
Bases:
ReversibleJumpMove
Generate Revesible-Jump proposals from a distribution.
The prior can be entered as the
generate_dist
to generate proposals directly from the prior.- Parameters:
generate_dist (dict) – Keys are branch names and values are
ProbDistContainer
objects that havelogpdf
andrvs
methods. If you*args (tuple, optional) – Additional arguments to pass to parent classes.
**kwargs (dict, optional) – Keyword arguments passed to parent classes.
- get_model_change_proposal(inds, random, nleaves_min, nleaves_max)
Helper function for changing the model count by 1.
This helper function works with nested models where you want to add or remove one leaf at a time.
- Parameters:
inds (np.ndarray) –
inds
values for this specific branch with shape(ntemps, nwalkers, nleaves_max)
.random (object) – Current random state of the sampler.
nleaves_min (int) – Minimum allowable leaf count for this branch.
nleaves_max (int) – Maximum allowable leaf count for this branch.
- Returns:
- Keys are
"+1"
and"-1"
. Values are indexing information. "+1"
and"-1"
indicate if a source is being added or removed, respectively. The indexing information is a 2D array with shape(number changing, 3)
. The length 3 is the index into each of the(ntemps, nwalkers, nleaves_max)
.
- Keys are
- Return type:
dict
- get_proposal(all_coords, all_inds, nleaves_min_all, nleaves_max_all, random, **kwargs)
Make a proposal
- Parameters:
all_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.all_inds (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max]. These are the boolean arrays marking which leaves are currently used within each walker.nleaves_min_all (dict) – Minimum values of leaf ount for each model. Must have same order as
all_cords
.nleaves_max_all (dict) – Maximum values of leaf ount for each model. Must have same order as
all_cords
.random (object) – Current random state of the sampler.
**kwargs (ignored) – For modularity.
- Returns:
- Tuple containing proposal information.
First entry is the new coordinates as a dictionary with keys as
branch_names
and values asdouble `` np.ndarray[ntemps, nwalkers, nleaves_max, ndim] containing proposed coordinates. Second entry is the new ``inds
array with boolean values flipped for added or removed sources. Third entry is the factors associated with the proposal necessary for detailed balance. This is effectively any term in the detailed balance fraction. +log of factors if in the numerator. -log of factors if in the denominator.
- Return type:
tuple
- property acceptance_fraction
Acceptance fraction for this move.
- property accepted
Accepted counts for this move.
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- propose(model, state)
Use the move to generate a proposal and compute the acceptance
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
State of sampler after proposal is complete.
- Return type:
State
- setup(branches_coords)
Any setup for the proposal.
- Parameters:
branches_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- property temperature_control
Temperature controller
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
Mutliple Try Moves
Mutliple Try Parent Class
- class eryn.moves.MultipleTryMove(num_try=1, independent=False, symmetric=False, rj=False, use_gpu=None, **kwargs)
Bases:
ABC
Generate multiple proposal tries.
This class should be inherited by another proposal class with the
@classmethods
overwritten. Seeeryn.moves.MTDistGenMove
andMTDistGenRJ
for examples.- Parameters:
num_try (int, optional) – Number of tries. (default: 1)
independent (bool, optional) – Set to
True
if the proposal is independent of the current points. (default:False
).symmetric (bool, optional) – Set to
True
if the proposal is symmetric. (default:False
).rj (bool, optional) – Set to
True
if this is a nested reversible jump proposal. (default:False
).**kwargs (dict, optional) – for compatibility with other proposals.
Raises – ValueError: Input issues.
- classmethod special_like_func(generated_coords, *args, inds_leaves_rj=None, **kwargs)
Calculate the Likelihood for sampled points.
- Parameters:
generated_coords (np.ndarray) – Generated coordinates with shape
(number of independent walkers, num_try)
.*args (tuple, optional) – additional arguments passed by overwriting the
get_proposal
function and passingargs_like
keyword argument.inds_leaves_rj (np.ndarray) – Index into each individual walker giving the leaf index associated with this proposal. Should only be used if
self.rj is True
. (default:None
)**kwargs (tuple, optional) – additional keyword arguments passed by overwriting the
get_proposal
function and passingkwargs_like
keyword argument.
- Returns:
Likelihood values with shape
(generated_coords.shape[0], num_try).
- Return type:
np.ndarray
- Raises:
NotImplementedError – Function not included.
- classmethod special_prior_func(generated_coords, *args, **kwargs)
Calculate the Prior for sampled points.
- Parameters:
generated_coords (np.ndarray) – Generated coordinates with shape
(number of independent walkers, num_try)
.*args (tuple, optional) – additional arguments passed by overwriting the
get_proposal
function and passingargs_prior
keyword argument.inds_leaves_rj (np.ndarray) – Index into each individual walker giving the leaf index associated with this proposal. Should only be used if
self.rj is True
. (default:None
)**kwargs (tuple, optional) – additional keyword arguments passed by overwriting the
get_proposal
function and passingkwargs_prior
keyword argument.
- Returns:
Prior values with shape
(generated_coords.shape[0], num_try).
- Return type:
np.ndarray
- Raises:
NotImplementedError – Function not included.
- classmethod special_generate_func(random, size=1, *args, fill_tuple=None, fill_values=None, **kwargs)
Generate samples and calculate the logpdf of their proposal function.
- Parameters:
coords (np.ndarray) – Current coordinates of walkers.
random (obj) – Random generator.
*args (tuple, optional) – additional arguments passed by overwriting the
get_proposal
function and passingargs_generate
keyword argument.size (int, optional) – Number of tries to generate.
fill_tuple (tuple, optional) – Length 2 tuple with the indexing of which values to fill when generating. Can be used for auxillary proposals or reverse RJ proposals. First index is the index into walkers and the second index is the index into the number of tries. (default:
None
)fill_values (np.ndarray) – values to fill associated with
fill_tuple
. Should have size(len(fill_tuple[0]), ndim)
. (default:None
).**kwargs (tuple, optional) – additional keyword arguments passed by overwriting the
get_proposal
function and passingkwargs_generate
keyword argument.
- Returns:
(generated points, logpdf of generated points).
- Return type:
tuple
- Raises:
NotImplementedError – Function not included.
- classmethod special_generate_logpdf(coords)
Get logpdf of generated coordinates.
- Parameters:
coords (np.ndarray) – Current coordinates of walkers.
- Returns:
logpdf of generated points.
- Return type:
np.ndarray
- Raises:
NotImplementedError – Function not included.
- get_mt_log_posterior(ll, lp, betas=None)
Calculate the log of the posterior for all tries.
- Parameters:
ll (np.ndarray) – Log Likelihood values with shape
(nwalkers, num_tries)
.lp (np.ndarray) – Log Prior values with shape
(nwalkers, num_tries)
.betas (np.ndarray, optional) – Inverse temperatures to include in log Posterior computation. (default:
None
)
- Returns:
Log of the Posterior with shape
(nwalkers, num_tries)
.- Return type:
np.ndarray
- readout_adjustment(out_vals, all_vals_prop, aux_all_vals)
Read out values from the proposal.
Allows the user to read out any values from the proposal that may be needed elsewhere. This function must be overwritten.
- Parameters:
out_vals (list) –
[logP_out, ll_out, lp_out, log_proposal_pdf_out, log_sum_weights]
.all_vals_prop (list) –
[logP, ll, lp, log_proposal_pdf, log_sum_weights]
.aux_all_vals (list) –
[aux_logP, aux_ll, aux_lp, aux_log_proposal_pdf, aux_log_sum_weights]
.
- get_mt_proposal(coords, random, args_generate=(), kwargs_generate={}, args_like=(), kwargs_like={}, args_prior=(), kwargs_prior={}, betas=None, ll_in=None, lp_in=None, inds_leaves_rj=None, inds_reverse_rj=None)
Make a multiple-try proposal
Here,
nwalkers
refers to all independent walkers which generally will meannwalkers * ntemps
in terms of the rest of the sampler.- Parameters:
coords (np.ndarray) – Current coordinates of walkers.
random (obj) – Random generator.
args_generate (tuple, optional) – Additional
*args
to pass to generate function. Must overwriteget_proposal
function to use these. (default:()
)kwargs_generate (dict, optional) – Additional
**kwargs
to pass to generate function. (default:{}
) Must overwriteget_proposal
function to use these.args_like (tuple, optional) – Additional
*args
to pass to Likelihood function. Must overwriteget_proposal
function to use these. (default:()
)kwargs_like (dict, optional) – Additional
**kwargs
to pass to Likelihood function. Must overwriteget_proposal
function to use these. (default:{}
)args_prior (tuple, optional) – Additional
*args
to pass to Prior function. Must overwriteget_proposal
function to use these. (default:()
)kwargs_prior (dict, optional) – Additional
**kwargs
to pass to Prior function. Must overwriteget_proposal
function to use these. (default:{}
)betas (np.ndarray, optional) – Inverse temperatures passes to the proposal with shape
(nwalkers,)
.ll_in (np.ndarray, optional) – Log Likelihood values coming in for current coordinates. Must be provided if
self.rj is True
. Ifself.rj is True
, must be nested. Also, for all proposed removals, this value must be the Likelihood with the binary removed so all proposals are pretending to add a binary. Useful ifself.independent is True
. (default:None
)lp_in (np.ndarray, optional) – Log Prior values coming in for current coordinates. Must be provided if
self.rj is True
. Ifself.rj is True
, must be nested. Also, for all proposed removals, this value must be the Likelihood with the binary removed so all proposals are pretending to add a binary. Useful ifself.independent is True
. (default:None
)inds_leaves_rj (np.ndarray, optional) – Array giving the leaf index of each incoming walker. Must be provided if
self.rj is True
. (default:None
)inds_reverse_rj (np.ndarray, optional) – Array giving the walker index for which proposals are reverse proposal removing a leaf. Must be provided if
self.rj is True
. (default:None
)
- Returns:
(generated points, factors).
- Return type:
tuple
- Raises:
ValueError – Inputs are incorrect.
- get_proposal(branches_coords, random, branches_inds=None, **kwargs)
Get proposal
- Parameters:
branches_coords (dict) – Keys are
branch_names
and values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim] representing coordinates for walkers.random (object) – Current random state object.
branches_inds (dict, optional) – Keys are
branch_names
and values are np.ndarray[ntemps, nwalkers, nleaves_max] representing which leaves are currently being used. (default:None
)**kwargs (ignored) – This is added for compatibility. It is ignored in this function.
- Returns:
(Proposed coordinates, factors) -> (dict, np.ndarray)
- Return type:
tuple
- Raises:
ValueError – Input issues.
Implemented Multiple Try Moves
- class eryn.moves.MTDistGenMove(generate_dist, **kwargs)
Bases:
MultipleTryMove
,MHMove
Perform a multiple try MH move based on a distribution.
Distribution must be independent of the current point.
This is effectively an example of the mutliple try class inheritance structure.
- Parameters:
generate_dist (dict) – Keys are branch names and values are
ProbDistContainer
objects that havelogpdf
andrvs
methods. If you*args (tuple, optional) – Additional arguments to pass to parent classes.
**kwargs (dict, optional) – Keyword arguments passed to parent classes.
- special_generate_logpdf(generated_coords)
Get logpdf of generated coordinates.
- Parameters:
generated_coords (np.ndarray) – Current coordinates of walkers.
- Returns:
logpdf of generated points.
- Return type:
np.ndarray
- special_generate_func(coords, random, size=1, fill_tuple=None, fill_values=None, **kwargs)
Generate samples and calculate the logpdf of their proposal function.
- Parameters:
coords (np.ndarray) – Current coordinates of walkers.
random (obj) – Random generator.
*args (tuple, optional) – additional arguments passed by overwriting the
get_proposal
function and passingargs_generate
keyword argument.size (int, optional) – Number of tries to generate.
fill_tuple (tuple, optional) – Length 2 tuple with the indexing of which values to fill when generating. Can be used for auxillary proposals or reverse RJ proposals. First index is the index into walkers and the second index is the index into the number of tries. (default:
None
)fill_values (np.ndarray) – values to fill associated with
fill_tuple
. Should have size(len(fill_tuple[0]), ndim)
. (default:None
).**kwargs (tuple, optional) – additional keyword arguments passed by overwriting the
get_proposal
function and passingkwargs_generate
keyword argument.
- Returns:
(generated points, logpdf of generated points).
- Return type:
tuple
- set_coords_and_inds(generated_coords)
Setup coordinates for prior and Likelihood
- Parameters:
generated_coords (np.ndarray) – Generated coordinates with shape
(number of independent walkers, num_try)
.- Returns:
Coordinates for Likelihood and Prior.
- Return type:
dict
- special_like_func(generated_coords, **kwargs)
Calculate the Likelihood for sampled points.
- Parameters:
generated_coords (np.ndarray) – Generated coordinates with shape
(number of independent walkers, num_try)
.**kwargs (dict, optional) – For compatibility.
- Returns:
Likelihood values with shape
(generated_coords.shape[0], num_try).
- Return type:
np.ndarray
- special_prior_func(generated_coords, **kwargs)
Calculate the Prior for sampled points.
- Parameters:
generated_coords (np.ndarray) – Generated coordinates with shape
(number of independent walkers, num_try)
.**kwargs (dict, optional) – For compatibility.
- Returns:
Prior values with shape
(generated_coords.shape[0], num_try).
- Return type:
np.ndarray
- property acceptance_fraction
Acceptance fraction for this move.
- property accepted
Accepted counts for this move.
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- get_mt_log_posterior(ll, lp, betas=None)
Calculate the log of the posterior for all tries.
- Parameters:
ll (np.ndarray) – Log Likelihood values with shape
(nwalkers, num_tries)
.lp (np.ndarray) – Log Prior values with shape
(nwalkers, num_tries)
.betas (np.ndarray, optional) – Inverse temperatures to include in log Posterior computation. (default:
None
)
- Returns:
Log of the Posterior with shape
(nwalkers, num_tries)
.- Return type:
np.ndarray
- get_mt_proposal(coords, random, args_generate=(), kwargs_generate={}, args_like=(), kwargs_like={}, args_prior=(), kwargs_prior={}, betas=None, ll_in=None, lp_in=None, inds_leaves_rj=None, inds_reverse_rj=None)
Make a multiple-try proposal
Here,
nwalkers
refers to all independent walkers which generally will meannwalkers * ntemps
in terms of the rest of the sampler.- Parameters:
coords (np.ndarray) – Current coordinates of walkers.
random (obj) – Random generator.
args_generate (tuple, optional) – Additional
*args
to pass to generate function. Must overwriteget_proposal
function to use these. (default:()
)kwargs_generate (dict, optional) – Additional
**kwargs
to pass to generate function. (default:{}
) Must overwriteget_proposal
function to use these.args_like (tuple, optional) – Additional
*args
to pass to Likelihood function. Must overwriteget_proposal
function to use these. (default:()
)kwargs_like (dict, optional) – Additional
**kwargs
to pass to Likelihood function. Must overwriteget_proposal
function to use these. (default:{}
)args_prior (tuple, optional) – Additional
*args
to pass to Prior function. Must overwriteget_proposal
function to use these. (default:()
)kwargs_prior (dict, optional) – Additional
**kwargs
to pass to Prior function. Must overwriteget_proposal
function to use these. (default:{}
)betas (np.ndarray, optional) – Inverse temperatures passes to the proposal with shape
(nwalkers,)
.ll_in (np.ndarray, optional) – Log Likelihood values coming in for current coordinates. Must be provided if
self.rj is True
. Ifself.rj is True
, must be nested. Also, for all proposed removals, this value must be the Likelihood with the binary removed so all proposals are pretending to add a binary. Useful ifself.independent is True
. (default:None
)lp_in (np.ndarray, optional) – Log Prior values coming in for current coordinates. Must be provided if
self.rj is True
. Ifself.rj is True
, must be nested. Also, for all proposed removals, this value must be the Likelihood with the binary removed so all proposals are pretending to add a binary. Useful ifself.independent is True
. (default:None
)inds_leaves_rj (np.ndarray, optional) – Array giving the leaf index of each incoming walker. Must be provided if
self.rj is True
. (default:None
)inds_reverse_rj (np.ndarray, optional) – Array giving the walker index for which proposals are reverse proposal removing a leaf. Must be provided if
self.rj is True
. (default:None
)
- Returns:
(generated points, factors).
- Return type:
tuple
- Raises:
ValueError – Inputs are incorrect.
- get_proposal(branches_coords, random, branches_inds=None, **kwargs)
Get proposal
- Parameters:
branches_coords (dict) – Keys are
branch_names
and values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim] representing coordinates for walkers.random (object) – Current random state object.
branches_inds (dict, optional) – Keys are
branch_names
and values are np.ndarray[ntemps, nwalkers, nleaves_max] representing which leaves are currently being used. (default:None
)**kwargs (ignored) – This is added for compatibility. It is ignored in this function.
- Returns:
(Proposed coordinates, factors) -> (dict, np.ndarray)
- Return type:
tuple
- Raises:
ValueError – Input issues.
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- propose(model, state)
Use the move to generate a proposal and compute the acceptance
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
State of sampler after proposal is complete.
- Return type:
State
- readout_adjustment(out_vals, all_vals_prop, aux_all_vals)
Read out values from the proposal.
Allows the user to read out any values from the proposal that may be needed elsewhere. This function must be overwritten.
- Parameters:
out_vals (list) –
[logP_out, ll_out, lp_out, log_proposal_pdf_out, log_sum_weights]
.all_vals_prop (list) –
[logP, ll, lp, log_proposal_pdf, log_sum_weights]
.aux_all_vals (list) –
[aux_logP, aux_ll, aux_lp, aux_log_proposal_pdf, aux_log_sum_weights]
.
- setup(branches_coords)
Any setup for the proposal.
- Parameters:
branches_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- property temperature_control
Temperature controller
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
Implemented Multiple Try RJ Moves
- class eryn.moves.MTDistGenMoveRJ(generate_dist, *args, **kwargs)
Bases:
MultipleTryMoveRJ
,DistributionGenerateRJ
- special_generate_logpdf(generated_coords)
Get logpdf of generated coordinates.
- Parameters:
generated_coords (np.ndarray) – Current coordinates of walkers.
- Returns:
logpdf of generated points.
- Return type:
np.ndarray
- special_generate_func(coords, random, size=1, fill_tuple=None, fill_values=None)
Generate samples and calculate the logpdf of their proposal function.
- Parameters:
coords (np.ndarray) – Current coordinates of walkers.
random (obj) – Random generator.
*args (tuple, optional) – additional arguments passed by overwriting the
get_proposal
function and passingargs_generate
keyword argument.size (int, optional) – Number of tries to generate.
fill_tuple (tuple, optional) – Length 2 tuple with the indexing of which values to fill when generating. Can be used for auxillary proposals or reverse RJ proposals. First index is the index into walkers and the second index is the index into the number of tries. (default:
None
)fill_values (np.ndarray) – values to fill associated with
fill_tuple
. Should have size(len(fill_tuple[0]), ndim)
. (default:None
).**kwargs (tuple, optional) – additional keyword arguments passed by overwriting the
get_proposal
function and passingkwargs_generate
keyword argument.
- Returns:
(generated points, logpdf of generated points).
- Return type:
tuple
- set_coords_and_inds(generated_coords, inds_leaves_rj=None)
Setup coordinates for prior and Likelihood
- Parameters:
generated_coords (np.ndarray) – Generated coordinates with shape
(number of independent walkers, num_try)
.inds_leaves_rj (np.ndarray) – Index into each individual walker giving the leaf index associated with this proposal. Should only be used if
self.rj is True
. (default:None
)
- Returns:
Coordinates for Likelihood and Prior.
- Return type:
dict
- special_like_func(generated_coords, inds_leaves_rj=None, **kwargs)
Calculate the Likelihood for sampled points.
- Parameters:
generated_coords (np.ndarray) – Generated coordinates with shape
(number of independent walkers, num_try)
.inds_leaves_rj (np.ndarray) – Index into each individual walker giving the leaf index associated with this proposal. Should only be used if
self.rj is True
. (default:None
)
- Returns:
Likelihood values with shape
(generated_coords.shape[0], num_try).
- Return type:
np.ndarray
- special_prior_func(generated_coords, inds_leaves_rj=None, **kwargs)
Calculate the Prior for sampled points.
- Parameters:
generated_coords (np.ndarray) – Generated coordinates with shape
(number of independent walkers, num_try)
.inds_leaves_rj (np.ndarray) – Index into each individual walker giving the leaf index associated with this proposal. Should only be used if
self.rj is True
. (default:None
)
- Returns:
Prior values with shape
(generated_coords.shape[0], num_try).
- Return type:
np.ndarray
- property acceptance_fraction
Acceptance fraction for this move.
- property accepted
Accepted counts for this move.
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- get_model_change_proposal(inds, random, nleaves_min, nleaves_max)
Helper function for changing the model count by 1.
This helper function works with nested models where you want to add or remove one leaf at a time.
- Parameters:
inds (np.ndarray) –
inds
values for this specific branch with shape(ntemps, nwalkers, nleaves_max)
.random (object) – Current random state of the sampler.
nleaves_min (int) – Minimum allowable leaf count for this branch.
nleaves_max (int) – Maximum allowable leaf count for this branch.
- Returns:
- Keys are
"+1"
and"-1"
. Values are indexing information. "+1"
and"-1"
indicate if a source is being added or removed, respectively. The indexing information is a 2D array with shape(number changing, 3)
. The length 3 is the index into each of the(ntemps, nwalkers, nleaves_max)
.
- Keys are
- Return type:
dict
- get_mt_log_posterior(ll, lp, betas=None)
Calculate the log of the posterior for all tries.
- Parameters:
ll (np.ndarray) – Log Likelihood values with shape
(nwalkers, num_tries)
.lp (np.ndarray) – Log Prior values with shape
(nwalkers, num_tries)
.betas (np.ndarray, optional) – Inverse temperatures to include in log Posterior computation. (default:
None
)
- Returns:
Log of the Posterior with shape
(nwalkers, num_tries)
.- Return type:
np.ndarray
- get_mt_proposal(coords, random, args_generate=(), kwargs_generate={}, args_like=(), kwargs_like={}, args_prior=(), kwargs_prior={}, betas=None, ll_in=None, lp_in=None, inds_leaves_rj=None, inds_reverse_rj=None)
Make a multiple-try proposal
Here,
nwalkers
refers to all independent walkers which generally will meannwalkers * ntemps
in terms of the rest of the sampler.- Parameters:
coords (np.ndarray) – Current coordinates of walkers.
random (obj) – Random generator.
args_generate (tuple, optional) – Additional
*args
to pass to generate function. Must overwriteget_proposal
function to use these. (default:()
)kwargs_generate (dict, optional) – Additional
**kwargs
to pass to generate function. (default:{}
) Must overwriteget_proposal
function to use these.args_like (tuple, optional) – Additional
*args
to pass to Likelihood function. Must overwriteget_proposal
function to use these. (default:()
)kwargs_like (dict, optional) – Additional
**kwargs
to pass to Likelihood function. Must overwriteget_proposal
function to use these. (default:{}
)args_prior (tuple, optional) – Additional
*args
to pass to Prior function. Must overwriteget_proposal
function to use these. (default:()
)kwargs_prior (dict, optional) – Additional
**kwargs
to pass to Prior function. Must overwriteget_proposal
function to use these. (default:{}
)betas (np.ndarray, optional) – Inverse temperatures passes to the proposal with shape
(nwalkers,)
.ll_in (np.ndarray, optional) – Log Likelihood values coming in for current coordinates. Must be provided if
self.rj is True
. Ifself.rj is True
, must be nested. Also, for all proposed removals, this value must be the Likelihood with the binary removed so all proposals are pretending to add a binary. Useful ifself.independent is True
. (default:None
)lp_in (np.ndarray, optional) – Log Prior values coming in for current coordinates. Must be provided if
self.rj is True
. Ifself.rj is True
, must be nested. Also, for all proposed removals, this value must be the Likelihood with the binary removed so all proposals are pretending to add a binary. Useful ifself.independent is True
. (default:None
)inds_leaves_rj (np.ndarray, optional) – Array giving the leaf index of each incoming walker. Must be provided if
self.rj is True
. (default:None
)inds_reverse_rj (np.ndarray, optional) – Array giving the walker index for which proposals are reverse proposal removing a leaf. Must be provided if
self.rj is True
. (default:None
)
- Returns:
(generated points, factors).
- Return type:
tuple
- Raises:
ValueError – Inputs are incorrect.
- get_proposal(branches_coords, branches_inds, nleaves_min_all, nleaves_max_all, random, **kwargs)
Make a proposal
- Parameters:
all_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.all_inds (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max]. These are the boolean arrays marking which leaves are currently used within each walker.nleaves_min_all (list) – Minimum values of leaf ount for each model. Must have same order as
all_cords
.nleaves_max_all (list) – Maximum values of leaf ount for each model. Must have same order as
all_cords
.random (object) – Current random state of the sampler.
**kwargs (ignored) – For modularity.
- Returns:
- Tuple containing proposal information.
First entry is the new coordinates as a dictionary with keys as
branch_names
and values asdouble `` np.ndarray[ntemps, nwalkers, nleaves_max, ndim] containing proposed coordinates. Second entry is the new ``inds
array with boolean values flipped for added or removed sources. Third entry is the factors associated with the proposal necessary for detailed balance. This is effectively any term in the detailed balance fraction. +log of factors if in the numerator. -log of factors if in the denominator.
- Return type:
tuple
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- propose(model, state)
Use the move to generate a proposal and compute the acceptance
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
State of sampler after proposal is complete.
- Return type:
State
- readout_adjustment(out_vals, all_vals_prop, aux_all_vals)
Read out values from the proposal.
Allows the user to read out any values from the proposal that may be needed elsewhere. This function must be overwritten.
- Parameters:
out_vals (list) –
[logP_out, ll_out, lp_out, log_proposal_pdf_out, log_sum_weights]
.all_vals_prop (list) –
[logP, ll, lp, log_proposal_pdf, log_sum_weights]
.aux_all_vals (list) –
[aux_logP, aux_ll, aux_lp, aux_log_proposal_pdf, aux_log_sum_weights]
.
- setup(branches_coords)
Any setup for the proposal.
- Parameters:
branches_coords (dict) – Keys are
branch_names
. Values are np.ndarray[ntemps, nwalkers, nleaves_max, ndim]. These are the curent coordinates for all the walkers.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- property temperature_control
Temperature controller
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
Utility Moves
- class eryn.moves.CombineMove(moves, *args, verbose=False, **kwargs)
Bases:
Move
Move that combines specific moves in order.
- Parameters:
moves (list) – List of moves, similar to how
moves
is submitted toeryn.ensemble.EnsembleSampler
. If weights are provided, they will be ignored.*args (tuple, optional) – args to be passed to
Move
.verbose (bool, optional) – If
True
, usetqdm
to show progress throught steps. This can be very helpful when debugging.**kwargs (dict, optional) – kwargs to be passed to
Move
.
- property accepted
Accepted counts for each move.
- property acceptance_fraction
get acceptance fraction averaged over all moves
- property acceptance_fraction_separate
get acceptance fraction from each move
- property temperature_control
temperature controller
- cleanup_proposals_gibbs(branch_names_run, inds_run, q, branches_coords, new_inds=None, branches_inds=None, new_branch_supps=None, branches_supplemental=None)
Set all not Gibbs-sampled parameters back
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.q (dict) – Dictionary of new coordinate arrays for all proposal branches.
branches_coords (dict) – Dictionary of old coordinate arrays for all branches.
new_inds (dict, optional) – Dictionary of new inds arrays for all proposal branches.
branches_inds (dict, optional) – Dictionary of old inds arrays for all branches.
new_branch_supps (dict, optional) – Dictionary of new branches supplemental for all proposal branches.
branches_supplemental (dict, optional) – Dictionary of old branches supplemental for all branches.
- compute_log_posterior_basic(logl, logp)
Compute the log of posterior
\(\log{P} = \log{L} + \log{p}\)
This method is to mesh with the tempered log posterior computation.
- Parameters:
logl (np.ndarray[ntemps, nwalkers]) – Log-likelihood values.
logp (np.ndarray[ntemps, nwalkers]) – Log-prior values.
- Returns:
Log-Posterior values.
- Return type:
np.ndarray[ntemps, nwalkers]
- ensure_ordering(correct_key_order, q, new_inds, new_branch_supps)
Ensure proper order of key in dictionaries.
- Parameters:
correct_key_order (list) – Keys in correct order.
q (dict) – Dictionary of new coordinate arrays for all branches.
new_inds (dict) – Dictionary of new inds arrays for all branches.
new_branch_supps (dict or None) – Dictionary of new branches supplemental for all proposal branches.
- Returns:
(q, new_inds, new_branch_supps) in correct key order.
- Return type:
Tuple
- fix_logp_gibbs(branch_names_run, inds_run, logp, inds)
Set any walker with no leaves to have logp = -np.inf
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.logp (np.ndarray) – Log of the prior going into final posterior computation.
inds (dict) – Dictionary of
inds
arrays for all branches.
- gibbs_sampling_setup_iterator(all_branch_names)
Iterate through the gibbs splits as a generator
- Parameters:
all_branch_names (list) – List of all branch names.
- Yields:
2-tuple –
- Gibbs sampling split.
First entry is the branch names to run and the second entry is the index into the leaves/parameters for this Gibbs split.
- Raises:
ValueError – Incorrect inputs.
- setup_proposals(branch_names_run, inds_run, branches_coords, branches_inds)
Setup proposals when gibbs sampling.
Get inputs into the proposal including Gibbs split information.
- Parameters:
branch_names_run (list) – List of branch names to run concurrently.
inds_run (list) – List of
inds
arrays including Gibbs sampling information.branches_coords (dict) – Dictionary of coordinate arrays for all branches.
branches_inds (dict) – Dictionary of
inds
arrays for all branches.
- Returns:
- (coords, inds, at_least_one_proposal)
Coords including Gibbs sampling info.
inds
including Gibbs sampling info.at_least_one_proposal
is boolean. It is passed out toindicate there is at least one leaf available for the requested branch names.
- Return type:
tuple
- tune(state, accepted)
Tune a proposal
This is a place holder for tuning.
- Parameters:
state (
eryn.state.State
) – Current state of sampler.accepted (np.ndarray[ntemps, nwalkers]) – Accepted values for last pass through proposal.
- update(old_state, new_state, accepted, subset=None)
Update a given subset of the ensemble with an accepted proposal
This class was updated from
emcee
to handle the added structure of Eryn.- Parameters:
old_state (
eryn.state.State
) – State with current information. New information is added to this state.new_state (
eryn.state.State
) – State with information from proposed points.accepted (np.ndarray[ntemps, nwalkers]) – A vector of booleans indicating which walkers were accepted.
subset (np.ndarray[ntemps, nwalkers], optional) – A boolean mask indicating which walkers were included in the subset. This can be used, for example, when updating only the primary ensemble in a
RedBlueMove
. (default:None
)
- Returns:
old_state
with accepted points added fromnew_state
.- Return type:
- property periodic
periodic parameter information
- propose(model, state)
Propose a combined move.
- Parameters:
model (
eryn.model.Model
) – Carrier of sampler information.state (
State
) – Current state of the sampler.
- Returns:
- (state, accepted)
The first return is the state of the sampler after the move. The second return value is the accepted count array for each walker counting for all proposals.
- Return type:
tuple