| Title: | A Compendium of Bayesian Statistical Routines Written in 'C++' |
|---|---|
| Description: | This is a compendium of 'C++' routines useful for Bayesian statistics. We steal other people's 'C++' code, repurpose it, and export it so developers of 'R' packages can use it in their 'C++' code. We actually don't steal anything, or claim that Thomas Bayes did, but copy code that is compatible with our GPL 3 licence, fully acknowledging the authorship of the original code. |
| Authors: | Tomasz Woźniak [aut, cre] (ORCID: <https://orcid.org/0000-0003-2212-2378>, contributions: contributions: rmvnorm1_precision_sampler, rgennorm, rgig1), Xiaolei Wang [aut] (ORCID: <https://orcid.org/0009-0005-6192-9061>, contributions: contributions: rhaar1), Longcan Li [aut] (ORCID: <https://orcid.org/0009-0007-2190-768X>, contributions: contributions: sample_variances_horseshoe), Jianying Shelly Xie [aut] (ORCID: <https://orcid.org/0009-0008-6451-6416>, contributions: contributions: sample_variances_normal_gamma), Filip Reierson [aut] (ORCID: <https://orcid.org/0009-0003-2393-5810>, contributions: contributions: rtmvnorm), Kenyon Ng [aut] (ORCID: <https://orcid.org/0000-0002-6315-9831>, contributions: contributions: rtmvnorm_hmc) |
| Maintainer: | Tomasz Woźniak <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.0.0.9000 |
| Built: | 2026-06-03 11:28:25 UTC |
| Source: | https://github.com/bsvars/steallikebayes |
Samples random numbers from a generalised normal distribution
for a restricted full rank matrix . The matrix is drawn
row-by-row from their full conditional distributions using the Gibbs sampler
by Waggoner & Zha (2003a). The density is proportional to
specified by the scale matrices , a positive
scalar-valued shape parameter , and selection
matrices defining the restrictions. Zero restrictions may be
imposed on th matrix row-by-row as
where vectors collect the unrestricted elements
of and the matrices place them in appropriate spots of the
row . The output may be normalised using the
method by Waggoner & Zha (2003b) ensuring positive diagonal elements of the
output matrices.
This method is useful for sampling the structural matrix of the structural vector autoregressive models identified by exclusion restrictions, sign and exclusion restrictions, heteroskedasticity, and instrumental variables.
rgennorm(n, X, S, nu, V, normalise = TRUE)rgennorm(n, X, S, nu, V, normalise = TRUE)
n |
a positive integer with the number of draws to be sampled.
C++: a |
X |
an |
S |
an |
nu |
a positive integer number with the shape parameter |
V |
a list with |
normalise |
a logical value of whether the output should be normalised
following the method by Waggoner & Zha (2003b) ensuring positive signs of the
diagonal elements of the sampled matrices. C++: an |
This function is based on C++ code from the R package bsvars by Woźniak (2024,2025) and is using objects and commands from the armadillo library by Sanderson & Curtin (2025) thanks to the RcppArmadillo package by Eddelbuettel, Francois, Bates, Ni, & Sanderson (2025).
an -array with random draws from the
generalised normal distribution. C++: an arma::cube object.
Tomasz Woźniak [email protected]
Eddelbuettel D., Francois R., Bates D., Ni B., Sanderson C. (2025). RcppArmadillo: 'Rcpp' Integration for the 'Armadillo' Templated Linear Algebra Library. R package version 15.0.2-2. <doi:10.32614/CRAN.package.RcppArmadillo>
Sanderson C., Curtin R. (2025). Armadillo: An Efficient Framework for Numerical Linear Algebra. International Conference on Computer and Automation Engineering, 303-307, <doi:10.1109/ICCAE64891.2025.10980539>
Waggoner D.F., Zha T., (2003a). A Gibbs Sampler for Structural Vector Autoregressions. Journal of Economic Dynamics and Control, 28(2), 349-366, <doi:10.1016/S0165-1889(02)00168-9>.
Waggoner, D.F., Zha, T. (2003b). Likelihood Preserving Normalization in Multiple Equation Models. Journal of Econometrics, 114(2), 329-347. <doi:10.1016/S0304-4076(03)00087-3>
Woźniak T. (2024). bsvars: Bayesian Estimation of Structural Vector Autoregressive Models, R package version 3.2, <doi:10.32614/CRAN.package.bsvars>.
Woźniak T. (2025). Fast and Efficient Bayesian Analysis of Structural Vector Autoregressions Using the R Package bsvars. University of Melbourne Working Paper, 1–25. <doi:10.48550/arXiv.2410.15090>.
rgennorm(1, diag(2), array(diag(2), c(2,2,2)), 3, list(diag(2), matrix(1,1,2)))rgennorm(1, diag(2), array(diag(2), c(2,2,2)), 3, list(diag(2), matrix(1,1,2)))
Samples random numbers from the generalized inverse Gaussian distribution with density proportional to:
with the parameters having to satisfy the conditions:
The sampling algorithm proposed by Hörmann & Leydold (2013) is implemented. If the arguments do not satisfy the restrictions a std::logic_error exception is thrown in C++.
This method is useful for sampling variance coefficients that arise in problems with their likelihood in the form of th inverse gamma distribution and assuming a gamma prior.
rgig1(lambda, chi, psi)rgig1(lambda, chi, psi)
lambda |
a real scalar representing parameter |
chi |
a real scalar representing parameter |
psi |
a real scalar representing parameter |
This function is based on C++ code from the R package shrinkTVP by Knaus, Bitto-Nemling, Cadonna, Frühwirth-Schnatter (2021) and also uses functionality of the GIGrvg package by Leydold & Hörmann (2023). It uses random number generators from the armadillo library by Sanderson & Curtin (2025) thanks to the RcppArmadillo package by Eddelbuettel, Francois, Bates, Ni, & Sanderson (2025)
random draw from the GIG distribution. C++: a double object.
Tomasz Woźniak [email protected]
Eddelbuettel D., Francois R., Bates D., Ni B., Sanderson C. (2025). RcppArmadillo: 'Rcpp' Integration for the 'Armadillo' Templated Linear Algebra Library. R package version 15.0.2-2. <doi:10.32614/CRAN.package.RcppArmadillo>
Hörmann W., Leydold J. (2013). Generating generalized inverse Gaussian random variates, Statistics and Computing 24, 547–557, <doi:10.1007/s11222-013-9387-3>.
Leydold J., Hörmann W. (2023). GIGrvg: Random Variate Generator for the GIG Distribution, R package version 0.8, <doi:10.32614/CRAN.package.GIGrvg>.
Knaus, P., Bitto-Nemling, A., Cadonna, A., & Frühwirth-Schnatter, S. (2021). Shrinkage in the Time-Varying Parameter Model Framework Using the R Package shrinkTVP. Journal of Statistical Software, 100(13), 1–32. <doi:10.18637/jss.v100.i13>.
Sanderson C., Curtin R. (2025). Armadillo: An Efficient Framework for Numerical Linear Algebra. International Conference on Computer and Automation Engineering, 303-307, <doi:10.1109/ICCAE64891.2025.10980539>
rgig1(1, 1, 1)rgig1(1, 1, 1)
This function samples random orthogonal matrices from the Haar distribution, that is, the uniform distribution over the space of orthogonal matrices.
rhaar1(n)rhaar1(n)
n |
a positive integer scalar specifying the dimension of the orthogonal
matrix. C++: an |
This function is based on C++ code from the R package bsvarSIGNs by Wang X., Woźniak T. (2025a,2025b) and is using objects and commands from the armadillo library by Sanderson & Curtin (2025) thanks to the RcppArmadillo package by Eddelbuettel, Francois, Bates, Ni, & Sanderson (2025)
An matrix with a random draw of the orthogonal matrix
from the Haar distribution. C++: an arma::mat object.
Xiaolei Wang [email protected]
Eddelbuettel D., Francois R., Bates D., Ni B., Sanderson C. (2025). RcppArmadillo: 'Rcpp' Integration for the 'Armadillo' Templated Linear Algebra Library. R package version 15.0.2-2. <doi:10.32614/CRAN.package.RcppArmadillo>
Sanderson C., Curtin R. (2025). Armadillo: An Efficient Framework for Numerical Linear Algebra. International Conference on Computer and Automation Engineering, 303-307, <doi:10.1109/ICCAE64891.2025.10980539>
Stewart, G. W. (1980). The efficient generation of random orthogonal matrices with an application to condition estimators. SIAM Journal on Numerical Analysis, 17(3), 403-409. <doi:10.1137/0717034>
Wang X., Woźniak T. (2025a). bsvarSIGNs: Bayesian SVARs with Sign, Zero, and Narrative Restrictions. R package version 2.0, <doi:0.32614/CRAN.package.bsvarSIGNs>.
Wang X., Woźniak T. (2025b). Bayesian Analyses of Structural Vector Autoregressions with Sign, Zero, and Narrative Restrictions Using the R Package bsvarSIGNs, <doi:10.48550/arXiv.2501.16711>.
rhaar1(3)rhaar1(3)
Samples random numbers from an -variate normal distribution
specified by the precision matrix and
location vector as per:
where the precision matrix is bi-diagonal with the diagonal elements
given in the vector argument precision_diag and the off-diagonal element
is given in the scalar argument precision_offdiag, and the location
vector is provided in the vector argument location.
This method is useful for the simulation smoother of the linear Gaussian state-space models with the state variable specified by the autoregressive dynamics with one lag, AR(1). See Woźniak (2021) for more details.
rmvnorm1_precision_sampler(location, precision_diag, precision_offdiag)rmvnorm1_precision_sampler(location, precision_diag, precision_offdiag)
location |
an |
precision_diag |
an |
precision_offdiag |
a numeric scalar with the off-diagonal element of
the precision matrix |
This function is based on C++ code from the R package stochvol by Hosszejni & Kastner (2025) and Kastner G. (2016) and is using objects and commands from the armadillo library by Sanderson & Curtin (2025) thanks to the RcppArmadillo package by Eddelbuettel, Francois, Bates, Ni, & Sanderson (2025)
an -vector with random draws from the multivariate normal
distribution. C++: an arma::vec vector object.
Tomasz Woźniak [email protected]
Chan J.C.C., Jeliazkov I. (2009). Efficient simulation and integrated likelihood estimation in state space models. International Journal of Mathematical Modelling and Numerical Optimisation, 1(1/2), <doi:10.1504/IJMMNO.2009.030090>.
Eddelbuettel D., Francois R., Bates D., Ni B., Sanderson C. (2025). RcppArmadillo: 'Rcpp' Integration for the 'Armadillo' Templated Linear Algebra Library. R package version 15.0.2-2. <doi:10.32614/CRAN.package.RcppArmadillo>
Hosszejni D., Kastner G. (2025). stochvol: Efficient Bayesian Inference for Stochastic Volatility (SV) Models. R package version 3.2.8, <doi:10.32614/CRAN.package.stochvol>
Kastner G. (2016). Dealing with Stochastic Volatility in Time Series Using the R Package stochvol. Journal of Statistical Software, 69(5), 1–30. <doi:10.18637/jss.v069.i05>.
Sanderson C., Curtin R. (2025). Armadillo: An Efficient Framework for Numerical Linear Algebra. International Conference on Computer and Automation Engineering, 303-307, <doi:10.1109/ICCAE64891.2025.10980539>
Woźniak T. (2021). Simulation Smoother using RcppArmadillo, RcppGallery https://gallery.rcpp.org/articles/simulation-smoother-using-rcpparmadillo/
rmvnorm1_precision_sampler(rep(0, 100), rep(1, 100), -0.5)rmvnorm1_precision_sampler(rep(0, 100), rep(1, 100), -0.5)
Samples random numbers from a truncated multivariate normal distribution
with parameters mean vector, covariance matrix, and linear inequality constraints
of the form , where is a constraint matrix and
and are lower and upper bounds. The function uses a Gibbs sampling
algorithm to generate draws from the constrained distribution.
The truncated multivariate normal is important for research in Bayesian statistics, econometrics, and any field requiring parameter estimation subject to inequality constraints. Common applications include censored regression models, portfolio optimization with constraints, and prior distributions with bounded support.
rtmvnorm(mean, sigma, blc, lower, upper, init, burn = 10)rtmvnorm(mean, sigma, blc, lower, upper, init, burn = 10)
mean |
an |
sigma |
a |
blc |
an |
lower |
an |
upper |
an |
init |
an |
burn |
number of iterations used as burn-in. Defaults is 10.
C++: an |
This function is based on C++ code from the R package tmvtnsim by Lu (2025) and is using objects and commands from the armadillo library by Sanderson & Curtin (2025) thanks to the RcppArmadillo package by Eddelbuettel, Francois, Bates, Ni, & Sanderson (2025).
An matrix of draws from the specified truncated multivariate normal. C++: an arma::mat object.
Filip Reierson [email protected]
Eddelbuettel D., Francois R., Bates D., Ni B., Sanderson C. (2025). RcppArmadillo: 'Rcpp' Integration for the 'Armadillo' Templated Linear Algebra Library. R package version 15.0.2-2. <doi:10.32614/CRAN.package.RcppArmadillo>
Sanderson C., Curtin R. (2025). Armadillo: An Efficient Framework for Numerical Linear Algebra. International Conference on Computer and Automation Engineering, 303-307, <doi:10.1109/ICCAE64891.2025.10980539>
Li, Y., Ghosh, S.K. Efficient sampling methods for truncated multivariate normal and student-t distributions subject to linear inequality constraints. J Stat Theory Pract 9, 712–732 (2015). <doi:10.1080/15598608.2014.996690>
Lu K. (2025). tmvtnsim: Truncated Multivariate Normal and t Distribution Simulation. R package version 0.1.4, <doi:10.32614/CRAN.package.tmvtnsim>
rtmvnorm(mean = matrix(c(0, 0), nrow = 1), sigma = diag(2), blc = diag(2), lower = matrix(c(-Inf, -Inf), nrow = 1), upper = matrix(c(1, 1), nrow = 1), init = matrix(c(0, 0), nrow = 1), burn = 10)rtmvnorm(mean = matrix(c(0, 0), nrow = 1), sigma = diag(2), blc = diag(2), lower = matrix(c(-Inf, -Inf), nrow = 1), upper = matrix(c(1, 1), nrow = 1), init = matrix(c(0, 0), nrow = 1), burn = 10)
Generate -dimensional truncated multivariate normal samples that satisfy
an inequality constraint
where is a column
vector of the generated sample. The sampler is an exact Hamiltonian Monte Carlo
(HMC) sampler as described in Pakman and Paninski (2014).
rtmvnorm_hmc(n, mean, cov, initial, Fmat, g, burn)rtmvnorm_hmc(n, mean, cov, initial, Fmat, g, burn)
n |
a positive integer with the number of samples.
C++: an |
mean |
a |
cov |
a |
initial |
a |
Fmat |
an |
g |
an |
burn |
a non-negative integer with the number of burn-in iterations
before collecting samples.
C++: an |
The function generates samples from a truncated multivariate normal distribution
with mean mean and covariance cov, subject to linear constraints
defined by Fmat and g.
The user should supply an initial value that strictly satisfies the inequality constraints, although the generated samples only satisfy them weakly.
No check for symmetry is performed on the covariance matrix.
It is advisable to use a small burn-in period (e.g., 10) to allow the Markov chain to reach stationarity.
This function is a wrapper around a C++ implementation adapted from the 'tnorm' R package by Kenyon Ng.
An matrix with each row corresponding to a sample.
C++: an Eigen::MatrixXd object.
Pakman, A. and Paninski, L. (2014). Exact Hamiltonian Monte Carlo for Truncated Multivariate Gaussians. Journal of Computational and Graphical Statistics, 23(2), 518–542. <doi:10.1080/10618600.2013.788448>
Ng, K. (2024). tnorm: Generate Multivariate Truncated Normal Samples. R package version 0.0.1. <https://github.com/weiyaw/tnorm>
Bates, D. and Eddelbuettel, D. (2013). Fast and Elegant Numerical Linear Algebra Using the RcppEigen Package. Journal of Statistical Software, 52(5), 1–24. <doi:10.18637/jss.v052.i05>
rtmvnorm_hmc(1, c(0, 0), diag(2), c(0, 2), diag(2), c(1, -1), 1)rtmvnorm_hmc(1, c(0, 0), diag(2), c(0, 2), diag(2), c(1, -1), 1)
Performs one Gibbs sampling iteration for the **exported hyper-parameters** #' of the horseshoe prior variance parameters: local variances (, argument theta),
global variance (, argument zeta), and their corresponding auxiliary variables
( and , arguments nu and varpi). The horseshoe prior
Carvalho, Polson, Scott (2010) is a continuous shrinkage prior for Bayesian
variable selection with the hierarchical structure:
where denotes the half-Cauchy distribution,
are the local shrinkage parameters (argument theta), and
is the global shrinkage parameter (argument zeta). The prior variance
for coefficient is .
The half-Cauchy distributions are represented using auxiliary variables
and to facilitate Gibbs sampling. This implementation
allows updating only a subset of coefficients specified by the indices in
argument ind.
sample_variances_horseshoe(coefs, theta, zeta, nu, varpi)sample_variances_horseshoe(coefs, theta, zeta, nu, varpi)
coefs |
a |
theta |
a |
zeta |
a numeric scalar with the global variance parameter |
nu |
a |
varpi |
a numeric scalar with the auxiliary variable for the global
shrinkage, updated by reference. C++: a |
This function is based on C++ code from the R package bayesianVARs by Gruber & Kastner (2024) and is using objects and commands from the armadillo library. Thanks to the RcppArmadillo package by Eddelbuettel, Francois, Bates, Ni, & Sanderson (2025).
A **list** of C++ objects of class Rcpp::List containing the updated variance parameters.
The elements are:
V_i: A -vector of the total prior variances .
R: a **numeric vector**. C++: an arma::vec object.
theta: The updated -vector of local variances .
R: a **numeric vector**. C++: an arma::vec object.
zeta: The updated scalar global variance .
R: a **positive scalar**. C++: a double scalar.
nu: The updated -vector of local auxiliary variables .
R: a **numeric vector**. C++: an arma::vec object.
varpi: The updated scalar global auxiliary variable .
R: a **positive scalar**. C++: a double scalar.
Longcan Li [email protected]
Carvalho C.M., Polson N.G., Scott J.G. (2010). The horseshoe estimator for sparse signals. Biometrika, 97(2), 465-480. <doi:10.1093/biomet/asq017>
Eddelbuettel D., Francois R., Bates D., Ni B., Sanderson C. (2025). RcppArmadillo: 'Rcpp' Integration for the 'Armadillo' Templated Linear Algebra Library. R package version 15.0.2-2. <doi:10.32614/CRAN.package.RcppArmadillo>
Gruber L.,Kastner G. (2024). bayesianVARs: MCMC Estimation of Bayesian Vector Autoregressions. R package version 0.1.5, <doi:10.32614/CRAN.package.bayesianVARs>
Makalic E., Schmidt D.F. (2016). A Simple Sampler for the Horseshoe Estimator. IEEE Signal Processing Letters, 23(1), 179-182. <doi:10.1109/LSP.2015.2503725>
Sanderson C., Curtin R. (2025). Armadillo: An Efficient Framework for Numerical Linear Algebra. International Conference on Computer and Automation Engineering, 303-307, <doi:10.1109/ICCAE64891.2025.10980539>
sample_variances_horseshoe( rep(0, 2), rep(0, 2), 1, rep(1, 2), 1)sample_variances_horseshoe( rep(0, 2), rep(0, 2), 1, rep(1, 2), 1)
This function samples variances as well as the hyper-parameters and
from a Normal-Gamma prior distribution.
The prior distribution has a hierarchical structure where each element of a -vector follows:
for . The hyperparameter
follows an i.i.d. discrete hyperprior with , where
is the vector of strictly
positive support points. See Brown & Griffin (2010) and Gruber & Kastner (2025) for further details.
sample_variances_normal_gamma( x, theta_tilde, zeta, a, a_vec, varrho0, varrho1, hyper, tol = 1e-06 )sample_variances_normal_gamma( x, theta_tilde, zeta, a, a_vec, varrho0, varrho1, hyper, tol = 1e-06 )
x |
A starting values vector of the variances. C++: an |
theta_tilde |
A starting values vector of |
zeta |
A starting value of |
a |
Prior shape parameter of the Gamma distribution for |
a_vec |
Multinomial grid for updating shape parameter of the Gamma distribution.C++:
an |
varrho0 |
Prior shape parameter of the Gamma distribution for |
varrho1 |
Prior scale parameter of the Gamma distribution for |
hyper |
A logical value. TRUE or FALSE. C++: an |
tol |
The numerical tolerance, default is '1e-06'. C++: an |
This function is based on C++ code from the R package bayesianVARs by Gruber (2025) and is using objects and commands from the armadillo library by Sanderson & Curtin (2025) thanks to the RcppArmadillo package by Eddelbuettel, Francois, Bates, Ni, & Sanderson (2025).
A **list** of C++ objects of class Rcpp::List containing the updated variance parameters.
The elements are:
V_i A vector of variances of the Normal-Gamma prior distribution.
R: a **numeric vector**. C++: an arma::vec object.
theta_tilde An updated vector of .
R: a **numeric vector**. C++: an arma::vec object.
zeta: An updated vector of .
R: a **positive scalar**. C++: an double object.
Jianying Shelly Xie [email protected]
Gruber, L. (2025). bayesianVARs: MCMC Estimation of Bayesian Vectorautoregressions. R package version 0.1.5.9000, <doi: 10.32614/CRAN.package.bayesianVARs>.
Gruber, L., & Kastner, G. (2025). Forecasting macroeconomic data with Bayesian VARs: Sparse or dense? It depends!. International Journal of Forecasting, 41(4), 1589-1619, <doi:org/10.1016/j.ijforecast.2025.02.001>.
Philip J. Brown., Jim E. Griffin (2010). Inference with normal-gamma prior distributions in regression problems. Bayesian Analysis, 5(1), 171-188, <doi:org/10.1214/10-BA507>.
Eddelbuettel D., Francois R., Bates D., Ni B., Sanderson C. (2025). RcppArmadillo: 'Rcpp' Integration for the 'Armadillo' Templated Linear Algebra Library. R package version 15.0.2-2. <doi:10.32614/CRAN.package.RcppArmadillo>
Sanderson C., Curtin R. (2025). Armadillo: An Efficient Framework for Numerical Linear Algebra. International Conference on Computer and Automation Engineering, 303-307, <doi:10.1109/ICCAE64891.2025.10980539>
sample_variances_normal_gamma(rep(0,2), rep(1,2), 1, 1, rep(1,2), 1, 1, TRUE, 1e-6)sample_variances_normal_gamma(rep(0,2), rep(1,2), 1, 1, rep(1,2), 1, 1, TRUE, 1e-6)