# Truncated Distributions

The package provides the `truncated`

function which creates the most appropriate distribution to represent a truncated version of a given distribution.

A truncated distribution can be constructed using the following signature:

`Distributions.truncated`

— Function`truncated(d, l, u):`

Truncate a distribution between `l`

and `u`

. Builds the most appropriate distribution for the type of `d`

, the fallback is constructing a `Truncated`

wrapper.

To implement a specialized truncated form for a distribution `D`

, the method `truncate(d::D, l::T, u::T) where {T <: Real}`

should be implemented.

**Arguments**

`d::UnivariateDistribution`

: The original distribution.`l::Real`

: The lower bound of the truncation, which can be a finite value or`-Inf`

.`u::Real`

: The upper bound of the truncation, which can be a finite value of`Inf`

.

Throws an error if `l >= u`

.

In the general case, this will create a `Truncated{typeof(d)}`

structure, defined as follows:

`Distributions.Truncated`

— Type`Truncated(d, l, u):`

Create a generic wrapper for a truncated distribution. Prefer calling the function `truncated(d, l, u)`

, which can choose the appropriate representation of the truncated distribution.

**Arguments**

`d::UnivariateDistribution`

: The original distribution.`l::Real`

: The lower bound of the truncation, which can be a finite value or`-Inf`

.`u::Real`

: The upper bound of the truncation, which can be a finite value of`Inf`

.

Many functions, including those for the evaluation of pdf and sampling, are defined for all truncated univariate distributions:

`maximum(::UnivariateDistribution)`

`minimum(::UnivariateDistribution)`

`insupport(::UnivariateDistribution, x::Any)`

`pdf(::UnivariateDistribution, ::Real)`

`logpdf(::UnivariateDistribution, ::Real)`

`cdf(::UnivariateDistribution, ::Real)`

`logcdf(::UnivariateDistribution, ::Real)`

`logdiffcdf(::UnivariateDistribution, ::T, ::T) where {T <: Real}`

`ccdf(::UnivariateDistribution, ::Real)`

`logccdf(::UnivariateDistribution, ::Real)`

`quantile(::UnivariateDistribution, ::Real)`

`cquantile(::UnivariateDistribution, ::Real)`

`invlogcdf(::UnivariateDistribution, ::Real)`

`invlogccdf(::UnivariateDistribution, ::Real)`

`rand(::UnivariateDistribution)`

`rand!(::UnivariateDistribution, ::AbstractArray)`

`median(::UnivariateDistribution)`

Functions to compute statistics, such as `mean`

, `mode`

, `var`

, `std`

, and `entropy`

, are not available for generic truncated distributions. Generally, there are no easy ways to compute such quantities due to the complications incurred by truncation. However, these methods are supported for truncated normal distributions `Truncated{<:Normal}`

.

`Distributions.TruncatedNormal`

— Function`TruncatedNormal(mu, sigma, l, u)`

The *truncated normal distribution* is a particularly important one in the family of truncated distributions. We provide additional support for this type with `TruncatedNormal`

which calls `Truncated(Normal(mu, sigma), l, u)`

. Unlike the general case, truncated normal distributions support `mean`

, `mode`

, `modes`

, `var`

, `std`

, and `entropy`

.