Bayesian
bayesian
Bayesian conjugate models and decision-making parameters.
This module provides the BayesianInference class, which estimates conjugate posterior distributions
(such as Beta-Binomial and Normal-Normal), computing decision metrics including the Probability of Being Best (PBB),
Expected Loss, and Region of Practical Equivalence (ROPE).
| CLASS | DESCRIPTION |
|---|---|
BayesianInference |
Computes Bayesian posterior parameters, probability of being best, expected loss, and ROPE. |
BayesianInference
Computes Bayesian posterior parameters, probability of being best, expected loss, and ROPE.
Bayesian inference offers a direct, probabilistic interpretation of treatment effects, avoiding the complex and frequently misunderstood reasoning of frequentist p-values. It provides answers to intuitive questions like: "What is the probability that Treatment B is superior to Control A?" or "What is the expected loss if I ship Treatment B?"
Mathematical Formulation of Conjugate Models
Conjugate models allow the analytical calculation of posterior distributions without requiring expensive Markov Chain Monte Carlo (MCMC) sampling:
- Beta-Binomial Model (for binary conversion rates, \(p \in [0, 1]\)):
- Prior: \(p \sim \text{Beta}(\alpha_0, \beta_0)\) (e.g., \(\text{Beta}(1, 1)\) for a flat, uniform prior).
- Likelihood: Binomial (\(k\) conversions out of \(n\) trials).
- Posterior: $$ p|k, n \sim \text{Beta}(\alpha_0 + k, \ \beta_0 + n - k) $$
- Normal-Normal Model (for continuous averages, \(\mu \in \mathbb{R}\), with known variance \(\sigma^2\)):
- Prior: \(\mu \sim \mathcal{N}(\mu_0, \sigma_0^2)\).
- Likelihood: Normal (\(N\) observations with sample mean \(\bar{Y}\) and variance \(\sigma^2\)).
- Posterior: $$ \mu| \bar{Y} \sim \mathcal{N}(\mu_N, \sigma_N^2) $$ where the posterior precision (\(1/\sigma_N^2\)) and posterior mean (\(\mu_N\)) are calculated as: $$ \frac{1}{\sigma_N^2} = \frac{1}{\sigma_0^2} + \frac{N}{\sigma^2} \quad \text{and} \quad \mu_N = \sigma_N^2 \left( \frac{\mu_0}{\sigma_0^2} + \frac{N\bar{Y}}{\sigma^2} \right) $$
- Gamma-Poisson Model (for discrete counts/rates, \(\lambda \in [0, \infty)\)):
- Prior: \(\lambda \sim \text{Gamma}(\alpha, \beta)\) (shape \(\alpha\), rate \(\beta\)).
- Likelihood: Poisson (\(k\) total counts across \(n\) units).
- Posterior: $$ \lambda|k, n \sim \text{Gamma}(\alpha + k, \beta + n) $$
Decision-Making Criteria and Analytics
- Probability of Being Best (PBB): The probability that the treatment parameter \(\theta_T\) is strictly greater than the control parameter \(\theta_C\): $$ \text{PBB} = P(\theta_T > \theta_C) = \int_{-\infty}^{\infty} f_C(\theta_C) [1 - F_T(\theta_C)] \, d\theta_C $$
- Expected Loss (\(L\)): The expected metric drop if the treatment is shipped but is actually inferior: $$ L(T) = \mathbb{E}[\max(\theta_C - \theta_T, 0)] = \int_{-\infty}^{\infty} \int_{-\infty}^{\theta_C} (\theta_C - \theta_T) f_T(\theta_T) f_C(\theta_C) \, d\theta_T \, d\theta_C $$
| ATTRIBUTE | DESCRIPTION |
|---|---|
model_type |
Conjugate model pairing label. Options:
TYPE:
|
| PARAMETER | DESCRIPTION |
|---|---|
model_type
|
Conjugate model to use (
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
estimate_posterior |
Estimates conjugate posterior distributions based on prior settings and raw observations. |
Source code in src\xpyrment\analyze\inference\bayesian.py
estimate_posterior
Estimates conjugate posterior distributions based on prior settings and raw observations.
Performs the analytical conjugate update formulas, then computes PBB, Expected Loss, and credible intervals.
| PARAMETER | DESCRIPTION |
|---|---|
prior_params
|
Prior parameters (e.g.,
TYPE:
|
observed_data
|
Observed outcomes (conversions and counts, or means and variances).
TYPE:
|
exact
|
Whether to use numerical integration for exact PBB and Expected Loss. If False, uses Monte Carlo sampling (20k samples). Defaults to True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Posterior distribution parameters, credible intervals, and decision metrics.
TYPE:
|
Source code in src\xpyrment\analyze\inference\bayesian.py
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 | |