| 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 and mode; the Fox-Wright Psi normalizing constant is evaluated internally. The MHN distribution arises as a conditional posterior in Bayesian Markov chain Monte Carlo and generalizes the half-normal, truncated normal, and square-root gamma distributions. Implements efficient sampling via Algorithms 1 and 3 of Sun, Kong & Pal (2023) <doi:10.1080/03610926.2021.1934700>, the relaxed transformed density rejection method of Gao & Wang (2025) <doi:10.1080/03610918.2025.2524551>, and, for the truncated normal special case, the sampler of Robert (1995) <doi:10.1007/BF00143942>. |
| Authors: | Tomotaka Momozaki [aut, cre] (ORCID: <https://orcid.org/0000-0001-6735-0804>) |
| Maintainer: | Tomotaka Momozaki <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.2 |
| Built: | 2026-10-23 13:56:58 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 |
Rate-type parameter ( |
gamma |
Tilt parameter ( |
log |
Logical; if |
The MHN density (Sun et al. 2023, Lemma 1a) is
where is the Fox-Wright function
(Sun et al. 2023, Supplementary Eq. (1)).
The default parameters alpha = 1, beta = 1, gamma = 0 correspond
to the half-normal distribution
.
At and at
exactly the density is computed from the closed form of the law it
reduces to; a shape or a tilt merely close to those values is a different
law, and is computed as in the general case:
: sqrt-Gamma distribution, for
; a larger shape is computed as in
the general case.
: truncated normal distribution (Sun et al.,
2023, Lemma 6b), the normal law of mean
and standard deviation
restricted to
. However far 0 lies from the mean, the
density holds to the Exponential and normal limits it tends to; where
passes the largest double under
a negative tilt, it is the Exponential density
.
Otherwise the log density is computed from the formula above; for a large
shape or tilt, where the terms of that formula grow too large, the law is
measured instead from the peak of the density of .
Accuracy and range. With
the scale-free tilt,
for ,
and in
the log density is accurate
to the working tolerance sqrt(.Machine$double.eps). Outside that
range the law can be narrower than the spacing of the doubles near its
peak (for above about , or
above about ), and the density is
still that of the exact law at the double x given, almost always
0 there. For a positive tilt whose mode
lies beyond the largest double,
which needs a subnormal , log = TRUE returns
-Inf where the log density is finite. Two cases give NA
with a warning instead: within 45 standard deviations of the peak of a
law whose peak cannot be placed to within of its
standard deviation, which needs a law narrower than the spacing by a
factor of order or more and then happens for some
parameters only (as at
);
and where the whole law lies beyond the largest double
without a positive tilt to place it (a shape near the largest double
against a subnormal ). Where itself
overflows, the law is its limit: Gamma(, rate
) under a negative tilt and normal about its mode
with variance under a positive one.
A subnormal shape, below the smallest normal double
, is computed apart, at
too, from a form of the law in which
only multiplies and lies below the smallest
positive double with probability , where ,
defined in Appendix A.4 of the vignette cited below, grows with the tilt. Below a tilt of about 52 (53 at
the smallest shapes) that probability is 1 in double precision:
pmhn is 1 at every positive , qmhn is
0 for every lower-tail probability below 1 given on the probability
scale (the upper tail, or log.p = TRUE, still gives the quantiles
of the far upper tail), and rmhn returns 0. Above a tilt
between about 55 and 56.3, as falls, the mass by the
origin is at most , about
, of the whole, and the density, the
upper tail, the moments and the law of the draws no longer depend on
.
When any of alpha, beta, gamma is a vector, the
density is evaluated element-wise. The Fox-Wright
normalizing constant is computed once for each position of the recycling
cycle while that cycle is at most 256 long, and otherwise once for each
run of consecutive elements that share the
triple. So recycled parameters, and
parameter vectors in which equal triples are adjacent, are much faster
than triples that alternate along a long vector, and than calling
dmhn inside an R loop.
The numerical methods, and the reasons for the ranges above, are
described in Appendix A of vignette("theory", package = "mhn").
A numeric vector. The output length equals
max(length(x), length(alpha), length(beta), length(gamma)), or 0
if any of them has length zero; each input is recycled to that length
following standard R recycling rules.
For x < 0, the density is 0 (-Inf if log = TRUE).
At x = 0 it is Inf for alpha < 1, the truncated
normal density at 0 for alpha = 1 ( at gamma = 0), and 0 for alpha > 1.
As in dgamma, an element whose parameters lie
outside the parameter space (alpha or beta not positive
and finite, or gamma not finite) is NaN, with a warning,
and an element with an NA or NaN among its inputs is
NA or NaN (NA if any of them is NA),
without one; the other elements are unaffected. The arguments must be
numeric (double, integer or logical): a factor, a character string, a
complex number, a list or NULL is an error, as in
dnorm.
As with dnorm, the attributes of x (names,
dim, dimnames) are kept when the result has the length of
x; a classed x, such as a Date, is taken as its numbers
and gives a plain vector.
Unlike dnorm, attributes of a longer parameter vector are not
carried over.
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), 1591–1613.
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 |
Rate-type parameter ( |
gamma |
Tilt parameter ( |
The excess kurtosis is formed from the second and fourth central moments. In exact arithmetic these follow from the mean of Sun et al. (2023, Lemma 2a) and the recurrence for the raw moments of their Lemma 2b; the function obtains them instead, wherever it can, by numerical integration of the unnormalised density, which needs no normalising constant and stays accurate under a large tilt.
Against independent quadratures, for shapes from 1 to 60 and tilts
from -20 to 12, the
absolute error stayed below 1e-9; the largest found, 6e-10, is for a shape
between 2 and 3 under a positive tilt.
For a subnormal shape, below the smallest normal double 2.2e-308, the
excess kurtosis depends on the tilt
as follows.
While the mass at the peak of the density is small against the mass by
the origin (up to of about 53 at the largest subnormal shape
and 54.5 at the smallest), it grows like as
the shape falls, by a factor that depends on the tilt: 2 at and
at . It is Inf
where it passes the largest double: below a shape of about 1.1e-308 at
, at every subnormal shape for , and at none for
. Where the two masses are about equal ( from
about 53.3 to 54.7) the peak takes over, and above a tilt of 55 to 56.3
the kurtosis no longer depends on .
The function is not vectorised in its parameters: alpha,
beta and gamma must each be a single number. To evaluate
several parameter triples, use mapply, as in the examples.
The numerical methods are described in Appendix A of
vignette("theory", package = "mhn").
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), 1591–1613. (Lemmas 2a and 2b)
mhn_kurtosis(alpha = 2, beta = 1, gamma = 0) mapply(mhn_kurtosis, alpha = c(0.5, 2, 5), beta = 1, gamma = c(-1, 0, 3))mhn_kurtosis(alpha = 2, beta = 1, gamma = 0) mapply(mhn_kurtosis, alpha = c(0.5, 2, 5), beta = 1, gamma = c(-1, 0, 3))
Computes for
.
mhn_mean(alpha, beta, gamma)mhn_mean(alpha, beta, gamma)
alpha |
Shape parameter ( |
beta |
Rate-type parameter ( |
gamma |
Tilt parameter ( |
By Sun et al. (2023, Lemma 2a) the mean is a ratio of Fox-Wright
functions,
At the ratio is
(Sun et al. 2023, Lemma 6a). For every the
mean is at most
(Sun et al. 2023, Lemma 4a and
the remark after it).
The ratio is used where it can be evaluated accurately. Where it cannot,
mainly under a strong tilt or at a very large , the
mean is instead obtained by numerical integration of the unnormalised
density, which needs no value.
The function is not vectorised in its parameters: alpha,
beta and gamma must each be a single number. To evaluate
several parameter triples, use mapply, as in the examples.
The numerical methods are described in Appendix A of
vignette("theory", package = "mhn").
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), 1591–1613. (Lemmas 2a, 4a and 6a)
mhn_mean(alpha = 2, beta = 1, gamma = 0) mapply(mhn_mean, alpha = c(0.5, 2, 5), beta = 1, gamma = c(-1, 0, 3))mhn_mean(alpha = 2, beta = 1, gamma = 0) mapply(mhn_mean, alpha = c(0.5, 2, 5), beta = 1, gamma = c(-1, 0, 3))
Computes the mode of the MHN distribution. For
this is the maximiser of the density. For
the density is unbounded as
, so a non-NA result is an interior local
maximum, not the most probable value.
mhn_mode(alpha, beta, gamma)mhn_mode(alpha, beta, gamma)
alpha |
Shape parameter ( |
beta |
Rate-type parameter ( |
gamma |
Tilt 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). For
this is the boundary point 0. Sun et
al. (2023, Lemma 3d) state that no mode exists for
and ; at the density
is bounded and decreases from its supremum at , and that
point is returned. Only
itself takes this branch. The Lemma 3b value tends
to it as from above, and for
so does the Lemma 3c value from below; for
and there is no mode
(NA).
An interior local maximum exists only when and
,
and it is then located at
(Sun et al. 2023, Lemma 3c). Lemma 3c states the condition with
; the strict inequality is used here because at
the two stationary points coincide at
, where
, so the density is still
strictly decreasing and has an inflection point, not a maximum,
there. In every other case the density is strictly decreasing on
(Sun et al. 2023, Lemma 3d, for
; for the derivative
of
is negative) and NA is returned.
The function is not vectorised in its parameters: alpha,
beta and gamma must each be a single number. To evaluate
several parameter triples, use mapply, as in the examples.
A numeric scalar. For with no interior
local maximum (density strictly decreasing on ) the
result is NA; at with
the mode sits on the boundary and 0 is
returned. See Details.
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), 1591–1613. (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) # NA mhn_mode(alpha = 0.5, beta = 1, gamma = 5) # a local maximum only mapply(mhn_mode, alpha = c(0.5, 2, 5), beta = 1, gamma = c(-1, 0, 3))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) # NA mhn_mode(alpha = 0.5, beta = 1, gamma = 5) # a local maximum only mapply(mhn_mode, alpha = c(0.5, 2, 5), beta = 1, gamma = c(-1, 0, 3))
Computes the skewness
for
.
mhn_skewness(alpha, beta, gamma)mhn_skewness(alpha, beta, gamma)
alpha |
Shape parameter ( |
beta |
Rate-type parameter ( |
gamma |
Tilt parameter ( |
The skewness is formed from the second and third central moments. In exact arithmetic these follow from the mean of Sun et al. (2023, Lemma 2a) and the recurrence for the raw moments of their Lemma 2b; the function obtains them instead, wherever it can, by numerical integration of the unnormalised density, which needs no normalising constant and stays accurate under a large tilt.
Against independent quadratures, for shapes from 1 to 60 and tilts
from -20 to 12, the
absolute error stayed below 1e-9; the largest found, 3e-10, is for a shape
between 2 and 3 under a positive tilt.
For a subnormal shape, below the smallest normal double 2.2e-308, the
skewness depends on the tilt
as follows.
While the mass at the peak of the density is small against the mass by
the origin (up to of about 53 at the largest subnormal shape
and 54.5 at the smallest), it grows like
as the shape falls, by a factor
that depends on the tilt: 1.25 at and
at . It turns negative
where the two masses are about equal ( from about 53.3 to
54.7), and above a tilt of 55 to 56.3 it no longer depends on
.
The function is not vectorised in its parameters: alpha,
beta and gamma must each be a single number. To evaluate
several parameter triples, use mapply, as in the examples.
The numerical methods are described in Appendix A of
vignette("theory", package = "mhn").
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), 1591–1613. (Lemmas 2a and 2b)
mhn_skewness(alpha = 2, beta = 1, gamma = 0) mapply(mhn_skewness, alpha = c(0.5, 2, 5), beta = 1, gamma = c(-1, 0, 3))mhn_skewness(alpha = 2, beta = 1, gamma = 0) mapply(mhn_skewness, alpha = c(0.5, 2, 5), beta = 1, gamma = c(-1, 0, 3))
Computes for
.
mhn_var(alpha, beta, gamma)mhn_var(alpha, beta, gamma)
alpha |
Shape parameter ( |
beta |
Rate-type parameter ( |
gamma |
Tilt parameter ( |
Uses the formula (Sun et al. 2023, Lemma 2c)
where it is accurate. Where it would lose accuracy, under a strong tilt
of either sign and at
a large unless , the variance is
instead obtained by numerical integration of the unnormalised density.
For , the variance satisfies
(Sun et al.,
2023, Lemma 4c).
The function is not vectorised in its parameters: alpha,
beta and gamma must each be a single number. To evaluate
several parameter triples, use mapply, as in the examples.
The numerical methods are described in Appendix A of
vignette("theory", package = "mhn").
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), 1591–1613. (Lemmas 2c and 4c)
mhn_var(alpha = 2, beta = 1, gamma = 0) mapply(mhn_var, alpha = c(0.5, 2, 5), beta = 1, gamma = c(-1, 0, 3))mhn_var(alpha = 2, beta = 1, gamma = 0) mapply(mhn_var, alpha = c(0.5, 2, 5), beta = 1, gamma = c(-1, 0, 3))
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 |
Rate-type parameter ( |
gamma |
Tilt parameter ( |
lower.tail |
Logical; if |
log.p |
Logical; if |
In the general case the lower tail is computed, where that is accurate (see below), from the series representation (Sun et al. 2023, Lemma 1b)
where ,
, and is the regularized lower
incomplete gamma function.
The default parameters alpha = 1, beta = 1, gamma = 0 correspond
to the half-normal distribution
.
At and at
exactly the distribution function is computed from the law it reduces to;
a shape or a tilt merely close to those values is a different law, and is
computed as in the general case:
: , the regularized incomplete gamma function of
the square-root-Gamma law (Sun et al. 2023, Lemma 6a), for
; a larger shape is computed as in
the general case.
: the truncated-normal CDF (Sun et al. 2023,
Lemma 6b). Near the truncation point the lower tail keeps its
relative accuracy however small it is. Where
overflows the law
is Exponential with rate .
The series is used, where it is accurate, for
with , and
for up to of about 830 (for
it costs milliseconds per value); elsewhere
the lower tail is computed by numerical integration of the density. The
upper tail is computed directly, by numerical integration
of the density over , and not as
, which would round to 0 once it falls below about
. Each tail is computed directly on its own side of
the peak of the density of , which lies at or above
the median, and as one minus the other tail beyond it. For a large shape
or tilt, where the terms of these forms grow too large, both tails are
measured from that peak instead. With log.p = TRUE both tails are
formed on the log scale throughout, so that is still
resolved where rounds to 1 or is below the smallest positive
double, and a log probability close to 0, about minus the other tail,
keeps the relative accuracy of that other tail to within about
(Appendix A.4 of the vignette cited below).
Accuracy and range. With
as above, for
,
and in
both tails are accurate to
the working tolerance sqrt(.Machine$double.eps) within 10 standard
deviations of the peak, and to about twice that beyond. Outside that
range the law can be narrower than the spacing of the doubles near its
peak (for above about , or
above about ); the probabilities
are then still those of the exact law at the doubles q given, a
step from 0 to 1 between two adjacent doubles, whose value at the double
nearest the peak is set by where the peak lies within its spacing. Two
cases give NA with a warning instead: within 45 standard
deviations of the peak of a law whose peak cannot be placed to within
of its standard deviation, which needs a law
narrower than the spacing by a factor of order or
more and then happens for some parameters only (as at
);
and where the whole law lies beyond the largest
double without a positive tilt to place it (a shape near the largest
double against a subnormal ). A third case lies at the
other end of the range: where and the tilt are both
close to the largest double ( from about
with near the
largest double, or from about with
from about ), at some points
above the peak where is above about half
the largest double, the log upper tail is -Inf where it is finite
(the upper tail itself is then 0, which is its value in double
precision), and next to the point where overflows
both are NA with a warning.
Where itself overflows, the law is its limit:
Gamma(, rate ) under a negative
tilt, and normal about its mode with variance
under a positive one. A subnormal shape,
below the smallest normal double, is computed apart (see
dmhn), and there keeps its relative
accuracy too; below a tilt of about 52 (53 at the smallest
shapes) pmhn is then 1 at every positive q.
When any of alpha, beta, gamma is a vector, the CDF
is evaluated element-wise. The Fox-Wright normalizing
constant is computed once for each position of the recycling cycle while
that cycle is at most 256 long, and otherwise once for each run of
consecutive elements that share the
triple. So recycled parameters, and parameter vectors in which equal
triples are adjacent, are much faster than triples that alternate along a
long vector, and than calling pmhn inside an R loop.
The numerical methods, and the reasons for the ranges above, are
described in Appendix A of vignette("theory", package = "mhn").
A numeric vector. The output length equals
max(length(q), length(alpha), length(beta), length(gamma)), or 0
if any of them has length zero; 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.
As in pgamma, an element whose parameters lie
outside the parameter space (alpha or beta not positive
and finite, or gamma not finite) is NaN, with a warning,
and an element with an NA or NaN among its inputs is
NA or NaN (NA if any of them is NA),
without one; the other elements are unaffected. The arguments must be
numeric (double, integer or logical): a factor, a character string, a
complex number, a list or NULL is an error, as in
pnorm.
As with pnorm, the attributes of q (names,
dim, dimnames) are kept when the result has the length of
q; a classed q, such as a Date, is taken as its numbers
and gives a plain vector.
Unlike pnorm, attributes of a longer parameter vector are not
carried over.
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), 1591–1613.
# 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 |
Rate-type parameter ( |
gamma |
Tilt parameter ( |
lower.tail |
Logical; if |
log.p |
Logical; if |
For the general case, is obtained by a
root-finder applied to the distribution function computed as in
pmhn, or for lower.tail = FALSE to the logarithm of
its directly computed upper tail.
The default parameters alpha = 1, beta = 1, gamma = 0 correspond
to the half-normal distribution
.
At and at
exactly the quantile is computed from the law it reduces to; a shape or a
tilt merely close to those values is a different law, and is computed as
in the general case:
: ,
where for the regularized incomplete gamma
function (Sun et al. 2023, Lemma 6a), for
; a larger shape is computed as in
the general case.
: the truncated-normal inverse (Sun et al. 2023,
Lemma 6b). The quantile of a small lower-tail probability keeps its
relative accuracy. Where overflows the law is Exponential with rate
, and its quantile is that one's.
A probability close to 1 is solved, where that is more accurate, on the
other tail for one minus it, which is formed exactly: a lower-tail
p on the upper tail, as the root of
, since the distribution
function is resolved there only to the rounding of 1 (and, for a small
shape, to the error of the lower tail; see pmhn), and an
upper-tail p on the distribution function. Where pmhn
takes the distribution function from its series
( with up to about 830, and
with ,
), a lower-tail
p is solved on the series itself, and for
with from about 100 one
quantile can take from a tenth of a second to seconds (about 1 s at
, and
); where is known exactly,
lower.tail = FALSE gives the same quantile at once.
Accuracy and range. The root is found to a relative
, and where pmhn measures the law from its
peak (see there) also to within of the law's
standard deviation, which can be narrower. The quantile inverts
pmhn, whose range of accuracy it inherits: for
,
and in
it is the root, to that
tolerance or to the spacing of the doubles near it, whichever is larger,
of an equation whose tail probabilities are as accurate as those of
pmhn (to the working tolerance within 10 standard deviations of
the peak, and to about twice that beyond; see there), which places it
within about that many of the law's standard deviations.
Next to the origin, where is about a multiple of
, its relative error is that of the smaller tail
probability divided by about . A quantile below
.Machine$double.xmin is resolved to the spacing of the subnormal
doubles there, about , and one below
that spacing is returned as 0, as qgamma returns a
quantile that underflows; a quantile beyond the largest double is
returned as Inf, as qgamma returns one that overflows.
Where the law is narrower than the spacing of the doubles (for
above about , or above
about ) its distribution function is a step between
two adjacent doubles, and the quantile of every p strictly inside
is the one of the two nearer to the true quantile, placed
from the exact offset of the law's peak. Where pmhn is NA
(see there), so is the quantile, with a warning. Where the log upper
tail of pmhn is -Inf in place of a finite value
( and the tilt both close to the largest double, see
there), the quantile of a log.p probability beyond that point is
the point at which it turns -Inf, without a warning. For a
subnormal shape (see dmhn) and a tilt below about
52 (53 at the smallest shapes), qmhn is 0 for every lower-tail
probability below 1 given on the probability scale; the upper tail, or
log.p = TRUE, still gives the quantiles of the far upper tail.
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
computed once for each position of the recycling cycle while that cycle
is at most 256 long, and otherwise once for each run of consecutive
elements that share the triple.
The numerical methods, and the reasons for the ranges above, are
described in Appendix A of vignette("theory", package = "mhn").
A numeric vector. The output length equals
max(length(p), length(alpha), length(beta), length(gamma)), or 0
if any of them has length zero; each input is recycled to that length
following standard R recycling rules.
As in qgamma, an element whose parameters lie
outside the parameter space (alpha or beta not positive
and finite, or gamma not finite) is NaN, with a warning,
and an element with an NA or NaN among its inputs is
NA or NaN (NA if any of them is NA),
without one; the other elements are unaffected. The arguments must be
numeric (double, integer or logical): a factor, a character string, a
complex number, a list or NULL is an error, as in
qnorm.
qmhn(0) = 0 and qmhn(1) = Inf. Probabilities outside
(log probabilities above 0 when log.p = TRUE)
yield NaN, with a warning as in qnorm. If
the distribution function cannot be evaluated where the root is sought,
a warning is issued and NA is returned for that element. The
attributes of p (names, dim, dimnames) are kept
when the result has the length of p; a classed p, such
as a Date, is taken as its numbers and gives a plain vector.
Unlike qnorm, attributes of a longer parameter vector are not
carried over.
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), 1591–1613.
# 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 |
Number of variates to draw: a non-negative number no larger than
|
alpha |
Shape parameter ( |
beta |
Rate-type parameter ( |
gamma |
Tilt parameter ( |
method |
Sampling algorithm. One of |
The MHN density is
where is the Fox-Wright function
.
rmhn does not evaluate ; the rejection-sampling kernels
cancel it out. The one exception is the weight of the origin,
, that it draws for a subnormal shape (see Subnormal
shapes below).
The default parameters alpha = 1, beta = 1, gamma = 0 correspond
to the half-normal distribution .
Two closed-form special cases are taken first, whatever method:
a negligible tilt, within
sqrt(.Machine$double.eps) of 0, is drawn as the square root of a
Gamma variate, and a shape within
sqrt(.Machine$double.eps) of 1 from a truncated normal. Both are
exact samplers of their special-case law, which next to
or is within a
total variation distance of of the law
requested. The one exception is a negligible tilt above
, which is drawn by the Normal
proposal of Sun et al. (2023) Algorithm 1 centred at the mode, or by RTDR
under method = "rtdr" (Sun et al. state Algorithm 1 for
; centred at the mode, its Normal envelope
holds for either sign). Otherwise method selects the
rejection sampler:
"auto" (default): the faster of the provably correct
samplers, as measured by timing them against each other. For
and it uses
RTDR. Otherwise it chooses between Sun et al. Algorithm 1
() or Algorithm 3
() and RTDR by the shape, the tilt
and the number of variates
per setup (see Vector parameters and setups); Section 7 of
vignette("theory", package = "mhn") gives the decision tree.
The choice changes the speed and the stream of draws
(see Reproducibility), not the law drawn.
"rtdr": the Relaxed Transformed Density Rejection method of
Gao & Wang (2025). Gao & Wang bound its acceptance probability below
by for every parameter value,
provided the contact points of the envelope lie in the band of their
Eq. (7); the search for them does not always reach that band,
and the acceptance measured stays above the bound there too. Where
RTDR cannot place a law of tiny shape under a strong negative tilt,
Sun et al. Algorithm 3 is used instead (see Limits of double
precision). 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": the algorithms of Sun et al. (2023): Algorithm 1
for and , and Algorithm 3 for
, up to , beyond
which the call stops with an error (see Limits of double precision).
The combination with is
unsupported: unless a special case answers it first, the call stops
with the error Sun method not available for alpha<1 and
gamma>0; use method="rtdr".
Vector parameters are recycled to length n following standard R
rules: only the first n elements of each parameter are used, and any
further elements are silently ignored, matching the convention of
rnorm.
The setup of the chosen sampler is built once for each run of equal
consecutive triples. Short parameter
vectors repeat their triples after a period, the least common multiple
of the lengths of alpha, beta and gamma; when that
period is at most 256 draws and they are recycled over at least four
such periods, a setup is kept for each position within the period. So
passing parameters grouped by triple, or as short recycled vectors, is
faster than calling rmhn inside an R loop, and a long run of one
triple is drawn fastest by its own scalar call.
The number of variates per setup, from which method = "auto"
chooses its sampler, and which therefore changes the draws for a fixed
seed, is n for scalar parameters, and n divided by the
period, rounded down, where a setup is kept for each position within the
period. Otherwise it is 1: parameters given as vectors of length
n count one per setup, as in a Gibbs sampler, even when equal
triples are grouped into runs, and such a call draws what the loop of
single draws would. Grouping saves setups but does not change this
number, and so not the choice of sampler.
Under one seed, method = "rtdr" and method = "sun" draw one
stream whatever n: the first k draws of rmhn(n, ...)
are those of rmhn(k, ...), and draws split over several calls are
those of one call. The default method does not keep this, as it chooses
its sampler from the number of variates per setup (see above):
rmhn(4, 5, 1, 2) and the first four draws of
rmhn(100, 5, 1, 2) come from different samplers. Its stream
therefore changes with n, with how the parameters are arranged,
and with a version of the package that moves a threshold, although the law
drawn does not. Where the draws must not depend on n, as when a
pilot run is compared with the main one, set method = "rtdr" or
method = "sun".
Double precision limits which laws can be sampled, and how finely. Every
sampler draws from the exact law and rounds the draw to double precision,
and a draw below the smallest positive double is returned as 0. A
rounding error that would shift every draw alike is kept to a few times
of a standard deviation. For this reason the
square-root-Gamma closed form is used only up to
, and Sun et al. Algorithm 3 only up
to (about
). Above
a negative tilt that is not
negligible is drawn by RTDR under the default method, while
method = "sun" stops with an error; a positive tilt is drawn by
Algorithm 1 under both.
The RTDR sampler stops with an error for the whole call once the standard
deviation is no larger than the spacing of the double precision numbers
at the mode, so that its draws could take only a few distinct values:
from of about
( at ),
and, for , from between
and
( to ),
depending on the tilt and on where the mode falls between two powers of
two. There the law is normal to far below double precision, with mean
= mhn_mode(alpha, beta, gamma) and variance
, and
can be drawn from directly, as the error message suggests. Sun et al.
Algorithm 1, which the default method uses for such laws with
and , and the truncated normal of
do not stop there: their draws
are placed at the mode to within a few units in the last place, so that
they take only a few distinct values near it.
RTDR cannot place the law in three cases, all under a negative tilt:
where passes the largest
double; for , where is below
about times
, which needs
below , and almost
all the mass of lies below the smallest positive double; and for
a shape of a few, where
comes within a small factor of the largest double. In the second case,
and in the first for ,
method = "rtdr" draws from Algorithm 3, which samples the law
exactly there, except for a subnormal shape, which is drawn as described
under Subnormal shapes; otherwise it stops with
an error that names the Gamma distribution with shape and
rate , which the law is to double precision. The
default method uses Algorithm 3 in all three cases, except where
passes the largest double
at a shape above ; there it stops as RTDR does. Where
the distribution lies beyond the largest double precision number, no draw
can represent it, and every sampler, the closed forms included, stops
with an error.
A subnormal shape, below the smallest normal double
, is drawn as the mixture its law is
(see the Details of dmhn): 0 with probability
, the probability of a value below the smallest positive
double, and otherwise from the peak, through a proxy: the law of the
shape above
, drawn by the sampler
method selects for that shape. The choice between 0 and the peak
compares one uniform variate with , and so has the
resolution of the generator, for R's default. Where
is 1 in double precision, below a tilt of about 52 (53
at the smallest shapes), every draw is 0 and uses no random number; a
shape from up is drawn by the samplers above,
which use random numbers there too, so that the stream after the draws
moves as the shape crosses although every draw
is 0 on either side. A refusal names the shape asked for.
The algorithms, the timings behind the choice of method = "auto",
and the handling of extreme parameters are described in
vignette("theory", package = "mhn") (Section 7 and Appendix A).
A numeric vector of length n. As in
rnorm, an element whose parameters (after
recycling to length n) are NA or NaN, or lie
outside the parameter space (alpha or beta not positive
and finite, or gamma not finite), is NaN, with a single
warning "NAs produced", and draws no random numbers. A zero-length
alpha, beta or gamma makes every element
NA, with the same warning. The parameters must be numeric
(double, integer or logical): a factor, a character string, a complex
number, a list or NULL is an error. If a rejection sampler
accepts none of 1000 proposals for an element (for a subnormal shape,
none of 1000 draws of the proxy described under Subnormal shapes in
Details, each of up to 1000 proposals),
that element is NaN, and a single warning reports how many draws
were affected;
the acceptance rates of the samplers make this vanishingly unlikely.
Where the distribution lies beyond the largest double precision number,
and at some other extreme values, rmhn instead stops with an
error for the whole call (see Details).
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), 1591–1613. doi:10.1080/03610926.2021.1934700
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, 1–25. Advance online publication. doi:10.1080/03610918.2025.2524551
Robert, C. P. (1995). Simulation of truncated normal variables. Statistics and Computing, 5(2), 121–125. doi:10.1007/BF00143942
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))