Conformal Test Martingales¶
Martingales¶
online_cp.martingale.PluginMartingale
¶
Bases: ConformalTestMartingale
Plugin martingale using a betting strategy with cautious-start mixing.
The martingale wraps a BettingStrategy and applies cautious mixing:
during the first min_sample_size observations, the strategy's density
is linearly mixed towards uniform to avoid catastrophic loss before the
density estimate is reliable.
Protocol per step:
1. Predict: evaluate strategy.bet(p) (uses past data only)
2. Mix: apply cautious start b = λ * f + (1 - λ)
3. Accumulate: logM += log(b)
4. Learn: call strategy.update(p)
5. Expose: set b_n and B_n for the next step
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
betting_strategy
|
BettingStrategy or type
|
An instantiated strategy, or a class to be instantiated with kwargs. |
GaussianKDE
|
min_sample_size
|
int
|
Number of steps over which to linearly ramp up from uniform to full betting. |
100
|
**kwargs
|
Any
|
Passed to the strategy constructor if a class is given. |
{}
|
Examples:
>>> strat = FixedStrategy(pdf=lambda x: 2 if x < 0.5 else 0, check_integration=False)
>>> m = PluginMartingale(betting_strategy=strat, min_sample_size=0)
>>> m.update(0.1)
>>> bool(np.isclose(m.M, 2.0))
True
>>> m.update(0.9)
>>> bool(m.M == 0.0)
True
Source code in src/online_cp/martingale.py
977 978 979 980 981 982 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 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 | |
online_cp.martingale.SimpleMixtureMartingale
¶
Bases: ConformalTestMartingale
Simple Mixture Martingale using the incomplete gamma function.
This is the canonical "parameter-free" test martingale that averages over all power alternatives t^epsilon with epsilon ~ Exp(1). It has a closed-form solution based on the regularized incomplete gamma function.
After each step, b_n and B_n are set analytically:
- b_n(p) = M_n(p) / M_{n-1} where M_n(p) is the martingale value if the
next observation were p.
- B_n(p) = integral of b_n from 0 to p.
Examples:
>>> sm = SimpleMixtureMartingale()
>>> sm.update(0.01)
>>> sm.update(0.01)
>>> bool(sm.M > 1)
True
>>> sm.update(1.0)
>>> bool(sm.M < sm.martingale_values[-2])
True
Source code in src/online_cp/martingale.py
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 | |
online_cp.martingale.SimpleJumper
¶
Bases: ConformalTestMartingale
Simple Jumper betting martingale (Algorithm 8.1 of ALRW2).
Uses a set of experts indexed by epsilon with betting functions f_epsilon(p) = 1 + epsilon*(p - 0.5). A Markov chain with jump rate J tracks the best expert, enabling adaptation to changing alternatives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
J
|
float
|
Jump rate (probability of switching expert per step). |
0.01
|
E
|
list of float or None
|
Expert grid. Default is [-1, -0.5, 0, 0.5, 1] (Algorithm 8.1). |
None
|
References
Vovk, Gammerman & Shafer (2022). Algorithmic Learning in a Random World, 2nd edition, Algorithm 8.1. Cambridge University Press.
Examples:
Source code in src/online_cp/martingale.py
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 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 | |
online_cp.martingale.CompositeJumper
¶
Bases: ConformalTestMartingale
Composite Jumper that averages over multiple jump rates.
Examples:
Source code in src/online_cp/martingale.py
online_cp.martingale.SleeperStayer
¶
Bases: ConformalTestMartingale
Sleeper/Stayer conformal test martingale (Algorithm 9.4 of ALRW2).
Maintains a grid of piecewise-constant betting experts indexed by (a, b) together with a sleeping capital account. At each step, a fraction R of the sleeping capital is redistributed equally to all active experts.
Each expert uses the betting function f_{(a,b)}(p) = b/a if p <= a, else (1-b)/(1-a). This targets change-points where the conformal p-values shift from Uniform to having mass b below threshold a.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
R
|
float
|
Wake-up rate: fraction of sleeping capital redistributed per step. |
0.001
|
G
|
int
|
Grid resolution. The grid is {1/G, 2/G, ..., (G-1)/G}^2. |
10
|
References
Vovk, Gammerman & Shafer (2022). Algorithmic Learning in a Random World, 2nd edition, Algorithm 9.4 (Sleeper). Cambridge University Press.
Examples:
>>> ss = SleeperStayer(R=0.01, G=5)
>>> for _ in range(50):
... ss.update(0.05)
>>> bool(ss.M > 1.0)
True
Source code in src/online_cp/martingale.py
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 | |
online_cp.martingale.SleeperDrifter
¶
Bases: ConformalTestMartingale
Sleeper/Drifter conformal test martingale (Algorithm 9.5 of ALRW2).
Extension of the Sleeper/Stayer that wakes experts in batches every M steps and uses a drifting threshold that interpolates between the initial guess a and the target b over time.
The drifting threshold for expert (i, a, b) at step n is: a' = (iM/n)a + (1 - iM/n)b
This makes the martingale more sensitive to gradual distribution shifts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
R
|
float
|
Wake-up rate per batch: fraction of sleeping capital allocated when a new batch wakes up. |
0.001
|
G
|
int
|
Grid resolution. The grid is {1/G, 2/G, ..., (G-1)/G}^2. |
10
|
M
|
int
|
Batch interval: new experts wake up every M steps. |
100
|
References
Vovk, Gammerman & Shafer (2022). Algorithmic Learning in a Random World, 2nd edition, Algorithm 9.5 (Drifter). Cambridge University Press.
Examples:
>>> sd = SleeperDrifter(R=0.01, G=5, M=10)
>>> for _ in range(50):
... sd.update(0.05)
>>> bool(sd.M > 1.0)
True
Source code in src/online_cp/martingale.py
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 | |
Change-Point Detection Wrappers¶
online_cp.martingale.VilleWrapper
¶
Ville's inequality procedure for change-point detection.
The simplest test based on a conformal test martingale: reject the exchangeability hypothesis when the running maximum of the martingale exceeds a threshold c. By Ville's inequality:
P(∃n : S_n >= c) <= 1/c
So threshold c = 20 gives a 5% significance level, c = 100 gives 1%, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
martingale
|
ConformalTestMartingale
|
The underlying martingale to wrap. |
required |
threshold
|
float
|
Default alarm threshold (default 20, i.e. 5% significance). |
20
|
References
Vovk, Gammerman & Shafer (2022). Algorithmic Learning in a Random World, 2nd edition, §8.4.1 (The Ville Procedure). Cambridge University Press.
Examples:
>>> from online_cp.martingale import SimpleJumper, VilleWrapper
>>> sj = SimpleJumper(J=0.1)
>>> ville = VilleWrapper(sj, threshold=20)
>>> for _ in range(10):
... ville.update(0.5)
>>> bool(ville.rejected)
False
Source code in src/online_cp/martingale.py
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 | |
log_max
property
¶
Log of the running maximum of the martingale.
max
property
¶
Running maximum of the martingale.
rejected
property
¶
Whether the exchangeability hypothesis has been rejected.
rejection_time
property
¶
Step at which the hypothesis was first rejected, or None.
update(p: float) -> None
¶
Update the inner martingale and track the running maximum.
Source code in src/online_cp/martingale.py
alarm(threshold=None)
¶
Check whether max(S_n) exceeds the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float or None
|
Override threshold. If None, uses the threshold set at construction. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the running maximum exceeds the threshold. |
Source code in src/online_cp/martingale.py
online_cp.martingale.CUSUMWrapper
¶
CUSUM change-detection wrapper for any conformal test martingale.
Computes the Page CUSUM statistic as the ratio of the current martingale value to its running minimum:
gamma_n = S_n / min_{i <= n} S_i
In log-space: log(gamma_n) = logM_n - min_{i <= n} logM_i
This removes any accumulated "debt" from an initial in-control period, giving faster detection after the change-point. Optionally accepts a linear barrier for controlling the false alarm rate over long horizons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
martingale
|
ConformalTestMartingale
|
The underlying martingale to wrap. |
required |
barrier_slope
|
float or None
|
If not None, the alarm threshold grows linearly as barrier_slope * n. |
None
|
References
Vovk, Gammerman & Shafer (2022). Algorithmic Learning in a Random World, 2nd edition, §8.3. Cambridge University Press.
Examples:
>>> from online_cp.martingale import SimpleJumper, CUSUMWrapper
>>> sj = SimpleJumper(J=0.01)
>>> cusum = CUSUMWrapper(sj)
>>> for _ in range(10):
... cusum.update(0.5)
>>> bool(cusum.gamma >= 1.0) # gamma is always >= 1 (since S_n >= min S_i is not guaranteed)
True
Source code in src/online_cp/martingale.py
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 | |
gamma
property
¶
Current CUSUM statistic.
log_gamma
property
¶
Current log CUSUM statistic.
cusum_values
property
¶
All CUSUM statistic values.
log_cusum_values
property
¶
All log CUSUM statistic values.
update(p: float) -> None
¶
Update the inner martingale and recompute the CUSUM statistic.
Source code in src/online_cp/martingale.py
alarm(threshold)
¶
Check whether gamma_n exceeds the threshold (optionally with barrier).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
The alarm threshold. If barrier_slope is set, the effective threshold at step n is threshold + barrier_slope * n. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if gamma_n exceeds the (possibly time-varying) threshold. |
Source code in src/online_cp/martingale.py
online_cp.martingale.ShiryaevRobertsWrapper
¶
Shiryaev-Roberts change-detection wrapper for any conformal test martingale.
Computes the Shiryaev-Roberts statistic as:
R_n = sum_{i=1}^{n} S_n / S_i
In log-space: R_n = sum_{i=1}^{n} exp(logM_n - logM_{i-1})
This is always >= the CUSUM statistic (sum >= max), giving a slightly different power/false-alarm trade-off.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
martingale
|
ConformalTestMartingale
|
The underlying martingale to wrap. |
required |
References
Vovk, Gammerman & Shafer (2022). Algorithmic Learning in a Random World, 2nd edition, §8.3. Cambridge University Press.
Examples:
>>> from online_cp.martingale import SimpleJumper, ShiryaevRobertsWrapper
>>> sj = SimpleJumper(J=0.01)
>>> sr = ShiryaevRobertsWrapper(sj)
>>> for _ in range(10):
... sr.update(0.5)
>>> sr.R >= 0
True
Source code in src/online_cp/martingale.py
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 | |
R
property
¶
Current Shiryaev-Roberts statistic.
sr_values
property
¶
All Shiryaev-Roberts statistic values.
update(p: float) -> None
¶
Update the inner martingale and recompute the SR statistic.
Uses the O(1) recursive formula (eq. 8.18 of ALRW2): R_n = (S_n / S_{n-1}) * (R_{n-1} + 1)
Source code in src/online_cp/martingale.py
alarm(threshold)
¶
Check whether R_n exceeds the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
The alarm threshold. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if R_n exceeds the threshold. |
Source code in src/online_cp/martingale.py
Betting Strategies¶
online_cp.martingale.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/martingale.py
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 373 374 375 376 377 378 379 380 | |
online_cp.martingale.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/martingale.py
online_cp.martingale.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/martingale.py
online_cp.martingale.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/martingale.py
online_cp.martingale.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 or None
|
Random seed 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/martingale.py
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 | |
plot_parameters(title='Particle Filter Parameter Evolution')
¶
Plot the evolution of the learned Beta parameters.
Source code in src/online_cp/martingale.py
online_cp.martingale.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/martingale.py
online_cp.martingale.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/martingale.py
758 759 760 761 762 763 764 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 | |
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/martingale.py
online_cp.martingale.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