cstr

ckbit.cstr

ckbit.cstr.MAP(filename, model_name='cstr', pH=False, R_units='kJ/mol/K', priors=None, A0_up_lim=35, Ea_up_lim=350, init_random=False, seed=None, verbose=True, A0_init=10, Ea_init=80, sigma_init=1)

MAP estimation for CSTR parameter estimation

Parameters
  • filename (str) – Filename of Excel input file that contains the appropriately formated CSTR data (see examples)

  • model_name (str, optional) – Name of model, used for saving/loading compilied Stan code, Default is ‘cstr’

  • pH (bool, optional) – Indicates whether pH is in the reaction equations and dataset, Default is false

  • R_units (str, optional) – Ideal gas constant units, Default is ‘kJ/mol/K’, note: if changing these units, the prior for Ea should be updated accordingly as well as Ea_up_lim

  • priors (list of str, optional) – User defined prior distributions, Must have appropriate format (see examples), Default is None

  • A0_up_lim (float, optional) – Upper limit of A0 terms to be estimated, Default is 35

  • Ea_up_lim (str, optional) – Upper limit of Ea terms to be estimated, Default is 350

  • init_random (bool, optional) – Flag to signal whether the initialization should be random or if it should use user specified values, Default is False

  • seed (int, optional) – A positive integer used to seed the random number generation, Default is np.random.randint(0, 1E9)

  • verbose (bool, optional) – Flag to signal whether Stan intermediate output should be piped to terminal, Default is True

  • A0_init (float, optional) – Initialization point for the sampler for A0, Default is 10

  • Ea_init (float, optional) – Initialization point for the sampler for Ea, Default is 80

  • sigma_init (float, optional) – Initialization point for the sampler for sigma, Default is 1

Returns

point_estimates – Dictionary containing values corresponding to modes of posterior

Return type

dict

ckbit.cstr.MCMC(filename, model_name='cstr', pH=False, warmup=None, iters=2000, chains=2, n_jobs=1, verbose=True, R_units='kJ/mol/K', seed=None, control={'adapt_delta': 0.8, 'max_treedepth': 20}, trace=True, A0_up_lim=35, Ea_up_lim=350, priors=None, init_random=False, A0_init=10, Ea_init=80, sigma_init=1)

Bayesian inference using MCMC sampling for CSTR parameter estimation

Parameters
  • filename (str) – Filename of Excel input file that contains the appropriately formated CSTR data (see examples)

  • model_name (str, optional) – Name of model, used for saving/loading compilied Stan code, Default is ‘cstr’

  • pH (bool, optional) – Indicates whether pH is in the reaction equations and dataset, Default is false

  • warmup (int, optional) – Number of warmup iterations for MCMC sampler for each chain, Must be less than the number of total iterations, Default is None, which sets warmup equal to half of iters (the Stan default)

  • iters (int, optional) – Number of total interations for MCMC sampler for each chain, Must be greater than the warmup, total number of samples useable for MCMC inference will equal (chains*(iters-warmup)), Default is 2000

  • chains (int, optional) – Number of chains for MCMC sampler, Default is 2

  • n_jobs (int, optional) – Number of jobs to run in parallel for MCMC sampler, maximum is number of cores the computer has, Default is 1

  • verbose (bool, optional) – Flag to signal whether Stan intermediate output should be piped to terminal, Default is True

  • R_units (str, optional) – Ideal gas constant units, Default is ‘kJ/mol/K’, note: if changing these units, the prior for Ea should be updated accordingly as well as Ea_up_lim

  • seed (int, optional) – A positive integer used to seed the random number generation, use one seed even when multiple chains are used since the other chain’s seeds are generated from the first chain’s to avoid dependency among random number streams, set this seed for repeatable inference sampling runs, Default is np.random.randint(0, 1E9)

  • control (dict, optional) – Dictionary of settings for MCMC sampler, Default is {‘adapt_delta’:0.9999, ‘max_treedepth’:100}, more information at: https://mc-stan.org/docs/2_23/reference-manual/hmc-algorithm-parameters.html

  • trace (bool, optional) – Flag to signal whether traceplots should be generated upon the run’s completion, Default is True

  • A0_up_lim (float, optional) – Upper limit of A0 terms to be estimated, Default is 35

  • Ea_up_lim (str, optional) – Upper limit of Ea terms to be estimated, Default is 350

  • priors (list of str, optional) – User defined prior distributions, Must have appropriate format (see examples) in accordance with Stan, Default is None

  • init_random (bool, optional) – Flag to signal whether the initialization should be random or if it should use user specified values, Default is False

  • A0_init (float, optional) – Initialization point for the sampler for A0, Default is 10

  • Ea_init (float, optional) – Initialization point for the sampler for Ea, Default is 80

  • sigma_init (float, optional) – Initialization point for the sampler for sigma, Default is 1

Returns

  • fit (Stan object) – Object containing results of the MCMC run

  • sample_vals (dict) – Dictionary of values collected by the MCMC sampler

ckbit.cstr.StanModel_cache(model_code, model_name, **kwargs)

Function for saving/loading compiled Stan code to avoid recompilation

Parameters
  • model_code (str) – Stan code written in proper format

  • model_name (str, optional) – Name of model, used for saving/loading compilied Stan code

Returns

sm – Stan object from pystan function StanModel

Return type

Stan model

