# Plotting functions for the examples
add_approx_dens <- function(x, dfs, weights, ncps) {
lines(x, d_wschisq(x, weights = weights, dfs = dfs, ncps = ncps,
method = "SW", exact_chisq = FALSE), col = 3)
lines(x, d_wschisq(x, weights = weights, dfs = dfs, ncps = ncps,
method = "HBE", exact_chisq = FALSE), col = 4)
lines(x, d_wschisq(x, weights = weights, dfs = dfs, ncps = ncps,
method = "I", exact_chisq = TRUE), col = 2)
legend("topright", legend = c("True", "SW", "HBE", "I"), lwd = 2,
col = c(1, 3:4, 2))
}
add_approx_distr <- function(x, dfs, weights, ncps, ...) {
lines(x, p_wschisq(x, weights = weights, dfs = dfs, ncps = ncps,
method = "SW", exact_chisq = FALSE), col = 3)
lines(x, p_wschisq(x, weights = weights, dfs = dfs, ncps = ncps,
method = "HBE", exact_chisq = FALSE), col = 4)
lines(x, p_wschisq(x, weights = weights, dfs = dfs, ncps = ncps,
method = "MC", exact_chisq = FALSE), col = 5,
type = "s")
lines(x, p_wschisq(x, weights = weights, dfs = dfs, ncps = ncps,
method = "I", exact_chisq = TRUE), col = 2)
legend("bottomright", legend = c("True", "SW", "HBE", "MC", "I"), lwd = 2,
col = c(1, 3:5, 2))
}
add_approx_quant <- function(u, dfs, weights, ncps, ...) {
lines(u, q_wschisq(u, weights = weights, dfs = dfs, ncps = ncps,
method = "SW", exact_chisq = FALSE), col = 3)
lines(u, q_wschisq(u, weights = weights, dfs = dfs, ncps = ncps,
method = "HBE", exact_chisq = FALSE), col = 4)
lines(u, q_wschisq(u, weights = weights, dfs = dfs, ncps = ncps,
method = "MC", exact_chisq = FALSE), col = 5,
type = "s")
lines(u, q_wschisq(u, weights = weights, dfs = dfs, ncps = ncps,
method = "I", exact_chisq = TRUE), col = 2)
legend("topleft", legend = c("True", "SW", "HBE", "MC", "I"), lwd = 2,
col = c(1, 3:5, 2))
}
# Validation plots for density, distribution, and quantile functions
u <- seq(0.01, 0.99, l = 100)
old_par <- par(mfrow = c(1, 3))
# Case 1: 1 * ChiSq_3(0) + 1 * ChiSq_3(0) = ChiSq_6(0)
weights <- c(1, 1)
dfs <- c(3, 3)
ncps <- 0
x <- seq(-1, 30, l = 100)
main <- expression(1 * chi[3]^2 * (0) + 1 * chi[3]^2 * (0))
plot(x, dchisq(x, df = 6), type = "l", main = main, ylab = "Density")
add_approx_dens(x = x, weights = weights, dfs = dfs, ncps = ncps)
plot(x, pchisq(x, df = 6), type = "l", main = main, ylab = "Distribution")
add_approx_distr(x = x, weights = weights, dfs = dfs, ncps = ncps)
plot(u, qchisq(u, df = 6), type = "l", main = main, ylab = "Quantile")
add_approx_quant(u = u, weights = weights, dfs = dfs, ncps = ncps)
# \donttest{
# Case 2: 2 * ChiSq_3(1) + 1 * ChiSq_6(0.5) + 0.5 * ChiSq_12(0.25)
weights <- c(2, 1, 0.5)
dfs <- c(3, 6, 12)
ncps <- c(1, 0.5, 0.25)
x <- seq(0, 70, l = 100)
main <- expression(2 * chi[3]^2 * (1)+ 1 * chi[6]^2 * (0.5) +
0.5 * chi[12]^2 * (0.25))
samp <- r_wschisq(n = 1e4, weights = weights, dfs = dfs, ncps = ncps)
hist(samp, breaks = 50, freq = FALSE, main = main, ylab = "Density",
xlim = range(x), xlab = "x"); box()
add_approx_dens(x = x, weights = weights, dfs = dfs, ncps = ncps)
plot(x, ecdf(samp)(x), main = main, ylab = "Distribution", type = "s")
add_approx_distr(x = x, weights = weights, dfs = dfs, ncps = ncps)
plot(u, quantile(samp, probs = u), type = "s", main = main,
ylab = "Quantile")
add_approx_quant(u = u, weights = weights, dfs = dfs, ncps = ncps)
# Case 3: \sum_{k = 1}^K k^(-3) * ChiSq_{5k}(1 / k^2)
K <- 1e2
weights<- 1 / (1:K)^3
dfs <- 5 * 1:K
ncps <- 1 / (1:K)^2
x <- seq(0, 25, l = 100)
main <- substitute(sum(k^(-3) * chi[5 * k]^2 * (1 / k^2), k == 1, K),
list(K = K))
samp <- r_wschisq(n = 1e4, weights = weights, dfs = dfs, ncps = ncps)
hist(samp, breaks = 50, freq = FALSE, main = main, ylab = "Density",
xlim = range(x), xlab = "x"); box()
add_approx_dens(x = x, weights = weights, dfs = dfs, ncps = ncps)
plot(x, ecdf(samp)(x), main = main, ylab = "Distribution", type = "s")
add_approx_distr(x = x, weights = weights, dfs = dfs, ncps = ncps)
plot(u, quantile(samp, probs = u), type = "s", main = main,
ylab = "Quantile")
add_approx_quant(u = u, weights = weights, dfs = dfs, ncps = ncps)
par(old_par)
# Cutoffs for infinite series of the last example
K <- 1e7
log_weights<- -3 * log(1:K)
log_dfs <- log(5) + log(1:K)
(cutoff <- cutoff_wschisq(thre = 10^(-(1:4)), weights = log_weights,
dfs = log_dfs, log = TRUE))
# Approximation
x <- seq(0, 25, l = 100)
l <- length(cutoff$mean)
main <- expression(sum(k^(-3) * chi[5 * k]^2, k == 1, K))
col <- viridisLite::viridis(l)
plot(x, d_wschisq(x, weights = exp(log_weights[1:cutoff$mean[l]]),
dfs = exp(log_dfs[1:cutoff$mean[l]])), type = "l",
ylab = "Density", col = col[l], lwd = 3)
for(i in rev(seq_along(cutoff$mean)[-l])) {
lines(x, d_wschisq(x, weights = exp(log_weights[1:cutoff$mean[i]]),
dfs = exp(log_dfs[1:cutoff$mean[i]])), col = col[i])
}
legend("topright", legend = paste0(rownames(cutoff), " (", cutoff$mean, ")"),
lwd = 2, col = col)
# }
Run the code above in your browser using DataLab