Betting Strategies¶
Density estimators on [0, 1] for constructing martingale-based tests of exchangeability.
Base Class¶
online_cp.betting.BettingStrategy
¶
Base class for betting strategies (pure density estimators on [0,1]).
A betting strategy estimates the density of conformal p-values. It exposes:
- bet(p): evaluate the current density at p (using past data only)
- integrate(p): evaluate the current CDF at p (protection function)
- update(p): incorporate a new p-value into the estimate
The critical invariant is predict then learn: bet(p) uses only data
seen before p, ensuring the martingale property.
Strategies are pure — they do not apply cautious mixing. That is the responsibility of the martingale that wraps them.
Source code in src/online_cp/betting.py
Implementations¶
online_cp.betting.GaussianKDE
¶
Bases: BettingStrategy
Gaussian Kernel Density Estimation betting strategy with boundary reflection.
Uses a reflected Gaussian kernel to properly handle the [0,1] boundary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bandwidth
|
str or float
|
Bandwidth selection: "silverman" (rule of thumb), "lcv" (likelihood cross-validation), or a fixed float value. |
'silverman'
|
window_size
|
int or None
|
If set, only use the last |
None
|
max_iter
|
int
|
Maximum iterations for LCV bandwidth optimization. |
20
|
bw_min
|
float
|
Bandwidth search bounds for LCV. |
0.001
|
bw_max
|
float
|
Bandwidth search bounds for LCV. |
0.001
|
growth_factor
|
float
|
Re-optimize bandwidth when sample size grows by this factor. |
1.1
|
Examples:
>>> gkde = GaussianKDE(bandwidth=0.1)
>>> for p in [0.1, 0.11, 0.09, 0.12, 0.08]:
... gkde.update(p)
>>> bool(gkde.bet(0.1) > 1.0) # should peak near the data
True
>>> bool(gkde.bet(0.9) < 1.0)
True
Source code in src/online_cp/betting.py
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 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
online_cp.betting.BetaKernel
¶
Bases: BettingStrategy
Beta Kernel Density Estimation betting strategy.
Uses the beta_kde package (if installed) to estimate the density
of p-values with a Beta kernel, which handles [0,1] boundaries well.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bandwidth
|
str or float
|
Bandwidth selection method (default: "beta-reference"). |
'beta-reference'
|
window_size
|
int or None
|
If set, only use the last |
None
|
normalize
|
bool
|
Whether to normalize the KDE. |
True
|
Examples:
>>> bk = BetaKernel()
>>> for p in [0.1, 0.2, 0.15, 0.05, 0.1]:
... bk.update(p)
>>> bool(bk.bet(0.1) >= 1.0) # density peaks near the data (== 1.0 without beta_kde)
True
Source code in src/online_cp/betting.py
online_cp.betting.BetaMoments
¶
Bases: BettingStrategy
Betting strategy based on Beta distribution with method of moments.
Maintains online running mean and variance (Welford's algorithm) and uses method-of-moments to fit Beta(a, b) parameters.
Examples:
>>> bm = BetaMoments()
>>> for p in [0.01, 0.02, 0.05, 0.01, 0.03]:
... bm.update(p)
>>> bool(bm.bet(0.01) > 1.0) # should favor small p-values
True
>>> bool(bm.bet(0.99) < 1.0)
True
Source code in src/online_cp/betting.py
online_cp.betting.BetaMLE
¶
Bases: BettingStrategy
Betting strategy based on MLE for Beta distribution parameters.
Maintains sufficient statistics (sum of log) and re-optimizes Beta(a, b) via maximum likelihood at each step.
Examples:
>>> bmle = BetaMLE()
>>> for p in [0.01, 0.02, 0.01, 0.02, 0.01]:
... bmle.update(p)
>>> bool(bmle.bet(0.01) > 1.0) # should favor small p-values
True
Source code in src/online_cp/betting.py
online_cp.betting.ParticleFilterStrategy
¶
Bases: BettingStrategy
Particle filter betting strategy for adaptive Beta distribution estimation.
Maintains a particle cloud in (log-alpha, log-beta) space and uses sequential Monte Carlo to track the evolving distribution of p-values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_particles
|
int
|
Number of particles. |
1000
|
process_noise_std
|
float or 'auto'
|
Standard deviation of the random walk noise. "auto" learns volatility. |
0.05
|
vol_noise_std
|
float
|
Noise on the log-volatility process (when process_noise_std="auto"). |
0.01
|
seed
|
int, np.random.Generator, or None
|
Random seed or Generator for reproducibility. |
None
|
Examples:
>>> pf = ParticleFilterStrategy(num_particles=100, seed=42)
>>> for p in [0.1, 0.1, 0.1]:
... pf.update(p)
>>> bool(pf.bet(0.1) > 1.0) # should favor 0.1
True
Source code in src/online_cp/betting.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 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 | |
plot_parameters(title='Particle Filter Parameter Evolution')
¶
Plot the evolution of the learned Beta parameters.
Source code in src/online_cp/betting.py
online_cp.betting.FixedStrategy
¶
Bases: BettingStrategy
A strategy that uses a fixed, unchanging density function.
Accepts either a scipy-like distribution object OR explicit pdf/cdf callables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
object with .pdf() and .cdf() methods
|
E.g., a scipy.stats distribution. |
None
|
pdf
|
callable
|
Explicit PDF (overrides distribution). |
None
|
cdf
|
callable
|
Explicit CDF. If pdf given without cdf, numerical integration is used. |
None
|
check_integration
|
bool
|
Verify that the PDF integrates to 1. |
True
|
Examples:
>>> fs = FixedStrategy(distribution=uniform())
>>> fs.bet(0.5)
1.0
>>> fs2 = FixedStrategy(pdf=lambda x: 2 * (1 - x))
>>> fs2.bet(0.1)
1.8
Source code in src/online_cp/betting.py
online_cp.betting.ExpertAggregationStrategy
¶
Bases: BettingStrategy
Exponentially Weighted Average aggregation of expert betting strategies.
Maintains a portfolio over multiple expert strategies and reweights based on their performance (log-gains).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experts
|
list of BettingStrategy
|
The expert strategies to aggregate. |
required |
learning_rate
|
float
|
Step size for the exponential weights update. |
0.1
|
base_alpha
|
float
|
Base mixing rate towards uniform for regularization. |
0.01
|
Examples:
>>> expert_good = FixedStrategy(
... pdf=lambda x: norm.pdf(x, 0.1, 0.1) / (norm.cdf(1, 0.1, 0.1) - norm.cdf(0, 0.1, 0.1)),
... check_integration=False,
... )
>>> expert_bad = FixedStrategy(distribution=uniform())
>>> agg = ExpertAggregationStrategy(experts=[expert_good, expert_bad])
>>> agg.update(0.1)
>>> w = agg.get_current_weights()
>>> bool(w[0] > w[1])
True
Source code in src/online_cp/betting.py
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 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 | |
get_current_weights()
¶
plot_weights(expert_names=None, title='Evolution of Expert Weights', ax=None)
¶
Plot the evolution of expert weights over time.
Source code in src/online_cp/betting.py
online_cp.betting.PiecewiseConstantBetting
¶
Bases: BettingStrategy
Piecewise-constant betting function f_{(a,b)} from ALRW2 §9.2.
The betting function is defined as:
f_{(a,b)}(p) = b/a if p <= a
f_{(a,b)}(p) = (1-b)/(1-a) if p > a
This integrates to 1 over [0,1] for any a, b in (0,1), making it a valid betting density. It bets that fraction b of the probability mass falls below threshold a.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
float
|
Threshold in (0, 1). Splits the domain into [0, a] and (a, 1]. |
required |
b
|
float
|
Probability mass allocated to [0, a]. Must be in (0, 1). |
required |
References
Vovk, Gammerman & Shafer (2022). Algorithmic Learning in a Random World, 2nd edition, §9.2. Cambridge University Press.
Examples:
>>> pcb = PiecewiseConstantBetting(a=0.3, b=0.5)
>>> pcb.bet(0.1) # b/a = 0.5/0.3
1.6666666666666667
>>> pcb.bet(0.8) # (1-b)/(1-a) = 0.5/0.7
0.7142857142857143
>>> abs(pcb.integrate(1.0) - 1.0) < 1e-10
True