pymcmcstat.settings package

pymcmcstat.settings.DataStructure module

Created on Wed Jan 17 09:03:37 2018

@author: prmiles

class pymcmcstat.settings.DataStructure.DataStructure[source]

Bases: object

Structure for storing data in MCMC object. The following random data sets will be referenced in examples for the different class methods:

x1 = np.random.random_sample(size = (5, 1))
y1 = np.random.random_sample(size = (5, 2))

x2 = np.random.random_sample(size = (10, 1))
y2 = np.random.random_sample(size = (10, 3))
add_data_set(x, y, n=None, weight=1, user_defined_object=0)[source]

Add data set to MCMC object.

This method must be called first before using any of the other methods within DataStructure.

mcstat = MCMC() = x1, y = y1) = x2, y = y2)

This yields the following variables in the data structure.

  • xdata (list): List of numpy arrays
    • xdata[0] = x1, xdata[0].shape = (5,1)

    • xdata[1] = x2, xdata[1].shape = (10,1)

  • ydata (list): List of numpy arrays
    • ydata[0] = y1, ydata[0].shape = (5,2)

    • ydata[1] = y2, ydata[1].shape = (10,3)

  • n (list): List of integers. n = [5, 10]

  • shape (list): List of y.shape. shape = [(5,2),(10,3)]

  • weight (list): List of weights. weight = [1, 1]

  • user_defined_object (list): List of objects. user_defined_object = [0,0]

  • x (ndarray): Independent data. Recommend input as column vectors.

  • y (ndarray): Dependent data. Recommend input as column vectors.

  • n (list): List of integers denoting number of data points.

  • weight (list): Weight of each data set.

  • user_defined_object (User Defined): Any object can be stored in this variable.


In general, it is recommended that user’s format their data as a column vector. So, if you have nds independent data points, x and y should be [nds,1] or [nds,] numpy arrays. Note if a list is sent, the code will convert it to a numpy array.


Get number of batches in data structure. Essentially, each time you call the add_data_set() method you are adding another batch. It is also equivalent to say the number of batches is equal to the length of the list ydata. For example,

nb =

