Reference: Parameter inference¶
-
class
pydemic.SampleParameter(name, bounds, mean=None, uncertainty=None, sigma=None, guess=None)[source]¶ Representation of sample parameters as interpreted by
LikelihoodEstimator.-
name¶ The name of the parameter (as to be passed by keyword to likelihood estimators).
-
mean¶ The mean of the prior for the parameter. If None, no prior is assummed for the parameter.
-
sigma¶ The uncertainty of the prior for the parameter. If None, no prior is assummed for the parameter.
-
-
class
pydemic.LikelihoodEstimator(*, simulator, fixed_values, sample_parameters, data, norms)[source]¶ Driver for likelihood estimation.
The following keyword-only arguments are required:
- Parameters
simulator – A
classwith aget_model_datamethod to be used for sampling.get_model_datamust have signature(t, **kwargs)wheretis apandas.DatetimeIndexand paramter values (fromfixed_valuesand the particular sample ofsample_parameters) are passed through**kwargs.fixed_values – A
dictof values fixed for non-sample parameters.sample_parameters – A
listofSampleParameter’s for sampling.data – A
pandas.DataFrameof the real data to fit against.norms – A
dictspecifying the columns ofdata(and of the result ofsimulator.get_model_data) by key and the likelihood estimator to use for that dataset. The values may be'poisson'(specifying usage ofpoisson_norm()) or a function with signature(model, data, **kwargs). Values for sample parameters andfixed_valuesare propagated to norm functions by keyword.
-
__call__(theta)[source]¶ Method used internally to compute likelihoods for a set of parameters
theta(e.g., byemcee).- Parameters
theta – A
numpy.ndarrayof parameter values (with order specified bysample_parameters).- Returns
The likelihood.
-
get_log_likelihood(parameters)[source]¶ - Parameters
parameters – A
dictof parameter values for those specified specified bysample_parameters, to be passed tosimulator.get_model_data(along withfixed_values).- Returns
The likelihood.
-
get_initial_positions(walkers, method='normal')[source]¶ Generates initial samples for MCMC sampling.
- Parameters
walkers – The number of walkers used in sampling.
- Returns
A
numpy.ndarrayof initial walker positions with shape(walkers, len(sample_parameters)).
-
sample_uniform(num_points, pool=None)[source]¶ Driver for uniform sampling of the parameter space.
- Parameters
num_points – The number of points to sample across each dimension (with bounds specified by
SampleParameter.bounds).pool – An
multiprocessing.Poolto use for parallelization. Defaults to None, in which case sampling is not parallelized.
- Returns
A
tupleof twonumpy.ndarray’s containing the sample parameter values and the likelihoods.
-
sample_emcee(steps, walkers=None, pool=None, moves=None, progress=True, init_method='uniform', backend=None, backend_filename=None)[source]¶ Driver for MCMC sampling using
emcee.- Parameters
steps – The number of MCMC steps to take.
walkers – The number of MCMC walkers to use.
pool – An
multiprocessing.Poolto use for parallelization. Defaults to None, in which case sampling is not parallelized.init_method –
backend – The
pydemic.hdf.HDFBackendto use for sampling. Defaults to None, i.e., no backend.backend_filename – The filename to use to create a
pydemic.hdf.HDFBackend. Defaults to None, in which case no backend file is created.
Any remaining keyword arguments are used as specified by
emcee.EnsembleSampler.- Returns
-
differential_evolution(workers=-1, progress=True, backend=None, backend_filename=None, **kwargs)[source]¶ Performs global optimization using
scipy.optimize.differential_evolution().In addition to the arguments recognized by
scipy.optimize.differential_evolution()(which must be passed by keyword), the following arguments are recognized:- Parameters
progress – Whether to display a progress bar.
backend – The
pydemic.hdf.HDFBackendto use for sampling. Defaults to None, i.e., no backend.backend_filename – The filename to use to create a
pydemic.hdf.HDFBackend. Defaults to None, in which case no backend file is created.
Likelihood norms¶
-
pydemic.sampling.poisson_norm(model, data, **kwargs)[source]¶ - Parameters
model – A
numpy.ndarrayof model predictions.data – A
numpy.ndarrayof real data.
- Returns
The log-Poisson likelihood estimator.
Backends with HDF5¶
-
class
pydemic.hdf.HDFBackend(filename, sample_parameters=None, fixed_values=None, data=None, simulator=None, **kwargs)[source]¶ A subclass of
emcee.backends.HDFBackendwhich stores additional information used byLikelihoodEstimatorto automate resuming sampling.Note
This class requires
h5py.- Parameters
filename – The name of the HDF5 file to create.
The following optional parameters (corresponding to those passed to
pydemic.LikelihoodEstimator) will be stored in the file if passed.- Parameters
sample_parameters –
fixed_values –
data –
simulator –
Any remaining keyword arguments are passed to
emcee.backends.HDFBackend.The following attributes will be available if they were passed to
HDFBackend()upon creation of the file, and may be used to resume sampling:-
fixed_values¶
-
sample_parameters¶
-
data¶
-
simulator¶ The simulation class whose
get_model_datamethod is used for sampling. If the class is defined inpydemic.models, that class will be returned; otherwise the name of the class will be returned.