|
SedSat3 1.1.6
Sediment Source Apportionment Tool - Advanced statistical methods for environmental pollution research
|
Markov Chain Monte Carlo sampler for Bayesian parameter estimation. More...
#include <MCMC/MCMC.h>


Public Member Functions | |
| CMCMC (void) | |
| Default constructor. | |
| CMCMC (int nn, int nn_chains) | |
| Constructor with chain configuration. | |
| CMCMC (T *system) | |
| Constructor with model pointer. | |
| bool | SetProperty (const string &varname, const string &value) |
| Set MCMC properties from string key-value pairs. | |
| ~CMCMC (void) | |
| Destructor. | |
| void | initialize (CMBTimeSeriesSet *results, bool random=false) |
| Initialize MCMC chains with starting parameter values. | |
| void | initialize (vector< double > par) |
| Initialize all chains with specific parameter values. | |
| bool | step (int k, int chain_counter) |
| Perform single MCMC step for one chain. | |
| bool | step (int k, int nsamps, string filename, CMBTimeSeriesSet *results=nullptr, ProgressWindow *_rtw=0) |
| Perform multiple MCMC steps with progress tracking and output. | |
| vector< double > | purturb (int k) |
| Generate proposed parameter values by perturbing current state. | |
| void | writeoutput (string filename) |
| Write MCMC results to output file. | |
| double | posterior (vector< double > par, int chain_counter) |
| Calculate log-posterior probability for given parameters. | |
| void | model (T *Model1, vector< double > par) |
| Evaluate model with given parameters. | |
| int | getparamno (int i, int ts) const |
| Get parameter index for a specific variable and time series. | |
| int | get_act_paramno (int i) |
| Get active parameter number (legacy, may be unused) | |
| int | get_time_series (int i) |
| Get time series index for parameter (legacy, may be unused) | |
| Parameter * | parameter (int i) |
| Get pointer to specific parameter. | |
| Observation * | observation (int i) |
| Get pointer to specific observation. | |
| CVector | sensitivity (double d, vector< double > par) |
| Calculate sensitivity of model output to parameters. | |
| CVector | sensitivity_ln (double d, vector< double > par) |
| Calculate log-space sensitivity (for lognormal parameters) | |
| CMatrix | sensitivity_mat_lumped (double d, vector< double > par) |
| Calculate lumped sensitivity matrix. | |
| TimeSeriesSet< double > | prior_distribution (int n_bins) |
| Generate histogram of prior distributions. | |
| int | readfromfile (string filename) |
| Read MCMC state from file to continue previous run. | |
| TimeSeriesSet< double > | model (vector< double > par) |
| Evaluate model for given parameters and return predictions. | |
| void | ProduceRealizations (TimeSeriesSet< double > &MCMCout) |
| Generate posterior predictive realizations. | |
| void | get_outputpercentiles (TimeSeriesSet< double > &MCMCout) |
| Calculate percentiles of model outputs from MCMC samples. | |
| void | SetRunTimeWindow (ProgressWindow *_rtw) |
| Set progress window for GUI updates. | |
| void | Perform () |
| Main entry point to run complete MCMC analysis. | |
Public Attributes | |
| T * | Model |
| Pointer to the model object being calibrated. | |
| T | Model_out |
| Output model state after MCMC completion. | |
| _MCMC_settings | MCMC_Settings |
| MCMC algorithm configuration settings. | |
| vector< vector< double > > | Params |
| Parameter values for all samples and chains. | |
| vector< double > | pertcoeff |
| Perturbation coefficients for each parameter. | |
| vector< double > | logp |
| Log-posterior values for current state of each chain. | |
| vector< double > | logp1 |
| Log-posterior values for proposed states. | |
| vector< double > | u |
| Uniform random values for Metropolis-Hastings accept/reject. | |
| vector< CVector > | temp_predicted |
| Temporary storage for predicted model outputs. | |
| CMBTimeSeriesSet | predicted |
| Model predictions as time series set. | |
| vector< T > | CopiedModels |
| Copies of model object for parallel chain evaluation. | |
| vector< int > | params |
| Parameter indices for model evaluation. | |
| TimeSeriesSet< double > | MData |
| Measured data (observations) for likelihood calculation. | |
| _MCMC_file_names | FileInformation |
| File paths for MCMC input/output. | |
| vector< bool > | apply_to_all |
| Flags indicating if parameter applies to all entities. | |
| vector< Parameter > * | parameters = nullptr |
| Pointer to vector of Parameter objects. | |
| vector< Observation > * | observations = nullptr |
| Pointer to vector of Observation objects. | |
| ProgressWindow * | rtw = nullptr |
| Pointer to progress window for GUI updates. | |
| vector< vector< TimeSeriesSet< double > > > | BTCout_obs |
| Posterior predictive samples for observations. | |
| vector< vector< TimeSeriesSet< double > > > | BTCout_obs_noise |
| Posterior predictive samples with simulated measurement noise. | |
| vector< vector< TimeSeriesSet< double > > > | BTCout_obs_prcntle |
| Percentiles of posterior predictive distribution. | |
| vector< vector< TimeSeriesSet< double > > > | BTCout_obs_prcntle_noise |
| Percentiles of posterior predictive distribution with noise. | |
| vector< CMatrix > | global_sens_lumped |
| Global sensitivity analysis results (lumped) | |
| TimeSeriesSet< double > | paramsList |
| List of all parameter samples from MCMC. | |
| TimeSeriesSet< double > | realized_paramsList |
| Parameter samples for posterior predictive realizations. | |
| vector< double > | calc_output_percentiles |
| Percentiles to calculate for output distributions. | |
| double | accepted_count = 0 |
| Count of accepted proposals. | |
| double | total_count = 0 |
| Count of total proposals (accepted + rejected) | |
| string | last_error |
| Last error message from MCMC execution. | |
Private Member Functions | |
| int_value_pair | Min (const vector< double > &vec, int current_counter, int n_chains) |
| Find minimum value and its chain index. | |
| int_value_pair | Max (const vector< double > &vec, int current_counter, int n_chains) |
| Find maximum value and its chain index. | |
Markov Chain Monte Carlo sampler for Bayesian parameter estimation.
The CMCMC class implements a sophisticated Metropolis-Hastings MCMC algorithm with advanced features for Bayesian inference in sediment source fingerprinting.
MCMC generates samples from the posterior distribution p(θ|data) ∝ p(data|θ) × p(θ) using a random walk with accept/reject steps:
MCMC provides fully Bayesian estimates of source contributions with uncertainty:
Log-posterior calculation:
Where:
| T | Model class type (e.g., SourceSinkData) that provides:
|
Example usage:
Constructor with chain configuration.
| nn | Number of parameters |
| nn_chains | Number of parallel chains |
Initializes MCMC with specified dimensions and chain count.
| int CMCMC< T >::get_act_paramno | ( | int | i | ) |
Get active parameter number (legacy, may be unused)
| i | Parameter index |
| void CMCMC< T >::get_outputpercentiles | ( | TimeSeriesSet< double > & | MCMCout | ) |
Calculate percentiles of model outputs from MCMC samples.
| MCMCout | TimeSeriesSet containing MCMC samples |
Computes credible intervals (percentiles specified in calc_output_percentiles) for all model outputs, providing uncertainty bounds on predictions.
| int CMCMC< T >::get_time_series | ( | int | i | ) |
Get time series index for parameter (legacy, may be unused)
| i | Parameter index |
| int CMCMC< T >::getparamno | ( | int | i, |
| int | ts | ||
| ) | const |
Get parameter index for a specific variable and time series.
| i | Variable index |
| ts | Time series index |
| void CMCMC< T >::initialize | ( | CMBTimeSeriesSet * | results, |
| bool | random = false |
||
| ) |
Initialize MCMC chains with starting parameter values.
| results | Pointer to results structure to store output (optional) |
| random | If true, randomize initial values within parameter ranges |
Sets up chains at initial parameter values. If random=true and noinipurt=false, adds random perturbations to explore different starting regions.
Definition at line 200 of file MCMC.hpp.
References high, log, and low.
Referenced by Conductor::ExecuteTestCMBBayesian(), and SourceSinkData::MCMC().
|
private |
|
private |
Find minimum value and its chain index.
| vec | Vector of values (one per chain) |
| current_counter | Current sample number |
| n_chains | Number of chains |
Utility for tracking best (highest likelihood) chain.
Definition at line 771 of file MCMC.hpp.
References int_value_pair::value.
Evaluate model with given parameters.
| Model1 | Pointer to model object to evaluate |
| par | Parameter values to use |
Sets parameters in model and computes predictions. Used within posterior() for likelihood calculation.
| Observation * CMCMC< T >::observation | ( | int | i | ) |
Get pointer to specific observation.
| i | Observation index |
| void CMCMC< T >::Perform | ( | ) |
Main entry point to run complete MCMC analysis.
Executes full MCMC workflow:
Calculate log-posterior probability for given parameters.
| par | Parameter vector [θ1, θ2, ..., θn] |
| chain_counter | Chain index for thread-safe model evaluation |
Computes:
| TimeSeriesSet< double > CMCMC< T >::prior_distribution | ( | int | n_bins | ) |
| void CMCMC< T >::ProduceRealizations | ( | TimeSeriesSet< double > & | MCMCout | ) |
Generate posterior predictive realizations.
| MCMCout | TimeSeriesSet containing MCMC samples |
Samples parameters from posterior and evaluates model to create posterior predictive distribution for uncertainty quantification.
Number of realizations set by MCMC_Settings.number_of_post_estimate_realizations.
Generate proposed parameter values by perturbing current state.
| k | Chain index to perturb |
Proposal distribution: θ* ~ N(θ_current, diag(pertcoeff²)) Respects parameter bounds by reflecting or rejecting out-of-bounds proposals.
Read MCMC state from file to continue previous run.
| filename | Path to file containing saved MCMC state |
Restores:
Allows resuming long MCMC runs without starting over.
Calculate sensitivity of model output to parameters.
| d | Finite difference step size (fractional) |
| par | Current parameter values |
Computes numerical derivatives using forward finite differences: S_i = (model(θ + δθ_i) - model(θ)) / δθ_i
Used for sensitivity-based proposal adaptation.
Calculate lumped sensitivity matrix.
| d | Finite difference step size |
| par | Current parameter values |
Computes full Jacobian matrix of model outputs with respect to parameters. Used for global sensitivity analysis and identifiability assessment.
Set MCMC properties from string key-value pairs.
| varname | Property name (e.g., "number_of_samples", "acceptance_rate") |
| value | Property value as string |
Allows configuration from text files or user input.
Definition at line 68 of file MCMC.hpp.
Referenced by Conductor::ExecuteTestCMBBayesian(), and SourceSinkData::MCMC().
| void CMCMC< T >::SetRunTimeWindow | ( | ProgressWindow * | _rtw | ) |
Set progress window for GUI updates.
| _rtw | Pointer to ProgressWindow object |
Links MCMC to a progress dialog for real-time status updates.
| bool CMCMC< T >::step | ( | int | k, |
| int | chain_counter | ||
| ) |
Perform single MCMC step for one chain.
| k | Sample iteration number |
| chain_counter | Index of chain to advance |
Executes one Metropolis-Hastings update:
Definition at line 337 of file MCMC.hpp.
References log.
Referenced by Conductor::ExecuteTestCMBBayesian(), and SourceSinkData::MCMC().
| bool CMCMC< T >::step | ( | int | k, |
| int | nsamps, | ||
| string | filename, | ||
| CMBTimeSeriesSet * | results = nullptr, |
||
| ProgressWindow * | _rtw = 0 |
||
| ) |
Perform multiple MCMC steps with progress tracking and output.
| k | Starting sample number |
| nsamps | Number of samples to generate |
| filename | Output file for saving samples |
| results | Pointer to results structure (optional) |
| _rtw | Pointer to progress window for GUI updates (optional) |
Runs the main MCMC loop with:
Write MCMC results to output file.
| filename | Path to output file |
Saves:
Format is typically tab-delimited text for easy import into analysis software.
| double CMCMC< T >::accepted_count = 0 |
| _MCMC_file_names CMCMC< T >::FileInformation |
| _MCMC_settings CMCMC< T >::MCMC_Settings |
MCMC algorithm configuration settings.
All tuning parameters and flags controlling MCMC behavior. Modify before calling Perform() or initialize().
| TimeSeriesSet<double> CMCMC< T >::MData |
| T* CMCMC< T >::Model |
Pointer to the model object being calibrated.
The model provides likelihood evaluation and predictions. For SedSat3, this is typically a SourceSinkData object.
Definition at line 334 of file MCMC.h.
Referenced by Conductor::ExecuteTestCMBBayesian(), and SourceSinkData::MCMC().
| T CMCMC< T >::Model_out |
| vector<Observation>* CMCMC< T >::observations = nullptr |
Pointer to vector of Observation objects.
Contains observed tracer concentrations and measurement uncertainties. Must be set before running MCMC.
| TimeSeriesSet<double> CMCMC< T >::paramsList |
| CMBTimeSeriesSet CMCMC< T >::predicted |
Model predictions as time series set.
Contains predicted tracer concentrations for plotting and comparison with observations.
Definition at line 512 of file MCMC.h.
Referenced by SourceSinkData::MCMC().
| TimeSeriesSet<double> CMCMC< T >::realized_paramsList |
| ProgressWindow* CMCMC< T >::rtw = nullptr |
| double CMCMC< T >::total_count = 0 |