Last chance! 50% off unlimited learning
Sale ends in
Density and simulation of the von Mises--Fisher (vMF)
distribution on
The angular function of the vMF is
d_vMF(x, mu, kappa, log = FALSE)c_vMF(p, kappa, log = FALSE)
r_vMF(n, mu, kappa)
g_vMF(t, p, kappa, scaled = TRUE, log = FALSE)
r_g_vMF(n, p, kappa)
locations in c(nx, p)
or a vector of length p
. Normalized
internally if required (with a warning
message).
the directional mean p
.
concentration parameter
flag to indicate if the logarithm of the density (or the normalizing constant) is to be computed.
dimension of the ambient space
sample size, a positive integer.
a vector with values in
whether to scale the angular function by the von Mises--Fisher
normalizing constant. Defaults to TRUE
.
Depending on the function:
d_vMF
: a vector of length nx
or 1
with the
evaluated density at x
.
r_vMF
: a matrix of size c(n, p)
with the random sample.
c_vMF
: the normalizing constant.
g_vMF
: a vector of size length(t)
with the evaluated
angular function.
r_g_vMF
: a vector of length n
containing simulated
values from the cosines density associated to the angular function.
r_g_vMF
implements algorithm VM in Wood (1994). c_vMF
is
vectorized on p
and kappa
.
Wood, A. T. A. (1994) Simulation of the von Mises Fisher distribution. Commun. Stat. Simulat., 23(1):157--164.
# NOT RUN {
# Simulation and density evaluation for p = 2
mu <- c(0, 1)
kappa <- 2
n <- 1e3
x <- r_vMF(n = n, mu = mu, kappa = kappa)
col <- viridisLite::viridis(n)
r <- runif(n, 0.95, 1.05) # Radius perturbation to improve visualization
plot(r * x, pch = 16, col = col[rank(d_vMF(x = x, mu = mu, kappa = kappa))])
# Simulation and density evaluation for p = 3
mu <- c(0, 0, 1)
kappa <- 2
x <- r_vMF(n = n, mu = mu, kappa = kappa)
rgl::plot3d(x, col = col[rank(d_vMF(x = x, mu = mu, kappa = kappa))],
size = 5)
# Cosines density
g_tilde <- function(t, p, kappa) {
exp(w_p(p = p - 1, log = TRUE) +
g_vMF(t = t, p = p, kappa = kappa, scaled = TRUE, log = TRUE) +
((p - 3) / 2) * log(1 - t^2))
}
# Simulated data from the cosines density
n <- 1e3
p <- 3
kappa <- 2
hist(r_g_vMF(n = n, p = p, kappa = kappa), breaks = seq(-1, 1, l = 20),
probability = TRUE, main = "Simulated data from g_vMF", xlab = "t")
t <- seq(-1, 1, by = 0.01)
lines(t, g_tilde(t = t, p = p, kappa = kappa))
# Cosine density as a function of the dimension
M <- 100
col <- viridisLite::viridis(M)
plot(t, g_tilde(t = t, p = 2, kappa = kappa), col = col[2], type = "l",
ylab = "Density")
for (p in 3:M) {
lines(t, g_tilde(t = t, p = p, kappa = kappa), col = col[p])
}
# }
Run the code above in your browser using DataLab