File size: 1,986 Bytes
798602c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# stats/inference/ci_mean.py

import numpy as np
from scipy.stats import norm, t

from .estimators import estimate_mean, estimate_sigma


def ci_mean_analytic(

    *,

    data,

    estimator,

    alpha,

    dist,

    sigma_estimator,

    trim_param=None,

    winsor_limits=None,

    weights=None,

):
    """

    Analytic confidence interval for the mean.



    - Mean is computed with the user-chosen mean estimator.

    - σ is computed with the user-chosen deviation estimator.

    """
    n = len(data)

    mu_hat = estimate_mean(
        data,
        estimator,
        trim_param=trim_param,
        winsor_limits=winsor_limits,
        weights=weights,
    )

    sigma_hat = estimate_sigma(
        data=data,
        estimator=sigma_estimator,
    )

    scale = sigma_hat / np.sqrt(n)

    if dist == "t":
        return (
            t.ppf(alpha / 2, n - 1, loc=mu_hat, scale=scale),
            t.ppf(1 - alpha / 2, n - 1, loc=mu_hat, scale=scale),
        )

    return (
        norm.ppf(alpha / 2, loc=mu_hat, scale=scale),
        norm.ppf(1 - alpha / 2, loc=mu_hat, scale=scale),
    )


def ci_mean_bootstrap(

    *,

    data,

    estimator,

    alpha,

    B,

    trim_param=None,

    winsor_limits=None,

    weights=None,

):
    """

    Bootstrap CI for the mean using the user-chosen mean estimator.

    """
    data = np.asarray(data)
    n = len(data)

    boot_stats = np.empty(B)

    for b in range(B):
        idx = np.random.choice(n, size=n, replace=True)
        boot_data = data[idx]

        boot_weights = None
        if weights is not None:
            boot_weights = np.asarray(weights)[idx]

        boot_stats[b] = estimate_mean(
            boot_data,
            estimator,
            trim_param=trim_param,
            winsor_limits=winsor_limits,
            weights=boot_weights,
        )

    return np.quantile(boot_stats, [alpha / 2, 1 - alpha / 2])