Title: | Simulations of Matrix Variate Distributions |
---|---|
Description: | Provides samplers for various matrix variate distributions: Wishart, inverse-Wishart, normal, t, inverted-t, Beta type I, Beta type II, Gamma, confluent hypergeometric. Allows to simulate the noncentral Wishart distribution without the integer restriction on the degrees of freedom. |
Authors: | Stéphane Laurent |
Maintainer: | Stéphane Laurent <[email protected]> |
License: | GPL-3 |
Version: | 2.0.0.90000 |
Built: | 2024-11-10 05:45:12 UTC |
Source: | https://github.com/stla/matrixsampling |
Samples the inverse-Wishart distribution.
rinvwishart(n, nu, Omega, epsilon = 0, checkSymmetry = TRUE)
rinvwishart(n, nu, Omega, epsilon = 0, checkSymmetry = TRUE)
n |
sample size, a positive integer |
nu |
degrees of freedom, must satisfy |
Omega |
scale matrix, a positive definite real matrix |
epsilon |
threshold to force invertibility in the algorithm; see Details |
checkSymmetry |
logical, whether to check the symmetry of |
The inverse-Wishart distribution with scale matrix
Ω is
defined as the inverse of the Wishart distribution with scale matrix
Ω-1
and same number of degrees of freedom.
The argument epsilon
is a threshold whose role is to guarantee
the invertibility of the sampled Wishart distributions.
See Details in rwishart
.
A numeric three-dimensional array; simulations are stacked along the third dimension (see example).
nu <- 6 p <- 3 Omega <- toeplitz(p:1) IWsims <- rinvwishart(10000, nu, Omega) dim(IWsims) # 3 3 10000 apply(IWsims, 1:2, mean) # approximately Omega/(nu-p-1) # the epsilon argument: IWsims <- tryCatch(rinvwishart(10000, nu=p+0.001, Omega), error=function(e) e) IWsims <- tryCatch(rinvwishart(10000, nu=p+0.001, Omega, epsilon=1e-8), error=function(e) e)
nu <- 6 p <- 3 Omega <- toeplitz(p:1) IWsims <- rinvwishart(10000, nu, Omega) dim(IWsims) # 3 3 10000 apply(IWsims, 1:2, mean) # approximately Omega/(nu-p-1) # the epsilon argument: IWsims <- tryCatch(rinvwishart(10000, nu=p+0.001, Omega), error=function(e) e) IWsims <- tryCatch(rinvwishart(10000, nu=p+0.001, Omega, epsilon=1e-8), error=function(e) e)
Samples a matrix Beta (type I) distribution.
rmatrixbeta(n, p, a, b, Theta1 = NULL, Theta2 = NULL, def = 1, checkSymmetry = TRUE)
rmatrixbeta(n, p, a, b, Theta1 = NULL, Theta2 = NULL, def = 1, checkSymmetry = TRUE)
n |
sample size, a positive integer |
p |
dimension, a positive integer |
a , b
|
parameters of the distribution, positive numbers with constraints given in Details |
Theta1 |
numerator noncentrality parameter, a positive semidefinite real
matrix of order |
Theta2 |
denominator noncentrality parameter, a positive semidefinite real
matrix of order |
def |
|
checkSymmetry |
logical, whether to check the symmetry of |
A Beta random matrix is defined as follows.
Take two independent Wishart random matrices
S1 ~ Wp(2a,Ip,Θ1)
and
S2 ~ Wp(2b,Ip,Θ2).
definition 1: U = (S1+S2)-½S1(S1+S2)-½
definition 2: U = S1½(S1+S2)-1S1½
In the central case, the two definitions yield the same distribution. Under definition 2, the Beta distribution is related to the Beta type II distribution by U ~ V(I+V)-1.
Parameters a
and b
are positive numbers that satisfy the
following constraints:
if both Theta1
and Theta2
are the null matrix,
a+b > (p-1)/2
; if a < (p-1)/2
, it must be half an integer;
if b < (p-1)/2
, it must be half an integer
if Theta1
is not the null matrix, a >= (p-1)/2
;
if b < (p-1)/2
, it must be half an integer
if Theta2
is not the null matrix, b >= (p-1)/2
;
if a < (p-1)/2
, it must be half an integer
A numeric three-dimensional array; simulations are stacked along the third dimension (see example).
Definition 2 requires the calculation of the square root of
S1 ~ Wp(2a,Ip,Θ1)
(see Details). While S1 is always
positive semidefinite in theory, it could happen that the simulation of
S1 is not positive semidefinite,
especially when a
is small. In this case the calculation of the square root
will return NaN
.
The matrix variate Beta distribution is usually defined only for
and
. In this case, a random matrix
generated from this distribution satisfies
.
For an half integer
, it satisfies
and
.
For an half integer
, it satisfies
and
.
Bsims <- rmatrixbeta(10000, 3, 1, 1) dim(Bsims) # 3 3 10000
Bsims <- rmatrixbeta(10000, 3, 1, 1) dim(Bsims) # 3 3 10000
Samples a matrix Beta type II distribution.
rmatrixbetaII(n, p, a, b, Theta1 = NULL, Theta2 = NULL, def = 1, checkSymmetry = TRUE)
rmatrixbetaII(n, p, a, b, Theta1 = NULL, Theta2 = NULL, def = 1, checkSymmetry = TRUE)
n |
sample size, a positive integer |
p |
dimension, a positive integer |
a , b
|
parameters of the distribution, positive numbers with constraints given in Details |
Theta1 |
numerator noncentrality parameter, a positive semidefinite real
matrix of order |
Theta2 |
denominator noncentrality parameter, a positive semidefinite real
matrix of order |
def |
|
checkSymmetry |
logical, whether to check the symmetry of |
A Beta type II random matrix is defined as follows.
Take two independent Wishart random matrices
S1 ~ Wp(2a,Ip,Θ1)
and
S2 ~ Wp(2b,Ip,Θ2).
definition 1: V = S2-½S1S2-½
definition 2: V = S1½S2-1S1½
In the central case, the two definitions yield the same distribution. Under definition 2, the Beta type II distribution is related to the Beta distribution by V ~ U(I-U)-1.
Parameters a
and b
are positive numbers that satisfy the
following constraints:
in any case, b > (p-1)/2
if Theta1
is the null matrix and a < (p-1)/2
, then
a
must be half an integer
if Theta1
is not the null matrix, a >= (p-1)/2
A numeric three-dimensional array; simulations are stacked along the third dimension (see example).
The issue described in the Warning section of rmatrixbeta
also concerns rmatrixbetaII
.
The matrix variate Beta distribution of type II is usually defined only for
and
. In this case, a random matrix
generated from this distribution satisfies
.
For an half integer
, it satisfies
and
.
Bsims <- rmatrixbetaII(10000, 3, 1, 1.5) dim(Bsims) # 3 3 10000
Bsims <- rmatrixbetaII(10000, 3, 1, 1.5) dim(Bsims) # 3 3 10000
Samples the matrix variate type II confluent hypergeometric kind two distribution.
rmatrixCHIIkind2(n, nu, alpha, beta, theta = 1, p)
rmatrixCHIIkind2(n, nu, alpha, beta, theta = 1, p)
n |
sample size, a positive integer |
nu |
shape parameter, a positive number; if |
alpha , beta
|
shape parameters; |
theta |
scale parameter, a positive number |
p |
dimension (order of the sampled matrices), an integer greater than or equal to one |
A numeric three-dimensional array; simulations are stacked along the third dimension.
A. K. Gupta & D. K. Nagar. Matrix Variate Distributions. Chapman & Hall/CRC, Boca Raton (2000).
nu <- 5; alpha <- 13; beta <- 4; theta <- 2; p <- 2 sims <- rmatrixCHIIkind2(50000, nu, alpha, beta, theta, p) # simulations of the trace trsims <- apply(sims, 3, function(X) sum(diag(X))) mean(trsims) p * theta * nu * (nu+(p+1)/2-beta) / (alpha+nu+(p+1)/2-beta)
nu <- 5; alpha <- 13; beta <- 4; theta <- 2; p <- 2 sims <- rmatrixCHIIkind2(50000, nu, alpha, beta, theta, p) # simulations of the trace trsims <- apply(sims, 3, function(X) sum(diag(X))) mean(trsims) p * theta * nu * (nu+(p+1)/2-beta) / (alpha+nu+(p+1)/2-beta)
Samples the matrix variate type I confluent hypergeometric kind two distribution.
rmatrixCHIkind2(n, nu, alpha, beta, theta = 1, p)
rmatrixCHIkind2(n, nu, alpha, beta, theta = 1, p)
n |
sample size, a positive integer |
nu |
shape parameter, a positive number; if |
alpha , beta
|
shape parameters; |
theta |
scale parameter, a positive number |
p |
dimension (order of the sampled matrices), an integer greater than or equal to one |
A numeric three-dimensional array; simulations are stacked along the third dimension.
A. K. Gupta & D. K. Nagar. Matrix Variate Distributions. Chapman & Hall/CRC, Boca Raton (2000).
nu <- 5; alpha <- 13; beta <- 4; theta <- 2; p <- 2 sims <- rmatrixCHIkind2(50000, nu, alpha, beta, theta, p) # simulations of the trace trsims <- apply(sims, 3, function(X) sum(diag(X))) mean(trsims) p * theta * nu * (nu+(p+1)/2-beta) / (alpha-nu-(p+1)/2)
nu <- 5; alpha <- 13; beta <- 4; theta <- 2; p <- 2 sims <- rmatrixCHIkind2(50000, nu, alpha, beta, theta, p) # simulations of the trace trsims <- apply(sims, 3, function(X) sum(diag(X))) mean(trsims) p * theta * nu * (nu+(p+1)/2-beta) / (alpha-nu-(p+1)/2)
Samples the matrix variate confluent hypergometric kind one distribution.
rmatrixCHkind1(n, nu, alpha, beta, theta = 1, Sigma = NULL, p, checkSymmetry = TRUE)
rmatrixCHkind1(n, nu, alpha, beta, theta = 1, Sigma = NULL, p, checkSymmetry = TRUE)
n |
sample size, a positive integer |
nu |
shape parameter, a positive number; if |
alpha , beta
|
shape parameters with the following constraints:
|
theta |
scale parameter, a positive number |
Sigma |
scale matrix, a symmetric positive definite matrix, or
|
p |
if |
checkSymmetry |
logical, whether to check that |
A numeric three-dimensional array; simulations are stacked along the third dimension.
For alpha = beta
, this is the matrix variate Gamma distribution
with parameters nu
, theta
, Sigma
.
Gupta & al. Properties of Matrix Variate Confluent Hypergeometric Function Distribution. Journal of Probability and Statistics vol. 2016, Article ID 2374907, 12 pages, 2016.
nu <- 5; alpha <- 10; beta <- 12; theta <- 2; p <- 3; Sigma <- toeplitz(3:1) CHsims <- rmatrixCHkind1(10000, nu, alpha, beta, theta, Sigma) # simulations of the trace sims <- apply(CHsims, 3, function(X) sum(diag(X))) mean(sims) theta * nu * (nu-beta+(p+1)/2) / (nu-alpha+(p+1)/2) * sum(diag(Sigma))
nu <- 5; alpha <- 10; beta <- 12; theta <- 2; p <- 3; Sigma <- toeplitz(3:1) CHsims <- rmatrixCHkind1(10000, nu, alpha, beta, theta, Sigma) # simulations of the trace sims <- apply(CHsims, 3, function(X) sum(diag(X))) mean(sims) theta * nu * (nu-beta+(p+1)/2) / (nu-alpha+(p+1)/2) * sum(diag(Sigma))
Samples a matrix Gamma distribution.
rmatrixgamma(n, nu, theta, Sigma = NULL, p, checkSymmetry = TRUE)
rmatrixgamma(n, nu, theta, Sigma = NULL, p, checkSymmetry = TRUE)
n |
sample size, a positive integer |
nu |
shape parameter, a positive number; if |
theta |
scale parameter, a positive number |
Sigma |
scale matrix, a symmetric positive definite matrix, or
|
p |
if |
checkSymmetry |
logical, whether to check that |
This is the distribution of θ/2×S where S ~ Wp(2ν,Σ).
A numeric three-dimensional array; simulations are stacked along the third dimension.
Gupta & al. Properties of Matrix Variate Confluent Hypergeometric Function Distribution. Journal of Probability and Statistics vol. 2016, Article ID 2374907, 12 pages, 2016.
nu <- 3; theta <- 4; Sigma <- toeplitz(2:1) Gsims <- rmatrixgamma(10000, nu, theta, Sigma) apply(Gsims, c(1,2), mean) # should be nu * theta * Sigma nu * theta * Sigma
nu <- 3; theta <- 4; Sigma <- toeplitz(2:1) Gsims <- rmatrixgamma(10000, nu, theta, Sigma) apply(Gsims, c(1,2), mean) # should be nu * theta * Sigma nu * theta * Sigma
Samples the matrix inverted-t distribution.
rmatrixit(n, nu, M, U, V, checkSymmetry = TRUE, keep = TRUE)
rmatrixit(n, nu, M, U, V, checkSymmetry = TRUE, keep = TRUE)
n |
sample size, a positive integer |
nu |
degrees of freedom, any positive number or an
integer strictly greater than |
M |
mean matrix, without constraints |
U |
columns covariance matrix, a positive semidefinite matrix of order equal
to |
V |
rows covariance matrix, a positive semidefinite matrix of order equal
to |
checkSymmetry |
logical, whether to check the symmetry of |
keep |
logical, whether to return an array with class keep |
A numeric three-dimensional array; simulations are stacked along the third dimension (see example).
nu <- 0 m <- 2 p <- 3 M <- matrix(1, m, p) U <- toeplitz(m:1) V <- toeplitz(p:1) ITsims <- rmatrixit(10000, nu, M, U, V) dim(ITsims) # 2 3 10000 apply(ITsims, 1:2, mean) # approximates M vecITsims <- t(apply(ITsims, 3, function(X) c(t(X)))) round(cov(vecITsims),2) # approximates 1/(nu+m+p-1) * kronecker(U,V)
nu <- 0 m <- 2 p <- 3 M <- matrix(1, m, p) U <- toeplitz(m:1) V <- toeplitz(p:1) ITsims <- rmatrixit(10000, nu, M, U, V) dim(ITsims) # 2 3 10000 apply(ITsims, 1:2, mean) # approximates M vecITsims <- t(apply(ITsims, 3, function(X) c(t(X)))) round(cov(vecITsims),2) # approximates 1/(nu+m+p-1) * kronecker(U,V)
Samples the matrix normal distribution.
rmatrixnormal(n, M, U, V, checkSymmetry = TRUE, keep = TRUE)
rmatrixnormal(n, M, U, V, checkSymmetry = TRUE, keep = TRUE)
n |
sample size, a positive integer |
M |
mean matrix, without constraints |
U |
columns covariance matrix, a positive semidefinite matrix of order equal
to |
V |
rows covariance matrix, a positive semidefinite matrix of order equal
to |
checkSymmetry |
logical, whether to check the symmetry of |
keep |
logical, whether to return an array with class keep |
A numeric three-dimensional array; simulations are stacked along the third dimension (see example).
m <- 3 p <- 2 M <- matrix(1, m, p) U <- toeplitz(m:1) V <- toeplitz(p:1) MNsims <- rmatrixnormal(10000, M, U, V) dim(MNsims) # 3 2 10000 apply(MNsims, 1:2, mean) # approximates M vecMNsims <- t(apply(MNsims, 3, function(X) c(t(X)))) round(cov(vecMNsims)) # approximates kronecker(U,V)
m <- 3 p <- 2 M <- matrix(1, m, p) U <- toeplitz(m:1) V <- toeplitz(p:1) MNsims <- rmatrixnormal(10000, M, U, V) dim(MNsims) # 3 2 10000 apply(MNsims, 1:2, mean) # approximates M vecMNsims <- t(apply(MNsims, 3, function(X) c(t(X)))) round(cov(vecMNsims)) # approximates kronecker(U,V)
Samples the matrix t-distribution.
rmatrixt(n, nu, M, U, V, checkSymmetry = TRUE, keep = TRUE)
rmatrixt(n, nu, M, U, V, checkSymmetry = TRUE, keep = TRUE)
n |
sample size, a positive integer |
nu |
degrees of freedom, a positive number |
M |
mean matrix, without constraints |
U |
columns covariance matrix, a positive semidefinite matrix of order equal
to |
V |
rows covariance matrix, a positive semidefinite matrix of order equal
to |
checkSymmetry |
logical, whether to check the symmetry of |
keep |
logical, whether to return an array with class keep |
A numeric three-dimensional array; simulations are stacked along the third dimension (see example).
When p=1
and V=nu
or when m=1
and U=nu
, the
distribution is the multivariate t-distribution.
nu <- 4 m <- 2 p <- 3 M <- matrix(1, m, p) U <- toeplitz(m:1) V <- toeplitz(p:1) Tsims <- rmatrixt(10000, nu, M, U, V) dim(Tsims) # 2 3 10000 apply(Tsims, 1:2, mean) # approximates M vecTsims <- t(apply(Tsims, 3, function(X) c(t(X)))) round(cov(vecTsims), 1) # approximates 1/(nu-2) * kronecker(U,V) ## the `keep` class is nice when m=1 or p=1: Tsims <- rmatrixt(2, nu, M=1:3, U=diag(3), V=1) Tsims[,,1] # dimensions 3 1 # without `keep`, dimensions are lost: rmatrixt(2, nu, M=1:3, U=diag(3), V=1, keep=FALSE)[,,1]
nu <- 4 m <- 2 p <- 3 M <- matrix(1, m, p) U <- toeplitz(m:1) V <- toeplitz(p:1) Tsims <- rmatrixt(10000, nu, M, U, V) dim(Tsims) # 2 3 10000 apply(Tsims, 1:2, mean) # approximates M vecTsims <- t(apply(Tsims, 3, function(X) c(t(X)))) round(cov(vecTsims), 1) # approximates 1/(nu-2) * kronecker(U,V) ## the `keep` class is nice when m=1 or p=1: Tsims <- rmatrixt(2, nu, M=1:3, U=diag(3), V=1) Tsims[,,1] # dimensions 3 1 # without `keep`, dimensions are lost: rmatrixt(2, nu, M=1:3, U=diag(3), V=1, keep=FALSE)[,,1]
Samples a Wishart distribution.
rwishart(n, nu, Sigma, Theta = NULL, epsilon = 0, checkSymmetry = TRUE)
rwishart(n, nu, Sigma, Theta = NULL, epsilon = 0, checkSymmetry = TRUE)
n |
sample size, a positive integer |
nu |
degrees of freedom, a positive number;
if |
Sigma |
scale matrix, a positive semidefinite real matrix |
Theta |
noncentrality parameter, a positive semidefinite real matrix of
same order as |
epsilon |
a number involved in the algorithm only if it positive; its role is to guarantee the invertibility of the sampled matrices; see Details |
checkSymmetry |
logical, whether to check the symmetry of |
The argument epsilon
is a threshold whose role is to guarantee
that the algorithm samples invertible matrices when nu > p-1
and
Sigma
is positive definite.
The sampled matrices are theoretically invertible under these conditions,
but due to numerical issues, they are not always invertible when
nu
is close to p-1
, i.e. when nu-p+1
is small.
In this case, the chi-squared distributions involved in the algorithm can
generate zero values or values close to zero, yielding the non-invertibility
of the sampled matrices. These values are replaced with epsilon
if they are
smaller than epsilon
.
A numeric three-dimensional array; simulations are stacked along the third dimension (see example).
A sampled Wishart matrix is always positive semidefinite.
It is positive definite if nu > p-1
and Sigma
is positive
definite, in theory (see Details).
In the noncentral case, i.e. when Theta
is not null, the Ahdida & Alfonsi
algorithm is used if nu
is not an integer and p-1 < nu < 2p-1
, or
if nu = p-1
. The simulations are slower in this case.
A. Ahdida & A. Alfonsi. Exact and high-order discretization schemes for Wishart processes and their affine extensions. The Annals of Applied Probability 23, 2013, 1025-1073.
nu <- 4 p <- 3 Sigma <- toeplitz(p:1) Theta <- diag(p) Wsims <- rwishart(10000, nu, Sigma, Theta) dim(Wsims) # 3 3 10000 apply(Wsims, 1:2, mean) # approximately nu*Sigma+Theta # the epsilon argument: Wsims_det <- apply(rwishart(10000, nu=p-1+0.001, Sigma), 3, det) length(which(Wsims_det < .Machine$double.eps)) Wsims_det <- apply(rwishart(10000, nu=p-1+0.001, Sigma, epsilon=1e-8), 3, det) length(which(Wsims_det < .Machine$double.eps))
nu <- 4 p <- 3 Sigma <- toeplitz(p:1) Theta <- diag(p) Wsims <- rwishart(10000, nu, Sigma, Theta) dim(Wsims) # 3 3 10000 apply(Wsims, 1:2, mean) # approximately nu*Sigma+Theta # the epsilon argument: Wsims_det <- apply(rwishart(10000, nu=p-1+0.001, Sigma), 3, det) length(which(Wsims_det < .Machine$double.eps)) Wsims_det <- apply(rwishart(10000, nu=p-1+0.001, Sigma, epsilon=1e-8), 3, det) length(which(Wsims_det < .Machine$double.eps))
Samples the lower triangular Cholesky factor of a Wishart random matrix.
rwishart_chol(n, nu, Sigma, epsilon = 0)
rwishart_chol(n, nu, Sigma, epsilon = 0)
n |
sample size, a positive integer |
nu |
degrees of freedom, a number strictly greater than |
Sigma |
scale matrix, a positive definite real matrix |
epsilon |
a number involved in the algorithm only if it positive; its role is to guarantee the invertibility of the sampled matrices; see Details |
The argument epsilon
is a threshold whose role is to guarantee
that the algorithm samples invertible matrices.
The matrices sampled by the algorithm are theoretically invertible.
However, because of numerical precision, they are not always invertible when
nu
is close to p-1
, i.e. when nu-p+1
is small. In this case,
the simulations of chi-squared distributions involved in the algorithm can
generate zero values or values close to zero, yielding the non-invertibility
of the sampled matrices. These values are replaced with epsilon
if they are
smaller than epsilon
.
A numeric three-dimensional array; simulations are stacked along the third dimension (see example).
nu <- 4 p <- 3 Sigma <- diag(p) Wsims <- rwishart_chol(10000, nu, Sigma) dim(Wsims) # 3 3 10000 Wsims[,,1]
nu <- 4 p <- 3 Sigma <- diag(p) Wsims <- rwishart_chol(10000, nu, Sigma) dim(Wsims) # 3 3 10000 Wsims[,,1]