![]() |
Stan
1.0
probability, sampling & optimization
|
An mcmc::chains object stores parameter names and dimensionalities along with samples from multiple chains.
More...
#include <chains.hpp>
Public Member Functions | |
| chains (const size_t num_chains, const std::vector< std::string > &names, const std::vector< std::vector< size_t > > &dimss) | |
| Construct a chains object with the specified number of Markov chains, and the specified parameter names and matching parameter dimensions. More... | |
| size_t | num_chains () |
| Return the number of chains. More... | |
| size_t | num_params () |
| Return the total number of parameters. More... | |
| size_t | num_param_names () |
| Return the total number of parameter names. More... | |
| const std::vector< std::string > & | param_names () |
| Return the sequence of parameter names. More... | |
| const std::string & | param_name (size_t j) |
| Return the name of the parameter with the specified index. More... | |
| const std::vector< std::vector< size_t > > & | param_dimss () |
| Return the sequence of named parameter dimensions. More... | |
| const std::vector< size_t > & | param_dims (size_t j) |
| Return the dimensions of the parameter name with the specified index. More... | |
| const std::vector< size_t > & | param_starts () |
| Return the sequence of starting indexes for the named parameters in the underlying sequence of scalar parameters. More... | |
| size_t | param_start (size_t j) |
| Return the starting position of the named parameter with the specified index in the underlying sequence of scalar parameters. More... | |
| const std::vector< size_t > | param_sizes () |
| Return a copy of the sequence of named parameter sizes. More... | |
| size_t | param_size (size_t j) |
| Return the size of the named parameter with the specified index. More... | |
| size_t | param_name_to_index (const std::string &name) |
| Return the named parameter index for the specified parameter name. More... | |
| size_t | get_total_param_index (size_t j, const std::vector< size_t > &idxs) |
| Return the index in the underlying sequence of scalar parameters for the parameter with the specified name index and indexes. More... | |
| void | set_warmup (size_t warmup_iterations) |
| Set the warmup cutoff to the specified number of iterations. More... | |
| size_t | warmup () |
| Return the warmup iteration cutoff. More... | |
| void | add (size_t chain, std::vector< double > theta) |
| Add the specified sample to the end of the specified chain. More... | |
| size_t | num_warmup_samples (size_t k) |
| Return the number of warmup samples in the specified chain. More... | |
| size_t | num_warmup_samples () |
| Return the total number of warmup samples across chains. More... | |
| size_t | num_kept_samples (size_t k) |
| Return the number of samples in the specified chain not including warmup samples. More... | |
| size_t | num_kept_samples () |
| Return the total number of samples in all chains not including warmup samples. More... | |
| size_t | num_samples () |
| Return the total number of samples across chains including warmup and kept samples. More... | |
| size_t | num_samples (size_t k) |
| Return the number of samples including warmup and kept samples in the specified chain. More... | |
| void | get_samples (size_t n, std::vector< double > &samples) |
| Write into the specified vector the warmup and kept samples for the scalar parameter with the specified index. More... | |
| void | get_samples (size_t k, size_t n, std::vector< double > &samples) |
| Write into the specified vector the warmup and kept samples for the scalar parameter with the specified index in the chain with the specified index. More... | |
| void | get_kept_samples_permuted (size_t n, std::vector< double > &samples) |
| Write into the specified vector the kept samples for the scalar parameter with the specified index. More... | |
| template<typename F > | |
| void | apply_kept_samples (size_t k, size_t n, F &f) |
| Apply the specified functor to each kept sample for the specified parameter in the specified chain. More... | |
| template<typename F > | |
| void | apply_kept_samples (size_t n, F &f) |
| Apply the specified functor to each kept sample for the specified parameter across all chains. More... | |
| void | get_kept_samples (size_t k, size_t n, std::vector< double > &samples) |
| Write into the specified vector the kept samples for the scalar parameter with the specified index in the chain with the specified index. More... | |
| void | get_warmup_samples (size_t n, std::vector< double > &samples) |
| Write into the specified vector the warmup samples for the scalar parameter with the specified index. More... | |
| void | get_warmup_samples (size_t k, size_t n, std::vector< double > &samples) |
| Write into the specified vector the warmup samples for the parameter with the specified index in the chain with the specified index. More... | |
| double | mean (size_t k, size_t n) |
| Return the sample mean of the kept samples in the specified chain for the specified parameter. More... | |
| double | mean (size_t n) |
| Return the sample mean of the kept samples in all chains for the specified parameter. More... | |
| double | sd (size_t k, size_t n) |
| Return the sample standard deviation of the kept samples in the specified chain for the specified parameter. More... | |
| double | sd (size_t n) |
| Return the sample standard deviation of the kept samples in all chains for the specified parameter. More... | |
| double | variance (size_t k, size_t n) |
| Return the variance of the kept samples in the specified chain for the specified parameter. More... | |
| double | variance (size_t n) |
| Return the variance of the kept samples in all chains for the specified parameter. More... | |
| double | covariance (size_t k, size_t n1, size_t n2) |
| Return the covariance of the kept samples in the specified chain for the specified parameters. More... | |
| double | covariance (size_t n1, size_t n2) |
| Return the covariance of the kept samples for the specified parameters. More... | |
| double | correlation (size_t k, size_t n1, size_t n2) |
| Return the correlation of the kept samples in the specified chain for the specified parameters. More... | |
| double | correlation (size_t n1, size_t n2) |
| Return the correlation of the kept samples for the specified parameters. More... | |
| double | quantile (size_t k, size_t n, double prob) |
| Return the specified sample quantile for kept samples for the specified parameter in the specified chain. More... | |
| double | quantile (size_t n, double prob) |
| Return the specified sample quantile for kept samples for the specified parameter across all chains. More... | |
| void | quantiles (size_t k, size_t n, const std::vector< double > &probs, std::vector< double > &quantiles) |
| Write the specified sample quantiles into the specified vector for the kept samples for the specified parameter in the specified chain. More... | |
| void | quantiles (size_t n, const std::vector< double > &probs, std::vector< double > &quantiles) |
| Write the specified sample quantiles into the specified vector for the kept samples for the specified parameter across all chains. More... | |
| std::pair< double, double > | central_interval (size_t k, size_t n, double prob) |
| Return the specified sample central interval for the specified parameter in the kept samples in the specified chain. More... | |
| std::pair< double, double > | central_interval (size_t n, double prob) |
| Return the specified central interval for the specified parameter in the kept samples of all chains. More... | |
| void | autocorrelation (const size_t k, const size_t n, std::vector< double > &ac) |
| Returns the autocorrelations for the specified parameter in the kept samples of the chain specified. More... | |
| void | autocovariance (const size_t k, const size_t n, std::vector< double > &acov) |
| Returns the autocovariance for the specified parameter in the kept samples of the chain specified. More... | |
| double | effective_sample_size (size_t n) |
| Returns the effective sample size for the specified parameter across all kept samples. More... | |
| double | split_potential_scale_reduction (size_t n) |
| Return the split potential scale reduction (split R hat) for the specified parameter. More... | |
An mcmc::chains object stores parameter names and dimensionalities along with samples from multiple chains.
Synchronization: For arbitrary concurrent use, the read and write methods need to be read/write locked. Multiple writers can be used concurrently if they write to different chains. Readers for single chains need only be read/write locked with writers of that chain. For reading across chains, full read/write locking is required. Thus methods will be classified as global or single-chain read or write methods.
Storage Order: Storage is column/last-index major.
Definition at line 234 of file chains.hpp.
|
inline |
Construct a chains object with the specified number of Markov chains, and the specified parameter names and matching parameter dimensions.
The order of the parameter names and dimesnions should match the order in which samples are added to the constructed object.
The total number of parameters is determined by adding the parameters for each name. The number of parameters for each name is determined by multiplying its dimensionalities. For example, a 2 x 3 x 4 matrix parameter produces of 24 total parameters.
| num_chains | Number of Markov chains. |
| names | Sequence of paramter names. |
| dimss | Sequence of parameter dimensionalities. |
| std::invalid_argument | If the name and dimensions sequences are not the same size. |
Definition at line 351 of file chains.hpp.
|
inline |
Add the specified sample to the end of the specified chain.
Synchronization: Chain-specific write.
| chain | Markov chain identifier. |
| theta | Parameter values. |
| std::invalid_argument | if the size of the sample vector does not match the number of parameters. |
Definition at line 595 of file chains.hpp.
|
inline |
Apply the specified functor to each kept sample for the specified parameter in the specified chain.
The samples are visited in the order they were added.
| F | Type of functor to apply |
| k | Chain index |
| n | Parameter index |
| f | Functor to apply to kept samples |
Definition at line 800 of file chains.hpp.
|
inline |
Apply the specified functor to each kept sample for the specified parameter across all chains.
The samples are visited in the order of chain index, and within a chain, in the order they were added.
| F | Type of functor to apply |
| n | Parameter index |
| f | Functor to apply to kept samples |
Definition at line 822 of file chains.hpp.
|
inline |
Returns the autocorrelations for the specified parameter in the kept samples of the chain specified.
| [in] | k | Chain index |
| [in] | n | Parameter index |
| [out] | ac | Autocorrelations |
Definition at line 1388 of file chains.hpp.
|
inline |
Returns the autocovariance for the specified parameter in the kept samples of the chain specified.
| [in] | k | Chain index |
| [in] | n | Parameter index |
| [out] | acov | Autocovariances |
Definition at line 1404 of file chains.hpp.
|
inline |
Return the specified sample central interval for the specified parameter in the kept samples in the specified chain.
The central interval of width p is defined to be (ql,qh) where ql is the (1-p)/2 quantile and qh is the 1 - ql quantile.
| k | Chain index. |
| n | Parameter index. |
| prob | Width of central interval. |
| std::out_of_range | If the chain index is greater than or equal to the number of chains or if the parameter index is greater than the number of parameters |
| std::invalid_argument | If the the interval width is not between 0 and 1 inclusive. |
Definition at line 1340 of file chains.hpp.
|
inline |
Return the specified central interval for the specified parameter in the kept samples of all chains.
The central interval of width p is defined to be (ql,qh) where ql is the (1 - p) / 2 quantile and qh is the 1 - ql quantile.
| n | Parameter index. |
| prob | Width of central interval. |
| std::out_of_range | If the parameter index is greater than the number of parameters |
| std::invalid_argument | If the the interval width is not between 0 and 1 inclusive. |
Definition at line 1369 of file chains.hpp.
|
inline |
Return the correlation of the kept samples in the specified chain for the specified parameters.
| k | Chain index. |
| n1 | Parameter index 1. |
| n2 | Parameter index 2. |
| std::out_of_range | If the chain index is greater than or equal to the number of chains or the parameter index is greater than or equal to the number of parameters. |
Definition at line 1137 of file chains.hpp.
|
inline |
Return the correlation of the kept samples for the specified parameters.
| n1 | Parameter index 1. |
| n2 | Parameter index 2. |
| std::out_of_range | If the parameter index is greater than or equal to the number of parameters. |
Definition at line 1158 of file chains.hpp.
|
inline |
Return the covariance of the kept samples in the specified chain for the specified parameters.
| k | Chain index. |
| n1 | Parameter index 1. |
| n2 | Parameter index 2. |
| std::out_of_range | If the chain index is greater than or equal to the number of chains or the parameter index is greater than or equal to the number of parameters. |
Definition at line 1068 of file chains.hpp.
|
inline |
Return the covariance of the kept samples for the specified parameters.
| n1 | Parameter index 1. |
| n2 | Parameter index 2. |
| std::out_of_range | If the parameter index is greater than or equal to the number of parameters. |
Definition at line 1102 of file chains.hpp.
|
inline |
Returns the effective sample size for the specified parameter across all kept samples.
The implementation matches BDA3's effective size description.
Current implementation takes the minimum number of samples across chains as the number of samples per chain.
| [in] | n | Parameter index |
Definition at line 1426 of file chains.hpp.
|
inline |
Write into the specified vector the kept samples for the scalar parameter with the specified index in the chain with the specified index.
The order of samples is the order in which they were added.
Synchronization: Chain-specific read.
| k | Index of chain. |
| n | Index of parameter. |
| samples | Vector into which to write samples |
| std::out_of_range | If the specified chain index is greater than or equal to the number of chains, or if the specified parameter index is greater than or equal to the total number of parameters. |
Definition at line 844 of file chains.hpp.
|
inline |
Write into the specified vector the kept samples for the scalar parameter with the specified index.
The order of samples is permuted, but as long as no samples have been added in the interim, subsequent calls to this method will use the same permutation for all parameter indexes.
Synchronization: Cross-chain read.
| n | Index of parameter. |
| samples | Vector into which samples are written. |
| std::out_of_range | If the parameter index is greater than or equal to the total number of scalar parameters. |
Definition at line 771 of file chains.hpp.
|
inline |
Write into the specified vector the warmup and kept samples for the scalar parameter with the specified index in the chain with the specified index.
The order of samples is the order in which they were added.
Synchronization: Chain-specific read.
| k | Index of chain. |
| n | Index of parameter. |
| samples | Vector into which to write samples |
| std::out_of_range | If the specified chain index is greater than or equal to the number of chains, or if the specified parameter index is greater than or equal to the total number of parameters. |
Definition at line 743 of file chains.hpp.
|
inline |
Write into the specified vector the warmup and kept samples for the scalar parameter with the specified index.
The order of samples is by chain, then by order in which the sample was added to the chain.
Synchronization: Cross-chain read.
| n | Index of parameter. |
| samples | Vector into which samples are written. |
| std::out_of_range | If the parameter index is greater than or equal to the total number of scalar parameters. |
Definition at line 717 of file chains.hpp.
|
inline |
Return the index in the underlying sequence of scalar parameters for the parameter with the specified name index and indexes.
Synchronization: Thread safe.
| j | Index of parameter name. |
| idxs | Indexes into parameter. |
| std::out_of_range | If the named parameter index is greater than or equal to the number of named parameters or if any of the indexes is out of range for the named parameter with the specified index. |
Definition at line 552 of file chains.hpp.
|
inline |
Write into the specified vector the warmup samples for the parameter with the specified index in the chain with the specified index.
The order of samples is the order in which they were added.
Synchronization: Chain-specific read.
| k | Index of chain. |
| n | Index of parameter. |
| samples | Vector into which to write samples |
| std::out_of_range | If the specified chain index is greater than or equal to the number of chains, or if the specified parameter index is greater than or equal to the total number of parameters. |
Definition at line 904 of file chains.hpp.
|
inline |
Write into the specified vector the warmup samples for the scalar parameter with the specified index.
The order of samples is by chain, then by order in which the sample was added to the chain.
Synchronization: Cross-chain read.
| n | Index of parameter. |
| samples | Vector into which samples are written. |
| std::out_of_range | If the parameter index is greater than or equal to the total number of scalar parameters. |
Definition at line 871 of file chains.hpp.
|
inline |
Return the sample mean of the kept samples in the specified chain for the specified parameter.
| k | Chain index. |
| n | Parameter index. |
| std::out_of_range | If the chain index is greater than or equal to the number of chains or the parameter index is greater than or equal to the number of parameters. |
Definition at line 932 of file chains.hpp.
|
inline |
Return the sample mean of the kept samples in all chains for the specified parameter.
| n | Parameter index. |
| std::out_of_range | If the parameter index is greater than or equal to the number of parameters. |
Definition at line 952 of file chains.hpp.
|
inline |
Return the number of chains.
Synchronization: Thread safe.
Definition at line 371 of file chains.hpp.
|
inline |
Return the total number of samples in all chains not including warmup samples.
Synchronization: Warmup and cross-chain read.
Definition at line 664 of file chains.hpp.
|
inline |
Return the number of samples in the specified chain not including warmup samples.
Synchronization: Warmup and chain-specific read.
| k | Chain index. |
Definition at line 649 of file chains.hpp.
|
inline |
Return the total number of parameter names.
Synchronization: Thread safe.
Definition at line 396 of file chains.hpp.
|
inline |
Return the total number of parameters.
This is not the number of parameter names, but the total number of scalar parameters.
Synchronization: Thread safe.
Definition at line 385 of file chains.hpp.
|
inline |
Return the total number of samples across chains including warmup and kept samples.
Synchronization: Cross-chain read.
Definition at line 679 of file chains.hpp.
|
inline |
Return the number of samples including warmup and kept samples in the specified chain.
Synchronization: Chain-specific read.
| k | Markov chain index. |
| std::out_of_range | If the identifier is greater than or equal to the number of chains. |
Definition at line 697 of file chains.hpp.
|
inline |
Return the total number of warmup samples across chains.
Synchronization: Warmup and cross-chain read.
Definition at line 633 of file chains.hpp.
|
inline |
Return the number of warmup samples in the specified chain.
Synchronization: Warmup and chain-specific read.
| k | Chain index. |
Definition at line 622 of file chains.hpp.
|
inline |
Return the dimensions of the parameter name with the specified index.
Synchronization: Thread safe.
| j | Index of a parameter name. |
| std::out_of_range | If the index is greater than or equal to the number of parameter names. |
Definition at line 449 of file chains.hpp.
|
inline |
Return the sequence of named parameter dimensions.
Synchronization: Thread safe after construction.
Definition at line 433 of file chains.hpp.
|
inline |
Return the name of the parameter with the specified index.
Synchronization: Thread safe.
| j | Index of parameter. |
| std::out_of_range | If the parameter identifier is greater than or equal to the number of parameters. |
Definition at line 421 of file chains.hpp.
|
inline |
Return the named parameter index for the specified parameter name.
Synchronization: Thread safe.
| name | Parameter name. |
| std::out_of_range | If the parameter is not one of the named parameters. |
Definition at line 526 of file chains.hpp.
|
inline |
Return the sequence of parameter names.
Synchronization: Thread safe after construction.
Definition at line 407 of file chains.hpp.
|
inline |
Return the size of the named parameter with the specified index.
The size of a named parameter is the prouct of its dimensions.
Synchronization: Thread safe after construction.
| j | The index of a named parameter. |
| std::out_of_range | If the index is greater than or equal to the number of named parameters. |
Definition at line 508 of file chains.hpp.
|
inline |
Return a copy of the sequence of named parameter sizes.
The size of a named parameter is the prouct of its dimensions.
Synchronization: Thread safe.
Definition at line 490 of file chains.hpp.
|
inline |
Return the starting position of the named parameter with the specified index in the underlying sequence of scalar parameters.
Synchronization: Thread safe.
| j | The parameter name index. |
| std::out_of_range | If the parameter name index is greater than or equal to the number of named parameters. |
Definition at line 477 of file chains.hpp.
|
inline |
Return the sequence of starting indexes for the named parameters in the underlying sequence of scalar parameters.
Synchronization: Thread safe.
Definition at line 462 of file chains.hpp.
|
inline |
Return the specified sample quantile for kept samples for the specified parameter in the specified chain.
| k | Chain index |
| n | Parameter index |
| prob | Quantile probability |
| std::out_of_range | If the chain index is greater than or equal to the number of chains or if the parameter index is greater than the number of parameters |
| std::invalid_argument | If the probabilty is not between 0 and 1 inclusive. |
Definition at line 1180 of file chains.hpp.
|
inline |
Return the specified sample quantile for kept samples for the specified parameter across all chains.
| n | Parameter index |
| prob | Quantile probability |
| std::out_of_range | If the parameter index is greater than the number of parameters |
| std::invalid_argument | If the probabilty is not between 0 and 1 inclusive. |
Definition at line 1219 of file chains.hpp.
|
inline |
Write the specified sample quantiles into the specified vector for the kept samples for the specified parameter in the specified chain.
| k | Chain index |
| n | Parameter index |
| probs | Quantile probabilities |
| quantiles | Vector into which to write sample quantiles |
| std::out_of_range | If the chain index is greater than or equal to the number of chains or if the parameter index is greater than the number of parameters |
| std::invalid_argument | If the any quantile probabilty is not between 0 and 1 inclusive. |
Definition at line 1260 of file chains.hpp.
|
inline |
Write the specified sample quantiles into the specified vector for the kept samples for the specified parameter across all chains.
| n | Parameter index |
| probs | Quantile probabilities |
| quantiles | Vector into which to write sample quantiles |
| std::out_of_range | If the parameter index is greater than the number of parameters |
| std::invalid_argument | If the any quantile probabilty is not between 0 and 1 inclusive. |
Definition at line 1299 of file chains.hpp.
|
inline |
Return the sample standard deviation of the kept samples in the specified chain for the specified parameter.
This method uses the unbiased variance estimator (and thus divides by M-1 rather than M in the denominator)
| k | Chain index. |
| n | Parameter index. |
| std::out_of_range | If the chain index is greater than or equal to the number of chains or the parameter index is greater than or equal to the number of parameters. |
Definition at line 975 of file chains.hpp.
|
inline |
Return the sample standard deviation of the kept samples in all chains for the specified parameter.
This method divides by the number of kept samples minus 1 (and is htus based on an unbiased variance estimate from the samples).
| n | Parameter index. |
| std::out_of_range | If the parameter index is greater than or equal to the number of parameters. |
Definition at line 1000 of file chains.hpp.
|
inline |
Set the warmup cutoff to the specified number of iterations.
The first samples in each chain up to this number will be treated as warmup samples.
Synchronization: Warmup write method.
| warmup_iterations | Number of warmup iterations. |
Definition at line 568 of file chains.hpp.
|
inline |
Return the split potential scale reduction (split R hat) for the specified parameter.
Current implementation takes the minimum number of samples across chains as the number of samples per chain.
| [in] | n | Parameter index |
Definition at line 1483 of file chains.hpp.
|
inline |
Return the variance of the kept samples in the specified chain for the specified parameter.
| k | Chain index. |
| n | Parameter index. |
| std::out_of_range | If the chain index is greater than or equal to the number of chains or the parameter index is greater than or equal to the number of parameters. |
Definition at line 1022 of file chains.hpp.
|
inline |
Return the variance of the kept samples in all chains for the specified parameter.
| n | Parameter index. |
| std::out_of_range | If the parameter index is greater than or equal to the number of parameters. |
Definition at line 1045 of file chains.hpp.
|
inline |
Return the warmup iteration cutoff.
Synchronization: Warmup read method.
Definition at line 580 of file chains.hpp.