Sepia shared/hierarchical theta models¶
Collections of multiple models with shared thetas or hierarchically-modeled thetas are created using these classes.
- class sepia.SepiaSharedThetaModels.SepiaSharedThetaModels(model_list=None, shared_theta_inds=None)¶
Container for multiple models with sharing of selected thetas between models.
- Variables
model_list (list) – list of instantiated sepia.SepiaModel objects
shared_theta_inds (numpy.ndarray) – indices showing which thetas are shared across models, shape (n_hier_theta, n_models)
n_hier (int) – number of shared theta groups
n_models (int) – number of models
to_update (numpy.ndarray) – 0/1 matrix indicating which variables need to be shared
to_sample (numpy.ndarray) – 0/1 matrix indicating which variable should be sampled (then copied to others)
Instantiate shared theta model object.
- Parameters
model_list (list) – list of instantiated sepia.SepiaModel objects
shared_theta_inds (numpy.ndarray) – indices showing which thetas are shared, shape (n_shared_theta, n_models)
- Raises
TypeError – if number of models doesn’t match shared_theta_inds shape or if variable types aren’t the same (categorical vs continuous)
Note
In shared_theta_inds, each row corresponds to one group of shared thetas, and each column gives the index of the theta within a particular model, with -1 used to indicate no theta from a particular model is part of the shared group. Example: shared_theta_inds = np.array([(1, 1, 1), (2, -1, 4)) for 3 models, theta index 1 shared across all models, theta indices 2/4shared across models 1 and 3 but no corresponding theta in model 2.
- do_mcmc(nsamp, do_propMH=True, prog=True)¶
Do MCMC for shared theta model.
- Parameters
nsamp (int) – number of MCMC samples
do_propMH (bool) – use propMH sampling for params with stepType propMH?
prog (bool) – show progress bar for sampling?
- get_samples(nburn=0, sampleset=None, numsamples=None, flat=True, includelogpost=True)¶
Extract MCMC samples into dictionary format for each model in self.model_list. By default, all samples are returned, or samples can be subset using in various ways using the optional input arguments.
- Parameters
nburn (int) – number of samples to discard at beginning of chain
sampleset (list) – list of indices of samples to include
numsamples (int) – number of samples to include, evenly spaced from first to last
flat (bool) – flatten the resulting arrays (for parameters stored as matrices)?
includelogpost (bool) – include logPost values?
- Returns
dict of dict – one dict per model, each dict is array of samples for each parameter, keyed by parameter name
- Raises
TypeError if no samples exist or nburn inconsistent with number of draws
Note
Adds key theta_native with theta rescaled to original range.
- class sepia.SepiaHierarchicalThetaModels(model_list, hier_theta_inds)¶
Container for multiple models with hierarchical Normal model on selected thetas.
- Variables
model_list (list) – list of instantiated sepia.SepiaModel objects
hier_theta_inds (numpy.ndarray) – indices showing which thetas are hierarchically linked, shape (n_hier_theta, n_models)
n_hier (int) – number of hierarchical groups
n_models (int) – number of models
to_update (numpy.ndarray) – 0/1 matrix indicating which variables need to be hierarchically updated
hier_mu (list) – list of sepia.SepiaParam objects for hierarchical mu parameters
hier_lambda (list) – list of sepia.SepiaParam objects for hierarchical lambda parameters
hier_delta (list) – list of sepia.SepiaParam objects for hierarchical delta (lockstep update) parameters
Instantiate hierarchical model container.
- Parameters
model_list (list) – list of instantiated sepia.SepiaModel objects
hier_theta_inds (numpy.ndarray) – indices showing which thetas are hierarchically linked, shape (n_hier_theta, n_models)
- Raises
TypeError – if number of models doesn’t match hier_theta_inds or if user attempts to model categorical variable hierarchically.
Note
In hier_theta_inds, each row corresponds to one group of hierarchically modeled thetas, and each column gives the index of the theta within a particular model, with -1 used to indicate no theta from a particular model is part of the hierarchical group. Example: hier_theta_inds = np.array([[1, 1, 1], [2, -1, 4]]) for 3 models, theta index 1 hierarchical across all models, theta indices 2/4 hierarchical across models 1 and 3 but no corresponding theta in model 2.
- do_mcmc(nsamp, do_propMH=True, prog=True, do_lockstep=True)¶
Does MCMC for hierarchical model.
- Parameters
nsamp (int) – number of MCMC samples
do_propMH (bool) – use propMH sampling for params with stepType propMH?
prog (bool) – show progress bar for sampling?
do_lockstep (bool) – do lockstep updates?
- get_samples(nburn=0, sampleset=None, numsamples=None, flat=True)¶
Extract MCMC samples into dictionary format for each model in self.model_list. By default, all samples are returned, or samples can be subset using in various ways using the optional input arguments.
- Parameters
nburn (int) – number of samples to discard at beginning of chain
sampleset (list) – list of indices of samples to include
numsamples (int) – number of samples to include, evenly spaced from first to last
flat (bool) – flatten the resulting arrays (for parameters stored as matrices)?
- Returns
dict of dict – one dict per model, each dict is array of samples for each parameter, keyed by parameter name
- Raises
TypeError if no samples exist or nburn inconsistent with number of draws
Note
Adds key theta_native with theta rescaled to original range.