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).

bounds

A tuple (lower, upper) specifying the range of permitted values for the parameter.

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 class with a get_model_data method to be used for sampling. get_model_data must have signature (t, **kwargs) where t is a pandas.DatetimeIndex and paramter values (from fixed_values and the particular sample of sample_parameters) are passed through **kwargs.

  • fixed_values – A dict of values fixed for non-sample parameters.

  • sample_parameters – A list of SampleParameter’s for sampling.

  • data – A pandas.DataFrame of the real data to fit against.

  • norms – A dict specifying the columns of data (and of the result of simulator.get_model_data) by key and the likelihood estimator to use for that dataset. The values may be 'poisson' (specifying usage of poisson_norm()) or a function with signature (model, data, **kwargs). Values for sample parameters and fixed_values are propagated to norm functions by keyword.

__call__(theta)[source]

Method used internally to compute likelihoods for a set of parameters theta (e.g., by emcee).

Parameters

theta – A numpy.ndarray of parameter values (with order specified by sample_parameters).

Returns

The likelihood.

get_log_likelihood(parameters)[source]
Parameters

parameters – A dict of parameter values for those specified specified by sample_parameters, to be passed to simulator.get_model_data (along with fixed_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.ndarray of 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.Pool to use for parallelization. Defaults to None, in which case sampling is not parallelized.

Returns

A tuple of two numpy.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.Pool to use for parallelization. Defaults to None, in which case sampling is not parallelized.

  • init_method

  • backend – The pydemic.hdf.HDFBackend to 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

An emcee.EnsembleSampler.

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.HDFBackend to 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
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.HDFBackend which stores additional information used by LikelihoodEstimator to 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_data method is used for sampling. If the class is defined in pydemic.models, that class will be returned; otherwise the name of the class will be returned.