Last chance! 50% off unlimited learning
Sale ends in
## Simulation and density evaluation for p = 2
# Parameters
n <- 1e3
p <- 2
theta <- c(rep(0, p - 1), 1)
mu <- c(rep(0, p - 2), 1)
kappa_V <- 2
kappa_U <- 0.1
# The vMF scaled angular function
g_scaled <- function(t, log) {
g_vMF(t, p = p - 1, kappa = kappa_V, scaled = TRUE, log = log)
}
# Cosine density for the vMF distribution
d_V <- function(v, log) {
log_dens <- g_scaled(v, log = log) + (p - 3)/2 * log(1 - v^2)
switch(log + 1, exp(log_dens), log_dens)
}
# Multivariate signs density based on a vMF
d_U <- function(x, log) d_vMF(x = x, mu = mu, kappa = kappa_U, log = log)
# Simulation functions
r_V <- function(n) r_g_vMF(n = n, p = p, kappa = kappa_V)
r_U <- function(n) r_vMF(n = n, mu = mu, kappa = kappa_U)
# Sample and color according to density
x <- r_tang_norm(n = n, theta = theta, r_V = r_V, r_U = r_U)
r <- runif(n, 0.95, 1.05) # Radius perturbation to improve visualization
col <- viridisLite::viridis(n)
dens <- d_tang_norm(x = x, theta = theta, g_scaled = g_scaled, d_U = d_U)
# dens <- d_tang_norm(x = x, theta = theta, d_V = d_V, d_U = d_U) # The same
plot(r * x, pch = 16, col = col[rank(dens)])
## Simulation and density evaluation for p = 3
# Parameters
p <- 3
n <- 5e3
theta <- c(rep(0, p - 1), 1)
mu <- c(rep(0, p - 2), 1)
kappa_V <- 2
kappa_U <- 2
# Sample and color according to density
x <- r_tang_norm(n = n, theta = theta, r_V = r_V, r_U = r_U)
col <- viridisLite::viridis(n)
dens <- d_tang_norm(x = x, theta = theta, g_scaled = g_scaled, d_U = d_U)
if (requireNamespace("rgl")) {
rgl::plot3d(x, col = col[rank(dens)], size = 5)
}
## A non-vMF angular function: g(t) = 1 - t^2. It is sssociated to the
## Beta(1/2, (p + 1)/2) distribution.
# Scaled angular function
g_scaled <- function(t, log) {
log_c_g <- lgamma(0.5 * p) + log(0.5 * p / (p - 1)) - 0.5 * p * log(pi)
log_g <- log_c_g + log(1 - t^2)
switch(log + 1, exp(log_g), log_g)
}
# Cosine density
d_V <- function(v, log) {
log_dens <- w_p(p = p - 1, log = TRUE) + g_scaled(t = v, log = TRUE) +
(0.5 * (p - 3)) * log(1 - v^2)
switch(log + 1, exp(log_dens), log_dens)
}
# Simulation
r_V <- function(n) {
sample(x = c(-1, 1), size = n, replace = TRUE) *
sqrt(rbeta(n = n, shape1 = 0.5, shape2 = 0.5 * (p + 1)))
}
# Sample and color according to density
r_U <- function(n) r_unif_sphere(n = n, p = p - 1)
x <- r_tang_norm(n = n, theta = theta, r_V = r_V, r_U = r_U)
col <- viridisLite::viridis(n)
dens <- d_tang_norm(x = x, theta = theta, d_V = d_V, d_U = d_unif_sphere)
# dens <- d_tang_norm(x = x, theta = theta, g_scaled = g_scaled,
# d_U = d_unif_sphere) # The same
if (requireNamespace("rgl")) {
rgl::plot3d(x, col = col[rank(dens)], size = 5)
}
Run the code above in your browser using DataLab