| Title: | Adaptive Sampling Algorithms |
|---|---|
| Description: | For distributions whose probability density functions are log-concave, the adaptive rejection sampling algorithm can be used to build envelope functions for sampling. For others, we can use the modified adaptive rejection sampling algorithm, the concave-convex adaptive rejection sampling algorithm and the adaptive slice sampling algorithm. So we designed an R package mainly including 4 functions: rARS(), rMARS(), rCCARS() and rASS(). These functions can realize sampling based on the algorithms above. Version 1.2.0 fixes several correctness bugs and improves numerical robustness. |
| Authors: | Dong Zhang [aut, cre] |
| Maintainer: | Dong Zhang <[email protected]> |
| License: | GPL-2 | file LICENSE |
| Version: | 1.2.0 |
| Built: | 2026-09-24 10:04:26 UTC |
| Source: | https://github.com/cran/AdapSamp |
rARS generates a sequence of random numbers using the adaptive rejection sampling algorithm.
rARS(n, formula, min = -Inf, max = Inf, sp)rARS(n, formula, min = -Inf, max = Inf, sp)
n |
Desired sample size (positive integer). |
formula |
Kernel of the target density, as a character string (e.g. |
min, max
|
Domain of the target distribution, including |
sp |
Supporting set; a numeric vector of initial abscissae. |
A numeric vector of length n containing samples from the target distribution.
Dong Zhang
# Example 1: Standard normal distribution set.seed(123) x1 <- rARS(100, "exp(-x^2/2)", -Inf, Inf, c(-2, 2)) hist(x1, breaks = 20, probability = TRUE) # Example 2: Truncated normal distribution ## Not run: x2 <- rARS(100, "exp(-x^2/2)", -2.1, 2.1, c(-2, 2)) ## End(Not run) # Example 3: Exponential distribution with rate=3 ## Not run: x4 <- rARS(100, "exp(-3*x)", 0, Inf, c(2, 3, 100)) ## End(Not run) # Example 4: Beta distribution with alpha=3 and beta=4 ## Not run: x5 <- rARS(100, "x^2*(1-x)^3", 0, 1, c(0.4, 0.6)) ## End(Not run) # Example 5: Gamma distribution with alpha=5 and lambda=2 ## Not run: x6 <- rARS(100, "x^(5-1)*exp(-2*x)", 0, Inf, c(1, 10)) ## End(Not run)# Example 1: Standard normal distribution set.seed(123) x1 <- rARS(100, "exp(-x^2/2)", -Inf, Inf, c(-2, 2)) hist(x1, breaks = 20, probability = TRUE) # Example 2: Truncated normal distribution ## Not run: x2 <- rARS(100, "exp(-x^2/2)", -2.1, 2.1, c(-2, 2)) ## End(Not run) # Example 3: Exponential distribution with rate=3 ## Not run: x4 <- rARS(100, "exp(-3*x)", 0, Inf, c(2, 3, 100)) ## End(Not run) # Example 4: Beta distribution with alpha=3 and beta=4 ## Not run: x5 <- rARS(100, "x^2*(1-x)^3", 0, 1, c(0.4, 0.6)) ## End(Not run) # Example 5: Gamma distribution with alpha=5 and lambda=2 ## Not run: x6 <- rARS(100, "x^(5-1)*exp(-2*x)", 0, Inf, c(1, 10)) ## End(Not run)
rASS generates a sequence of random numbers by the adaptive slice sampling algorithm with stepping-out procedures (Neal, 2003).
rASS(n, x0 = 0, formula, w = 3, max_steps = 1000)rASS(n, x0 = 0, formula, w = 3, max_steps = 1000)
n |
Desired sample size (positive integer). |
x0 |
Initial value for the Markov chain. |
formula |
Target density function |
w |
Length of the initial coverage interval. |
max_steps |
Maximum number of stepping-out iterations (default 1000). |
A numeric vector of length n containing samples (excludes the initial value x0).
Dong Zhang
Neal R M. Slice sampling - Rejoinder[J]. Annals of Statistics, 2003, 31(3):758-767.
set.seed(123) x <- rASS(100, -1, "1.114283*exp(-(4-x^2)^2)", 3) plot(density(x))set.seed(123) x <- rASS(100, -1, "1.114283*exp(-(4-x^2)^2)", 3) plot(density(x))
rCCARS generates a sequence of random numbers by the concave-convex adaptive rejection sampling algorithm from target distributions with bounded domain.
rCCARS(n, cvformula, ccformula, min, max, sp)rCCARS(n, cvformula, ccformula, min, max, sp)
n |
Desired sample size (positive integer). |
cvformula |
Convex part of |
ccformula |
Concave part of |
min, max
|
Bounded domain limits (must be finite). |
sp |
Supporting set; a numeric vector of initial abscissae. |
Strictly speaking, the concave-convex adaptive rejection sampling algorithm can generate samples from target distributions with bounded domains. For distributions with unbounded domain, rCCARS can also be used for approximate sampling by truncating the domain.
A numeric vector of length n containing samples from the target distribution.
Dong Zhang
Teh Y W. Concave-Convex Adaptive Rejection Sampling[J]. Journal of Computational & Graphical Statistics, 2011, 20(3):670-691.
## Not run: set.seed(123) x <- rCCARS(100, "x+x^-1", "2*log(x)", 0.001, 100, 1) hist(x, breaks = 20, probability = TRUE) ## End(Not run)## Not run: set.seed(123) x <- rCCARS(100, "x+x^-1", "2*log(x)", 0.001, 100, 1) hist(x, breaks = 20, probability = TRUE) ## End(Not run)
rMARS generates a sequence of random numbers using the modified adaptive rejection sampling algorithm, which extends ARS to non-log-concave densities.
rMARS(n, formula, min = -Inf, max = Inf, sp, infp, m = 1e-04)rMARS(n, formula, min = -Inf, max = Inf, sp, infp, m = 1e-04)
n |
Desired sample size (positive integer). |
formula |
Kernel of the target distribution, as a character string. |
min, max
|
Domain of the target distribution, including |
sp |
Supporting set; a numeric vector of initial abscissae. |
infp |
Inflexion set; a numeric vector of inflexion points of |
m |
A small numeric parameter for judging concavity and convexity near inflexion points. |
A numeric vector of length n containing samples from the target distribution.
Dong Zhang
Martino L, Miguez J. A generalization of the adaptive rejection sampling algorithm[J]. Statistics & Computing, 2011, 21(4):633-647.
set.seed(123) x <- rMARS(100, "exp(-(4-x^2)^2)", -Inf, Inf, c(-2.5, 0, 2.5), c(-2/sqrt(3), 2/sqrt(3))) hist(x, probability = TRUE, xlim = c(-3, 3), ylim = c(0, 1.2), breaks = 20) lines(density(x, bw = 0.05), col = "blue")set.seed(123) x <- rMARS(100, "exp(-(4-x^2)^2)", -Inf, Inf, c(-2.5, 0, 2.5), c(-2/sqrt(3), 2/sqrt(3))) hist(x, probability = TRUE, xlim = c(-3, 3), ylim = c(0, 1.2), breaks = 20) lines(density(x, bw = 0.05), col = "blue")