Note that the samples are generated using inverse transform sampling, and the means and variances are estimated from samples.
dist_truncated(dist, lower = -Inf, upper = Inf)
The distribution(s) to truncate.
The range of values to keep from a distribution.
dist <- dist_truncated(dist_normal(2,1), lower = 0)
dist
#> <distribution[1]>
#> [1] N(2, 1)[0,Inf]
mean(dist)
#> [1] 2.055248
variance(dist)
#> [1] 0.8864519
generate(dist, 10)
#> [[1]]
#> [1] 1.9187894 2.6320558 1.8102240 3.4958546 0.8254419 1.5980963 0.7757352
#> [8] 2.2919659 1.0124909 0.8733140
#>
density(dist, 2)
#> [1] 0.4082296
density(dist, 2, log = TRUE)
#> [1] -0.8959256
cdf(dist, 4)
#> [1] 0.9767203
quantile(dist, 0.7)
#> [1] 2.544133
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)))
)
}