State
The eryn.state.State carries the information around for all walkers at a given step. The individual branches are carried in eryn.state.Branch class objects. A utility provided for state objects is the eryn.state.BranchSupplemental object. This object can carry information around the sampler while being indexed and moved around just like other sampler information. See the tutorial for more information.
- class eryn.state.State(coords, inds=None, branch_supplemental=None, supplemental=None, log_like=None, log_prior=None, betas=None, blobs=None, random_state=None, copy=False)
Bases:
objectThe state of the ensemble during an MCMC run
- Parameters:
coords (double ndarray[ntemps, nwalkers, nleaves_max, ndim], dict, or
State) – The current positions of the walkers in the parameter space. If dict, need to usebranch_namesfor the keys.inds (bool ndarray[ntemps, nwalkers, nleaves_max] or dict, optional) – The information on which leaves are used and which are not used. A value of True means the specific leaf was used in this step. If dict, need to use
branch_namesfor the keys. Input should beNoneif a completeStateobject is input forcoords. (default:None)branch_supplemental (object) –
BranchSupplementalobject specific to this branch. (default:None)log_like (ndarray[ntemps, nwalkers], optional) – Log likelihoods for the walkers at positions given by
coords. Input should beNoneif a completeStateobject is input forcoords. (default:None)log_prior (ndarray[ntemps, nwalkers], optional) – Log priors for the walkers at positions given by
coords. Input should beNoneif a completeStateobject is input forcoords. (default:None)betas (ndarray[ntemps], optional) – Temperatures in the sampler at the current step. Input should be
Noneif a completeStateobject is input forcoords. (default:None)blobs (ndarray[ntemps, nwalkers, nblobs], Optional) – The metadata “blobs” associated with the current position. The value is only returned if lnpostfn returns blobs too. Input should be
Noneif a completeStateobject is input forcoords. (default:None)random_state (Optional) – The current state of the random number generator. Input should be
Noneif a completeStateobject is input forcoords. (default:None)copy (bool, optional) – If True, copy the the arrays in the former
Stateobhect.
- Raises:
ValueError – Dimensions of inputs or input types are incorrect.
- property branches_inds
Get the
indsfrom all branch objects returned as a dictionary withbranch_namesas keys.
- property branches_coords
Get the
coordsfrom all branch objects returned as a dictionary withbranch_namesas keys.
- property branches_supplemental
Get the
branch.supplementalfrom all branch objects returned as a dictionary withbranch_namesas keys.
- property branch_names
Get the branch names in this state.
- get_log_posterior(temper: bool = False)
Get the posterior probability
- Parameters:
temper (bool, optional) – If
True, apply tempering to the posterior computation.- Returns:
Log of the posterior probability.
- Return type:
np.ndarray[ntemps, nwalkers]
- class eryn.state.Branch(coords, inds=None, branch_supplemental=None)
Bases:
objectSpecial container for one branch (model)
This class is a key component of Eryn. It this type of object that allows for different models to be considered simultaneously within an MCMC run.
- Parameters:
coords (4D double np.ndarray[ntemps, nwalkers, nleaves_max, ndim]) – The coordinates in parameter space of all walkers.
inds (3D bool np.ndarray[ntemps, nwalkers, nleaves_max], optional) – The information on which leaves are used and which are not used. A value of True means the specific leaf was used in this step. Parameters from unused walkers are still kept. When they are output to the backend, the backend saves a special number (default:
np.nan) for all coords related to unused leaves at that step. If None, inds will fill with all True values. (default:None)branch_supplemental (object) –
BranchSupplementalobject specific to this branch. (default:None)
- Raises:
ValueError –
indshas wrong shape or number of leaves is less than zero.
- property nleaves
Number of leaves for each walker
- class eryn.state.BranchSupplemental(obj_info: dict, base_shape: tuple, copy: bool = False)
Bases:
objectSpecial object to carry information through sampler.
The
BranchSupplementalobject is a holder of information that is passed through the sampler. It can also be indexed similar to other quantities carried throughout the sampler.This indexing is based on the
base_shape. You can store many objects that have the same base shape and then index across all of them. For example, if you want to store individual leaf information, the base shape will be(ntemps, nwalkers, nleaves_max). If you want to store a 2D array per individual leaf, the overall shape will be(ntemps, nwalkers, nleaves_max, dim2_extra, dim1_extra). Another type of information is stored in a class object (for example). Usingnumpyobject arrays,ntemps * nwalkers * nleaves_maxnumber of class objects can be stored in the array. Then, using special indexing functions, information can be updated/accessed across all objects stored simultaneously. If you index this class, it will give you back a dictionary with all objects stored indexed for each leaf. So if you index (0, 0, 0) in our running example, you will get back a dictionary with one 2D array and one class object from thenumpyobject array.All of these objects are stored in
self.holder.- Parameters:
obj_info (dict) – Initial information for storage. Keys are the names to be stored under and values are arrays. These arrays should have a base shape that is equivalent to
base_shape, meaningarray.shape[:len(base_shape)] == self.base_shape. The dimensions beyond the base shape can be anything.base_shape (tuple) – Base shape for indexing. Objects stored in the supplemental object will have a shape that at minimum is equivalent to
base_shape.copy (bool, optional) – If
True, copy whatever information is given in before it is stored. ifFalse, store directly the input information. (default:False)
- holder
All of the objects stored for this supplemental object.
- Type:
dict
- add_objects(obj_info: dict, copy=False)
Add objects to the holder.
- Parameters:
obj_info (dict) – Information for storage. Keys are the names to be stored under and values are arrays. These arrays should have a base shape that is equivalent to
base_shape, meaningarray.shape[:len(base_shape)] == self.base_shape. The dimensions beyond the base shape can be anything.copy (bool, optional) – If
True, copy whatever information is given in before it is stored. ifFalse, store directly the input information. (default:False)
- Raises:
ValueError – Shape matching issues.
- remove_objects(names)
Remove objects from the holder.
- Parameters:
names (str or list of str) – Strings associated with information to delete. Please note it does not return the information.
- Raises:
ValueError – Input issues.
- property contained_objects
The list of keys of contained objects.
- take_along_axis(indices, axis: int, skip_names=[])
Take information from contained arrays along an axis.
See
`numpy.take_along_axis<https://numpy.org/doc/stable/reference/generated/numpy.take_along_axis.html>`_.- Parameters:
indices (xp.ndarray) – Indices to take along each 1d slice of arr. This must match the dimension of
self.base_shape, but other dimensions only need to broadcast againstself.base_shape.axis (int) – The axis to take 1d slices along.
skip_names (list of str, optional) – By default, this function returns the results for all stored objects. This list gives the strings of objects to leave behind and not return.
- Returns:
Keys are names of stored objects and values are the proper array slices.
- Return type:
dict
- put_along_axis(indices, values_in: dict, axis: int)
Put information information into contained arrays along an axis.
See
`numpy.put_along_axis<https://numpy.org/doc/stable/reference/generated/numpy.put_along_axis.html>`_.Please note this function is not implemented in
cupy, so this is a custom implementation for bothcupyandnumpy.- Parameters:
indices (xp.ndarray) – Indices to put values along each 1d slice of arr. This must match
self.base_shape (the dimension of)
against (but other dimensions only need to broadcast)
self.base_shape.
axis (int) – The axis to put 1d slices along.
values_in (dict) – Keys are the objects contained to update. Values are the arrays of these objects with shape and dimension that can broadcast to match that of indices.
- property flat
Get flattened arrays from the stored objects.
Here “flat” is in relation to
self.base_shape. Beyondself.base_shape, the shape is mainted.