Paired Model — Laplace Approximation¶
Paired logistic model with MAP + Hessian (Laplace) posterior inference.
The sequential variant SequentialPairedBayesPropTest is documented on
the Sequential designs page.
bayes_paired_laplace
¶
Pooled Bernoulli logistic regression for paired A/B model comparison (Laplace).
This module provides :class:PairedBayesPropTest, a self-contained class
for fitting a pooled Bayesian Bernoulli logistic model to paired binary
scores via Laplace approximation (MAP + analytical Hessian), performing
hypothesis testing via the Savage-Dickey density ratio, running
posterior-predictive diagnostics, and generating publication-ready plots.
For exact MCMC inference via Pólya-Gamma data augmentation, see
:mod:ai_eval.resources.bayes_paired_pg.
Typical workflow::
from ai_eval.resources.bayes_paired_laplace import PairedBayesPropTest
model = PairedBayesPropTest(seed=42).fit(y_A, y_B)
model.print_summary()
model.plot_posterior_delta()
model.plot_savage_dickey()
For multi-metric comparisons::
results = {"Relevancy": model_rel, "Faithfulness": model_faith}
PairedBayesPropTest.plot_forest(results, label_A="v2", label_B="v1")
PairedBayesPropTest(prior_sigma_delta=1.0, seed=0, n_samples=8000, decision_rule='all', rope_epsilon=0.02)
¶
Pooled Bernoulli logistic model for paired A/B comparison.
Uses Laplace approximation (MAP + Hessian) instead of full MCMC for fast, analytic posterior inference on binarized scores.
Generative model::
μ ~ N(0, 2) (overall intercept)
δ_A ~ N(0, σ_δ) (model-A advantage)
y_A,i ~ Bernoulli(σ(μ + δ_A))
y_B,i ~ Bernoulli(σ(μ))
Only 2 parameters — no confounding between item effects and model effect when >90% of pairs are concordant.
The prior width on delta_A is fixed (not learned) so the
Savage-Dickey density ratio remains exactly consistent.
Attributes:
| Name | Type | Description |
|---|---|---|
laplace |
dict[str, Any] | None
|
Dict with MAP estimate, covariance, Hessian, and
posterior samples ( |
summary |
dict[str, Any] | None
|
Dict with |
trace_summary |
DataFrame | None
|
|
delta_A_samples |
ndarray | None
|
1-D array of posterior draws for |
y_A_obs |
ndarray | None
|
Observed binary scores for model A (set by :meth: |
y_B_obs |
ndarray | None
|
Observed binary scores for model B (set by :meth: |
Initialise model configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prior_sigma_delta
|
float
|
Standard deviation of the N(0, σ) prior on
|
1.0
|
seed
|
int
|
Random seed for reproducibility. |
0
|
n_samples
|
int
|
Number of draws from the Laplace posterior. |
8000
|
decision_rule
|
DecisionRuleType
|
Default decision framework — one of
|
'all'
|
rope_epsilon
|
float
|
Half-width of the ROPE interval (default 0.02 = 2 pp). |
0.02
|
Source code in bayesprop/resources/bayes_paired_laplace.py
fit(y_A_obs, y_B_obs)
¶
Fit the pooled Bernoulli model via Laplace approximation.
Reduces (y_A_obs, y_B_obs) to the four sufficient statistics
(n_A, k_A, n_B, k_B) and delegates to
:func:_paired_laplace_from_counts, which solves for the MAP
via damped Newton (closed-form 2x2 gradient and Hessian, with
Armijo backtracking line search) and returns the Laplace
covariance Σ = H⁻¹.
Log-posterior (up to constant)::
log p(μ, δ|y) = Σᵢ [y_Aᵢ log σ(μ+δ) + (1-y_Aᵢ) log(1-σ(μ+δ))]
+ Σᵢ [y_Bᵢ log σ(μ) + (1-y_Bᵢ) log(1-σ(μ))]
- μ²/(2σ_μ²) - δ²/(2σ_δ²)
Gradient::
∂/∂μ = (k_A - n_A·p_A) + (k_B - n_B·p_B) - μ/σ_μ²
∂/∂δ = (k_A - n_A·p_A) - δ/σ_δ²
Hessian of the negative log-posterior (observed information)::
H[0,0] = n_A·w_A + n_B·w_B + 1/σ_μ²
H[1,1] = n_A·w_A + 1/σ_δ²
H[0,1] = H[1,0] = n_A·w_A
where w_A = p_A(1-p_A), w_B = p_B(1-p_B), evaluated at MAP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_A_obs
|
ndarray
|
Binary observed scores for model A (0 or 1). |
required |
y_B_obs
|
ndarray
|
Binary observed scores for model B (0 or 1). |
required |
Returns:
| Type | Description |
|---|---|
PairedBayesPropTest
|
self (for method chaining). |
Source code in bayesprop/resources/bayes_paired_laplace.py
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
_check_fitted()
¶
Raise RuntimeError if the model has not been fitted yet.
savage_dickey_test(null_value=0.0)
¶
Savage-Dickey density-ratio Bayes factor for H0: delta_A = null_value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
null_value
|
float
|
The point null hypothesis value for delta_A. |
0.0
|
Returns:
| Type | Description |
|---|---|
SavageDickeyResult
|
class: |
SavageDickeyResult
|
interpretation, and decision. |
Source code in bayesprop/resources/bayes_paired_laplace.py
posterior_probability_H0(BF_01, prior_H0=0.5)
staticmethod
¶
Convert BF_01 to posterior probability of H0 (spike-and-slab).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
BF_01
|
float
|
Bayes factor in favour of H0. |
required |
prior_H0
|
float
|
Prior probability of H0 (default 0.5). |
0.5
|
Returns:
| Type | Description |
|---|---|
PosteriorProbH0Result
|
class: |
PosteriorProbH0Result
|
and model probabilities. |
Source code in bayesprop/resources/bayes_paired_laplace.py
rope_test(rope=None, ci_mass=0.95)
¶
ROPE analysis on the posterior of Δ = p_A − p_B (probability scale).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rope
|
tuple[float, float] | None
|
(lower, upper) ROPE bounds. Defaults to
|
None
|
ci_mass
|
float
|
Credible interval mass (default 95%). |
0.95
|
Returns:
| Type | Description |
|---|---|
ROPEResult
|
class: |
ROPEResult
|
decision. |
Source code in bayesprop/resources/bayes_paired_laplace.py
decide(rule=None)
¶
Run the chosen decision framework(s) and return a composite result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule
|
DecisionRuleType | None
|
Override the default |
None
|
Returns:
| Type | Description |
|---|---|
HypothesisDecision
|
class: |
HypothesisDecision
|
populated. |
Source code in bayesprop/resources/bayes_paired_laplace.py
ppc_pvalues(seed=None)
¶
Posterior predictive p-values for summary statistics.
Returns:
| Type | Description |
|---|---|
dict[str, PPCStatistic]
|
Dict mapping statistic name to :class: |
Source code in bayesprop/resources/bayes_paired_laplace.py
plot_laplace_posterior(**kwargs)
¶
Two-panel posterior plot: overlaid p_A / p_B and Δ = p_A − p_B.
The implied success probabilities p_A = σ(μ + δ_A) and
p_B = σ(μ) are computed from the Laplace posterior samples
and displayed as overlaid KDE densities in the left panel.
The right panel shows the difference Δ = p_A − p_B.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Accepts |
{}
|
Source code in bayesprop/resources/bayes_paired_laplace.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 | |
plot_posterior_delta(color='#2196F3', **kwargs)
¶
KDE posterior density of delta_A (logit scale) with 95% CI.
Source code in bayesprop/resources/bayes_paired_laplace.py
plot_savage_dickey(color='#2196F3', **kwargs)
¶
Posterior vs prior density with Savage-Dickey BF annotation.
Source code in bayesprop/resources/bayes_paired_laplace.py
plot_ppc(seed=None, **kwargs)
¶
Three-column PPC plot: P(perfect) A, P(perfect) B, rate difference.
Source code in bayesprop/resources/bayes_paired_laplace.py
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 | |
plot_sensitivity(prior_H0=0.5, **kwargs)
¶
Two-panel sensitivity: P(H0|data) vs prior P(H0), and slab-width sweep.
Source code in bayesprop/resources/bayes_paired_laplace.py
print_summary()
¶
Print posterior summary, Savage-Dickey test, and PPC p-values.
Source code in bayesprop/resources/bayes_paired_laplace.py
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 | |
plot_forest(results, label_A='Model A', label_B='Model B', **kwargs)
staticmethod
¶
Forest plot + P(A>B) bar chart for multiple metrics.
Source code in bayesprop/resources/bayes_paired_laplace.py
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 | |
print_comparison_table(results)
staticmethod
¶
Print a formatted comparison table across metrics.
Source code in bayesprop/resources/bayes_paired_laplace.py
sigmoid(x)
¶
_format_bf(value)
¶
Format a Bayes Factor for human-readable display.
Source code in bayesprop/resources/bayes_paired_laplace.py
_paired_laplace_from_counts(n_A, k_A, n_B, k_B, prior_sigma_delta, prior_sigma_mu=2.0, x0=(0.0, 0.0), tol=1e-08, max_iter=50)
¶
Compute the Laplace posterior of (mu, delta_A) directly from counts.
Solves for the MAP via damped Newton iterations using the closed-form
gradient and Hessian of the pooled Bernoulli logistic log-posterior
(no raw data is materialised, no external optimizer invoked). The
objective depends on the data only through the four sufficient
statistics (n_A, k_A, n_B, k_B).
Newton converges quadratically; warm-starting x0 from the
previous MAP (as the sequential test does) typically requires only
1-3 iterations per update.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_A
|
int
|
Cumulative sample size for arm A. |
required |
k_A
|
int
|
Cumulative successes for arm A. |
required |
n_B
|
int
|
Cumulative sample size for arm B. |
required |
k_B
|
int
|
Cumulative successes for arm B. |
required |
prior_sigma_delta
|
float
|
Std of the N(0, sigma) prior on delta_A. |
required |
prior_sigma_mu
|
float
|
Std of the N(0, sigma) prior on mu. |
2.0
|
x0
|
tuple[float, float]
|
Warm-start for Newton as (mu0, delta0). |
(0.0, 0.0)
|
tol
|
float
|
Convergence tolerance on the gradient infinity-norm. |
1e-08
|
max_iter
|
int
|
Maximum number of Newton iterations. |
50
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple |
ndarray
|
of |
ndarray
|
(closed-form inverse of the observed information), and |
tuple[ndarray, ndarray, ndarray]
|
the 2x2 Hessian of the negative log-posterior at the MAP. |
Source code in bayesprop/resources/bayes_paired_laplace.py
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 | |