kappa1 <- c(1, 2, 3)
kappa2 <- c(1, 6, 5)
kappa3 <- c(0, 1, 2)
mu1 <- c(1, 2, 5)
mu2 <- c(0, 1, 3)
x <- diag(2, 2)
n <- 10
# when x is a bivariate vector and parameters are all scalars,
# dvmcos returns single density
dvmcos(x[1, ], kappa1[1], kappa2[1], kappa3[1], mu1[1], mu2[1])
# when x is a two column matrix and parameters are all scalars,
# dmvsin returns a vector of densities calculated at the rows of
# x with the same parameters
dvmcos(x, kappa1[1], kappa2[1], kappa3[1], mu1[1], mu2[1])
# if x is a bivariate vector and at least one of the parameters is
# a vector, all parameters are recycled to the same length, and
# dvmcos returns a vector with ith element being the density
# evaluated at x with parameter values kappa1[i], kappa2[i],
# kappa3[i], mu1[i] and mu2[i]
dvmcos(x[1, ], kappa1, kappa2, kappa3, mu1, mu2)
# if x is a two column matrix and at least one of the parameters is
# a vector, rows of x and the parameters are recycled to the same
# length, and dvmcos returns a vector with ith element being the
# density evaluated at ith row of x with parameter values kappa1[i],
# kappa2[i], # kappa3[i], mu1[i] and mu2[i]
dvmcos(x, kappa1, kappa2, kappa3, mu1, mu2)
# when parameters are all scalars, number of observations generated
# by rvmcos is n
rvmcos(n, kappa1[1], kappa2[1], kappa3[1], mu1[1], mu2[1])
# when at least one of the parameters is a vector, all parameters are
# recycled to the same length, n is ignored, and the number of
# observations generated by rvmcos is the same as the length of the
# recycled vectors
rvmcos(n, kappa1, kappa2, kappa3, mu1, mu2)
# \donttest{
## Visualizing (quasi) Monte Carlo based approximations of
## the normalizing constant through density evaluations.
# "good" setup, where the analytic formula for C_c can be
# calculated without numerical issues
# kappa1 = 1, kappa2 = 1, kappa3 = -2, mu1 = pi, mu2 = pi
n_qrnd <- (1:500)*20
# analytic
good.a <- dvmcos(c(3,3), 1, 1, -2, pi, pi, log=TRUE)
# using quasi Monte Carlo
good.q <- sapply(n_qrnd,
function(j)
dvmcos(c(3,3), 1, 1, -2, pi, pi,
log=TRUE, n_qrnd = j,
force_approx_const = TRUE))
# using ordinary Monte Carlo
set.seed(1)
good.r <- sapply(n_qrnd,
function(j)
dvmcos(c(3,3), 1, 1, -2, pi, pi,
log=TRUE,
qrnd = matrix(runif(2*j), ncol = 2),
force_approx_const = TRUE))
plot(n_qrnd, good.q, ylim = range(good.a, good.q, good.r),
col = "orange", type = "l",
ylab = "",
main = "dvmcos(c(3,3), 1, 1, -2, pi, pi, log = TRUE)")
points(n_qrnd, good.r, col = "skyblue", type = "l")
abline(h = good.a, lty = 2, col = "grey")
legend("topright",
legend = c("Sobol", "Random", "Analytic"),
col = c("orange", "skyblue", "grey"),
lty = c(1, 1, 2))
# "bad" setup, where the calculating C_c
# numerically using the analytic formula is problematic
# kappa1 = 100, kappa2 = 100, kappa3 = -200, mu1 = pi, mu2 = pi
n_qrnd <- (1:500)*20
# using quasi Monte Carlo
bad.q <- sapply(n_qrnd,
function(j)
dvmcos(c(3,3), 100, 100, -200, pi, pi,
log=TRUE, n_qrnd = j,
force_approx_const = TRUE))
# using ordinary Monte Carlo
set.seed(1)
bad.r <- sapply(n_qrnd,
function(j)
dvmcos(c(3,3), 100, 100, -200, pi, pi,
log=TRUE,
qrnd = matrix(runif(2*j), ncol = 2),
force_approx_const = TRUE))
plot(n_qrnd, bad.q, ylim = range(bad.q, bad.r),
col = "orange", type = "l",
ylab = "",
main = "dvmcos(c(3,3), 100, 100, -200, pi, pi, log = TRUE)")
points(n_qrnd, bad.r, col = "skyblue", type = "l")
legend("topright",
legend = c("Sobol", "Random"),
col = c("orange", "skyblue"), lty = 1)
# }
Run the code above in your browser using DataLab