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)
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.641306 2.319159 1.685074 1.813147 2.258445 2.212465 3.780368 1.603581
#> [9] 2.478463 1.941879
#>
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)))
)
}