| Title: | The Modified Half-Normal Distribution |
|---|---|
| Description: | Provides density, distribution, quantile, and random generation functions for the Modified Half-Normal (MHN) distribution, along with moments, mode, and the Fox-Wright Psi function used as the normalizing constant. The MHN distribution arises as a conditional posterior in Bayesian MCMC and generalizes the half-normal, truncated normal, and square-root gamma distributions. Implements efficient sampling via the Sun, Kong & Pal (2023) <doi:10.1080/03610926.2021.1934700> algorithms and the Gao & Wang (2025) <doi:10.1080/03610918.2025.2524551> RTDR method. |
| Authors: | Tomotaka Momozaki [aut, cre] |
| Maintainer: | Tomotaka Momozaki <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-28 11:36:45 UTC |
| Source: | https://github.com/t-momozaki/mhn |
Computes the probability density function (or log-density) of the
Modified Half-Normal (MHN) distribution with parameters alpha,
beta, and gamma.
dmhn(x, alpha = 1, beta = 1, gamma = 0, log = FALSE)dmhn(x, alpha = 1, beta = 1, gamma = 0, log = FALSE)
x |
Numeric vector of evaluation points. |
alpha |
Shape parameter ( |
beta |
Scale parameter ( |
gamma |
Location parameter ( |
log |
Logical; if |
The MHN density is
where is the Fox-Wright Psi function
(Sun et al., 2023, Lemma 1a).
The default parameters alpha = 1, beta = 1, gamma = 0 correspond
to the half-normal distribution .
Special cases are detected and dispatched to closed-form solutions:
: sqrt-Gamma distribution
: truncated normal distribution
Computation is performed in log-space to avoid numerical underflow/overflow.
When any of alpha, beta, gamma is a vector, the
density is evaluated element-wise. The Fox-Wright
normalizing constant is recomputed only when consecutive elements
present a different triple, so passing
grouped parameters is significantly faster than calling dmhn
inside an R loop.
A numeric vector. The output length equals
max(length(x), length(alpha), length(beta), length(gamma)); each
input is recycled to that length following standard R recycling rules.
For x < 0, the density is 0 (-Inf if log = TRUE).
Sun, J., Kong, M., & Pal, S. (2023). The Modified-Half-Normal distribution: Properties and an efficient sampling scheme. Communications in Statistics - Theory and Methods, 52(5), 1507–1536.
x <- seq(0, 5, length.out = 100) plot(x, dmhn(x, alpha = 2, beta = 1, gamma = 1), type = "l") # Log-density dmhn(1, alpha = 2, beta = 1, gamma = 1, log = TRUE)x <- seq(0, 5, length.out = 100) plot(x, dmhn(x, alpha = 2, beta = 1, gamma = 1), type = "l") # Log-density dmhn(1, alpha = 2, beta = 1, gamma = 1, log = TRUE)
Computes the excess kurtosis
for .
mhn_kurtosis(alpha, beta, gamma)mhn_kurtosis(alpha, beta, gamma)
alpha |
Shape parameter ( |
beta |
Scale parameter ( |
gamma |
Location parameter ( |
Uses the moment recurrence (Sun et al., 2023, Lemma 2b) to compute raw moments up to fourth order, then converts to central moments.
A numeric scalar.
Sun, J., Kong, M., & Pal, S. (2023). The Modified-Half-Normal distribution: Properties and an efficient sampling scheme. Communications in Statistics - Theory and Methods, 52(5), 1507–1536. (Lemma 2b)
mhn_kurtosis(alpha = 2, beta = 1, gamma = 0)mhn_kurtosis(alpha = 2, beta = 1, gamma = 0)
Computes for .
mhn_mean(alpha, beta, gamma)mhn_mean(alpha, beta, gamma)
alpha |
Shape parameter ( |
beta |
Scale parameter ( |
gamma |
Location parameter ( |
The mean is computed as a ratio of Fox-Wright Psi functions:
A numeric scalar.
Sun, J., Kong, M., & Pal, S. (2023). The Modified-Half-Normal distribution: Properties and an efficient sampling scheme. Communications in Statistics - Theory and Methods, 52(5), 1507–1536. (Lemma 2a)
mhn_mean(alpha = 2, beta = 1, gamma = 0)mhn_mean(alpha = 2, beta = 1, gamma = 0)
Computes the mode (most probable value) of the MHN distribution.
mhn_mode(alpha, beta, gamma)mhn_mode(alpha, beta, gamma)
alpha |
Shape parameter ( |
beta |
Scale parameter ( |
gamma |
Location parameter ( |
The mode depends on :
(Sun et al., 2023, Lemma 3b).
, obtained as the mode of the
truncated normal that the MHN reduces to in this case (Sun et al., 2023,
Lemma 6b).
An interior mode exists only when and
(Sun et al., 2023,
Lemma 3c); otherwise the density is monotonically decreasing
(Sun et al., 2023, Lemma 3d) and NA is returned.
A numeric scalar. Returns NA when no interior mode exists
(density is monotonically decreasing on ).
Sun, J., Kong, M., & Pal, S. (2023). The Modified-Half-Normal distribution: Properties and an efficient sampling scheme. Communications in Statistics - Theory and Methods, 52(5), 1507–1536. (Lemma 3b–d, Lemma 6b)
mhn_mode(alpha = 2, beta = 1, gamma = 1) mhn_mode(alpha = 1, beta = 1, gamma = 2) mhn_mode(alpha = 0.5, beta = 1, gamma = -1) # NAmhn_mode(alpha = 2, beta = 1, gamma = 1) mhn_mode(alpha = 1, beta = 1, gamma = 2) mhn_mode(alpha = 0.5, beta = 1, gamma = -1) # NA
Computes the skewness
for .
mhn_skewness(alpha, beta, gamma)mhn_skewness(alpha, beta, gamma)
alpha |
Shape parameter ( |
beta |
Scale parameter ( |
gamma |
Location parameter ( |
Uses the moment recurrence (Sun et al., 2023, Lemma 2b) to compute raw moments up to third order, then converts to central moments.
A numeric scalar.
Sun, J., Kong, M., & Pal, S. (2023). The Modified-Half-Normal distribution: Properties and an efficient sampling scheme. Communications in Statistics - Theory and Methods, 52(5), 1507–1536. (Lemma 2b)
mhn_skewness(alpha = 2, beta = 1, gamma = 0)mhn_skewness(alpha = 2, beta = 1, gamma = 0)
Computes for
.
mhn_var(alpha, beta, gamma)mhn_var(alpha, beta, gamma)
alpha |
Shape parameter ( |
beta |
Scale parameter ( |
gamma |
Location parameter ( |
Uses the formula (Sun et al., 2023, Lemma 2c):
For , the variance satisfies
(Sun et al., 2023, Lemma 4c).
A numeric scalar.
Sun, J., Kong, M., & Pal, S. (2023). The Modified-Half-Normal distribution: Properties and an efficient sampling scheme. Communications in Statistics - Theory and Methods, 52(5), 1507–1536. (Lemma 2c)
mhn_var(alpha = 2, beta = 1, gamma = 0)mhn_var(alpha = 2, beta = 1, gamma = 0)
Computes the cumulative distribution function (CDF) of the
Modified Half-Normal (MHN) distribution with parameters alpha,
beta, and gamma.
pmhn(q, alpha = 1, beta = 1, gamma = 0, lower.tail = TRUE, log.p = FALSE)pmhn(q, alpha = 1, beta = 1, gamma = 0, lower.tail = TRUE, log.p = FALSE)
q |
Numeric vector of quantiles. |
alpha |
Shape parameter ( |
beta |
Scale parameter ( |
gamma |
Location parameter ( |
lower.tail |
Logical; if |
log.p |
Logical; if |
The CDF is computed via the series representation
where , , and
is the regularized lower incomplete gamma function
(Sun et al., 2023, Lemma 1b; equivalent to the paper's form via the
identity , where
is the lower incomplete gamma function used in
the paper). The infinite sum is truncated at the constructive bound
from Sun et al. (2023), Supplementary
Lemma 10(d), which makes the truncation residual bounded by the
user's tolerance divided by . When
double-precision cancellation in the alternating-sign accumulator
for would exceed that tolerance, the series is
replaced by a Gauss-Kronrod (or tanh-sinh for )
numerical integration of the density on .
Special cases are detected and dispatched to standard R primitives:
: pgamma(q^2, alpha/2, scale = 1/beta)
: truncated-normal CDF via pnorm
When any of alpha, beta, gamma is a vector, the CDF
is evaluated element-wise. The Fox-Wright normalizing
constant is recomputed only when consecutive elements present a
different triple, so passing grouped
parameters is significantly faster than calling pmhn inside an
R loop.
A numeric vector. The output length equals
max(length(q), length(alpha), length(beta), length(gamma)); each
input is recycled to that length following standard R recycling rules.
For q <= 0 the CDF is 0; for q = Inf it is 1.
Sun, J., Kong, M., & Pal, S. (2023). The Modified-Half-Normal distribution: Properties and an efficient sampling scheme. Communications in Statistics - Theory and Methods, 52(5), 1507–1536.
# Basic evaluation pmhn(c(0.5, 1, 1.5), alpha = 2, beta = 1, gamma = 1) # Tail / log forms pmhn(2, alpha = 2, beta = 1, gamma = 1, lower.tail = FALSE) pmhn(2, alpha = 2, beta = 1, gamma = 1, log.p = TRUE) # Special case: gamma = 0 reduces to sqrt-Gamma all.equal(pmhn(1.5, alpha = 2, beta = 1, gamma = 0), pgamma(1.5^2, shape = 1, rate = 1))# Basic evaluation pmhn(c(0.5, 1, 1.5), alpha = 2, beta = 1, gamma = 1) # Tail / log forms pmhn(2, alpha = 2, beta = 1, gamma = 1, lower.tail = FALSE) pmhn(2, alpha = 2, beta = 1, gamma = 1, log.p = TRUE) # Special case: gamma = 0 reduces to sqrt-Gamma all.equal(pmhn(1.5, alpha = 2, beta = 1, gamma = 0), pgamma(1.5^2, shape = 1, rate = 1))
Computes the quantile (inverse cumulative) function of the
Modified Half-Normal (MHN) distribution with parameters alpha,
beta, and gamma.
qmhn(p, alpha = 1, beta = 1, gamma = 0, lower.tail = TRUE, log.p = FALSE)qmhn(p, alpha = 1, beta = 1, gamma = 0, lower.tail = TRUE, log.p = FALSE)
p |
Numeric vector of probabilities. |
alpha |
Shape parameter ( |
beta |
Scale parameter ( |
gamma |
Location parameter ( |
lower.tail |
Logical; if |
log.p |
Logical; if |
For the general case, is obtained by a TOMS 748
root-finder applied to the series CDF (Sun et al., 2023, Lemma 1b).
The initial bracket is
and is doubled on the right (up to 30 times) until it brackets the
target probability.
Special cases are detected and dispatched to standard R primitives:
: sqrt(qgamma(p, alpha/2, scale = 1/beta))
: truncated-normal inverse via qnorm
When any of alpha, beta, gamma is a vector, the
quantile is evaluated element-wise. The Fox-Wright
normalizing constant and moments ,
(used to size the root-finder bracket) are recomputed only when
consecutive elements present a different
triple.
A numeric vector. The output length equals
max(length(p), length(alpha), length(beta), length(gamma)); each
input is recycled to that length following standard R recycling rules.
qmhn(0) = 0 and qmhn(1) = Inf. Probabilities outside
yield NaN.
Sun, J., Kong, M., & Pal, S. (2023). The Modified-Half-Normal distribution: Properties and an efficient sampling scheme. Communications in Statistics - Theory and Methods, 52(5), 1507–1536.
# Basic evaluation qmhn(c(0.1, 0.5, 0.9), alpha = 2, beta = 1, gamma = 1) # Round-trip: F(F^-1(p)) ~ p p <- c(0.05, 0.25, 0.5, 0.75, 0.95) all.equal(pmhn(qmhn(p, alpha = 2, beta = 1, gamma = 1), alpha = 2, beta = 1, gamma = 1), p, tolerance = 1e-6) # Tail / log forms qmhn(0.95, alpha = 2, beta = 1, gamma = 1, lower.tail = FALSE) qmhn(log(0.05), alpha = 2, beta = 1, gamma = 1, log.p = TRUE)# Basic evaluation qmhn(c(0.1, 0.5, 0.9), alpha = 2, beta = 1, gamma = 1) # Round-trip: F(F^-1(p)) ~ p p <- c(0.05, 0.25, 0.5, 0.75, 0.95) all.equal(pmhn(qmhn(p, alpha = 2, beta = 1, gamma = 1), alpha = 2, beta = 1, gamma = 1), p, tolerance = 1e-6) # Tail / log forms qmhn(0.95, alpha = 2, beta = 1, gamma = 1, lower.tail = FALSE) qmhn(log(0.05), alpha = 2, beta = 1, gamma = 1, log.p = TRUE)
Draws random variates from the Modified Half-Normal (MHN) distribution
with parameters alpha, beta, and gamma.
rmhn(n, alpha = 1, beta = 1, gamma = 0, method = c("auto", "rtdr", "sun"))rmhn(n, alpha = 1, beta = 1, gamma = 0, method = c("auto", "rtdr", "sun"))
n |
Non-negative integer giving the number of variates to draw.
|
alpha |
Shape parameter ( |
beta |
Scale parameter ( |
gamma |
Location parameter ( |
method |
Sampling algorithm. One of |
The MHN density is
where is the Fox-Wright Psi function. rmhn does
not evaluate ; the rejection-sampling kernels cancel it out.
The default parameters alpha = 1, beta = 1, gamma = 0 correspond
to the half-normal distribution .
The method argument selects the rejection sampler:
"auto": Special-case shortcuts when applicable
( -> sqrt-Gamma, ->
truncated normal). Otherwise dispatches to RTDR (Gao & Wang, 2025).
"rtdr": Force the Relaxed Transformed Density Rejection
method of Gao & Wang (2025). The acceptance probability is bounded
below by uniformly over the parameter space.
Note: Gao & Wang (2025) use the parameterization
with density proportional to
; the mapping to
the Sun et al. parameterization used here is
,
,
(sign flip on the linear term).
"sun": Force the Sun et al. (2023) algorithms.
Algorithm 1 is used when and ;
Algorithm 3 is used when . The combination
with is unsupported and triggers
an error.
Vector parameters are recycled to length n following standard R
rules. Trailing parameter elements beyond index n - 1 are
silently ignored, matching the convention of rnorm.
Internally the setup state of the chosen sampler is reused as long as
consecutive triples are equal, so passing
parameters grouped by triple is faster than calling rmhn inside
an R loop.
A numeric vector of length n. If any of alpha,
beta, gamma (after recycling to length n) is
NA or non-finite (Inf, -Inf, NaN), the
corresponding output element is NA.
Sun, J., Kong, M., & Pal, S. (2023). The Modified-Half-Normal distribution: Properties and an efficient sampling scheme. Communications in Statistics - Theory and Methods, 52(5), 1507–1536.
Gao, F. & Wang, H.-B. (2025). Generating modified-half-normal random variates by a relaxed transformed density rejection method. Communications in Statistics - Simulation and Computation.
Robert, C. P. (1995). Simulation of truncated normal variables. Statistics and Computing, 5(2), 121–125.
set.seed(1) rmhn(10, alpha = 2, beta = 1, gamma = 0.5) # Vector parameters are recycled to length n. set.seed(1) rmhn(5, alpha = c(1, 2, 3, 4, 5))set.seed(1) rmhn(10, alpha = 2, beta = 1, gamma = 0.5) # Vector parameters are recycled to length n. set.seed(1) rmhn(5, alpha = c(1, 2, 3, 4, 5))