Create a Truncated Logistic Distribution

Description

Class and methods for left-, right-, and interval-truncated logistic distributions using the workflow from the distributions3 package.

Usage

TruncatedLogistic(location = 0, scale = 1, left = -Inf, right = Inf)

Arguments

location numeric. The location parameter of the underlying untruncated logistic distribution, typically written \(\mu\) in textbooks. Can be any real number, defaults to 0.
scale numeric. The scale parameter (standard deviation) of the underlying untruncated logistic distribution, typically written \(\sigma\) in textbooks. Can be any positive number, defaults to 1.
left numeric. The left truncation point. Can be any real number, defaults to -Inf (untruncated). If set to a finite value, the distribution has a point mass at left whose probability corresponds to the cumulative probability function of the untruncated logistic distribution at this point.
right numeric. The right truncation point. Can be any real number, defaults to Inf (untruncated). If set to a finite value, the distribution has a point mass at right whose probability corresponds to 1 minus the cumulative probability function of the untruncated logistic distribution at this point.

Details

The constructor function TruncatedLogistic sets up a distribution object, representing the truncated logistic probability distribution by the corresponding parameters: the latent mean location = \(\mu\) and latent standard deviation scale = \(\sigma\) (i.e., the parameters of the underlying untruncated logistic variable), the left truncation point (with -Inf corresponding to untruncated), and the right truncation point (with Inf corresponding to untruncated).

The truncated logistic distribution has probability density function (PDF):

\(f(x) = 1/\sigma \lambda((x - \mu)/\sigma) / (\Lambda((right - \mu)/\sigma) - \Lambda((left - \mu)/\sigma))\)

for \(left \le x \le right\), and 0 otherwise, where \(\Lambda\) and \(\lambda\) are the cumulative distribution function and probability density function of the standard logistic distribution, respectively.

All parameters can also be vectors, so that it is possible to define a vector of truncated logistic distributions with potentially different parameters. All parameters need to have the same length or must be scalars (i.e., of length 1) which are then recycled to the length of the other parameters.

For the TruncatedLogistic distribution objects there is a wide range of standard methods available to the generics provided in the distributions3 package: pdf and log_pdf for the (log-)density (PDF), cdf for the probability from the cumulative distribution function (CDF), quantile for quantiles, random for simulating random variables, crps for the continuous ranked probability score (CRPS), and support for the support interval (minimum and maximum). Internally, these methods rely on the usual d/p/q/r functions provided for the truncated logistic distributions in the crch package, see dtlogis, and the crps_tlogis function from the scoringRules package. The methods is_discrete and is_continuous can be used to query whether the distributions are discrete on the entire support (always FALSE) or continuous on the entire support (always TRUE).

See the examples below for an illustration of the workflow for the class and methods.

Value

A TruncatedLogistic distribution object.

See Also

dtlogis, Logistic, CensoredLogistic, TruncatedNormal, TruncatedStudentsT

Examples

library("crch")


## package and random seed
library("distributions3")
set.seed(6020)

## three truncated logistic distributions:
## - untruncated standard logistic
## - left-truncated at zero with latent location = 1 and scale = 1
## - interval-truncated in [0, 5] with latent location = 2 and scale = 1
X <- TruncatedLogistic(
  location = c(   0,   1, 2),
  scale    = c(   1,   1, 1),
  left     = c(-Inf,   0, 0),
  right    = c( Inf, Inf, 5)
)

X
[1] "TruncatedLogistic distribution (location = 0, scale = 1, left = -Inf, right = Inf)"
[2] "TruncatedLogistic distribution (location = 1, scale = 1, left =    0, right = Inf)"
[3] "TruncatedLogistic distribution (location = 2, scale = 1, left =    0, right =   5)"
## compute mean of the truncated distribution
mean(X)
[1] 0.000000 1.796384 2.209353
## higher moments (variance, skewness, kurtosis) are not implemented yet

## support interval (minimum and maximum)
support(X)
      min max
[1,] -Inf Inf
[2,]    0 Inf
[3,]    0   5
## simulate random variables
random(X, 5)
            r_1       r_2        r_3         r_4         r_5
[1,] -0.5489266 0.7114939 -0.9934677 -0.09368233 -3.16777672
[2,]  1.1006458 2.4089727  2.4675811  1.05483091  0.02403454
[3,]  2.7604891 0.7532299  0.6488695  1.49733364  1.04806366
## histograms of 1,000 simulated observations
x <- random(X, 1000)
hist(x[1, ], main = "untruncated")

hist(x[2, ], main = "left-truncated at zero")

hist(x[3, ], main = "interval-truncated in [0, 5]")

## probability density function (PDF) and log-density (or log-likelihood)
x <- c(0, 0, 1)
pdf(X, x)
[1] 0.2500000 0.2689414 0.2359236
pdf(X, x, log = TRUE)
[1] -1.386294 -1.313262 -1.444247
log_pdf(X, x)
[1] -1.386294 -1.313262 -1.444247
## cumulative distribution function (CDF)
cdf(X, x)
[1] 0.500000 0.000000 0.179678
## quantiles
quantile(X, 0.5)
[1] 0.000000 1.551445 2.143801
## cdf() and quantile() are inverses (except at truncation points)
cdf(X, quantile(X, 0.5))
[1] 0.5 0.5 0.5
quantile(X, cdf(X, 1))
[1] 1 1 1
## all methods above can either be applied elementwise or for
## all combinations of X and x, if length(X) = length(x),
## also the result can be assured to be a matrix via drop = FALSE
p <- c(0.05, 0.5, 0.95)
quantile(X, p, elementwise = FALSE)
         q_0.05    q_0.5   q_0.95
[1,] -2.9444390 0.000000 2.944439
[2,]  0.1787310 1.551445 4.271756
[3,]  0.3482419 2.143801 4.324742
quantile(X, p, elementwise = TRUE)
[1] -2.944439  1.551445  4.324742
quantile(X, p, elementwise = TRUE, drop = FALSE)
      quantile
[1,] -2.944439
[2,]  1.551445
[3,]  4.324742
## compare theoretical and empirical mean from 1,000 simulated observations
cbind(
  "theoretical" = mean(X),
  "empirical" = rowMeans(random(X, 1000))
)
     theoretical  empirical
[1,]    0.000000 -0.0299458
[2,]    1.796384  1.7546601
[3,]    2.209353  2.2205845
## evaluate continuous ranked probability score (CRPS) using scoringRules
library("scoringRules")
crps(X, x)
[1] 0.3862944 1.0893568 0.6860449