### Install invgamma package Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Installation commands for the CRAN and development versions of the package. ```R install.packages("invgamma") ``` ```R # install.packages("devtools") devtools::install_github("dkahle/invgamma") ``` -------------------------------- ### Inverse Chi-Squared Distribution Examples Source: https://cran.r-project.org/web/packages/invgamma/refman/invgamma.html Examples demonstrating plotting, integration, and verification of inverse chi-squared distribution functions. ```R s <- seq(0, 3, .01) plot(s, dinvchisq(s, 3), type = 'l') f <- function(x) dinvchisq(x, 3) q <- 2 integrate(f, 0, q) (p <- pinvchisq(q, 3)) qinvchisq(p, 3) # = q mean(rinvchisq(1e5, 3) <= q) f <- function(x) dinvchisq(x, 3, ncp = 2) q <- 1.5 integrate(f, 0, q) (p <- pinvchisq(q, 3, ncp = 2)) qinvchisq(p, 3, ncp = 2) # = q mean(rinvchisq(1e7, 3, ncp = 2) <= q) ``` -------------------------------- ### Inverse Exponential Distribution Examples Source: https://cran.r-project.org/web/packages/invgamma/refman/invgamma.html Examples demonstrating plotting, integration, and verification of inverse exponential distribution functions. ```R s <- seq(0, 10, .01) plot(s, dinvexp(s, 2), type = 'l') f <- function(x) dinvexp(x, 2) q <- 3 integrate(f, 0, q) (p <- pinvexp(q, 2)) qinvexp(p, 2) # = q mean(rinvexp(1e5, 2) <= q) pinvgamma(q, 1, 2) ``` -------------------------------- ### Load Libraries and Set Up Parallel Computing Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Loads necessary R libraries for data manipulation, plotting, and parallel computing. It also sets up a minimal theme for plots and configures furrr for parallel execution using available CPU cores. Ensure tidyverse, patchwork, scales, and furrr are installed. ```R # load tidyverse and related library("tidyverse"); library("patchwork"); library("scales", warn.conflicts = FALSE) theme_set(theme_minimal()); theme_update(panel.grid.minor = element_blank()) # load furrr for parallel computing library("furrr"); furrr_options(seed = TRUE) # plan(multisession(workers = parallelly::availableCores())) ``` -------------------------------- ### Verify sampling consistency Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Verify the implementation using kernel density estimation and quantile plots. ```R plot(density(draws), xlim = c(0,5)) curve(f(x), col = "red", add = TRUE) ``` ```R qqplot( "x" = ppoints(n) |> qinvgamma(shape, rate), "y" = draws, xlab = "Theoretical quantiles", ylab = "Sample quantiles", main = "QQ plot for inverse gamma draws" ) abline(0, 1, col = "red") ``` -------------------------------- ### Generate Parameter Grid for Testing Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Creates a grid of shape and rate parameter values to test the rinvgamma() sampler. It generates values on a logarithmic scale from 10^-4 to 10^4. This grid defines the design space for the Monte Carlo experiment. ```R # set parameter values to test n_grid <- 51 param_vals <- 10^seq(-4, 4, length.out = n_grid) (param_grid <- expand_grid("shape" = param_vals, "rate" = param_vals)) ``` -------------------------------- ### Visualize Parameter Space Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Generates a scatter plot visualizing the parameter space (shape vs. rate) for testing rinvgamma(). Uses logarithmic scales for both axes and custom labels for scientific notation. This plot helps understand the distribution of test points. ```R # make axes labeller fmt <- scales::math_format(10^.x) # make plot ggplot(param_grid, aes(shape, rate)) + geom_point() + scale_x_log10(expression(alpha), n.breaks = 10, labels = fmt(-5:5)) + scale_y_log10(expression(lambda), n.breaks = 10, labels = fmt(-5:5)) + labs("title" = "Parameter Values at Which to Test `rinvgamma()`") + coord_equal() ``` -------------------------------- ### Evaluate inverse gamma quantiles Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Calculate quantiles using qinvgamma. ```R qinvgamma(p, shape, rate) # = q # [1] 2 ``` -------------------------------- ### Inverse Gamma Distribution Analysis Source: https://cran.r-project.org/web/packages/invgamma/refman/invgamma.html Demonstrates plotting, integration, probability calculation, and verification of moments for the inverse gamma distribution. ```R s <- seq(0, 5, .01) plot(s, dinvgamma(s, 7, 10), type = 'l') f <- function(x) dinvgamma(x, 7, 10) q <- 2 integrate(f, 0, q) (p <- pinvgamma(q, 7, 10)) qinvgamma(p, 7, 10) # = q mean(rinvgamma(1e5, 7, 10) <= q) shape <- 3; rate <- 7 x <- rinvgamma(1e6, shape, rate) mean(x) # = rate / (shape - 1) var(x) # = rate^2 / ( (shape - 1)^2 * (shape - 2) ) shape <- 7; rate <- 2.01 x <- rinvgamma(1e6, shape, rate) mean(x) # = rate / (shape - 1) var(x) # = rate^2 / ( (shape - 1)^2 * (shape - 2) ) qnorm(log(.25), log.p = TRUE) qnorm(.25) qinvgamma(log(.25), shape = shape, rate = rate, log.p = TRUE) qinvgamma(.25, shape = shape, rate = rate) ## Not run: `rinvgamma()` warns when shape <= .01 rinvgamma(10, .01, rate) # warns ## End(Not run) ``` -------------------------------- ### Check numerical stability warning Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Demonstrate the warning issued when shape parameters are too small. ```R rinvgamma(10, shape = .001, rate = 7) # Warning: `rinvgamma()` is unreliable for `shape` <= .01. # [1] Inf Inf 1.192692e+213 Inf 3.289218e+167 # [6] Inf Inf 7.899428e+197 3.938612e+97 Inf ``` -------------------------------- ### Perform Kolmogorov-Smirnov test Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Use a KS test to check the sampling accuracy. ```R ks.test( draws, function(p) pinvgamma(p, shape, rate) ) # # Asymptotic one-sample Kolmogorov-Smirnov test # # data: draws # D = 0.0029822, p-value = 0.3361 # alternative hypothesis: two-sided ``` -------------------------------- ### Visualize KS Test Results Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Creates a scatter plot showing the shape and rate parameters, colored by the p-value obtained from the KS test. The color scale is binned at 0.05 to highlight rejections at the 5% significance level. This visualization helps identify regions where the sampler is unreliable. ```R ggplot(param_grid, aes(shape, rate, color = p_val)) + geom_point() + scale_x_log10(expression(alpha), n.breaks = 10, labels = fmt(-5:5)) + scale_y_log10(expression(lambda), n.breaks = 10, labels = fmt(-5:5)) + scale_color_binned(breaks = c(0, .05, 1)) + labs(color = "p value") + labs("title" = "KS GoF Test of Draws for Different Parameter Values") + coord_equal() ``` -------------------------------- ### Run Parallel Monte Carlo Tests Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Applies the previously defined `test_invgamma` function to each combination of shape and rate in the `param_grid` using parallel processing with `furrr`. This computes the p-value for each parameter combination. Warnings from `rinvgamma` are suppressed in the original context. ```R param_grid <- param_grid |> mutate(p_val = future_map2_dbl(shape, rate, test_invgamma)) ``` -------------------------------- ### Evaluate inverse gamma CDF Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Calculate the cumulative distribution function using pinvgamma and compare with numerical integration. ```R f <- function(x) dinvgamma(x, shape, rate) q <- 2 integrate(f, 0, q) # 0.7621835 with absolute error < 7.3e-05 (p <- pinvgamma(q, shape, rate)) # [1] 0.7621835 ``` -------------------------------- ### Monte Carlo estimation with rinvgamma Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Estimate probability using Monte Carlo methods with random draws. ```R n <- 1e5 draws <- rinvgamma(n, shape, rate) mean(draws <= q) # [1] 0.76401 ``` -------------------------------- ### Illustrate rinvexp Reliability Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Tests the reliability of the rinvexp function across different rate parameters. This illustration confirms that rinvexp is trustworthy for all tested values. ```R test_rinvexp <- function(rate, n = 1e5) { draws <- rinvexp(n, rate = rate) ks.test(draws, function(p) pinvexp(p, rate))$p.value } tibble("rate" = 10^seq(-4, 4, length.out = 2*n_grid)) |> mutate("p_val" = future_map_dbl(rate, test_rinvexp)) |> ggplot(aes(rate, 0, color = p_val)) + geom_point() + scale_x_log10(expression(lambda), n.breaks = 10, labels = fmt(-5:5)) + scale_color_binned(breaks = c(0, .05, 1), guide = FALSE) + theme(axis.text.y = element_blank(), axis.title.y = element_blank(), panel.grid.major.y = element_blank()) + coord_equal() ``` -------------------------------- ### Inverse Exponential Distribution Functions Source: https://cran.r-project.org/web/packages/invgamma/refman/invgamma.html Functions for the inverse exponential distribution including density, distribution, quantile, and random generation. ```APIDOC ## Inverse Exponential Functions ### Description Density, distribution function, quantile function and random generation for the inverse exponential distribution. ### Parameters - **x, q** (vector) - Required - Vector of quantiles. - **rate** (numeric) - Required - Degrees of freedom (non-negative). - **log, log.p** (logical) - Optional - If TRUE, probabilities p are given as log(p). - **lower.tail** (logical) - Optional - If TRUE (default), probabilities are P[X≤x]; if FALSE, P[X>x]. - **p** (vector) - Required - Vector of probabilities. - **n** (numeric) - Required - Number of observations. ``` -------------------------------- ### Inverse Gamma Distribution Functions Source: https://cran.r-project.org/web/packages/invgamma/refman/invgamma.html Functions for the inverse gamma distribution including density, distribution, quantile, and random generation. ```APIDOC ## Inverse Gamma Functions ### Description Density, distribution function, quantile function and random generation for the inverse gamma distribution. ### Parameters - **x, q** (vector) - Required - Vector of quantiles. - **shape, rate, scale** (numeric) - Required - Shape, rate, and scale parameters of the corresponding gamma distribution. - **log, log.p** (logical) - Optional - If TRUE, probabilities p are given as log(p). - **lower.tail** (logical) - Optional - If TRUE (default), probabilities are P[X≤x]; if FALSE, P[X>x]. - **p** (vector) - Required - Vector of probabilities. - **n** (numeric) - Required - Number of observations. ``` -------------------------------- ### Evaluate inverse gamma density Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Plot the PDF of an inverse gamma distribution. ```R library("invgamma") x <- seq(0, 5, .01) shape <- 7; rate <- 10 plot(x, dinvgamma(x, shape, rate), type = "l") ``` -------------------------------- ### Inverse Exponential Distribution Usage Source: https://cran.r-project.org/web/packages/invgamma/refman/invgamma.html Functions for density, distribution, quantile, and random generation for the inverse exponential distribution. ```R dinvexp(x, rate = 1, log = FALSE) pinvexp(q, rate = 1, lower.tail = TRUE, log.p = FALSE) qinvexp(p, rate = 1, lower.tail = TRUE, log.p = FALSE) rinvexp(n, rate = 1) ``` -------------------------------- ### Inverse Chi-Squared Distribution Functions Source: https://cran.r-project.org/web/packages/invgamma/refman/invgamma.html Functions for the inverse chi-squared distribution including density, distribution, quantile, and random generation. ```APIDOC ## Inverse Chi-Squared Functions ### Description Density, distribution function, quantile function and random generation for the inverse chi-squared distribution. ### Parameters - **x, q** (vector) - Required - Vector of quantiles. - **df** (numeric) - Required - Degrees of freedom (non-negative). - **ncp** (numeric) - Optional - Non-centrality parameter (non-negative). - **log, log.p** (logical) - Optional - If TRUE, probabilities p are given as log(p). - **lower.tail** (logical) - Optional - If TRUE (default), probabilities are P[X≤x]; if FALSE, P[X>x]. - **p** (vector) - Required - Vector of probabilities. - **n** (numeric) - Required - Number of observations. ``` -------------------------------- ### Inverse Gamma Distribution Usage Source: https://cran.r-project.org/web/packages/invgamma/refman/invgamma.html Functions for density, distribution, quantile, and random generation for the inverse gamma distribution. ```R dinvgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE) pinvgamma(q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE) qinvgamma(p, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE) rinvgamma(n, shape, rate = 1, scale = 1/rate) ``` -------------------------------- ### Monte Carlo Test Function for rinvgamma() Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Defines a function to perform a Monte Carlo test on the rinvgamma() sampler. It generates a specified number of draws and performs a KS test against the inverse gamma distribution, returning the p-value. Use this to check sampler quality for given shape and rate. ```R test_invgamma <- function(shape, rate, n = 1e5) { draws <- rinvgamma(n, shape, rate) ks.test(draws, function(p) pinvgamma(p, shape, rate))$p.value } test_invgamma(3, 7) # [1] 0.3464601 ``` -------------------------------- ### Inverse Chi-Squared Distribution Usage Source: https://cran.r-project.org/web/packages/invgamma/refman/invgamma.html Functions for density, distribution, quantile, and random generation for the inverse chi-squared distribution. ```R dinvchisq(x, df, ncp = 0, log = FALSE) pinvchisq(q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE) qinvchisq(p, df, ncp = 0, lower.tail = TRUE, log.p = FALSE) rinvchisq(n, df, ncp = 0) ``` -------------------------------- ### Generate random inverse gamma numbers Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Generate random draws from an inverse gamma distribution. ```R set.seed(1234) rinvgamma(5, shape, rate) # [1] 2.640734 1.364965 1.342591 2.095975 1.273201 ``` -------------------------------- ### Illustrate rinvchisq Reliability Source: https://cran.r-project.org/web/packages/invgamma/readme/README.html Tests the reliability of the rinvchisq function across different degrees of freedom (df) and non-centrality parameters (ncp). Use this to identify parameter ranges where rinvchisq may not be trustworthy. ```R test_rinvchisq <- function(df, ncp, n = 1e5) { draws <- rinvchisq(n, df, ncp) ks.test(draws, function(p) pinvchisq(p, df, ncp))$p.value } expand_grid("df" = param_vals, "ncp" = param_vals) |> mutate("p_val" = future_map2_dbl(df, ncp, test_rinvchisq)) |> ggplot(aes(df, ncp, color = p_val)) + geom_point() + scale_x_log10(expression(nu), n.breaks = 10, labels = fmt(-5:5)) + scale_y_log10("ncp", n.breaks = 10, labels = fmt(-5:5)) + scale_color_binned(breaks = c(0, .05, 1)) + labs(color = "p value") + coord_equal() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.