ckbit.cstr.VI(filename, model_name='cstr', pH=False, R_units='kJ/mol/K', priors=None, A0_up_lim=35, Ea_up_lim=350, iters=2000000, algorithm='fullrank', seed=None, sample_file='./samples.csv', diagnostic_file='./diagnostics.csv', verbose=True, grad_samples=1, elbo_samples=100, tol_rel_obj=0.01, adapt_iter=50, eval_elbo=100, output_samples=10000, eta=0.2, adapt_engaged=False, trace=True, init_random=False, A0_init=10, Ea_init=80, sigma_init=1)

Bayesian inference using VI for CSTR parameter estimation

Parameters
  • filename (str) – Filename that contains the appropriately formated CSTR data (see examples)

  • model_name (str, optional) – Name of model, used for saving/loading compilied Stan code, Default is ‘cstr’

  • pH (bool, optional) – Indicates whether pH is in the reaction equations and dataset, Default is false

  • R_units (str, optional) – Ideal gas constant units, Default is ‘kJ/mol/K’, note: if changing these units, the prior for Ea should be updated accordingly as well as Ea_up_lim

  • priors (list of str, optional) – User defined prior distributions, Must have appropriate format (see examples) in accordance with Stan, Default is None

  • A0_up_lim (float, optional) – Upper limit of A0 terms to be estimated, Default is 35

  • Ea_up_lim (str, optional) – Upper limit of Ea terms to be estimated, Default is 350

  • iters (int, optional) – Maximum number of interations for VI sampler, Default is 2,000,000

  • algorithm (str, optional) – Algorithm to use for VI, either ‘meanfield’ (for uncorrelated posteriors) or ‘fullrank’ (for correlated posteriors), Default is ‘fullrank’

  • seed (int, optional) – A positive integer used to seed the random number generation, Default is np.random.randint(0, 1E9)

  • sample_file (str, optional) – Filename where the VI samples are saved to, Default is ‘./samples. csv’

  • diagnostic_file (str, optional) – Filename where the VI diagonstics are saved to, Default is ‘./ diagnostics.csv’

  • verbose (bool, optional) – Flag to signal whether Stan intermediate output should be piped to terminal, Default is True

  • grad_samples (int, optional) – Number of gradient evaluations to make to estimate gradient for VI solver, Default is 1

  • elbo_samples (int, optional) – Number of elbo evaluations to make to estimate ELBO for VI solver, Default is 100

  • tol_rel_obj (float, optional) – Relative tolerance for VI solver, Default is 0.01

  • adapt_iter (int, optional) – Number of iterations for adaptive tuning of eta, Default is 50

  • eval_elbo (int, optional) – Number of iterations between ELBO evaluations for VI solver, Default is 100

  • output_samples (int, optional) – Number of samples to draw from final approximation of posterior from VI solver, Default is 10,000

  • eta (float, optional) – Positive, stepsize weighing parameter for VI solver, Ignored if adapt_iter is True, Default is 0.2

  • adapt_engaged – Flag to signal whether eta should be automatically tuned, Default is False

  • trace (bool, optional) – Flag to signal whether traceplots should be generated upon the run’s completion, Default is True

  • init_random (bool, optional) – Flag to signal whether the initialization should be random or if it should use user specified values, Default is False

  • A0_init (float, optional) – Initialization point for the sampler for A0, Default is 10

  • Ea_init (float, optional) – Initialization point for the sampler for Ea, Default is 80

  • sigma_init (float, optional) – Initialization point for the sampler for sigma, Default is 1

Returns

  • fit (Stan object) – Stan object containing results of the VI run

  • sample_vals (dict) – Dictionary of values collected by the VI sampler

ckbit.cstr.cstr_exp_data(filename, pH=False)

Processes Excel file with CSTR data that can be used for CSTR estimation

Parameters
  • filename (str) – Filename of Excel input file that contains the appropriately formated CSTR data (see examples)

  • pH (bool, optional) – Indicates whether pH is in the reaction equations and dataset, Default is false

Returns

  • n_runs (int) – Number of experimental runs in dataset

  • rxns (int) – Number of reactions in network

  • S (int) – Number of species in dataset

  • rxn_eqs (str) – Reaction equations

  • c_in (list) – Inlet concentrations, each run is a list within the list

  • cstr_data (dict) – Dictionary containing CSTR data inputs for Stan code

ckbit.cstr.write_cstr_stan_code(runs, rxns, species, rxn_eqs, c_in, pH=False, R=0.0083144598, A0_up_lim=35, Ea_up_lim=350, priors=None)

Writes Stan code that can be used for CSTR estimation

Parameters
  • runs (int) – Number of experimental runs in dataset

  • rxns (int) – Number of reactions in network

  • species (int) – Number of species in dataset

  • rxn_eqs (str) – Reaction equations

  • c_in (list) – Inlet concentrations, each run is a list within the list

  • pH (bool, optional) – Indicates whether pH is in the reaction equations and dataset, Default is false

  • R (float, optional) – Ideal gas constant for user selected units, Default is 0.0083144598 which corresponds to kJ/mol/K, altering this value will necessitate altering Ea_up_lim and the priors for Ea accordingly

  • A0_up_lim (float, optional) – Upper limit of A0 terms to be estimated, Default is 35

  • Ea_up_lim (str, optional) – Upper limit of Ea terms to be estimated, Default is 350

  • priors (list of str, optional) – User defined prior distributions, Must have appropriate format (see examples) in accordance with Stan, Default is None

Returns

code_cstr – Code written in Stan syntax used for CSTR estimation

Return type

str