dist_truncated: Truncate a distribution

View source: R/dist_truncated.R

dist_truncatedR Documentation

Truncate a distribution

Description

[Stable]

Note that the samples are generated using inverse transform sampling, and the means and variances are estimated from samples.

Usage

dist_truncated(dist, lower = -Inf, upper = Inf)

Arguments

dist

The distribution(s) to truncate.

lower, upper

The range of values to keep from a distribution.

Details

We recommend reading this documentation on pkgdown which renders math nicely. https://pkg.mitchelloharawild.com/distributional/reference/dist_truncated.html

In the following, let X be a truncated random variable with underlying distribution Y, truncation bounds lower = a and upper = b, where F_Y(x) is the c.d.f. of Y and f_Y(x) is the p.d.f. of Y.

Support: [a, b]

Mean: For the general case, the mean is approximated numerically. For a truncated Normal distribution with underlying mean \mu and standard deviation \sigma, the mean is:

E(X) = \mu + \frac{\phi(\alpha) - \phi(\beta)}{\Phi(\beta) - \Phi(\alpha)} \sigma

where \alpha = (a - \mu)/\sigma, \beta = (b - \mu)/\sigma, \phi is the standard Normal p.d.f., and \Phi is the standard Normal c.d.f.

Variance: Approximated numerically for all distributions.

Probability density function (p.d.f):

f(x) = \begin{cases} \frac{f_Y(x)}{F_Y(b) - F_Y(a)} & \text{if } a \le x \le b \\ 0 & \text{otherwise} \end{cases}

Cumulative distribution function (c.d.f):

F(x) = \begin{cases} 0 & \text{if } x < a \\ \frac{F_Y(x) - F_Y(a)}{F_Y(b) - F_Y(a)} & \text{if } a \le x \le b \\ 1 & \text{if } x > b \end{cases}

Quantile function:

Q(p) = F_Y^{-1}(F_Y(a) + p(F_Y(b) - F_Y(a)))

clamped to the interval [a, b].

Examples

dist <- dist_truncated(dist_normal(2,1), lower = 0)

dist
mean(dist)
variance(dist)

generate(dist, 10)

density(dist, 2)
density(dist, 2, log = TRUE)

cdf(dist, 4)

quantile(dist, 0.7)

if(requireNamespace("ggdist")) {
library(ggplot2)
ggplot() +
  ggdist::stat_dist_halfeye(
    aes(y = c("Normal", "Truncated"),
        dist = c(dist_normal(2,1), dist_truncated(dist_normal(2,1), lower = 0)))
  )
}


distributional documentation built on June 11, 2026, 9:07 a.m.