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))
Attributes:
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()
mcstat.data.add_data_set(x = x1, y = y1)
mcstat.data.add_data_set(x = 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]
Args:
  • 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.

Note

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()[source]

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 = mcstat.data.get_number_of_batches()

should return nb = 2 because len(mcstat.data.ydata) = 2.

Returns:
  • nbatch (int): Number of batches.
get_number_of_data_sets()[source]

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 = mcstat.data.get_number_of_data_sets()

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

Returns:
  • Number of columns in ydata (int)
get_number_of_observations()[source]

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 = mcstat.data.get_number_of_observations()

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

Returns:
  • 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.

Example:

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)
mcstat.parameters.display_model_parameter_settings()

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)
Attributes:
add_model_parameter(name=None, theta0=None, minimum=-inf, maximum=inf, prior_mu=array([0.]), prior_sigma=inf, sample=True, local=0, adapt=True)[source]

Add model parameter to MCMC simulation.

Args:
  • 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

Args:
  • 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

Args:
  • nbatch (int): Number of data batches
  • parameters (list): List of model parameters.
Returns:
  • local (ndarray): Array with local flag indices.
classmethod setup_adaptation_indices(parind, adapt)[source]

Setup adaptation parameter indices.

Args:
  • parind (ndarray): Array of boolean flags from parameter structure.
  • adapt (ndarray): Array of boolean flags from parameter structure.
Returns:
  • 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.

..note:

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.

Args:
  • adapt (bool): Flag from parameter structure.
  • sample (bool): Flag from parameter structure.
Returns:
classmethod setup_prior_mu(mu, value)[source]

Setup prior mean.

Args:
  • mu (float): defined mean
  • value (float): default value
Returns:
  • Prior mean
classmethod setup_prior_sigma(sigma)[source]

Setup prior variance.

Args:
  • sigma (float): defined variance
Returns:
  • Prior mean
pymcmcstat.settings.ModelParameters.check_noadaptind(no_adapt, npar)[source]

Check if noadaptind is None -> Empty List

Args:
  • no_adapt (ndarray): Boolean array of indices not to be adapted.
  • npar (int): Number of parameters.
Returns:
  • no_adapt (ndarray): Boolean array of indices not to be adapted.
pymcmcstat.settings.ModelParameters.check_verbosity(verbosity)[source]

Check if verbosity is None -> 0

Args:
  • verbosity (int): Verbosity level
Returns:
  • verbosity (int): Returns 0 if verbosity was initially None
pymcmcstat.settings.ModelParameters.format_number_to_str(number)[source]

Format number for display

Args:
  • number (float): Number to be formatted
Returns:
  • (str): Formatted string display
pymcmcstat.settings.ModelParameters.generate_default_name(nparam)[source]

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

names = 'p_{3}'
Args:
  • nparam (int): Number of parameter names to generate
Returns:
  • name (str): Name based on size of parameter list
pymcmcstat.settings.ModelParameters.less_than_or_equal_to_zero(x)[source]

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

Args:
  • x (float): Number to be tested
Returns:
  • (bool): Result of test: x<=0
pymcmcstat.settings.ModelParameters.noadapt_display_setting(no_adapt)[source]

Define display settins if index not being adapted.

Args:
  • no_adapt (bool): Flag to determine whether or not it is to be adapted..
Returns:
  • st (str): String to be displayed.
pymcmcstat.settings.ModelParameters.prior_display_setting(x)[source]

Define display string for prior.

Args:
Returns:
  • 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.

Args:
  • x (list): List of numbers to be tested
  • testfunction (testfunction()): Test function
  • value (float): Value to assign if test function return True
Returns:
  • 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

Attributes:
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.

Args:
  • 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().

Note

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

display_model_settings(print_these=None)[source]

Display subset of the simulation options.

Args:
  • 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.

Attributes:
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., 4., 3.]), 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.

Args:
  • 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.

Note

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_simulation_options(print_these=None)[source]

Display subset of the simulation options.

Args:
  • 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.

Args:
  • 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