get_elts {genscore} | R Documentation |
The function wrapper to get the elements necessary for calculations for all settings.
Description
The function wrapper to get the elements necessary for calculations for all settings.
Usage
get_elts(
h_hp,
x,
setting,
domain,
centered = TRUE,
profiled_if_noncenter = TRUE,
scale = "",
diagonal_multiplier = 1,
use_C = TRUE,
tol = .Machine$double.eps^0.5,
unif_dist = NULL
)
Arguments
h_hp |
A function that returns a list containing |
x |
An |
setting |
A string that indicates the distribution type, must be one of |
domain |
A list returned from |
centered |
A boolean, whether in the centered setting(assume |
profiled_if_noncenter |
A boolean, whether in the profiled setting ( |
scale |
A string indicating the scaling method. If contains |
diagonal_multiplier |
A number >= 1, the diagonal multiplier. |
use_C |
Optional. A boolean, use C ( |
tol |
Optional. A positive number. If |
unif_dist |
Optional, defaults to |
Details
Computes the \boldsymbol{\Gamma}
matrix and the \boldsymbol{g}
vector for generalized score matching.
Here, \boldsymbol{\Gamma}
is block-diagonal, and in the non-profiled non-centered setting, the j
-th block is composed of \boldsymbol{\Gamma}_{\mathbf{KK},j}
, \boldsymbol{\Gamma}_{\mathbf{K}\boldsymbol{\eta},j}
and its transpose, and finally \boldsymbol{\Gamma}_{\boldsymbol{\eta\eta},j}
. In the centered case, only \boldsymbol{\Gamma}_{\mathbf{KK},j}
is computed. In the profiled non-centered case,
\boldsymbol{\Gamma}_{j}\equiv\boldsymbol{\Gamma}_{\mathbf{KK},j}-\boldsymbol{\Gamma}_{\mathbf{K}\boldsymbol{\eta},j}\boldsymbol{\Gamma}_{\boldsymbol{\eta}\boldsymbol{\eta},j}^{-1}\boldsymbol{\Gamma}_{\mathbf{K}\boldsymbol{\eta}}^{\top}.
Similarly, in the non-profiled non-centered setting, \boldsymbol{g}
can be partitioned p
parts, each with a p
-vector \boldsymbol{g}_{\mathbf{K},j}
and a scalar g_{\boldsymbol{\eta},j}
. In the centered setting, only \boldsymbol{g}_{\mathbf{K},j}
is needed. In the profiled non-centered case,
\boldsymbol{g}_j\equiv\boldsymbol{g}_{\mathbf{K},j}-\boldsymbol{\Gamma}_{\mathbf{K}\boldsymbol{\eta},j}\boldsymbol{\Gamma}_{\boldsymbol{\eta\eta},j}^{-1}g_{\boldsymbol{\eta},j}.
The formulae for the pieces above are
\boldsymbol{\Gamma}_{\mathbf{KK},j}\equiv\frac{1}{n}\sum_{i=1}^nh\left(X_j^{(i)}\right){X_j^{(i)}}^{2a-2}{\boldsymbol{X}^{(i)}}^a{{\boldsymbol{X}^{(i)}}^a}^{\top},
\boldsymbol{\Gamma}_{\mathbf{K}\boldsymbol{\eta},j}\equiv-\frac{1}{n}\sum_{i=1}^nh\left(X_j^{(i)}\right){X_j^{(i)}}^{a+b-2}{\boldsymbol{X}^{(i)}}^a,
\boldsymbol{\Gamma}_{\boldsymbol{\eta\eta},j}\equiv\frac{1}{n}\sum_{i=1}^nh\left(X_j^{(i)}\right){X_j^{(i)}}^{2b-2},
\boldsymbol{g}_{\mathbf{K},j}\equiv\frac{1}{n}\sum_{i=1}^n\left(h'\left(X_j^{(i)}\right){X_j^{(i)}}^{a-1}+(a-1)h\left(X_j^{(i)}\right){X_j^{(i)}}^{a-2}\right){\boldsymbol{X}^{(i)}}^a+ah\left(X_j^{(i)}\right){X_j^{(i)}}^{2a-2}\boldsymbol{e}_{j,p},
\boldsymbol{g}_{\boldsymbol{\eta},j}\equiv\frac{1}{n}\sum_{i=1}^n-h'\left(X_j^{(i)}\right){X_j^{(i)}}^{b-1}-(b-1)h\left(X_j^{(i)}\right){X_j^{(i)}}^{b-2},
where \boldsymbol{e}_{j,p}
is the p
-vector with 1 at the j
-th position and 0 elsewhere.
In the profiled non-centered setting, the function also returns t_1
and t_2
defined as
\boldsymbol{t}_1\equiv\boldsymbol{\Gamma}_{\boldsymbol{\eta\eta}}^{-1}\boldsymbol{g}_{\boldsymbol{\eta}},\quad\boldsymbol{t}_2\equiv\boldsymbol{\Gamma}_{\boldsymbol{\eta\eta}}^{-1}\boldsymbol{\Gamma}_{\mathbf{K}\boldsymbol{\eta}}^{\top},
so that \hat{\boldsymbol{\eta}}=\boldsymbol{t}_1-\boldsymbol{t}_2\mathrm{vec}(\hat{\mathbf{K}}).
Value
A list that contains the elements necessary for estimation.
n |
The sample size. |
p |
The dimension. |
centered |
The centered setting or not. Same as input. |
scale |
The scaling method. Same as input. |
diagonal_multiplier |
The diagonal multiplier. Same as input. |
diagonals_with_multiplier |
A vector that contains the diagonal entries of |
domain_type |
The domain type. Same as domain$type in the input. |
setting |
The setting. Same as input. |
g_K |
The |
Gamma_K |
The |
g_eta |
Returned in the non-profiled non-centered setting. The |
Gamma_K_eta |
Returned in the non-profiled non-centered setting. The |
Gamma_eta |
Returned in the non-profiled non-centered setting. The |
t1 , t2 |
Returned in the profiled non-centered setting, where the |
If domain$type == "simplex", the following are also returned.
Gamma_K_jp |
A matrix of size |
Gamma_Kj_etap |
Non-centered only. A matrix of size |
Gamma_Kp_etaj |
Non-centered only. A matrix of size |
Gamma_eta_jp |
Non-centered only. A vector of size |
Examples
n <- 30
p <- 10
eta <- rep(0, p)
K <- diag(p)
dm <- 1 + (1-1/(1+4*exp(1)*max(6*log(p)/n, sqrt(6*log(p)/n))))
# Gaussian on R^p:
domain <- make_domain("R", p=p)
x <- mvtnorm::rmvnorm(n, mean=solve(K, eta), sigma=solve(K))
# Equivalently:
x2 <- gen(n, setting="gaussian", abs=FALSE, eta=eta, K=K, domain=domain, finite_infinity=100,
xinit=NULL, burn_in=1000, thinning=100, verbose=FALSE)
elts <- get_elts(NULL, x, "gaussian", domain, centered=TRUE, scale="norm", diag=dm)
elts <- get_elts(NULL, x, "gaussian", domain, FALSE, profiled=FALSE, scale="sd", diag=dm)
# Gaussian on R_+^p:
domain <- make_domain("R+", p=p)
x <- tmvtnorm::rtmvnorm(n, mean = solve(K, eta), sigma = solve(K),
lower = rep(0, p), upper = rep(Inf, p), algorithm = "gibbs",
burn.in.samples = 100, thinning = 10)
# Equivalently:
x2 <- gen(n, setting="gaussian", abs=FALSE, eta=eta, K=K, domain=domain,
finite_infinity=100, xinit=NULL, burn_in=1000, thinning=100, verbose=FALSE)
h_hp <- get_h_hp("min_pow", 1, 3)
elts <- get_elts(h_hp, x, "gaussian", domain, centered=TRUE, scale="norm", diag=dm)
# Gaussian on sum(x^2) > 1 && sum(x^(1/3)) > 1 with x allowed to be negative
domain <- make_domain("polynomial", p=p, rule="1 && 2",
ineqs=list(list("expression"="sum(x^2)>1", abs=FALSE, nonnegative=FALSE),
list("expression"="sum(x^(1/3))>1", abs=FALSE, nonnegative=FALSE)))
xinit <- rep(sqrt(2/p), p)
x <- gen(n, setting="gaussian", abs=FALSE, eta=eta, K=K, domain=domain, finite_infinity=100,
xinit=xinit, seed=2, burn_in=1000, thinning=100, verbose=FALSE)
h_hp <- get_h_hp("min_pow", 1, 3)
elts <- get_elts(h_hp, x, "gaussian", domain, centered=FALSE,
profiled_if_noncenter=TRUE, scale="", diag=dm)
# exp on ([0, 1] v [2,3])^p
domain <- make_domain("uniform", p=p, lefts=c(0,2), rights=c(1,3))
x <- gen(n, setting="exp", abs=FALSE, eta=eta, K=K, domain=domain,
xinit=NULL, seed=2, burn_in=1000, thinning=100, verbose=FALSE)
h_hp <- get_h_hp("min_pow", 1.5, 3)
elts <- get_elts(h_hp, x, "exp", domain, centered=TRUE, scale="", diag=dm)
elts <- get_elts(h_hp, x, "exp", domain, centered=FALSE,
profiled_if_noncenter=FALSE, scale="", diag=dm)
# gamma on {x1 > 1 && log(1.3) < x2 < 1 && x3 > log(1.3) && ... && xp > log(1.3)}
domain <- make_domain("polynomial", p=p, rule="1 && 2 && 3",
ineqs=list(list("expression"="x1>1", abs=FALSE, nonnegative=TRUE),
list("expression"="x2<1", abs=FALSE, nonnegative=TRUE),
list("expression"="exp(x)>1.3", abs=FALSE, nonnegative=TRUE)))
set.seed(1)
xinit <- c(1.5, 0.5, abs(stats::rnorm(p-2))+log(1.3))
x <- gen(n, setting="gamma", abs=FALSE, eta=eta, K=K, domain=domain, finite_infinity=100,
xinit=xinit, seed=2, burn_in=1000, thinning=100, verbose=FALSE)
h_hp <- get_h_hp("min_pow", 1.5, 3)
elts <- get_elts(h_hp, x, "gamma", domain, centered=TRUE, scale="", diag=dm)
elts <- get_elts(h_hp, x, "gamma", domain, centered=FALSE,
profiled_if_noncenter=FALSE, scale="", diag=dm)
# a0.6_b0.7 on {x in R_+^p: sum(log(x))<2 || (x1^(2/3)-1.3x2^(-3)<1 && exp(x1)+2.3*x2>2)}
domain <- make_domain("polynomial", p=p, rule="1 || (2 && 3)",
ineqs=list(list("expression"="sum(log(x))<2", abs=FALSE, nonnegative=TRUE),
list("expression"="x1^(2/3)-1.3x2^(-3)<1", abs=FALSE, nonnegative=TRUE),
list("expression"="exp(x1)+2.3*x2^2>2", abs=FALSE, nonnegative=TRUE)))
xinit <- rep(1, p)
x <- gen(n, setting="ab_3/5_7/10", abs=FALSE, eta=eta, K=K, domain=domain, finite_infinity=100,
xinit=xinit, seed=2, burn_in=1000, thinning=100, verbose=FALSE)
h_hp <- get_h_hp("min_pow", 1.4, 3)
elts <- get_elts(h_hp, x, "ab_3/5_7/10", domain, centered=TRUE, scale="", diag=dm)
elts <- get_elts(h_hp, x, "ab_3/5_7/10", domain, centered=FALSE,
profiled_if_noncenter=TRUE, scale="", diag=dm)
# log_log model on {x in R_+^p: sum_j j * xj <= 1}
domain <- make_domain("polynomial", p=p,
ineqs=list(list("expression"=paste(paste(sapply(1:p,
function(j){paste(j, "x", j, sep="")}), collapse="+"), "<1"),
abs=FALSE, nonnegative=TRUE)))
x <- gen(n, setting="log_log", abs=FALSE, eta=eta, K=K, domain=domain,
finite_infinity=100, xinit=NULL, seed=2, burn_in=1000, thinning=100,
verbose=FALSE)
h_hp <- get_h_hp("min_pow", 2, 3)
elts <- get_elts(h_hp, x, "log_log", domain, centered=TRUE, scale="", diag=dm)
elts <- get_elts(h_hp, x, "log_log", domain, centered=FALSE,
profiled_if_noncenter=FALSE, scale="", diag=dm)
# Example of using the uniform distance function to boundary as in Liu (2019)
g0 <- function(x) {
row_min <- apply(x, 1, min)
row_which_min <- apply(x, 1, which.min)
dist_to_sum_boundary <- apply(x, 1, function(xx){
(1 - sum(1:p * xx)) / sqrt(p*(p+1)*(2*p+1)/6)})
grad_sum_boundary <- -(1:p) / sqrt(p*(p+1)*(2*p+1)/6)
g0 <- pmin(row_min, dist_to_sum_boundary)
g0d <- t(sapply(1:nrow(x), function(i){
if (row_min[i] < dist_to_sum_boundary[i]){
tmp <- numeric(ncol(x)); tmp[row_which_min[i]] <- 1
} else {tmp <- grad_sum_boundary}
tmp
}))
list("g0"=g0, "g0d"=g0d)
}
elts <- get_elts(NULL, x, "exp", domain, centered=TRUE, profiled_if_noncenter=FALSE,
scale="", diag=dm, unif_dist=g0)
# log_log_sum0 model on the simplex with K having row and column sums 0 (Aitchison model)
domain <- make_domain("simplex", p=p)
K <- -cov_cons("band", p=p, spars=3, eig=1)
diag(K) <- diag(K) - rowSums(K) # So that rowSums(K) == colSums(K) == 0
eigen(K)$val[(p-1):p] # Make sure K has one 0 and p-1 positive eigenvalues
x <- gen(n, setting="log_log_sum0", abs=FALSE, eta=eta, K=K, domain=domain,
xinit=NULL, seed=2, burn_in=1000, thinning=100, verbose=FALSE)
h_hp <- get_h_hp("min_pow", 2, 3)
h_hp_dx <- h_of_dist(h_hp, x, domain) # h and h' applied to distance from x to boundary
# Does not assume K has 0 row and column sums
elts_simplex_0 <- get_elts(h_hp, x, "log_log", domain, centered=TRUE, profiled=FALSE,
scale="", diag=1.5)
# If want K to have row sums and column sums equal to 0 (Aitchison); estimate off-diagonals only
elts_simplex_1 <- get_elts(h_hp, x, "log_log_sum0", domain, centered=FALSE,
profiled=FALSE, scale="", diag=1.5)
# All entries corresponding to the diagonals of K should be 0:
max(abs(sapply(1:p, function(j){c(elts_simplex_1$Gamma_K[j, (j-1)*p+1:p],
elts_simplex_1$Gamma_K[, (j-1)*p+j])})))
max(abs(diag(elts_simplex_1$Gamma_K_eta)))
max(abs(diag(matrix(elts_simplex_1$g_K, nrow=p))))