Skip to contents

Estimates the posterior modes for the mean (mu) and standard deviation (sigma) of the underlying normal distribution, given truncated data with known truncation point(s).

Usage

trunc_est(x, a, b, mu_start = 0, sigma_start = 1, ci_level = 0.95, ...)

Arguments

x

Vector of observations from truncated normal.

a

Left truncation limit.

b

Right truncation limit.

mu_start

Initial value for mu.

sigma_start

Initial value for sigma.

ci_level

Confidence level of the interval – gives a 100*ci_level% symmetric HPD interval (defaults to 95%).

...

Parameters to pass to rstan::sampling().

Value

A list with two elements:

stats

A data frame with two rows and the columns param (mu, sd), mode (posterior mode), mean (posterior mean), median (posterior median), se (standard error), ci_lower (lower CI bound), ci_upper (upper CI bound), rhat.

fit

A stanfit object (the result of fitting the model).

References

Zhou X, Giacometti R, Fabozzi FJ, Tucker AH (2014). “Bayesian estimation of truncated data with applications to operational risk measurement.” Quantitative Finance, 14(5), 863--888. doi:10.1080/14697688.2012.752103 .

Stan Development Team (2022). “RStan: the R interface to Stan.” R package version 2.21.5. https://mc-stan.org.

Examples

set.seed(22)
x <- truncnorm::rtruncnorm(100, a = -1, b = 2, mean = 0.5, sd = 0.5)
trunc_est(x, a = -1, b = 2)
#> 
#> SAMPLING FOR MODEL 'trunc_est' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 9e-05 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.9 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 0.29 seconds (Warm-up)
#> Chain 1:                0.352 seconds (Sampling)
#> Chain 1:                0.642 seconds (Total)
#> Chain 1: 
#> 
#> SAMPLING FOR MODEL 'trunc_est' NOW (CHAIN 2).
#> Chain 2: 
#> Chain 2: Gradient evaluation took 6.6e-05 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.66 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2: 
#> Chain 2: 
#> Chain 2: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 2: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 2: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 2: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 2: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 2: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 2: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 2: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 2: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 2: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 2: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 2: 
#> Chain 2:  Elapsed Time: 0.293 seconds (Warm-up)
#> Chain 2:                0.29 seconds (Sampling)
#> Chain 2:                0.583 seconds (Total)
#> Chain 2: 
#> 
#> SAMPLING FOR MODEL 'trunc_est' NOW (CHAIN 3).
#> Chain 3: 
#> Chain 3: Gradient evaluation took 6.3e-05 seconds
#> Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.63 seconds.
#> Chain 3: Adjust your expectations accordingly!
#> Chain 3: 
#> Chain 3: 
#> Chain 3: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 3: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 3: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 3: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 3: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 3: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 3: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 3: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 3: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 3: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 3: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 3: 
#> Chain 3:  Elapsed Time: 0.29 seconds (Warm-up)
#> Chain 3:                0.331 seconds (Sampling)
#> Chain 3:                0.621 seconds (Total)
#> Chain 3: 
#> 
#> SAMPLING FOR MODEL 'trunc_est' NOW (CHAIN 4).
#> Chain 4: 
#> Chain 4: Gradient evaluation took 6.2e-05 seconds
#> Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.62 seconds.
#> Chain 4: Adjust your expectations accordingly!
#> Chain 4: 
#> Chain 4: 
#> Chain 4: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 4: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 4: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 4: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 4: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 4: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 4: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 4: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 4: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 4: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 4: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 4: 
#> Chain 4:  Elapsed Time: 0.295 seconds (Warm-up)
#> Chain 4:                0.261 seconds (Sampling)
#> Chain 4:                0.556 seconds (Total)
#> Chain 4: 
#> $stats
#>   param      mode      mean    median           se  ci_lower  ci_upper
#> 1    mu 0.5173298 0.5181258 0.5191518 0.0008993825 0.4261705 0.6125553
#> 2 sigma 0.4699284 0.4799267 0.4772683 0.0006998036 0.4134367 0.5599469
#>        rhat
#> 1 0.9998396
#> 2 0.9999681
#> 
#> $fit
#> Inference for Stan model: trunc_est.
#> 4 chains, each with iter=2000; warmup=1000; thin=1; 
#> post-warmup draws per chain=1000, total post-warmup draws=4000.
#> 
#>             mean se_mean   sd   2.5%    25%    50%    75%  97.5% n_eff Rhat
#> mu          0.52    0.00 0.05   0.43   0.48   0.52   0.55   0.61  2914    1
#> sigma       0.48    0.00 0.04   0.41   0.45   0.48   0.50   0.56  2807    1
#> log_lik   -67.62    0.02 0.98 -70.25 -68.04 -67.33 -66.90 -66.63  1636    1
#> log_prior   6.35    0.00 0.19   5.93   6.23   6.37   6.49   6.70  2789    1
#> log_post  -61.27    0.03 1.01 -63.99 -61.68 -60.97 -60.52 -60.24  1630    1
#> lp__       29.89    0.02 1.00  27.22  29.48  30.18  30.62  30.90  1625    1
#> 
#> Samples were drawn using NUTS(diag_e) at Wed Aug 23 03:13:47 2023.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at 
#> convergence, Rhat=1).
#>