Package {AdapSamp}


Type: Package
Title: Adaptive Sampling Algorithms
Version: 1.2.0
Date: 2026-08-25
Author: Dong Zhang [aut, cre]
Maintainer: Dong Zhang <dzhang0716@126.com>
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.
Imports: pracma, stats, utils
License: GPL-2 | file LICENSE
Encoding: UTF-8
NeedsCompilation: no
Config/roxygen2/version: 8.1.0
Packaged: 2026-08-25 09:14:10 UTC; Honor
Repository: CRAN
Date/Publication: 2026-08-25 09:50:17 UTC

Adaptive Rejection Sampling Algorithm

Description

rARS generates a sequence of random numbers using the adaptive rejection sampling algorithm.

Usage

rARS(n, formula, min = -Inf, max = Inf, sp)

Arguments

n

Desired sample size (positive integer).

formula

Kernel of the target density, as a character string (e.g. "exp(-x^2/2)").

min, max

Domain of the target distribution, including -Inf and Inf.

sp

Supporting set; a numeric vector of initial abscissae.

Value

A numeric vector of length n containing samples from the target distribution.

Author(s)

Dong Zhang

Examples


# 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)


Adaptive Slice Sampling Algorithm With Stepping-Out Procedures

Description

rASS generates a sequence of random numbers by the adaptive slice sampling algorithm with stepping-out procedures (Neal, 2003).

Usage

rASS(n, x0 = 0, formula, w = 3, max_steps = 1000)

Arguments

n

Desired sample size (positive integer).

x0

Initial value for the Markov chain.

formula

Target density function p(x), as a character string.

w

Length of the initial coverage interval.

max_steps

Maximum number of stepping-out iterations (default 1000).

Value

A numeric vector of length n containing samples (excludes the initial value x0).

Author(s)

Dong Zhang

References

Neal R M. Slice sampling - Rejoinder[J]. Annals of Statistics, 2003, 31(3):758-767.

Examples

set.seed(123)
x <- rASS(100, -1, "1.114283*exp(-(4-x^2)^2)", 3)
plot(density(x))


Concave-Convex Adaptive Rejection Sampling Algorithm

Description

rCCARS generates a sequence of random numbers by the concave-convex adaptive rejection sampling algorithm from target distributions with bounded domain.

Usage

rCCARS(n, cvformula, ccformula, min, max, sp)

Arguments

n

Desired sample size (positive integer).

cvformula

Convex part of -log(p(x)), as a character string.

ccformula

Concave part of -log(p(x)), as a character string.

min, max

Bounded domain limits (must be finite).

sp

Supporting set; a numeric vector of initial abscissae.

Details

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.

Value

A numeric vector of length n containing samples from the target distribution.

Author(s)

Dong Zhang

References

Teh Y W. Concave-Convex Adaptive Rejection Sampling[J]. Journal of Computational & Graphical Statistics, 2011, 20(3):670-691.

Examples

## 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)


Modified Adaptive Rejection Sampling Algorithm

Description

rMARS generates a sequence of random numbers using the modified adaptive rejection sampling algorithm, which extends ARS to non-log-concave densities.

Usage

rMARS(n, formula, min = -Inf, max = Inf, sp, infp, m = 1e-04)

Arguments

n

Desired sample size (positive integer).

formula

Kernel of the target distribution, as a character string.

min, max

Domain of the target distribution, including -Inf and Inf.

sp

Supporting set; a numeric vector of initial abscissae.

infp

Inflexion set; a numeric vector of inflexion points of -log(p(x)).

m

A small numeric parameter for judging concavity and convexity near inflexion points.

Value

A numeric vector of length n containing samples from the target distribution.

Author(s)

Dong Zhang

References

Martino L, Miguez J. A generalization of the adaptive rejection sampling algorithm[J]. Statistics & Computing, 2011, 21(4):633-647.

Examples

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")