-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLattice_mvt.R
More file actions
30 lines (30 loc) · 1.02 KB
/
Lattice_mvt.R
File metadata and controls
30 lines (30 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Lattice multivariate t-distribution.
#
# Generates a grid of points coming from a multivariate t-distribution.
# @param mean Numeric vector indicating the multivariate mean.
# @param cvar A matrix which specifies the covariance matrix.
# @param df Numeric value indicating the degrees of freedom for the multivariate
# t-distribution.
# @param m Numeric value. Number of draws = b^m.
# @param b Numeric value indicating the base (default = 2).
# @return Matrix of lattice points drawn from a multivariate t-distribution.
# Each row is a draw.
Lattice_mvt <- function (mean, cvar, df, m, b=2) {
# Dimension
dim <- length(mean)
# Generate lattice from standard normal
lattice <- Lat(K = dim, b, m)
mean <- t(mean)
X <- matrix(NA, nrow(lattice), dim)
A <- chol(cvar)
# Transform to multivariate t distribution
for (i in 1:nrow(lattice)) {
inv <- stats::rgamma(1, df / 2)
invw <- inv / (df / 2)
W <- 1 / invw
Z <- lattice[i, ]
r <- mean + sqrt(W) * (Z %*% t(A))
X[i, ] <- t(r)
}
return (X)
}