File size: 8,030 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | import pandas as pd
from core.estimation.inference.ci import (
ci_mean_analytic,
ci_median_analytic,
ci_deviation_analytic,
ci_mean_bootstrap,
ci_median_bootstrap,
ci_deviation_bootstrap,
)
from core.estimation.inference.pi import (
pi_mean,
pi_median,
pi_iqr,
pi_bootstrap,
)
from core.estimation.inference.confidence_regions import confidence_regions
# ---------------------------------------------------------------------
# Utilities
# ---------------------------------------------------------------------
def select_distribution(mean_estimator: str, sigma_estimator: str) -> str:
if mean_estimator == "Sample Mean" and sigma_estimator == "Deviation (1 ddof)":
return "t"
return "norm"
def validate_deviation_estimator(*, sigma_estimator: str, n: int):
if sigma_estimator == "Range (bias corrected)" and n > 25:
raise ValueError(
"Range-based confidence intervals require n ≤ 25. "
"Use another estimator or bootstrap."
)
# ---------------------------------------------------------------------
# Confidence Intervals
# ---------------------------------------------------------------------
def run_confidence_intervals(
*,
data,
alpha,
mean_estimator,
median_estimator,
sigma_estimator,
trim_param=None,
winsor_limits=None,
weights=None,
bootstrap_mean=False,
bootstrap_median=False,
bootstrap_deviation=False,
bootstrap_samples=1000,
):
n = len(data)
validate_deviation_estimator(
sigma_estimator=sigma_estimator,
n=n,
)
dist = select_distribution(mean_estimator, sigma_estimator)
# ---------------- Mean ----------------
if bootstrap_mean:
mean_ci = ci_mean_bootstrap(
data=data,
estimator=mean_estimator,
alpha=alpha,
B=bootstrap_samples,
trim_param=trim_param,
winsor_limits=winsor_limits,
weights=weights,
)
else:
mean_ci = ci_mean_analytic(
data=data,
estimator=mean_estimator,
alpha=alpha,
dist=dist,
sigma_estimator=sigma_estimator,
trim_param=trim_param,
winsor_limits=winsor_limits,
weights=weights,
)
# ---------------- Median ----------------
if bootstrap_median:
median_ci = ci_median_bootstrap(
data=data,
alpha=alpha,
B=bootstrap_samples,
)
else:
median_ci = ci_median_analytic(
data=data,
alpha=alpha,
sigma_estimator=sigma_estimator,
)
# ---------------- Deviation ----------------
if bootstrap_deviation:
sigma_ci = ci_deviation_bootstrap(
data=data,
alpha=alpha,
B=bootstrap_samples,
estimator=sigma_estimator,
)
else:
sigma_ci = ci_deviation_analytic(
data=data,
alpha=alpha,
estimator=sigma_estimator,
)
table = pd.DataFrame(
[
["Confidence", "Mean", *mean_ci],
["Confidence", "Median", *median_ci],
["Confidence", "Deviation", *sigma_ci],
],
columns=["Interval Type", "Statistic", "Lower", "Upper"],
)
return table, mean_ci, sigma_ci, median_ci
# ---------------------------------------------------------------------
# Prediction Intervals
# ---------------------------------------------------------------------
def run_prediction_intervals(
*,
data,
alpha,
mean_estimator,
median_estimator,
sigma_estimator,
trim_param=None,
winsor_limits=None,
weights=None,
bootstrap=False,
bootstrap_samples=1000,
):
dist = select_distribution(mean_estimator, sigma_estimator)
rows = []
# Mean-based PI
mean_pi = pi_mean(
data=data,
alpha=alpha,
estimator=mean_estimator,
dist=dist,
sigma_estimator=sigma_estimator,
trim_param=trim_param,
winsor_limits=winsor_limits,
weights=weights,
)
rows.append(["Prediction", "Mean", *mean_pi])
# Median-based PI (uses same deviation estimator)
median_pi = pi_median(
data=data,
alpha=alpha,
sigma_estimator=sigma_estimator,
)
rows.append(["Prediction", "Median", *median_pi])
# IQR-based PI
iqr_pi = pi_iqr(
data=data,
alpha=alpha,
)
rows.append(["Prediction", "IQR", *iqr_pi])
# Optional bootstrap PI
if bootstrap:
boot_pi = pi_bootstrap(
data=data,
alpha=alpha,
B=bootstrap_samples,
)
rows.append(["Prediction", "Bootstrap", *boot_pi])
return pd.DataFrame(
rows,
columns=["Interval Type", "Statistic", "Lower", "Upper"],
)
# ---------------------------------------------------------------------
# Confidence Regions
# ---------------------------------------------------------------------
def run_confidence_regions(
*,
data,
alpha,
mean_estimator,
median_estimator,
sigma_estimator,
trim_param,
winsor_limits,
weights,
bootstrap_mean,
bootstrap_median,
bootstrap_deviation,
bootstrap_samples,
mu_ci_source,
probs,
eps_mu,
eps_sigma,
add_ci_box,
):
"""
Use the CI machinery to compute CIs for mean, median and deviation,
then choose which CI to use for μ (mean-based or median-based) and
pass that CI plus the σ CI into the likelihood-based confidence
regions function.
"""
ci_table, mean_ci, sigma_ci, median_ci = run_confidence_intervals(
data=data,
alpha=alpha,
mean_estimator=mean_estimator,
median_estimator=median_estimator,
sigma_estimator=sigma_estimator,
trim_param=trim_param,
winsor_limits=winsor_limits,
weights=weights,
bootstrap_mean=bootstrap_mean,
bootstrap_median=bootstrap_median,
bootstrap_deviation=bootstrap_deviation,
bootstrap_samples=bootstrap_samples,
)
if mu_ci_source == "Median-based CI":
mu_ci = median_ci
else:
# default: mean-based CI
mu_ci = mean_ci
fig = confidence_regions(
data=data,
mean_ci=mu_ci,
sigma_ci=sigma_ci,
probs=probs,
eps_mu=eps_mu,
eps_sigma=eps_sigma,
add_ci_box=add_ci_box,
)
return fig
# ---------------------------------------------------------------------
# Combined Runner (used by UI)
# ---------------------------------------------------------------------
def run_intervals(
*,
data,
alpha,
mean_estimator,
median_estimator,
sigma_estimator,
bootstrap_mean,
bootstrap_median,
bootstrap_deviation,
bootstrap_samples,
):
ci_table, mean_ci, sigma_ci = run_confidence_intervals(
data=data,
alpha=alpha,
mean_estimator=mean_estimator,
median_estimator=median_estimator,
sigma_estimator=sigma_estimator,
bootstrap_mean=bootstrap_mean,
bootstrap_median=bootstrap_median,
bootstrap_deviation=bootstrap_deviation,
bootstrap_samples=bootstrap_samples,
)
pi_table = run_prediction_intervals(
data=data,
alpha=alpha,
mean_estimator=mean_estimator,
median_estimator=median_estimator,
sigma_estimator=sigma_estimator,
bootstrap=bootstrap_mean,
bootstrap_samples=bootstrap_samples,
)
combined = pd.concat([ci_table, pi_table], ignore_index=True)
return ci_table, pi_table, combined
|