Mondrian Conformal Prediction¶
online_cp.mondrian.MondrianConformalRegressor
¶
Bases: SerializableMixin
Mondrian conformal regressor: group-conditional coverage.
Wraps a conformal regressor (ridge, kernel ridge, or lasso). A single pooled model is trained on ALL data; the Mondrian construction lives in the calibration step, not in the model. At prediction time the nonconformity scores are computed from the pooled model, but the interval is calibrated only against scores from training examples in the SAME Mondrian category as the test object. This is the Mondrian taxonomy construction of [ALRW2 §4.6] (Vovk et al.).
The result is valid group-conditional (object-conditional) coverage: for every category \(k\),
where \(\kappa\) is the category function. Unconditional conformal predictors only guarantee this marginally; conditioning on a Mondrian taxonomy restores the guarantee within each group, at the cost of a smaller effective calibration set per category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_model
|
ConformalRidgeRegressor, KernelConformalRidgeRegressor, or ConformalLassoRegressor
|
The underlying conformal regressor. Trained on all data (pooled). |
required |
category_fn
|
callable
|
A function |
required |
Source code in src/online_cp/mondrian.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 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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 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 | |
categories
property
¶
Set of Mondrian categories discovered in the training stream so far.
learn_initial_training_set(X, y)
¶
Batch-train the pooled model and record each object's category.
Fits base_model on the full training set (X, y) and stores the
Mondrian category \(\kappa(x_i)\) of every training object, so later
predictions can be calibrated within categories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like of shape (n, d)
|
Training objects. |
required |
y
|
array-like of shape (n,)
|
Training responses. |
required |
Source code in src/online_cp/mondrian.py
learn_one(x, y, **kwargs)
¶
Update the pooled model with one example and record its category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array-like of shape (d,)
|
New object. |
required |
y
|
float
|
Observed response. |
required |
**kwargs
|
Forwarded to the underlying model's |
{}
|
Source code in src/online_cp/mondrian.py
predict(x: NDArray[np.floating[Any]], epsilon: float | NDArray[np.floating[Any]] | None = None, bounds: str = 'both') -> ConformalPredictionInterval | MultiLevelPredictionInterval
¶
Compute the Mondrian (group-conditional) prediction interval.
Computes the pooled model's \(A\)/\(B\) nonconformity decomposition for the
test object, then builds the interval using only the training scores
whose category matches the test object's Mondrian category (a boolean
cat_mask over the pooled scores). This restricts calibration to the
test object's group while reusing the single pooled fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array-like of shape (d,)
|
Test object. |
required |
epsilon
|
float, array-like, or None
|
Significance level(s). If None, uses |
None
|
bounds
|
('both', 'lower', 'upper')
|
Which side(s) of the interval to compute. |
"both"
|
Returns:
| Type | Description |
|---|---|
ConformalPredictionInterval or MultiLevelPredictionInterval
|
The group-conditional prediction interval at |
Source code in src/online_cp/mondrian.py
compute_p_value(x, y, **kwargs)
¶
Mondrian conformal p-value for the candidate pair \((x, y)\).
Computes the (optionally smoothed) conformal p-value using only the pooled nonconformity scores from the same Mondrian category as \(x\), so the p-value is calibrated group-conditionally. Dispatches to the appropriate backend (ridge, kernel ridge, or lasso).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array-like of shape (d,)
|
Test object. |
required |
y
|
float
|
Candidate response. |
required |
**kwargs
|
Backend options such as |
{}
|
Returns:
| Type | Description |
|---|---|
float
|
The group-conditional p-value. |
Source code in src/online_cp/mondrian.py
online_cp.mondrian.MondrianConformalClassifier
¶
Bases: SerializableMixin
Mondrian conformal classifier: group-conditional coverage.
Wraps a conformal classifier (KNN or SVM). A single pooled model is trained on ALL data; only the p-value computation is restricted to training examples in the SAME Mondrian category as the (hypothesised) test example. This yields valid group-conditional coverage: for every category \(k\),
The most common special case is label-conditional conformal prediction
[ALRW2 §4.6.7], where the category of an example is its label,
\(\kappa(n, (x, y)) = y\). Use category_fn="label" for this; it guarantees
per-class coverage and is the standard remedy for class imbalance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_model
|
ConformalNearestNeighboursClassifier or ConformalSupportVectorMachine
|
The underlying conformal classifier. Trained on all data (pooled). |
required |
category_fn
|
str or callable
|
Determines the Mondrian taxonomy:
|
required |
Source code in src/online_cp/mondrian.py
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 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 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 | |
categories
property
¶
Set of Mondrian categories discovered in the training stream so far.
learn_initial_training_set(X: NDArray[np.floating[Any]], y: NDArray[Any]) -> None
¶
Batch-train the pooled model and record each example's category.
Fits base_model on the full training set and stores the Mondrian
category of every training example. For a label-aware taxonomy (e.g.
category_fn="label") the category depends on both \(x_i\) and \(y_i\);
for an object-conditional taxonomy it depends on \(x_i\) only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like of shape (n, d)
|
Training objects. |
required |
y
|
array-like of shape (n,)
|
Training labels. |
required |
Source code in src/online_cp/mondrian.py
learn_one(x: NDArray[np.floating[Any]], y: Any, **kwargs: Any) -> None
¶
Update the pooled model with one example and record its category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array-like of shape (d,)
|
New object. |
required |
y
|
hashable
|
Observed label. |
required |
**kwargs
|
Any
|
Forwarded to the underlying model's |
{}
|
Source code in src/online_cp/mondrian.py
predict(x: NDArray[np.floating[Any]], epsilon: float | NDArray[np.floating[Any]] | None = None, return_p_values: bool = False) -> Any
¶
Compute the Mondrian (group-conditional) prediction set.
For each candidate label the conformal p-value is computed from the pooled scores, restricted to the matching Mondrian category:
- Label-aware taxonomy (e.g.
category_fn="label"): each hypothesised label \(y\) defines its own category mask \(\kappa(x, y)\), so the candidate is calibrated against training examples sharing that category. This is what makes label-conditional coverage possible. - Object-conditional taxonomy: a single mask \(\kappa(x)\) is shared across all candidate labels.
For a multiclass SVM the scores are first restricted to the one-vs-rest positive class before the category mask is applied, matching the SVM's per-class nonconformity construction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array-like of shape (d,)
|
Test object. |
required |
epsilon
|
float, array-like, or None
|
Significance level(s). If None, uses |
None
|
return_p_values
|
bool
|
If True, also return the |
False
|
Returns:
| Type | Description |
|---|---|
prediction set, or ``(prediction set, p_values)`` if ``return_p_values``.
|
|