def _confidence_interval_bca(theta: float, thetas: np.ndarray, j_thetas: np.ndarray, alpha_half: float) -> Tuple[float, float]: norm = stats.norm # bias correction prop_less = np.mean( thetas < theta) # proportion of replicates less than obs z_naught = norm.ppf(prop_less) # acceleration j_thetas -= np.mean(j_thetas) num = np.sum((-j_thetas)**3) den = np.sum(j_thetas**2) acc = num / (6 * den**1.5) z_low = z_naught + norm.ppf(alpha_half) z_high = z_naught + norm.ppf(1 - alpha_half) p_low = norm.cdf(z_naught + z_low / (1 - acc * z_low)) p_high = norm.cdf(z_naught + z_high / (1 - acc * z_high)) quant = quantile_function_gen(thetas) return quant(p_low), quant(p_high)
def test_quantile_out_of_bounds_is_nan(arg): q = quantile_function_gen(np.array([0, 1, 2, 3])) assert np.isnan(q(arg))
def test_quantile_is_inverse_of_cdf(rng): x = rng.normal(size=30) y = cdf_gen(x)(x) assert_equal(quantile_function_gen(x)(y), x)
def test_quantile_on_array(): x = np.arange(4) q = quantile_function_gen(x) prob = (x + 1) / len(x) assert_equal(q(prob), x)
def test_quantile_simple_cases(): q = quantile_function_gen([0, 1, 2, 3]) assert q(0.25) == 0 assert q(0.5) == 1 assert q(0.75) == 2 assert q(1.0) == 3
def _confidence_interval_percentile( thetas: np.ndarray, alpha_half: float, ) -> Tuple[float, float]: quant = quantile_function_gen(thetas) return quant(alpha_half), quant(1 - alpha_half)