Extending
Summary
The estimators resolve their teacher and their regional model by name from
two registries. Registering a new name makes it available as teacher= or
curve_fitter=. Below the estimators sit two functions that turn regions you
already have into a model.
Usage
A new teacher: factory(task, **params) returns an object with fit(X, y)
and forward(X) (values for regression, positive-class probability for
classification). params is the user's teacher_params; random_state is
added if the factory accepts it.
from calm_additive import CALMRegressor, register_teacher
register_teacher("my_teacher", lambda task, **params: MyTeacher(**params))
calm = CALMRegressor(teacher="my_teacher", teacher_params={"depth": 6}).fit(X, y)
A new regional model: implement MaskedFitter. X is the masked design,
one column per (feature, region); mask marks which entries are active.
from calm_additive import MaskedFitter, register_curve_fitter
class MyFitter(MaskedFitter):
def fit(self, X, y, mask): ...
def predict(self, X, mask): ...
def curves(self): ... # optional: what the model learned
register_curve_fitter("mine", lambda ctx, **params: MyFitter(**params))
calm = CALMRegressor(curve_fitter="mine").fit(X, y)
curves() returns the fitted model as an AdditiveTerms: the intercept, one
Curve per masked column, and what each column adds when it is inactive.
With it, every view (print_summary, plot_curves, …) works for
your fitter; without it the model still predicts.
from calm_additive import AdditiveTerms, Curve
def curves(self):
columns = [Curve(knots, values, "linear") for knots, values in self.splines]
return AdditiveTerms(self.intercept, columns, self.inactive_scores)
👉 A teacher needs no registration if you pass it directly:
CALMRegressor(teacher=my_model).
API
calm_additive.engine.register_teacher(name, factory)
Register a teacher factory: factory(task, **params) → object with
fit(X, y) and forward(X) (and optionally jac(X) for derivative
methods), where params is the user's teacher_params (plus
random_state if the factory accepts it).
Source code in calm_additive/engine.py
217 218 219 220 221 222 | |
calm_additive.engine.register_curve_fitter(name, factory)
Register a fitter factory under a name (calm_additive does this
for "ebm"): factory(ctx, **params) → a MaskedFitter, where params
is the user's curve_fitter_params (plus random_state if the factory
accepts it). "surrogate" is built in and reserved.
Source code in calm_additive/engine.py
207 208 209 210 211 212 213 214 | |
calm_additive.engine.MaskedFitter
The refit contract: an additive model over masked columns.
X is the masked design (one column per (feature, region), inactive
entries zeroed), mask the parallel boolean activity matrix. Implement
fit(X, y, mask) and predict(X, mask); predict must return (N,)
scores (probabilities of the positive class for classification).
Implement curves() as well — the fitted model as an AdditiveTerms —
and the model can be printed and plotted (print_summary, plot_curves, …);
without it the model still predicts.
calm_additive.engine.Curve(x, y, kind='step', other=0.0, fn=None)
dataclass
A one-dimensional function, the way a fitter stores it.
kind |
x |
y |
value at v |
|---|---|---|---|
"step" |
(B-1,) ascending cut points |
(B,) |
y[i] on [x[i-1], x[i]) |
"linear" |
(T,) ascending knots |
(T,) |
linear between knots, flat outside |
"levels" |
(L,) ascending levels |
(L,) |
y of the matching level, else other |
fn, when given, is the exact evaluator and x/y only its drawable
sampling (a curve read from an effector effect).
Methods:
| Name | Description |
|---|---|
shifted |
The same curve moved up by |
shifted(c)
The same curve moved up by c.
Source code in calm_additive/engine.py
121 122 123 124 125 126 127 128 129 | |
calm_additive.engine.AdditiveTerms(intercept, columns, inactive, link='identity')
dataclass
What a MaskedFitter learned, column by column.
The fitter's score is intercept plus, per masked column, the column's
curve at the row's value when the column is active and inactive[c]
when it is not. link names the scale that sum lives on: "identity"
(the prediction itself) or "logit" (log-odds of the positive class).
calm_additive.engine.fit_chain(chain, X=None, y=None, *, curve_fitter='surrogate', curve_fitter_params=None, snapshot='final', task='regression', target='teacher', random_state=None)
Materialize a chain you inspected: snapshot (default the final CALM)
picks which decision-sequence prefix becomes the model.
Source code in calm_additive/engine.py
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 | |
calm_additive.engine.fit_partitions(effect, partitions, *, task='regression', curve_fitter='surrogate', curve_fitter_params=None, X=None, y=None, target='teacher', random_state=None, chain=None, calm=None)
Materialize {feature: Partition} into an executable model.
The lowest-level entry: both fit_chain and the sklearn twins land here.
partitions must be bound to effect. For the surrogate mode the offset
target is the teacher's predictions (target="teacher") or y
(target="y", requires y). Refit modes require y (and use
X — default effect.data — as the design source);
curve_fitter_params and random_state reach a fitter given by name.
Source code in calm_additive/engine.py
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 668 669 670 671 672 673 | |
calm_additive.engine.ExecutableCALM(task, *, partitions, chain=None, calm=None, surrogate=None, transform=None, curve_fitter=None)
A fitted, predict-capable CALM. Built by fit_chain/fit_partitions.
Carries its provenance: .chain (when built from one), .calm (the
snapshot), .partitions, .transform + .curve_fitter (refit mode) or
.surrogate (free mode). predict returns labels for classification
(predict_score gives the raw score / positive-class probability).
Methods:
| Name | Description |
|---|---|
predict |
|
predict_score |
|
terms |
The model as functions — one curve per (feature, region), each a |
Attributes:
| Name | Type | Description |
|---|---|---|
interactions_info |
dict
|
|
n_conditional_interactions |
int
|
The paper's metric: Σ_i |S_i| over the split features — one per |
Source code in calm_additive/engine.py
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
interactions_info
property
n_conditional_interactions
property
The paper's metric: Σ_i |S_i| over the split features — one per
(feature, conditioning feature) pair, so x1 | x2 and x2 | x1
count as two.
predict(X)
Source code in calm_additive/engine.py
543 544 545 546 547 | |
predict_score(X)
Source code in calm_additive/engine.py
536 537 538 539 540 541 | |
terms()
The model as functions — one curve per (feature, region), each a
feature's whole contribution inside its region. What print_summary and
plot_curves read; the same for every curve fitter.
Source code in calm_additive/engine.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | |