Skip to contents

Scope

This vignette is the theoretical companion to Introduction to the mhn Package. The Introduction shows what the four exported functions (dmhn / pmhn / qmhn / rmhn) and their moment helpers do; this note develops the mathematical and algorithmic theory the package implements. We summarise the Modified Half-Normal (MHN) family, the Fox–Wright Ψ\Psi function that appears in its normalising constant, and the two sampling schemes the package draws on — Sun, Kong & Pal (2023) and Gao & Wang (2025).

We deliberately keep the treatment compact. Full proofs and the finer numerical considerations live in the original papers (see References). Two implementation-relevant points — the typesetting errata in Sun et al. (2023) that the package silently corrects, and the benchmark-driven rationale for the rmhn(method = "auto") crossover at 25 — are inserted in §4 and §7 where they are most relevant.

The MHN(α\alpha, β\beta, γ\gamma) family

The MHN distribution has support (0,)(0, \infty) and density

f(xα,β,γ)=2βα/2xα1exp(βx2+γx)Ψ[α/2,γ/β],x>0,f(x \mid \alpha, \beta, \gamma) = \frac{2 \beta^{\alpha/2} \, x^{\alpha-1} \, \exp(-\beta x^2 + \gamma x)} {\Psi[\alpha/2,\, \gamma/\sqrt{\beta}]}, \qquad x > 0,

with α>0\alpha > 0, β>0\beta > 0, γ\gamma \in \mathbb{R}. The three parameters control three independent factors of the density kernel:

Parameter Factor it controls Role
α\alpha xα1x^{\alpha-1} shape
β\beta exp(βx2)\exp(-\beta x^2) Gaussian tail
γ\gamma exp(γx)\exp(\gamma x) exponential tilt

Three familiar distributions sit inside the family as exact special cases (Sun et al. 2023, Lemma 6):

Constraint Reduction
γ=0\gamma = 0 X2Gamma(α/2,β)X^2 \sim \mathrm{Gamma}(\alpha/2,\, \beta), i.e. sqrt-Gamma
α=1\alpha = 1 Truncated normal TN(γ/(2β),1/2β,0,)\mathrm{TN}(\gamma/(2\beta),\, 1/\sqrt{2\beta},\, 0,\, \infty)
α=1,γ=0\alpha = 1,\, \gamma = 0 Half-normal |Z|\lvert Z \rvert with ZN(0,1/(2β))Z \sim N(0,\, 1/(2\beta))

There is also a degenerate boundary limit, not listed in the table: as β0+\beta \to 0^+ with γ<0\gamma < 0, the MHN density converges to Gamma(α,γ)\mathrm{Gamma}(\alpha,\, -\gamma). The package does not handle this limit specially because β>0\beta > 0 is required by the MHN parameter space; for a Gamma draw with β=0\beta = 0, use stats::rgamma directly.

For each of the three finite special cases in the table above — γ=0\gamma = 0, α=1\alpha = 1, and both together — the package detects the constraint within sqrt(.Machine$double.eps) tolerance and dispatches to the corresponding closed-form primitive in stats, so all four exported functions (dmhn / pmhn / qmhn / rmhn) are exact in those three regimes, bypassing the general series-and-rejection machinery.

x  <- seq(0.001, 4, length.out = 400)
plot(x, dmhn(x, alpha = 2,   beta = 1, gamma = 1), type = "l", lwd = 2,
     ylim = c(0, 1.4), ylab = "f(x)",
     main = "MHN density at beta = 1, gamma = 1")
lines(x, dmhn(x, alpha = 5,   beta = 1, gamma = 1), lwd = 2, col = "steelblue")
lines(x, dmhn(x, alpha = 1,   beta = 1, gamma = 1), lwd = 2, col = "tomato")
lines(x, dmhn(x, alpha = 0.5, beta = 1, gamma = 1), lwd = 2, col = "darkorange")
legend("topright", bty = "n", lwd = 2,
       col = c("darkorange", "tomato", "black", "steelblue"),
       legend = c("alpha = 0.5  (no interior mode)",
                  "alpha = 1    (truncated normal)",
                  "alpha = 2    (log-concave)",
                  "alpha = 5    (log-concave, shifted right)"))
MHN density as alpha crosses the log-concavity threshold; beta = 1, gamma = 1 fixed.

MHN density as alpha crosses the log-concavity threshold; beta = 1, gamma = 1 fixed.

The figure illustrates the qualitative claims developed in the next section. The density is log-concave whenever α1\alpha \geq 1 (Sun et al. 2023, Lemma 3a). For α>1\alpha > 1 it has a finite interior mode in closed form (Sun et al. 2023, Lemma 3b); the borderline case α=1\alpha = 1 reduces to the truncated normal, whose mode is max(0,γ/(2β))\max(0,\, \gamma/(2\beta)) — interior when γ>0\gamma > 0, on the boundary x=0x = 0 otherwise. For α<1\alpha < 1 the density is typically monotone decreasing with a boundary singularity at x0+x \to 0^+. The one exception is γ>0\gamma > 0 together with α1γ2/(8β)\alpha \geq 1 - \gamma^2/(8\beta), where the density is not unimodal: it diverges at x0+x \to 0^+, dips to a local minimum, rises to a single interior local maximum, and then decays (Sun et al. 2023, Lemma 3c). At (β,γ)=(1,1)(\beta, \gamma) = (1, 1) this threshold is 11/8=0.8751 - 1/8 = 0.875, so the orange (α=0.5\alpha = 0.5) curve in the figure sits in the strict monotone-decreasing regime, with no interior local maximum.

Shape and moments

Three facts from Sun et al. (2023) drive everything the package does with shape, moments, and the mode.

Log-concavity is a function of α\alpha alone (Sun et al. 2023, Lemma 3a). For α1\alpha \geq 1 the density is log-concave irrespective of β\beta and γ\gamma; for α<1\alpha < 1 it is not. This single threshold is what splits the Gao & Wang (2025) RTDR sampler into its log-axis and original-axis branches in §6 below.

The mode has a closed form for α>1\alpha > 1 (Sun et al. 2023, Lemma 3b):

Xmode=γ+γ2+8β(α1)4β.X_{\mathrm{mode}} = \frac{\gamma + \sqrt{\gamma^2 + 8\beta(\alpha - 1)}}{4\beta}.

For α=1\alpha = 1 the mode is max(0,γ/(2β))\max(0,\, \gamma/(2\beta)) (the truncated-normal mode, Sun et al. 2023, Lemma 6b). For α<1\alpha < 1 the density is either monotone decreasing or develops a non-trivial local maximum and local minimum, depending on the sign of 1γ2/(8β)α1 - \gamma^2/(8\beta) - \alpha (Sun et al. 2023, Lemma 3c–d). The helper mhn_mode() implements this case split and returns NA when no interior mode exists.

Moments satisfy a two-term recurrence (Sun et al. 2023, Lemma 2b):

E(Xk+2)=α+k2βE(Xk)+γ2βE(Xk+1).E(X^{k+2}) = \frac{\alpha + k}{2 \beta}\, E(X^{k}) + \frac{\gamma}{2 \beta}\, E(X^{k+1}).

Together with the closed-form ratio for E(X)E(X) in terms of Ψ\Psi (Sun et al. 2023, Lemma 2a), this gives every higher moment. The helpers mhn_mean(), mhn_var(), mhn_skewness() and mhn_kurtosis() apply the recurrence in C++ via Rcpp; the Introduction vignette shows them in action. A useful sanity bound (Sun et al. 2023, Lemma 4c) is Var(X)1/(2β)\mathrm{Var}(X) \leq 1/(2\beta) for every α1\alpha \geq 1, γ\gamma \in \mathbb{R}.

The Fox–Wright Ψ\Psi normalising constant

The normalising constant in the MHN density is

Ψ[α2,z]=n=0Γ(α+n2)n!zn,z=γβ.\Psi\!\left[\frac{\alpha}{2},\, z\right] \;=\; \sum_{n=0}^{\infty} \frac{\Gamma\!\big(\tfrac{\alpha + n}{2}\big)}{n!} \, z^{n}, \qquad z = \frac{\gamma}{\sqrt{\beta}}.

This series is absolutely convergent for every α>0\alpha > 0 and every zz \in \mathbb{R}, and is strictly positive. It depends on (α,β,γ)(\alpha, \beta, \gamma) only through the two-dimensional combination (α,z)(\alpha, z).

A useful organisational point: Ψ\Psi matters only for some of the package’s exported functions.

Function group Needs Ψ\Psi? Why
dmhn, pmhn, qmhn yes Ψ\Psi sits in the density’s denominator and in the CDF series.
mhn_mean, mhn_var, mhn_skewness, mhn_kurtosis yes Moments are ratios Ψ[(α+k)/2,z]/Ψ[α/2,z]\Psi[(\alpha+k)/2,\, z] / \Psi[\alpha/2,\, z].
rmhn (Sun or RTDR path) no Ψ\Psi enters both proposal scale and target as a common factor that cancels.

The practical consequence is that any numerical issues in evaluating Ψ\Psi — accumulated rounding in lgamma, or truncation error in the series — can only affect d/p/q and the moment helpers; they cannot leak into the sampler.

For evaluation, the package follows the case split of Sun et al. (2023, Lemma 9–11):

  • γ=0\gamma = 0 gives Ψ[α/2,0]=Γ(α/2)\Psi[\alpha/2, 0] = \Gamma(\alpha/2).
  • α=1\alpha = 1, and the α=2,γ0\alpha = 2,\, \gamma \geq 0 corner, both reduce to closed forms involving Φ\Phi (Sun et al. 2023, Lemma 9c).
  • γ>0\gamma > 0 uses the alternating-free series of Sun et al. (2023, Lemma 10) with a pre-computed truncation point. All terms are positive, so there is no catastrophic cancellation.
  • γ<0\gamma < 0 switches to the integral representation (Sun et al. 2023, Lemma 11) and uses Boost’s gauss_kronrod (for α1\alpha \geq 1) or tanh_sinh (for α<1\alpha < 1, where the boundary singularity uα1u^{\alpha-1} needs the double-exponential rule).

Errata in Sun et al. (2023)

The published paper has two typesetting errors that touch the implementation. Each is corrected in the supplementary derivation, and the package follows the corrected form.

  • Mixture weights pip_{i}in Lemma 7. The main-text statement of Lemma 7 gives the weights for the Gamma\sqrt{\mathrm{Gamma}} mixture representation as pi=12βα/2Γ((α+1+i)/2)(γ/β)ii!Ψ[α/2,γ/β]p_{i} = \tfrac{1}{2\beta^{\alpha/2}} \cdot \tfrac{\Gamma((\alpha + 1 + i)/2)\, (\gamma/\sqrt{\beta})^{i}} {i!\, \Psi[\alpha/2,\, \gamma/\sqrt{\beta}]}. These do not sum to one, contradicting the definition of Ψ\Psi and the Poisson–Gamma hierarchical representation in Sun et al. (2023, Lemma 5a). The correct form, reproduced in the supplementary proof of Lemma 7 (Supplementary §2.13), is pi=Γ((α+i)/2)(γ/β)ii!Ψ[α/2,γ/β]p_{i} \;=\; \frac{\Gamma((\alpha + i)/2)\, (\gamma/\sqrt{\beta})^{i}} {i!\, \Psi[\alpha/2,\, \gamma/\sqrt{\beta}]} i.e. drop the leading 1/(2βα/2)1/(2\beta^{\alpha/2}) and use α+i\alpha + i in the Γ\Gamma argument instead of α+1+i\alpha + 1 + i. This affects Algorithm 2 only, which the package does not implement; the correction is recorded here for completeness.

  • Series truncation discriminants C1C_{1}, C2C_{2}in Lemma 10. The truncation point of the Ψ\Psi series is the smallest kk for which A(k+1)/A(k)qA(k+1)/A(k) \leq q (with an analogous bound for BB). Reducing this to a quadratic in kk gives a discriminant whose last term is αx2\alpha x^{2} (and (α+1)x2(\alpha+1) x^{2} for C2C_{2}). The main-text statement of Lemma 10 prints these as αx\alpha x and (α+1)x(\alpha+1) x — a single missing power of xx. The supplementary derivation (Supplementary §2.2.1, §2.2.2) carries z2z^{2} throughout, in agreement with the rederivation. The corrected x2x^{2} form is what the package uses in src/mhn_psi_series.cpp (marked with an // ERRATA comment); evaluating Ψ\Psi at the published xx form would give a too-small truncation point and silently lose precision.

Sampling: Sun et al. (2023)

Sun et al. (2023) propose three Accept–Reject schemes, indexed by the sign of γ\gamma and a coarse split on α\alpha. Two of them — Algorithm 1 and Algorithm 3 — are implemented in the package and reachable via rmhn(method = "sun"). Algorithm 2 is not, and this section explains the reason.

Algorithm 1 — γ>0\gamma > 0, α1\alpha \geq 1

For each parameter triple, Algorithm 1 constructs two proposal distributions — a Normal with mean μopt\mu_{\mathrm{opt}} and variance 1/(2β)1/(2\beta), and a Gamma(α/2,δopt)\sqrt{\mathrm{Gamma}(\alpha/2,\, \delta_{\mathrm{opt}})} — each with a multiplicative envelope constant K1K_{1} and K2K_{2}. Setup chooses whichever envelope is tighter (K1K2K_{1} \leq K_{2} or the reverse). The two optima μopt\mu_{\mathrm{opt}} and δopt\delta_{\mathrm{opt}} have closed forms (Sun et al. 2023, Theorem 1b), so setup is cheap. The acceptance probability is bounded below by 0.8 for α4\alpha \geq 4 (Sun et al. 2023, Theorem 2e). For 1α<41 \leq \alpha < 4 no uniform lower bound is proved, but the original Figure 2 shows empirically high acceptance throughout that range.

Algorithm 3 — γ0\gamma \leq 0

When the tilt is non-positive, Theorem 3 of Sun et al. (2023) supplies a proposal kernel based on an AM–GM-type inequality. With a tuning point m>0m > 0 and the exponent r=(βm+|γ|)/(2βm+|γ|)r = (\beta m + |\gamma|)/(2\beta m + |\gamma|), the candidate is

TGamma(αr,m(βm+|γ|)),X=mTr.T \sim \mathrm{Gamma}\!\big(\alpha \cdot r,\, m(\beta m + |\gamma|)\big), \qquad X = m \cdot T^{r}.

The construction works for every α>0\alpha > 0; the uniform acceptance bound 1/20.707\geq 1/\sqrt{2} \approx 0.707 (Sun et al. 2023, Theorem 4c) is proved only for α>1\alpha > 1, but the procedure also samples correctly when α1\alpha \leq 1. The matching point is initialised from a closed-form heuristic of Sun et al. (2023, §4.2): minit=α2/(1+α)m_{\mathrm{init}} = \alpha^{2}/(1 + \alpha) for α1.1\alpha \leq 1.1 and a more elaborate expression based on the mode and the rightmost inflection point for α>1.1\alpha > 1.1. The package then applies a single Newton-Raphson refinement step in both regimes, producing mrecommendm_{\mathrm{recommend}}, whose acceptance probability is within 0.2%0.2\% of the optimum across the parameter range tabulated in Sun et al. (2023, Table 1).

Why Algorithm 2 is not implemented

Algorithm 2 targets γ>0\gamma > 0 with α<1\alpha < 1 via the mixture representation

fMHN(xα,β,γ)=i=0pifi(xα,β),fiGamma((α+i)/2,β).f_{\mathrm{MHN}}(x \mid \alpha, \beta, \gamma) \;=\; \sum_{i=0}^{\infty} p_{i}\, f_{i}(x \mid \alpha, \beta), \qquad f_{i} \;\sim\; \sqrt{\mathrm{Gamma}\!\big((\alpha + i)/2,\, \beta\big)}.

The mixture-component sampler needs a truncation point MM^{\dagger} chosen so that the tail mass beyond MM^{\dagger} is below a target tolerance (Sun et al. 2023, Lemma 8). For large γ2/β\gamma^{2}/\beta this truncation grows like γ2/(ε2β)\gamma^{2}/(\varepsilon^{2} \beta), and the whole scheme degrades catastrophically — Gao & Wang (2025, Table 1) report a slowdown of up to 3×104×\sim 3 \times 10^{4}\times against RTDR in the most extreme case (α,γ)=(0.01,10000)(\alpha, \gamma) = (0.01, 10000). The package therefore declines to implement Algorithm 2 and routes (α<1,γ>0)(\alpha < 1, \gamma > 0) to RTDR, where the acceptance bound 1/e\geq 1/e (next section) holds uniformly.

Sampling: Gao & Wang (2025) RTDR

Gao & Wang (2025) introduce a Relaxed Transformed Density Rejection (RTDR) sampler that comes with a uniform lower bound on the acceptance probability across the entire parameter space. Three ideas drive the construction.

TcT_{c}-concavity

Standard TDR builds piecewise-linear envelopes on top of a log-transformed density. RTDR generalises to the TcT_{c} family

Tc[x]=sign(c)xc(c>1,c0),T0[x]=logx.T_{c}[x] = \mathrm{sign}(c) \cdot x^{c} \quad (c > -1,\, c \neq 0), \qquad T_{0}[x] = \log x.

A density ff is TcT_{c}-concave if Tc[f]T_{c}[f] is concave. Logarithmic concavity (the usual TDR setting) is the c=0c = 0 special case. The package’s RTDR implementation uses c=1/2c = -1/2 on the log-axis density for the α<1\alpha < 1 branches because T1/2[x]=x1/2T_{-1/2}[x] = -x^{-1/2} accommodates the slower decay of g(y)g(y) when the original ff has a boundary singularity at x=0x = 0.

Working on the log axis when α<1\alpha < 1

When α<1\alpha < 1 the density f(xα,γ)f(x \mid \alpha, \gamma) on (0,)(0, \infty) blows up at the left endpoint and is not amenable to direct envelope construction. The change of variable y=logxy = \log x yields

g(yα,γ)=exp(αye2y+γey),y,g(y \mid \alpha, \gamma) \;=\; \exp\!\big( \alpha y - e^{2y} + \gamma e^{y} \big), \qquad y \in \mathbb{R},

which is bounded and unimodal (Gao & Wang 2025, Lemma 3.3). RTDR operates on gg for α<1\alpha < 1 and on ff directly for α1\alpha \geq 1.

The four regions

After scale normalisation βXX\sqrt{\beta} X \to X (so we may assume β=1\beta = 1), the RTDR envelope construction splits into four regions based on (α,γ)(\alpha, \gamma). The γ\gamma in the table below is the normalised tilt γnorm=γ/β\gamma_{\mathrm{norm}} = \gamma/\sqrt{\beta}; for a general β\beta the package’s rmhn(method = "rtdr") rescales internally and compares γnorm\gamma_{\mathrm{norm}} against the boundary 2(112α)2(1 - \sqrt{1 - 2\alpha}).

Region α\alpha γ\gamma Envelope target Reference
(a) 1\geq 1 any ff log-concave Gao & Wang (2025, Theorem 3.1)
(b) [1/2,1)[1/2,\, 1) any ggT1/2T_{-1/2}-concave Gao & Wang (2025, Corollary 3.1; Theorem 3.2)
(c) (0,1/2)(0,\, 1/2) 2(112α)\leq 2(1 - \sqrt{1 - 2\alpha}) ggT1/2T_{-1/2}-concave Gao & Wang (2025, Lemma 3.5; Theorem 3.2)
(d) (0,1/2)(0,\, 1/2) >2(112α)> 2(1 - \sqrt{1 - 2\alpha}) gg with inflection at y*=log(γ/4)y^{*} = \log(\gamma/4) Gao & Wang (2025, Theorem 4.4)

Region (a) is the easiest: ff is log-concave, two tangent lines and a constant segment suffice. Regions (b) and (c) reuse the same T1/2T_{-1/2} envelope on gg, with only the validity argument differing. Region (d) is the hardest — gg has an inflection point and the envelope needs KK secant segments on the log-convex side, with KK growing logarithmically in γ\gamma.

Why “relaxed”

The classical TDR construction asks for optimal tangent contact points, which generally have no closed form and must be found by Newton iteration. Gao & Wang (2025, Theorem 2.1) show that the acceptance bound e2.719\leq e \approx 2.719 (equivalently, acceptance probability 1/e0.368\geq 1/e \approx 0.368) holds as long as the contact points lie anywhere inside a fixed interval — namely

logf(m)f(t){[0.46,2.49]f log-concave (region (a)),[0.93,1.99]gT1/2-concave (regions (b), (c)).\log\!\frac{f(m)}{f(t)} \in \begin{cases} [0.46,\, 2.49] & f \text{ log-concave (region (a))}, \\ [0.93,\, 1.99] & g \text{ $T_{-1/2}$-concave (regions (b), (c))}. \end{cases}

Setup is therefore a handful of cheap iterations that terminate as soon as the bracket is hit, with no need for a precise Newton solution. This is the main practical edge of RTDR over Algorithms 1 / 3 of Sun et al. (2023) in Gibbs-style workloads where (α,β,γ)(\alpha, \beta, \gamma) changes every iteration: the setup cost is amortised over a single draw instead of many, and a cheap setup wins.

Composing the bounds in Gao & Wang (2025, Theorems 3.1, 3.2, 4.4) yields the headline guarantee of RTDR: for every (α,β,γ)(\alpha, \beta, \gamma) with α>0\alpha > 0, β>0\beta > 0, γ\gamma \in \mathbb{R}, the acceptance probability is at least 1/e0.3681/e \approx 0.368. The three algorithms of Sun et al. (2023) only have parameter-dependent guarantees (and only on parts of the parameter space — see §5).

A quick sanity check that the RTDR sampler converges to the right distribution in all four regions — empirical means from RTDR vs theoretical means across regions (a), (b), (c), (d), each based on 5000 draws:

cases <- data.frame(
  region = c("(a)", "(b)", "(c)", "(d)"),
  alpha  = c(2.0,    0.7,    0.3,    0.3),
  gamma  = c(0.5,    1.0,    0.5,    5.0)
)
set.seed(42)
empirical <- mapply(
  function(a, g) mean(rmhn(5000, alpha = a, beta = 1, gamma = g, method = "rtdr")),
  cases$alpha, cases$gamma
)
theoretical <- mapply(mhn_mean, cases$alpha, beta = 1, cases$gamma)
data.frame(cases,
           empirical_mean   = round(empirical,   4),
           theoretical_mean = round(theoretical, 4))
#>   region alpha gamma empirical_mean theoretical_mean
#> 1    (a)   2.0   0.5         1.0126           1.0016
#> 2    (b)   0.7   1.0         0.6442           0.6424
#> 3    (c)   0.3   0.5         0.2972           0.2823
#> 4    (d)   0.3   5.0         2.3321           2.3221

The four rows hit the right means to two-to-three decimal places — about the sampling tolerance at n=5000n = 5000.

The rmhn(method = "auto") dispatch

rmhn exposes three values of method: "sun" (force the Sun Algorithm 1 / 3 path), "rtdr" (force the Gao & Wang RTDR path), and "auto" (default). The "auto" route is the package’s recommendation; it composes the four shortcuts above into one decision tree (this is the Step 3.7.3 final form, confirmed by the benchmarks under inst/benchmarks/):

                  rmhn(n, alpha, beta, gamma, method = "auto")
                                    │
                                    ▼
       ┌─ alpha = 1, gamma = 0  →  half-normal       (Sun et al. 2023, Lemma 6c)
       │
       ├─ gamma = 0             →  sqrt-Gamma        (Sun et al. 2023, Lemma 6a)
       │
       ├─ alpha = 1             →  truncated normal  (Sun et al. 2023, Lemma 6b)
       │
       ├─ alpha < 1 and gamma > 0
       │      →  RTDR  (regions (b) / (c) / (d), Gao & Wang 2025, Theorems 3.2, 4.4)
       │
       ├─ alpha > 1 and gamma > 0
       │      →  Sun Algorithm 1                    (Sun et al. 2023, Theorems 1, 2)
       │
       └─ gamma < 0
              ├─ samples_per_setup ≥ 25  →  RTDR    (region (a))
              └─ samples_per_setup < 25  →  Sun Algorithm 3

Write SS for the samples_per_setup quantity in the last branch,

S=max(1,nmax(Lα,Lβ,Lγ)),S \;=\; \max\!\Big(1,\; \frac{n}{\max(L_{\alpha},\, L_{\beta},\, L_{\gamma})}\Big),

where LαL_{\alpha}, LβL_{\beta}, LγL_{\gamma} are the recycled lengths of the three parameter vectors. The intuition follows from decomposing the expected time per draw as

E[time/draw]=Tsetupn+Creject×Tper-proposal,E[\text{time}/\text{draw}] \;=\; \frac{T_{\text{setup}}}{n} \;+\; C_{\text{reject}} \times T_{\text{per-proposal}},

where TsetupT_{\text{setup}} is the one-off cost of preparing the proposal (a single minitm_{\text{init}} formula plus at most one Newton step for Sun Algorithm 3; an iterative contact-point search for RTDR), Creject=1/acceptanceC_{\text{reject}} = 1/\text{acceptance} is the expected number of proposals per accepted draw, and Tper-proposalT_{\text{per-proposal}} is the cost of one proposal cycle (sample plus acceptance log-ratio evaluation).

For γ<0\gamma < 0 the two samplers sit on opposite ends of this trade-off:

  • Sun Algorithm 3 has a cheap setupminitm_{\text{init}} has a closed form, and the optional Newton refinement uses one boost::math::digamma evaluation — but a more expensive per-proposal: every proposal calls rgamma, evaluates the fractional power X=mTrX = m \cdot T^{r}, and the acceptance log-ratio carries the same (X/m)1/r(X/m)^{1/r} term.
  • RTDR (region (a)) has a more expensive setup: each contact point tlt_{l}, trt_{r} is found by an iterative search that terminates as soon as logf(m)/f(t)\log f(m)/f(t) lands in the bracket [0.46,2.49][0.46,\, 2.49]. But the per-proposal is light: sampling from the piecewise envelope is straight arithmetic plus a single log, and the acceptance ratio adds only two more logs.

When SS is large the per-proposal term dominates and RTDR wins; when it is small — the Gibbs case where each iteration redraws (α,β,γ)(\alpha, \beta, \gamma) — the setup term dominates and Sun Algorithm 3 wins. The crossover at 25 is the round value chosen inside the empirical break-even window n[10,25]n \in [10,\, 25] observed at every one of the 20 surveyed (α,γ)(\alpha, \gamma) cells in inst/benchmarks/auto_dispatch.R.

A small concrete demonstration: with set.seed aligned, method = "auto" and the expected forced method produce bit-identical draws on each branch.

# Branch: alpha < 1 and gamma > 0  -->  expects RTDR
set.seed(7); auto1 <- rmhn(20, alpha = 0.7, beta = 1, gamma = 2)
set.seed(7); rtdr1 <- rmhn(20, alpha = 0.7, beta = 1, gamma = 2, method = "rtdr")
identical(auto1, rtdr1)
#> [1] TRUE

# Branch: alpha > 1 and gamma > 0  -->  expects Sun Algorithm 1
set.seed(7); auto2 <- rmhn(20, alpha = 3,   beta = 1, gamma = 2)
set.seed(7); sun2  <- rmhn(20, alpha = 3,   beta = 1, gamma = 2, method = "sun")
identical(auto2, sun2)
#> [1] TRUE

# Branch: gamma < 0 with samples_per_setup < 25  -->  expects Sun Algorithm 3
set.seed(7); auto3 <- rmhn(5,  alpha = 2,   beta = 1, gamma = -1)
set.seed(7); sun3  <- rmhn(5,  alpha = 2,   beta = 1, gamma = -1, method = "sun")
identical(auto3, sun3)
#> [1] TRUE

The three TRUE outputs are the same invariant that tests/testthat/test-rmhn.R enforces as a regression test.

References

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.