modds package¶
Submodules¶
modds.h5io module¶
h5io.py
Convenience functions/classes for working with hdf5 files and saving the outputs from MCMC sampling.
The HDF5Backend class manages an hdf5 file which stores the setup of a model along with the outputs of sampling from that model.
HDF5Backend instances can be created from existing hdf5 files, by providing a MeasurementModel instance and dictionary of settings, or through the parse_yaml function, which will attempt to construct a model from a properly formatted yaml file.
-
class
modds.h5io.HDF5Backend(filename=None, model=None, settings=None, overwrite=False)¶ Bases:
objectManages an hdf5 file and defines the structure of saved MCMC runs.
Calling the constructor with a filename will load the filename and make convenience functions available to operate on the file, including sampling.
Calling the constructor with a filename along with a model and a dictionary of settings will allow you to create a new hdf5 file in addition to loading it as an HDF5Backend instance.
The actual structure of the hdf5 file should not be necessary to know to interact with the instance, but should you wish to manually interact with it, is specified as follows:
/model : bytestring representation of MeasurementModel instance /version : str, version of colossus associated with the file /ndim : int, number of dimensions of parameter space /observables/R : radial locations of observed quantities /observables/q : observed quantity /observables/q_err : observational uncertainty
/state/chain : (nwalker, niter, ndim)-shaped array of posterior samples /state/niter : int, number of iterations /state/nacc : (nwalker,)-shaped array of acceptance counts for walkers /state/ppc : (nwalker, niter, ngrid)-shaped array of posterior predictive
checks/state/lnp : (nwalker, niter)-shaped array of log posterior values /state/lnl : (nwalker, niter)-shaped array of log likelihood values
/settings/cosmo : Colossus cosmology (str, e.g., “planck15”) /settings/quantity : one of {‘density’, ‘enclosedMass’, ‘surfaceDensity’,
‘deltaSigma’}/settings/r_grid : grid of radii for posterior predictive checks /settings/nwalkers : number of walkers in the ensemble
Parameters: -
acceptance_fraction¶
-
append_state(pos, lnp=None, ppc=None, lnl=None, Mvir=None, cvir=None, rsp=None, gamma_min=None, acceptances=None)¶ Append sampling results to the existing datasets.
Parameters: - pos (array_like) – shape of (nwalkers, ndim) of parameter positions
- lnp (array_like) – shape of (nwalkers,) of log posterior values
- ppc (array_like) – shape of (nwalkers, ngrid) of posterior predictive checks
- lnl (array_like) – shape of (nwalkers,) of log likelihood values
- Mvir (array_like) – shape of (nwalkers,) of virial mass values
- cvir (array_like) – shape of (nwalkers,) of concentration values
- rsp (array_like) – shape of (nwalkers,) of splashback radius values
- gamma_min (array_like) – shape of (nwalkers,) of minimum gamma values
- acceptances (array_like) – shape of (nwalkers,) of acceptance counts (1 if accepted, 0 if not)
-
chain¶
-
cosmo¶
-
dump(filename, model, settings, overwrite=False)¶ Stores the model/settings and initializes arrays.
-
lnl¶
-
lnp¶
-
load(filename)¶ Sets the filename, reads the model, and sets the cosmology.
-
model¶
-
nacc¶
-
ndim¶
-
niter¶
-
nwalkers¶
-
observables¶
-
ppc¶
-
quantity¶
-
r_grid¶
-
sample(niter, pool=None)¶ Sample from the model for niter iterations, optionally with a user provided pool of workers.
-
settings¶
-
shape¶
-
version¶
-
-
modds.h5io.parse_yaml(filename, overwrite=False)¶ Read in a yaml file and construct an hdf5 output file.
modds.measurement module¶
measurement.py
MeasurementModel wraps up a halo.HaloDensityProfile instance with both a set of observables (e.g., {r, DeltaSigma, DeltaSigma_err}) and a prior on the model parameters of interest.
-
class
modds.measurement.MeasurementModel(profile, observables, quantity, parameters, constants=None, lnlike=<function lnlike_gauss>, priors=None)¶ Bases:
objectA wrapped up halo density profile with data constraints and priors.
Parameters: - profile (halo.profile_bass.HaloDensityProfile) –
- observables (dict or iterable) – Should have keys of ‘r’, ‘q’, ‘q_err’, else it is assumed to be an iterable containing three arrays in the order r, q, q_err.
- quantity (str) – One of {‘rho’, ‘M’, ‘Sigma’, ‘DeltaSigma’}, with standard colossus units. Denotes volume mass density, enclosed mass, surface density, and surface density deviation (i.e., the weak lensing observable) respectively.
- parameters (list of colossus.modeling.Parameter instances) –
- constants (dict) – Map from constant name (str) to fixed physical value (float). Should contain redshift (named “z”) if not defined as a parameter
- lnlike (callable, optional) – Map from (q_model, q, q_err) to log likelihood at each point. Defaults to a Gaussian likelihood, i.e., q ~ N(q_model, q_err**2)
- priors (iterable, optional) – List of functions, f(profile, **kwargs) -> log prior. Additional priors to consider (e.g., halo mass concentration relation, See examples in joint_priors.py. Keywords should be either constants or parameters of the model.
modds.parameter module¶
parameter.py
The Parameter class wraps up a prior probability distribution from scipy.stats with an optional parameter transformation.
-
class
modds.parameter.Parameter(name, prior, transform=None, inverse_transform=None)¶ Bases:
objectA model parameter with a prior.
Often we want to sample over some transformation of the parameter, e.g., sampling with a uniform distribution over the log of some scale parameter. Thus the user can specify a transformation to map between the value as seen by the physical functions and the value as seen by the sampling algorithm.
Priors can be specified from those built into scipy.stats. See the list here: https://docs.scipy.org/doc/scipy/reference/stats.html
You can either directly pass a frozen scipy.stats.rv_continous instance, or a string of the form “prior(loc=0,scale=1)” where “prior” is the name of the distribution, and “loc”/”scale” represent location and scale parameters of the distribution. For example, to specify a Gaussian prior with a mean of 0 and a standard deviation of 1, you would pass “norm(loc=0, scale=1)”, while for a uniform distribution between 3 and 10, you would pass “uniform(loc=3, scale=7)”. Other distributions may have more complicated call signatures. See the scipy documentation for more details.
Parameters: - name (str) –
- prior (str or stats._distn_infrastructure.rv_frozen instance) – Prior probability distribution. If a string is given, it should be of the form “prior(a=1.2, b=3.4) where “prior” is some built-in prior model and “a”/”b” are the prior parameters (e.g., loc and scale). If an instance of scipy.stats.rv_continous is passed, the parameters should be frozen-in (i.e., you can call it without specifying hyper-parameters).
- transform (callable or str, optional) – A map from the parameter as seen by the sampler to the parameter as seen by colossus (i.e., the physical value). If a string is given, it should be one of the built-in transforms. Currently these are “from_log” : transform from the base-10 logarithm
- inverse_transform (callable, optional) – If a user-specified transformation is given instead of a built-in one, then you must also pass in the inverse transformation from the physical parameter to the parameter as seen by the sampler. E.g., for the log transform, the inverse_transform could be given as “lambda x: 10**x”.