8. Bioscrape Inference
Bioscrape can be used to run Bayesian parameter inference. To get
started, make sure you have Python emcee package installed in your
environment. emcee is
an implementation of a Markov chain Monte Carlo (MCMC) method that can
be used for parameter estimation.
8.1. Examples
Ready-to-run examples are available in bioscrape to help you learn the various tools available with bioscrape inference package:
Fitting noisy data to a linear Model (y = mx + b) - This is a replica of an example demonstrated in the emcee package documentation - but with bioscrape.
Parameter estimation of a Birth-death process.
Using multiple trajectories, time points, and initial conditions in your experimental data is very easy with bioscrape inference. An example demonstrating this is available in the inference examples directory.
Stochastic inference (to run stochastic simulations for likelihood computation) options are available as well - simply set the
sim_typeto stochastic and that’s it. Check out the related example to learn how to use it.
Tutorial style Jupyter notebooks are available in this folder for all of the above examples.
8.2. Inference Setup
Priors
All prior types can be set to a inference object by using set_prior
function or in your py_inference function call in the prior
keyword argument. The expected signature is as follows (a dictionary
with keys as parameter names and a list that gives the parameters for
different prior types)
list_corresponding_to_prior_type = ['prior-type-key',
prior_parameter1, prior_parameter2,...,'positive',
custom_function]
## Here 'positive' is an optional keyword in the list that can make
## any prior accept only positive parameter values.
## The `custom_function` is an optional keyword, which has to be the
## last element in the list.
## This is used for a custom prior (see the last option in the list
## below)
prior = {'param_name': list_corresponding_to_prior_type}
pid.set_prior(prior)
Uniform: To give a uniform prior to a parameter use, prior-type-key:
"uniform". Theprior_parameter1is the lower bound of the uniform distribution and theprior_parameter2is the upper bound.Example:
{'k1': ['uniform', 0,10]}sets the prior for the parameterk1as uniform(0,10).Gaussian: To give a gaussian prior to a parameter use, prior-type-key:
"gaussian". Theprior_parameter1is the mean of the gaussian distribution and theprior_parameter2is the standard deviation.Example:
{'k1': ['gaussian', 0, 10]}sets the prior for the parameterk1as gaussian(0,10).Example:
{'k1': ['gaussian', 0, 10, 'positive']}sets the prior for the parameterk1as gaussian(0,10) but with only positive values.Exponential: To give a exponential prior to a parameter use, prior-type-key:
"exponential". Theprior_parameter1is the inverse of the mean of exponential distribution.Example:
{'k1': ['exponential', 5]}sets the prior for the parameterk1as exponential(5).Gamma: To give a gamma prior to a parameter use, prior-type-key:
"gamma". Theprior_parameter1is the shape parameter (alpha) and theprior_parameter2is the rate parameter (beta) of the gamma distribution.Example:
{'k1': ['gamma', 2, 3]}sets the prior for the parameterk1as gamma(alpha=2, beta=3).Beta: To give a beta prior to a parameter use, prior-type-key:
"beta". Theprior_parameter1andprior_parameter2are the alpha and beta shape parameters of the beta distribution. The parameter value is expected to lie in[0, 1].Example:
{'k1': ['beta', 2, 3]}sets the prior for the parameterk1as beta(alpha=2, beta=3).Log Uniform: To give a log uniform prior to a parameter use, prior-type-key:
"log-uniform". Theprior_parameter1is the lower bound of the log uniform probability distribution and theprior_parameter2is the upper bound.Example:
{'k1': ['log-uniform', 5, 10]}sets the prior for the parameterk1as log-uniform(5, 10).Log Gaussian: To give a log gaussian prior to a parameter use, prior-type-key:
"log-gaussian". Theprior_parameter1is the mean of the gaussian distribution and theprior_parameter2is the standard deviation.Example:
{'k1': ['log-gaussian', 0, 10]}sets the prior for the parameterk1as log-gaussian(0,10).Custom function:
“custom”type with signature: (param_name (str), param_value(float)). Build your own custom prior function that returns the log-probability given the parameter name and the parameter value as the function arguments.Example:
{'k1': ['custom', custom_prior]}sets the prior for the parameterk1as a custom function which is the callablecustom_prior.
def custom_prior(param_name, param_value):
'''
Returns log-probability
'''
# sanity check
return np.Inf
# calculate probability p
return p
For an example on how to run custom prior distribution, check out the
prior distribution notebook in inference examples directory.
Data Types
Experimental data passed to inference is held in one of the
Data subclasses, each corresponding to a
different kind of measurement:
BulkData: a single output measured for a whole culture over time (e.g. total fluorescence or optical density from a plate reader), where the measured value is effectively summed or averaged over all the cells. This is a bulk time series.FlowData: a distribution of single-cell outputs at one or more time points, with individual cells not tracked from one time point to the next (e.g. flow cytometry or mRNA FISH). This is a population snapshot.StochasticTrajectories: one or more individual cells tracked over time (e.g. time-lapse microscopy). This is a single-cell time series.
When using py_inference, the sim_type
argument selects the data type and likelihood together:
sim_type="deterministic" (the default) uses
BulkData, and sim_type="stochastic" uses
StochasticTrajectories.
FlowData is used by constructing the likelihood
object directly rather than through py_inference.
Likelihood options
Each type of experimental data is paired with a likelihood class that computes the log-likelihood of a parameter set given that data:
DeterministicLikelihood: compares a single deterministic (ODE) simulation against bulk time-series data. Used whensim_typeis"deterministic"(the default).StochasticTrajectoriesLikelihood: runs several stochastic simulations (see theN_simulationsoption) and scores how close the simulated single-cell trajectories are to the measured ones. Used whensim_typeis"stochastic".StochasticTrajectoryMomentLikelihood: a variant of the previous likelihood that compares the moments of the trajectories – their means withMoments=1, or means and second moments withMoments=2– rather than individual trajectories.StochasticStatesLikelihood: compares the distribution of simulated states to a measured distribution at each time point, for population-snapshot data.
MCMC parameter set-up options
nwalkers: The number of Markov chains to initialize, called the number of walkers. Useset_nwalkersfor the inference object.init_seed: Initial seeding for each Markov chain around the initial guesses for parameters. This argument is a float that sets the initial parameter guess to start MCMC as follows:
## p0 is the initial parameter vector set in MCMC algorithm
p0 = np.array(params_values) + init_seed * np.random.randn(
self.nwalkers, ndim)
## params_values are the list of all parameter values and ndim =
## number of parameters to estimate
Use set_init_seed for the inference object.
nsteps: Length of each Markov chain, called the number of walkers. Useset_nstepsfor the inference object.