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

Class FitModel

source code

object --+
         |
        FitModel

Fit a model to data generating Bayesian posterior distributions of input and outputs of the model.
Instance Methods [hide private]
 
__init__(self, K, model, inits, tf, thetanames, phinames, wl=None, nw=1, verbose=False, burnin=1000, constraints=[])
Initialize the model fitter.
source code
 
_plot_MAP(self, data, pmap)
Generates a plot of a full run of the model parameterized with the maximum a posteriori estimates of the parameters.
source code
 
AIC_from_RSS(self)
Calculates the Akaike information criterion from the residual sum of squares of the best fitting run.
source code
 
optimize(self, data, p0, optimizer='scipy', tol=0.0001, verbose=0, plot=0)
Finds best parameters using an optimization approach
source code
 
_rms_error(self, s1, s2)
Calculates a the error between a model- generated time series and a observed time series. It uses a normalized RMS deviation.
source code
 
set_priors(self, tdists, tpars, tlims, pdists, ppars, plims)
Set the prior distributions for Phi and Theta
source code
 
prior_sample(self)
Generates a set of sample from the starting theta prior distributions for reporting purposes.
source code
 
_init_priors(self, prior=None)
Initialize priors either from distributions or previous posteriors
source code
 
do_inference(self, prior, data, predlen, method, likvar) source code
 
_save_to_db(self, dbname, data)
Saves data to a sqlite3 db.
source code
 
run(self, data, method, likvar, pool=False, adjinits=True, ew=0, dbname='results', monitor=False, initheta=[])
Fit the model against data
source code
 
_format_db_tables(self, dbname, w, data, pt, series, predseries, AIC, BIC, DIC)
Formats results for writing to database
source code
 
_monitor_setup(self)
Sets up realtime plotting for inference
source code
 
_get95_bands(self, series, vname)
Returns 95% bands for series of all variables in vname
source code
 
_long_term_prediction_plot(self, cpars, vind, w)
Plots the simulated trajectory predicted from best fit parameters.
source code
 
_monitor_plot(self, series, prior, d2, w, data, vars)
Plots real time data
source code
 
plot_results(self, names=[], dbname="results", savefigs=0)
Plot the final results of the inference
source code
 
_read_results(self, nam)
read results from disk
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, model, inits, tf, thetanames, phinames, wl=None, nw=1, verbose=False, burnin=1000, constraints=[])
(Constructor)

source code 
Initialize the model fitter.
Parameters:
  • K - : Number of samples from the priors. On MCMC also the number of samples of the posterior.
  • model - : Callable (function) returning the output of the model, from a set of parameter values received as argument.
  • inits - : inits initial values for the model's variables.
  • tf - : Length of the simulation, in units of time.
  • phinames - : List of names (strings) with names of the model's variables
  • thetanames - : List of names (strings) with names of parameters included on the inference.
  • wl - : window lenght length of the inference window.
  • nw - : Number of windows to analyze on iterative inference mode
  • verbose - : Verbosity level: 0, 1 or 2.
  • burnin - : number of burnin samples, used in the case on mcmc method.
Overrides: object.__init__

_plot_MAP(self, data, pmap)

source code 
Generates a plot of a full run of the model parameterized with the maximum a posteriori estimates of the parameters.
Parameters:
  • data - : data dictionary as passed to optimize
  • pmap - : MAP parameter values

optimize(self, data, p0, optimizer='scipy', tol=0.0001, verbose=0, plot=0)

source code 
Finds best parameters using an optimization approach
Parameters:
  • data - : Dictionary of observed series
  • p0 - : Sequence (list or tuple) of initial values for the parameters
  • optimizer - : Optimization library to use: 'scipy': fmin (Nelder-Mead) or 'oo':OpenOpt.NLP
  • tol - : Tolerance of the error
  • verbose - : If true show stats of the optimization run at the end
  • plot - : If true plots a run based on the optimized parameters.

_rms_error(self, s1, s2)

source code 

Calculates a the error between a model- generated time series and a observed time series. It uses a normalized RMS deviation.

s1 and s2 can also be both lists of lists or lists of arrays of the same length.

Parameters:
  • s1 (: Record array or list.) - : model-generated time series.
  • s2 (: Dictionary or list; must be a dictionary if s1 is a RA) - : observed time series. dictionary with keys matching names of s1
Returns:
The Root mean square deviation between s1 and s2.

set_priors(self, tdists, tpars, tlims, pdists, ppars, plims)

source code 
Set the prior distributions for Phi and Theta
Parameters:
  • pdists - : distributions for the output variables. For example: [scipy.stats.uniform,scipy.stats.norm]
  • ppars - : paramenters for the distributions in pdists. For example: [(0,1),(0,1)]
  • plims - : Limits of the range of each phi. List of (min,max) tuples.
  • tdists - : same as pdists, but for input parameters (Theta).
  • tpars - : same as ppars, but for tdists.
  • tlims - : Limits of the range of each theta. List of (min,max) tuples.

prior_sample(self)

source code 
Generates a set of sample from the starting theta prior distributions for reporting purposes.
Returns:
Dictionary with (name,sample) pairs

_save_to_db(self, dbname, data)

source code 
Saves data to a sqlite3 db.
Parameters:
  • dbname - : name of the database file
  • data - : Data dictionary as created by format_db_tables method.

run(self, data, method, likvar, pool=False, adjinits=True, ew=0, dbname='results', monitor=False, initheta=[])

source code 
Fit the model against data
Parameters:
  • data - : dictionary with variable names and observed series, as Key and value respectively.
  • method - : Inference method: "ABC", "SIR", "MCMC" or "DREAM"
  • likvar - : Variance of the likelihood function in the SIR and MCMC method
  • pool - : Pool priors on model's outputs.
  • adjinits - : whether to adjust inits to data
  • ew - : Whether to use expanding windows instead of moving ones.
  • dbname - : name of the sqlite3 database
  • monitor - : Whether to monitor certains variables during the inference. If not False, should be a list of valid phi variable names.
  • initheta - : starting position in parameter space for the sampling to start. (only used by MCMC and DREAM)

_get95_bands(self, series, vname)

source code 
Returns 95% bands for series of all variables in vname
Parameters:
  • series - : record array containing the series
  • vname - : list of strings of variables in series for which we want to get the bands

_long_term_prediction_plot(self, cpars, vind, w)

source code 
Plots the simulated trajectory predicted from best fit parameters.
Parameters:
  • cpars - : best fit parameter set
  • vind - : List with indices(in self.phinames) to variables to be plotted
  • w - : current window number.

_monitor_plot(self, series, prior, d2, w, data, vars)

source code 
Plots real time data
Parameters:
  • series - : Record array with the simulated series.
  • prior - : Dctionary with the prior sample of Theta
  • d2 - : Dictionary with data for the current fitting window.
  • w - : Integer id of the current fitting window.
  • data - : Dictionary with the full dataset.
  • vars - : List with variable names to be plotted.