Package trunk :: Package BIP :: Package Bayes :: Module Melding :: Class Meld
[hide private]

Class Meld

source code

object --+
         |
        Meld

Bayesian Melding class
Instance Methods [hide private]
 
__init__(self, K, L, model, ntheta, nphi, alpha=0.5, verbose=0, viz=False)
Initializes the Melding class.
source code
 
current_plot(self, series, data, idx, vars=[], step=0)
Plots the last simulated series
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)], lims=[(0,1)])
Setup the models inputs and generate the samples from prior distributions needed for the dists the melding replicates.
source code
 
add_salt(self, dataset, band)
Adds a few extra uniformly distributed data points beyond the dataset range. This is done by adding from a uniform dist.
source code
 
setThetaFromData(self, names, data, limits)
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.
source code
 
setPhiFromData(self, names, data, limits)
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.
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. the model itself. The model is run self.K times to obtain phi = M(theta).
source code
 
getPosteriors(self, t)
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.
source code
 
filtM(self, cond, x, limits)
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].
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, pool=False, savetemp=False)
Runs the model for inference through Approximate Bayes Computation techniques. This method should be used as an alternative to the sir.
source code
 
imp_sample(self, n, data, w)
Importance sampling
source code
 
mcmc_run(self, data, t=1, likvariance=10, burnin=1000, nopool=False, method="MH", constraints=[])
MCMC based fitting
source code
 
_output_loglike(self, prop, data, likfun=like.Normal, likvar=1e-1, po=None)
Returns the log-likelihood of a simulated series
source code
 
sir(self, data={}, t=1, variance=0.1, pool=False, savetemp=False)
Run the model output through the Sampling-Importance-Resampling algorithm. Returns 1 if successful or 0 if not.
source code
 
runModel(self, savetemp, t=1, k=None)
Handles running the model k times keeping a temporary savefile for resuming calculation in case of interruption.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, K, L, model, ntheta, nphi, alpha=0.5, verbose=0, 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 - : 0,1, 2: whether to show more information about the computations
  • viz - : Boolean. Wether to show graphical outputs of the fitting process
Overrides: object.__init__

current_plot(self, series, data, idx, vars=[], step=0)

source code 

Plots the last simulated series

Parameters:
  • series - : Record array with the simulated series.
  • idx - : Integer index of the curve to plot .
  • data - : Dictionary with the full dataset.
  • vars - : List with variable names to be plotted.
  • step - : Step of the chain

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)], lims=[(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

add_salt(self, dataset, band)

source code 
Adds a few extra uniformly distributed data points beyond the dataset range. This is done by adding from a uniform dist.
Parameters:
  • dataset - : vector of data
  • band - : Fraction of range to extend [0,1[
Returns:
Salted dataset.

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.

getPosteriors(self, t)

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.

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, pool=False, 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.
  • pool - : if True, Pools the user provided priors on the model's outputs, with the model induced priors.
  • savetemp - : Should temp results be saved. Useful for long runs. Alows for resuming the simulation from last sa

imp_sample(self, n, data, w)

source code 
Importance sampling
Returns:
returns a sample of size n

mcmc_run(self, data, t=1, likvariance=10, burnin=1000, nopool=False, method="MH", constraints=[])

source code 
MCMC based fitting
Parameters:
  • data - : observed time series on the model's output
  • t - : length of the observed time series
  • likvariance - : variance of the Normal likelihood function
  • nopool - : True if no priors on the outputs are available. Leads to faster calculations
  • method - : Step method. defaults to Metropolis hastings

_output_loglike(self, prop, data, likfun=like.Normal, likvar=1e-1, po=None)

source code 
Returns the log-likelihood of a simulated series
Parameters:
  • prop (: array of shape (t,nphi) with series as columns.) - : Proposed output
  • data (: Dictionary with keys being the names (as in phinames) of observed variables) - : Data against which proposal will be measured
  • likfun (: Log likelihood function object) - : Likelihood function
  • likvar - : Variance of the likelihood function
  • po - : Pool of processes for parallel execution

sir(self, data={}, t=1, variance=0.1, pool=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
  • variance - : variance of the Normal likelihood function
  • pool - : False if no priors on the outputs are available. Leads to faster calculations
  • savetemp - : Boolean. create a temp file?

runModel(self, savetemp, t=1, k=None)

source code 
Handles running the model k times keeping a temporary savefile for resuming calculation in case of interruption.
Parameters:
  • savetemp - : Boolean. create a temp file?
  • t - : number of time steps
Returns:
  • self.phi: a record array of shape (k,t) with the results.