| Type: | Package |
| Title: | A Compendium of Bayesian Statistical Routines Written in 'C++' |
| Version: | 1.0 |
| Date: | 2025-11-26 |
| Maintainer: | Tomasz Woźniak <wozniak.tom@pm.me> |
| 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. |
| Imports: | Rcpp (≥ 1.1.0), GIGrvg |
| LinkingTo: | Rcpp, RcppArmadillo, RcppEigen, GIGrvg |
| Suggests: | tinytest |
| URL: | https://bsvars.org/StealLikeBayes/ |
| BugReports: | https://github.com/bsvars/StealLikeBayes/issues |
| License: | GPL (≥ 3) |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.3 |
| Config/Needs/website: | rmarkdown |
| NeedsCompilation: | yes |
| Packaged: | 2025-11-26 20:33:25 UTC; twozniak |
| Author: | Tomasz Woźniak |
| Repository: | CRAN |
| Date/Publication: | 2025-12-02 14:50:02 UTC |
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.
Note
This is a collaborative project. You are welcome to contribute. Please, read the documentation at https://bsvars.org/StealLikeBayes/dev/ and StealLikeBayes!
Author(s)
Maintainer: Tomasz Woźniak wozniak.tom@pm.me (ORCID) (contributions: rnorm1_precision_sampler, rgennorm)
Authors:
Xiaolei Wang adamwang15@gmail.com (ORCID) (contributions: rhaar1)
Longcan Li longcando@outlook.com (ORCID) (contributions: sample_variances_horseshoe)
Jianying Shelly Xie shellyyinggxie@gmail.com (ORCID) (contributions: sample_variances_normal_gamma)
Filip Reierson filip.reierson@gmail.com (ORCID) (contributions: rtmvnorm)
Kenyon Ng kenyon.ng@gmail.com (ORCID) (contributions: rtmvnorm_hmc)
See Also
Useful links:
Samples random draws from a generalised normal distribution using the Gibbs sampler by Waggoner & Zha (2003a)
Description
Samples random numbers from a generalised normal distribution
for a restricted N\times N full rank matrix X. 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
det(X)^{\nu - N}\exp(-\frac{1}{2}\sum_{n=1}^{N}x_nV_nS_nV_n'x_n')
specified by the N\times N scale matrices S_n, a positive
scalar-valued shape parameter \nu, and r_n\times N selection
matrices V_n defining the restrictions. Zero restrictions may be
imposed on th X matrix row-by-row as
X_{n.} = x_nV_n
where 1\times r_n vectors x_n collect the unrestricted elements
of X_{n.} and the matrices V_n place them in appropriate spots of the
1\times N row X_{n.}. 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.
Usage
rgennorm(n, X, S, nu, V, normalise = TRUE)
Arguments
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 |
Details
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).
Value
an N\times N\times n-array with random draws from the
generalised normal distribution. C++: an arma::cube object.
Author(s)
Tomasz Woźniak wozniak.tom@pm.me
References
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>.
Examples
rgennorm(1, diag(2), array(diag(2), c(2,2,2)), 3, list(diag(2), matrix(1,1,2)))
Sample random matrices from Haar distribution
Description
This function samples random orthogonal matrices from the Haar distribution, that is, the uniform distribution over the space of orthogonal matrices.
Usage
rhaar1(n)
Arguments
n |
a positive integer scalar specifying the dimension of the orthogonal
matrix. C++: an |
Details
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)
Value
An n\times n matrix with a random draw of the orthogonal matrix
from the Haar distribution. C++: an arma::mat object.
Author(s)
Xiaolei Wang adamwang15@gmail.com
References
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>.
Examples
rhaar1(3)
Samples random draws from a multivariate normal distribution using the precision sampler by Chan & Jeliazkov (2009)
Description
Samples random numbers from an N-variate normal distribution
specified by the N\times N precision matrix P and N\times 1
location vector L as per:
N(P^{-1}L, P^{-1})
where the precision matrix P 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 L 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.
Usage
rnorm1_precision_sampler(location, precision_diag, precision_offdiag)
Arguments
location |
an |
precision_diag |
an |
precision_offdiag |
a numeric scalar with the off-diagonal element of
the precision matrix |
Details
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)
Value
an N-vector with random draws from the multivariate normal
distribution. C++: an arma:vec vector object.
Author(s)
Tomasz Woźniak wozniak.tom@pm.me
References
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/
Examples
rnorm1_precision_sampler(rep(0, 100), rep(1, 100), -0.5)
Sample Random Draws From the Truncated Multivariate Normal Using the Algorithm Proposed by Yifang Li and Sujit K. Ghosh (2015)
Description
Samples random numbers from a truncated multivariate normal distribution
with parameters mean vector, covariance matrix, and linear inequality constraints
of the form l \leq Bx \leq u, where B is a constraint matrix and
l and u 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.
Usage
rtmvnorm(mean, sigma, blc, lower, upper, init, burn = 10)
Arguments
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 |
Details
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).
Value
An n\times p matrix of draws from the specified truncated multivariate normal. C++: an arma::mat object.
Author(s)
Filip Reierson filip.reierson@gmail.com
References
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>
Examples
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 Truncated Multivariate Normal Samples via Hamiltonian Monte Carlo
Description
Generate p-dimensional truncated multivariate normal samples that satisfy
an inequality constraint
F \times x + g \ge 0,
where x 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).
Usage
rtmvnorm_hmc(n, mean, cov, initial, Fmat, g, burn)
Arguments
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 |
Details
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.
Value
An n \times p matrix with each row corresponding to a sample.
C++: an Eigen::MatrixXd object.
References
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>
Examples
rtmvnorm_hmc(1, c(0, 0), diag(2), c(0, 2), diag(2), c(1, -1), 1)
Samples variances from the horseshoe prior using Gruber & Kastner (2024)
Description
Performs one Gibbs sampling iteration for the horseshoe prior variance parameters. The horseshoe prior Carvalho, Polson, Scott (2010) is a continuous shrinkage prior for Bayesian variable selection with the hierarchical structure:
\beta_j \sim N(0, \lambda_j^2 \tau^2)
\lambda_j \sim C^+(0, 1)
\tau \sim C^+(0, 1)
where C^+(0, 1) denotes the half-Cauchy distribution, \lambda_j^2
are the local shrinkage parameters (argument theta), and \tau^2
is the global shrinkage parameter (argument zeta). The prior variance
for coefficient j is V_{i,j} = \lambda_j^2 \tau^2.
The half-Cauchy distributions are represented using auxiliary variables
\nu_j and \varpi to facilitate Gibbs sampling. This implementation
allows updating only a subset of coefficients specified by the indices in
argument ind.
Usage
sample_variances_horseshoe(coefs, theta, zeta, nu, varpi)
Arguments
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 |
Details
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).
Value
a vector of variances' random draws. C++: an arma::vec vector object.
Author(s)
Longcan Li longcando@outlook.com
References
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>
Examples
sample_variances_horseshoe( rep(0, 2), rep(0, 2), 1, rep(1, 2), 1)
Samples variances of the Normal-Gamma prior distribution by Brown & Griffin (2010).
Description
This function samples variances from a Normal-Gamma prior distribution.
The prior distribution has a hierarchical structure where each element x_i of a k-vector X follows:
x_i \sim N(0,\vartheta_i \zeta_j), \vartheta_i \sim G(a_j, a_j / 2)
\text{, and } \zeta_j^{-1} \sim G(b,c)
for i=j=1,\dots,k. The hyperparameter a_j
follows an i.i.d. discrete hyperprior with Pr(a_j = \tilde{a}_r) = p_r, where
\tilde{a} = (\tilde{a}_1, \dots, \tilde{a}_R)' is the vector of strictly
positive support points. See Brown & Griffin (2010) and Gruber & Kastner (2025) for further details.
Usage
sample_variances_normal_gamma(
x,
theta_tilde,
zeta,
a,
a_vec,
varrho0,
varrho1,
hyper,
tol = 1e-06
)
Arguments
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 |
Details
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).
Value
A vector of variances of the Normal-Gamma prior distribution.
C++: an arma::vec object.
Author(s)
Jianying Shelly Xie shellyyinggxie@gmail.com
References
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>
Examples
sample_variances_normal_gamma(rep(0,2), rep(1,2), 1, 1, rep(1,2), 1, 1, TRUE, 1e-6)