should return nb = 2 because len( = 2.

  • nbatch (int): Number of batches.


Get number of data sets in data structure. A data set is strictly speaking defined as the total number of columns in each element of the ydata list. For example,

nds =

should return nds = 2 + 3 = 5 because the number of columns in y1 is 2 and the number of columns in y2 is 3.

  • Number of columns in ydata (int)


Get number of observations in data structure. An observation is essentially the total number of rows from each element of the ydata list. For example,

nobs =

should return nobs = 5 + 10 = 15 because the number of rows in y1 is 5 and the number of rows in y2 is 10.

  • Number of rows in ydata (ndarray)

pymcmcstat.settings.ModelParameters module

Created on Wed Jan 17 09:13:03 2018

@author: prmiles

class pymcmcstat.settings.ModelParameters.ModelParameters[source]

Bases: object

MCMC Model Parameters.


mcstat = MCMC()

mcstat.parameters.add_model_parameter(name = 'm', theta0 = 1., minimum = -10, maximum = 10)
mcstat.parameters.add_model_parameter(name = 'b', theta0 = -5., minimum = -10, maximum = 100)

This will display to screen:

Sampling these parameters:
name         start [   min,    max] N(  mu, sigma^2)
m         :   1.00 [-10.00,  10.00] N(0.00, inf)
b         :  -5.00 [-10.00, 100.00] N(0.00, inf)
add_model_parameter(name=None, theta0=None, minimum=- inf, maximum=inf, prior_mu=array([0.0]), prior_sigma=inf, sample=True, local=0, adapt=True)[source]

Add model parameter to MCMC simulation.

  • name (str): Parameter name

  • theta0 (float): Initial value

  • minimum (float): Lower parameter bound

  • maximum (float): Upper parameter bound

  • prior_mu (float): Mean value of prior distribution

  • prior_sigma (float): Standard deviation of prior distribution

  • sample (bool): Flag to turn sampling on (True) or off (False)

  • local (int): Local flag - still testing.

The default prior is a uniform distribution from minimum to maximum parameter value.

display_parameter_settings(verbosity=None, no_adapt=None)[source]

Display parameter settings

  • verbosity (int): Verbosity of display output. 0

  • no_adapt (ndarray): Boolean array of indices not to be adapted.

classmethod scan_for_local_variables(nbatch, parameters)[source]

Scan for local variables

  • nbatch (int): Number of data batches

  • parameters (list): List of model parameters.

  • local (ndarray): Array with local flag indices.

classmethod setup_adaptation_indices(parind, adapt)[source]

Setup adaptation parameter indices.

  • parind (ndarray): Array of boolean flags from parameter structure.

  • adapt (ndarray): Array of boolean flags from parameter structure.

  • parind (ndarray): Array of indices corresponding to sampling parameters.

  • adapt (ndarray): Array of indices corresponding to adaptating parameters.

  • no_adapt (ndarray): Boolean array of indices not being adapted.


The size of the returned arrays will equal the number of parameters being sampled.
classmethod setup_adapting(adapt, sample)[source]

Setup parameters being adapted.

All parameters that are not being sampled will automatically be thrown out of adaptation. This method checks that the default adaptation status is consistent.

  • adapt (bool): Flag from parameter structure.

  • sample (bool): Flag from parameter structure.

classmethod setup_prior_mu(mu, value)[source]

Setup prior mean.

  • mu (float): defined mean

  • value (float): default value

  • Prior mean

classmethod setup_prior_sigma(sigma)[source]

Setup prior variance.

  • sigma (float): defined variance

  • Prior mean

pymcmcstat.settings.ModelParameters.check_noadaptind(no_adapt, npar)[source]

Check if noadaptind is None -> Empty List

  • no_adapt (ndarray): Boolean array of indices not to be adapted.

  • npar (int): Number of parameters.

  • no_adapt (ndarray): Boolean array of indices not to be adapted.


Check if verbosity is None -> 0

  • verbosity (int): Verbosity level

  • verbosity (int): Returns 0 if verbosity was initially None


Generate generic parameter name. For example, if nparam = 4, then the generated name is:

names = 'p_{3}'
  • nparam (int): Number of parameter names to generate

  • name (str): Name based on size of parameter list


Return result of test on number based on less than or equal to

  • x (float): Number to be tested

  • (bool): Result of test: x<=0


Define display settins if index not being adapted.

  • no_adapt (bool): Flag to determine whether or not it is to be adapted..

  • st (str): String to be displayed.


Define display string for prior.

  • h2 (str): String to be displayed, depending on if x is infinity.

pymcmcstat.settings.ModelParameters.replace_list_elements(x, testfunction, value)[source]

Replace list elements based on results from testfunction.

  • x (list): List of numbers to be tested

  • testfunction (testfunction()): Test function

  • value (float): Value to assign if test function return True

  • x (list): Updated list

pymcmcstat.settings.ModelSettings module

Created on Wed Jan 17 09:06:51 2018

@author: prmiles

class pymcmcstat.settings.ModelSettings.ModelSettings[source]

Bases: object

MCMC Model Settings

define_model_settings(sos_function=None, prior_function=None, prior_type=1, prior_update_function=None, prior_pars=None, model_function=None, sigma2=None, N=None, S20=nan, N0=None, nbatch=None)[source]

Define model settings.

  • sos_function: Handle for sum-of-squares function

  • prior_function: Handle for prior function

  • prior_type: Pending…

  • prior_update_function: Pending…

  • prior_pars: Pending…

  • model_function: Handle for model function (needed if sos_function not specified)

  • sigma2 (float): List of initial error observations.

  • N (int): Number of observations - see DataStructure.

  • S20 (float): List of scaling parameter in observation error estimate.

  • N0 (float): List of scaling parameter in observation error estimate.

  • nbatch (int): Number of batch data sets - see get_number_of_batches().


Variables sigma2, N, S20, N0, and nbatch converted to ndarray for subsequent processing.


Display subset of the simulation options.

  • print_these (list): List of strings corresponding to keywords. Default below.

print_these = ['sos_function', 'model_function', 'sigma2', 'N', 'N0', 'S20', 'nsos', 'nbatch']

pymcmcstat.settings.SimulationOptions module

Created on Wed Jan 17 09:08:13 2018

@author: prmiles

class pymcmcstat.settings.SimulationOptions.SimulationOptions[source]

Bases: object

MCMC simulation options.

define_simulation_options(nsimu=10000, adaptint=None, ntry=None, method='dram', printint=None, adaptend=0, lastadapt=0, burnintime=0, waitbar=True, debug=0, qcov=None, updatesigma=False, stats=0, drscale=array([5.0, 4.0, 3.0]), adascale=None, savesize=0, maxmem=0, chainfile='chainfile', s2chainfile='s2chainfile', sschainfile='sschainfile', covchainfile='covchainfile', savedir=None, save_to_bin=False, skip=1, label=None, RDR=None, verbosity=1, maxiter=None, priorupdatestart=0, qcov_adjust=1e-08, burnin_scale=10, alphatarget=0.234, etaparam=0.7, initqcovn=None, doram=None, rndseq=None, results_filename=None, save_to_json=False, save_to_txt=False, json_restart_file=None, save_lightly=False)[source]

Define simulation options.

  • nsimu (int): Number of parameter samples to simulate. Default is 1e4.

  • adaptint (int): Number of interates between adaptation. Default is method dependent.

  • ntry (int): Number of tries to take before rejection. Default is method dependent.

  • method (str): Sampling method ('mh', 'am', 'dr', 'dram'). Default is 'dram'.

  • printint (int): Printing interval.

  • adaptend (int): Obsolete.

  • lastadapt (int): Last adaptation iteration (i.e., no more adaptation beyond this point).

  • burnintime (int):

  • waitbar (int): Flag to use progress bar. Default is 1 -> on (otherwise -> off).

  • debug (int): Flag to perform debug. Default is 0 -> off.

  • qcov (ndarray): Proposal parameter covariance matrix.

  • updatesigma (bool): Flag for updating measurement error variance. Default is 0 -> off (1 -> on).

  • stats (int): Calculate convergence statistics. Default is 0 -> off (1 -> on).

  • drscale (ndarray): Reduced scale for sampling in DR algorithm. Default is [5,4,3].

  • adascale (float): User defined covariance scale. Default is method dependent (untested).

  • savesize (int): Size of chain segments when saving to log files. Default is 0.

  • maxmem (int): Maximum memory available in mega bytes (Obsolete).

  • chainfile (str): File name for chain log file.

  • sschainfile (str): File name for sschain log file.

  • s2chainfile (str): File name for s2chain log file.

  • covchainfile (str): File name for qcov log file.

  • savedir (str): Output directory of log files. Default is current directory.

  • save_to_bin (bool): Save log files to binary. Default is False.

  • save_to_txt (bool): Save log files to text. Default is False.

  • skip (int):

  • label (str):

  • RDR (ndarray): R matrix for each stage of DR.

  • verbosity (int): Verbosity of display output.

  • maxiter (int): Obsolete.

  • priorupdatestart

  • qcov_adjust (float): Adjustment scale for covariance matrix.

  • burnin_scale (float): Scale for burnin.

  • alphatarget (float): Acceptance ratio target.

  • etaparam (float):

  • initqcovn (float): Proposal covariance weight in update.

  • doram (int): Flag to perform 'ram' algorithm (Obsolete).

  • rndseq (ndarray): Random number sequence (Obsolete).

  • results_filename (str): Output file name when saving results structure with json.

  • save_to_json (bool): Save results structure to json file. Default is False.

  • json_restart_file (str): Extract parameter covariance and last sample value from saved json file.

  • save_lightly (bool): Flag to indicate whether results json file should include chains.


For the log file names chainfile, sschainfile, s2chainfile and covchainfile do not include the extension. By specifying whether to save to text or to binary, the appropriate extension will be added.


Display subset of the simulation options.

  • print_these (list): List of strings corresponding to keywords. Default below.

print_these = ['nsimu', 'adaptint', 'ntry', 'method', 'printint', 'lastadapt', 'drscale', 'qcov']
pymcmcstat.settings.SimulationOptions.check_lightly_save(save_lightly, save_to_json, save_to_bin, save_to_txt)[source]

Check settings for lightly save

If saving to json, chains will be removed if already being stored via one of the logging methods, binary or text. If logging methods not being used, then chains will be included in json file.

  • save_lightly (bool): Flag to save results w/out arrays

  • save_to_json (bool): Flag to save to json

  • save_to_bin (bool): Flag to save to binary

  • save_to_txt (bool): Flag to save to text