SepiaModel¶
The SepiaModel object should be instantiated after the SepiaData object is created and manipulated (standardized, bases created).
- class sepia.SepiaModel(data, lamVzGroup=None, theta_fcon=None, theta_init=None, LamSim=None)¶
SepiaModel class contains data, SepiaParam objects, and precomputed elements for the likelihood.
- Variables
data (sepia.SepiaData) – instantiated sepia.SepiaData object
num (sepia.ModelContainer) – sepia.ModelContainer for computed numerical elements for use in evaluating likelihood/posterior
params (sepia.SepiaParamList) – sepia.SepiaParamList containing all SepiaParam objects for the model and mcmcList (references to params involved in MCMC)
verbose (bool) – print verbose output for this model?
Sets up SepiaModel object based on instantiated SepiaData object.
- Parameters
data (sepia.SepiaData) – instantiated sepia.SepiaData object with all transformations and basis creation done
lamVzGroup (numpy.ndarray/list) – indicate groups for lamVz (otherwise single lamVz for all D basis functions)
theta_fcon (function/NoneType) – constraint function for thetas; should take entire theta array and return True/False for constraint satisfied
theta_init (numpy.ndarray/NoneType) – if using theta_fcon, should provide theta_init array that satisfies constraints
LamSim (numpy.ndarray) – option to pass custom LamSim (warning: don’t use this unless know what you’re doing!)
- Returns
instantiated sepia.SepiaModel object
- Raises
ValueError – if K_sim and K_obs shapes are not conformal
TypeError – if lamVzGroup is not the correct size or specification
Note
if SepiaData input transformations have not been done, a warning will be printed and default transformations will be done to x, t, and y. Similarly, a K basis will be created if you did not do that first. Since a D basis is not required, this is not created automatically, but a warning will be printed in case creating a model without a D basis was not intentional.
- add_samples(sdict=None)¶
Add samples from the samples_dict to the model. Will be particularly useful in parallel chains, to re-integrate samples.
- Parameters
sdict (dict) – samples dict, as obtained from get_samples()
- Returns
no return value; model object will be modified
- clear_samples()¶
clear the mcmc samples in the model :return: no returned value
- do_mcmc(nsamp, prog=True, do_propMH=True, no_init=False, seed=None)¶
Run MCMC sampling on instantiated SepiaModel object.
- Parameters
nsamp (int) – number of MCMC samples
prog (bool) – show progress bar?
do_propMH (bool) – use propMH sampling for variables with ‘propMH’ stepType?
no_init (bool) – skip initialization? (only use if model has already been sampled from)
Note
Calling multiple times on the same model appends samples to the existing samples.
- get_last_sample_ind()¶
Return index of the last sample = the number of samples - 1
- Returns
number of samples - 1
- get_num_samples()¶
Return the number of samples recorded in the model
- Returns
number of samples
- get_samples(numsamples=None, nburn=0, sampleset=None, flat=True, includelogpost=True, effectivesamples=False, return_sampleset=False)¶
Extract MCMC samples into dictionary format. By default, all samples are returned, or samples can be subset using in various ways using the optional input arguments.
- Parameters
numsamples (int/NoneType) – number of samples to include, evenly spaced from first to last
nburn (int) – number of samples to discard at beginning of chain (default = 0)
sampleset (list/NoneType) – list of indices of samples to include; if given, numsamples and nburn are ignored.
flat (bool) – flatten the resulting arrays (for parameters stored as matrices)?
includelogpost (bool) – include logPost values?
effectivesamples (bool) – use effective sample size of thetas to subset samples? If True, numsamples and sampleset are ignored.
return_sampleset (bool) – whether to return the sampleset indices (default False)
- Returns
dict – array of samples for each parameter, keyed by parameter name; optionally, also return sampleset (list)
- Raises
TypeError if no samples exist or nburn inconsistent with number of draws
Note
If theta is in the model, will also add key theta_native with theta rescaled to original range.
- logLik(cvar='all', cindex=None)¶
Compute model log lik with current values of variables.
- Parameters
cvar (string) – name of variables changed since last call (controls recomputation of num components), or ‘all’
cindex (int/NoneType) – index of flattened cvar that has changed since last call (or None to indicate all indices should be considered changed)
- Returns
scalar – log lik value
- logPost(cvar='all', cindex=None)¶
Compute model log posterior with current values of variables.
- Parameters
cvar (string) – name of variables changed since last call (controls recomputation of num components), or ‘all’
cindex (int/NoneType) – index of flattened cvar that has changed since last call (or None to indicate all indices should be considered changed)
- Returns
scalar – log posterior value
- log_prior()¶
Evaluates log prior.
- Returns
float – summed log prior over all parameters
- print_mcmc_info(pnames=None)¶
Print some information about the MCMC setup.
- Parameters
pnames (list/NoneType) – list of parameter names to print information about; default is to print all.
- print_prior_info(pnames=None)¶
Print some information about the priors.
- Parameters
pnames (list/NoneType) – list of parameter names to print information about; default is to print all.
- print_priors_for_mcmc()¶
Print some information about the parameter priors that are defined for mcmc
- print_value_info(pnames=None)¶
Print some information about the parameter values. (Shows initial values if called before MCMC)
- Parameters
pnames (list/NoneType) – list of parameter names to print information about; default is to print all.
- restore_model_info(file_name=None)¶
Restores model state from model info in file.
- Parameters
file_name (string/NoneType) – file name for model info (with no extension); if None, uses default name.
- save_model_info(file_name=None, overwrite=False)¶
Saves some important model info needed to restore model state into new model.
- Parameters
file_name (string/NoneType) – optional file name for saving model (with no extension); if None, uses default name.
overwrite (bool) – okay to overwrite existing file? If False, and file_name exists, raises exception.
- Raises
FileExistsError – if file_name already exists and overwrite=False
- set_model_to_sample(samp=None)¶
Add samples from the samples_dict to the model Will be particularly useful in parallel chains, to re-integrate samples.
- Parameters
samp (int) – sample index to set model to. Default will be final stored sample.
- Returns
no return value; model object will be modified
- tune_step_sizes(n_burn, n_levels, prog=True, diagnostics=False, update_vals=True, verbose=True)¶
Auto-tune step size based on acceptance rate with YADAS approach.
- Parameters
n_burn (int) – number of samples to draw for each proposed step size
n_levels (int) – number of levels to propose for each step size
prog (bool) – show progress bar?
verbose (bool) – Print before and after info to console, default True
diagnostics (bool) – return some information on acceptance rates used inside step size tuning?
Note
Does not work for hierarchical or shared theta models.