Package BIP :: Package Bayes :: Module Melding :: Class Meld
[hide private]
[frames] | no frames]

Class Meld

source code

Bayesian Melding class
Instance Methods [hide private]
 
__init__(self, K, L, model, ntheta, nphi, alpha=0.5, verbose=False, viz=False)
Initializes the Melding class.
source code
 
setPhi(self, names, dists=[stats.norm], pars=[(0,1)], limits=[(-5,5)])
Setup the models Outputs, or Phi, and generate the samples from prior distributions needed for the melding replicates.
source code
 
setTheta(self, names, dists=[stats.norm], pars=[(0,1)])
Setup the models inputs and generate the samples from prior distributions needed for the dists the melding replicates.
source code
 
setThetaFromData(self, names, data, limits)
Setup the model inputs and set the prior distributions from the vectors in data.
source code
 
setPhiFromData(self, names, data, limits)
Setup the model outputs and set their prior distributions from the vectors in data.
source code
 
addData(self, data, model, limits, l=1024, **kwargs)
Calculates the likelihood functions of the dataset presented and add to self.likelist Likelihood function is a vector of lenght l
source code
 
run(self, *args)
Runs the model through the Melding inference.model model is a callable which return the output of the deterministic model, i.e.
source code
 
getPosteriors(self, t=1)
Updates the posteriors of the model's output for the last t time steps.
source code
 
filtM(self, cond, x, limits)
Multiple condition filtering.
source code
 
basicfit(self, s1, s2)
Calculates a basic fitness calculation between a model- generated time series and a observed time series.
source code
 
logPooling(self, phi)
Returns the probability associated with each phi[i] on the pooled pdf of phi and q2phi.
source code
 
abcRun(self, fitfun=None, data={}, t=1, savetemp=False)
Runs the model for inference through Approximate Bayes Computation techniques.
source code
 
sir(self, data={}, t=1, tau=0.1, nopool=False, savetemp=False)
Run the model output through the Sampling-Importance-Resampling algorithm.
source code
 
runModel(self, savetemp, t=1)
Handles running the model self.K times keeping a temporary savefile for resuming calculation in case of interruption.
source code
Method Details [hide private]

__init__(self, K, L, model, ntheta, nphi, alpha=0.5, verbose=False, viz=False)
(Constructor)

source code 
Initializes the Melding class.
Parameters:
  • K - Number of replicates of the model run. Also determines the prior sample size.
  • L - Number of samples from the Posterior distributions. Usually 10% of K.
  • model - Callable taking theta as argument and returning phi = M(theta).
  • ntheta - Number of inputs to the model (parameters).
  • nphi - Number of outputs of the model (State-variables)
  • verbose - Boolean: whether to show more information about the computations
  • viz - Boolean. Wether to show graphical outputs of the fitting process

setPhi(self, names, dists=[stats.norm], pars=[(0,1)], limits=[(-5,5)])

source code 
Setup the models Outputs, or Phi, and generate the samples from prior distributions needed for the melding replicates.
Parameters:
  • names - list of string with the names of the variables.
  • dists - is a list of RNG from scipy.stats
  • pars - is a list of tuples of variables for each prior distribution, respectively.
  • limits - lower and upper limits on the support of variables.

setTheta(self, names, dists=[stats.norm], pars=[(0,1)])

source code 
Setup the models inputs and generate the samples from prior distributions needed for the dists the melding replicates.
Parameters:
  • names - list of string with the names of the parameters.
  • dists - is a list of RNG from scipy.stats
  • pars - is a list of tuples of parameters for each prior distribution, respectivelydists

setThetaFromData(self, names, data, limits)

source code 
Setup the model inputs and set the prior distributions from the vectors in data. This method is to be used when the prior distributions are available in the form of a sample from an empirical distribution such as a bayesian posterior. In order to expand the samples provided, K samples are generated from a kernel density estimate of the original sample.
Parameters:
  • names - list of string with the names of the parameters.
  • data - list of vectors. Samples of a proposed distribution
  • limits - List of (min,max) tuples for each theta to make sure samples are not generated outside these limits.

setPhiFromData(self, names, data, limits)

source code 
Setup the model outputs and set their prior distributions from the vectors in data. This method is to be used when the prior distributions are available in the form of a sample from an empirical distribution such as a bayesian posterior. In order to expand the samples provided, K samples are generated from a kernel density estimate of the original sample.
Parameters:
  • names - list of string with the names of the variables.
  • data - list of vectors. Samples of the proposed distribution.
  • limits - list of tuples (ll,ul),lower and upper limits on the support of variables.

addData(self, data, model, limits, l=1024, **kwargs)

source code 
Calculates the likelihood functions of the dataset presented and add to self.likelist Likelihood function is a vector of lenght l
Parameters:
  • data - vector containing observations on a given variable.
  • model - string with the name of the distribution of the variable
  • limits - (ll,ul) tuple with lower and upper limits for the variable
  • l - Length (resolution) of the likelihood vector

run(self, *args)

source code 
Runs the model through the Melding inference.model model is a callable which return the output of the deterministic model, i.e. the model itself. The model is run self.K times to obtain phi = M(theta).

getPosteriors(self, t=1)

source code 
Updates the posteriors of the model's output for the last t time steps. Returns two record arrays: - The posteriors of the Theta - the posterior of Phi last t values of time-series. self.L by t arrays.
Parameters:
  • t - length of the posterior time-series to return.

filtM(self, cond, x, limits)

source code 
Multiple condition filtering. Remove values in x[i], if corresponding values in cond[i] are less than limits[i][0] or greater than limits[i][1].
Parameters:
  • cond - is an array of conditions.
  • limits - is a list of tuples (ll,ul) with length equal to number of lines in cond and x.
  • x - array to be filtered.

basicfit(self, s1, s2)

source code 
Calculates a basic fitness calculation between a model- generated time series and a observed time series. it uses a normalized RMS variation.
Parameters:
  • s1 - model-generated time series. record array.
  • s2 - observed time series. dictionary with keys matching names of s1

logPooling(self, phi)

source code 
Returns the probability associated with each phi[i] on the pooled pdf of phi and q2phi.
Parameters:
  • phi - prior of Phi induced by the model and q1theta.

abcRun(self, fitfun=None, data={}, t=1, savetemp=False)

source code 
Runs the model for inference through Approximate Bayes Computation techniques. This method should be used as an alternative to the sir.
Parameters:
  • fitfun - Callable which will return the goodness of fit of the model to data as a number between 0-1, with 1 meaning perfect fit
  • t - number of time steps to retain at the end of the of the model run for fitting purposes.
  • data - dict containing observed time series (lists of length t) of the state variables. This dict must have as many items the number of state variables, with labels matching variables names. Unorbserved variables must have an empty list as value.
  • savetemp - Should temp results be saved. Useful for long runs. Alows for resuming the simulation from last sa

sir(self, data={}, t=1, tau=0.1, nopool=False, savetemp=False)

source code 
Run the model output through the Sampling-Importance-Resampling algorithm. Returns 1 if successful or 0 if not.
Parameters:
  • data - observed time series on the model's output
  • t - length of the observed time series
  • tau - Precision of the Normal likelihood function
  • nopool - True if no priors on the outputs are available. Leads to faster calculations
  • savetemp - Boolean. create a temp file?

runModel(self, savetemp, t=1)

source code 
Handles running the model self.K times keeping a temporary savefile for resuming calculation in case of interruption.
Parameters:
  • savetemp - Boolean. create a temp file?