Backends
A backend fits the Riesz representer for a given estimand and loss. Five implementation packages ship today; each slots into RieszEstimator through one of two Protocols. rieszboost and krrr consume the augmented dataset of evaluation points with weights \((D_r, C_r)\) (Backend.fit_augmented). forestriesz ships both: an augmentation-style AugForestRieszRegressor that works on every estimand without per-estimand configuration (Backend.fit_augmented), and a moment-style ForestRieszRegressor that consumes original rows plus a user-supplied list of basis functions (MomentBackend.fit_rows) and supports honest-split confidence intervals. riesznet consumes the augmented dataset through Backend.fit_augmented, grouping it by original row to train a PyTorch model with autograd on minibatches of rows.
Pick a backend
Boosting (rieszboost) |
Kernel ridge (krrr) |
Forest (forestriesz) |
Neural (riesznet) |
|
|---|---|---|---|---|
| Method | Gradient boosting (xgboost or sklearn) | Closed-form solve of \((\mathrm{diag}(a) K + n\lambda I)\gamma = -b/2\) | Ensemble of single-tree Riesz regressors (augmented) or per-leaf linear-moment solve (moment) | PyTorch autograd on the per-row Bregman-Riesz loss |
| Algorithm | Lee & Schuler (2501.04871) | Singh (2102.11076) | Chernozhukov, Newey, Quintas-Martínez, Syrgkanis (ICML 2022) for the moment-style backend | Chernozhukov, Newey, Quintas-Martínez, Syrgkanis (2110.03031) |
| Best for | Tabular data, \(n \gtrsim 1{,}000\), mixed feature types | Smooth \(\alpha_0\), small/medium \(n\), low-dimensional \(X\) | Many covariates; the moment-style flavor adds asymptotic CIs on \(\alpha(z)\) | High-dimensional inputs, custom architectures, image / text features |
| Iterative? | Yes (rounds + early stopping) | No (closed-form per λ) | No (single forest fit) | Yes (epochs + early stopping) |
| Hyperparameters | n_estimators, learning_rate, max_depth, reg_lambda, subsample |
kernel, lambda_grid, solver |
n_estimators, max_depth, min_samples_leaf, plus honest / riesz_feature_fns for the moment-style flavor |
hidden_sizes, learning_rate, epochs, dropout, or any module_factory / optimizer_factory |
| Loss support | SquaredLoss, KLLoss, BernoulliLoss, BoundedSquaredLoss |
SquaredLoss only (today) |
All four for the augmented flavor; SquaredLoss only for the moment flavor |
SquaredLoss, KLLoss, BernoulliLoss, BoundedSquaredLoss |
| Scaling | Linear in \(n\) via xgboost | \(O(n^3)\) direct; Nyström + RFF for larger \(n\) | \(O(n \log n)\) per tree | Per-epoch cost is one forward pass over \(n + \sum_i k_i\) points; scales to GPU |
| Confidence intervals | No | No | Yes — predict_interval on single-basis fits (moment-style only) |
No |
| Convenience class | rieszboost.RieszBooster |
krrr.KernelRieszRegressor |
forestriesz.AugForestRieszRegressor (recommended; works on every estimand) and forestriesz.ForestRieszRegressor (the published ForestRiesz method, kept for comparison; supports CIs) |
riesznet.RieszNet (default MLP); riesznet.TorchBackend for arbitrary nn.Module factories |
A fifth learner, riesztree, fits a single decision tree (RieszTreeRegressor). It is fast and easy to inspect, and it supports all four losses.
Where to start
Start with RieszBooster at its default settings. For \(n\) in the low thousands to tens of thousands of rows it is a good first choice. In a cross-fit ATE simulation with the defaults, the one-step estimate had bias of about \(-0.001\) and 95% intervals covered the truth at least 95% of the time.
Switch when your problem fits another learner’s strengths:
- Kernel ridge (
krrr) for small \(n\) or a smooth, low-dimensional \(\alpha\). - Neural net (
riesznet) for large \(n\), many covariates, or image and text features. - Forest (
forestriesz) when you need confidence intervals on \(\alpha(z)\) (ForestRieszRegressorwithhonest=True, inference=True). - Single tree (
riesztree) when you want a rule you can read.
The same setting under different names
Each learner names its settings after the library it follows (xgboost, sklearn, PyTorch). This table lines them up.
| Setting | RieszBooster |
KernelRieszRegressor |
AugForestRieszRegressor |
RieszTreeRegressor |
RieszNet |
|---|---|---|---|---|---|
| Amount of fitting | n_estimators (trees) |
— | n_estimators (trees) |
max_depth, max_leaf_nodes |
epochs |
| Step size | learning_rate |
— | — | — | learning_rate |
| Regularization | reg_lambda, max_depth |
lambda_grid (chosen automatically) |
min_samples_leaf, max_depth |
min_samples_leaf, ccp_alpha |
weight_decay, dropout |
| Early stopping | early_stopping_rounds |
— | — | early_stopping_rounds |
early_stopping_rounds |
| Held-out fraction | validation_fraction=0.1 |
validation_fraction=0.2 |
— | validation_fraction=0.1 |
validation_fraction=0.1 |
validation_fraction is used only when early stopping is on. KernelRieszRegressor always holds out rows, because it picks λ from lambda_grid by held-out loss. Passing eval_set= to fit replaces the internal split in every learner.
Compose explicitly
Both convenience classes are subclasses of rieszreg.RieszEstimator with the backend defaulted. To swap backends or build your own, compose explicitly:
import functools
from rieszreg import RieszEstimator, ATE, SquaredLoss
from rieszboost.backends import XGBoostBackend, SklearnBackend
from krrr import KernelRidgeBackend, Gaussian
from forestriesz import AugForestRieszBackend, ForestRieszBackend
from riesznet import TorchBackend
from riesznet.modules import build_adam, build_mlp
# Default: xgboost
est = RieszEstimator(estimand=ATE(), backend=XGBoostBackend())
# Sklearn boosting with KernelRidge as the base learner
from sklearn.kernel_ridge import KernelRidge
est = RieszEstimator(
estimand=ATE(),
backend=SklearnBackend(lambda: KernelRidge(alpha=1.0, kernel="rbf", gamma=2.0)),
)
# Kernel ridge directly
est = RieszEstimator(
estimand=ATE(),
backend=KernelRidgeBackend(kernel=Gaussian(length_scale="median")),
)
# Random forest (augmentation-style backend; fit_augmented path)
est = RieszEstimator(
estimand=ATE(),
backend=AugForestRieszBackend(n_estimators=500),
)
# Random forest (moment-style backend; fit_rows path; supports CIs)
est = RieszEstimator(
estimand=ATE(),
backend=ForestRieszBackend(n_estimators=500),
)
# Neural network (augmentation-style backend; fit_augmented path)
est = RieszEstimator(
estimand=ATE(),
backend=TorchBackend(
module_factory=functools.partial(build_mlp, hidden_sizes=(64, 64)),
optimizer_factory=functools.partial(build_adam, lr=5e-3),
epochs=300,
),
)The convenience classes are thin subclasses that bake in the backend and surface backend-specific hyperparameters as constructor args, so that they fit cleanly into GridSearchCV. See the boosting, kernel, forest, tree, and neural pages for each.
Adding a backend
The RieszEstimator orchestrator is learner-agnostic. To run it with an algorithm we don’t ship, wrap that algorithm as a small class that satisfies one Protocol and register a predictor loader. This section walks through the minimum viable wrapper, a worked example, and how to plug it into cross_val_predict, GridSearchCV, and Pipeline.
This is the end-user route: keep the wrapper in your own module, depend on rieszreg, ship nothing back. Contributing a learner package to the family adds a few requirements (R parity, the full conformance suite, in-tree tier placement) covered in DESIGN.md Part B.
Checklist
A backend needs one fit method and a predictor with a registered loader. Everything else is optional: the orchestrator looks each optional method up by name and uses a default when it’s missing, so add only what your learner needs.
| Piece | Needed? | Without it | Section |
|---|---|---|---|
fit_augmented or fit_rows |
Required, one of the two | — | Pick an entry point |
A predictor with kind, predict_eta, predict_alpha and save |
Required | — | Minimum viable wrapper |
register_predictor_loader(kind, loader) at import |
For load |
RieszEstimator.load raises |
Save, load, conformance |
Subclass BaseEstimator and store constructor arguments unchanged |
Recommended | GridSearchCV can’t reach backend__<field> |
Sklearn composition |
| Write the backend as a dataclass with JSON-serializable fields | Recommended | After load, the backend must be set again before refitting |
Save, load, conformance |
holdout_fraction() |
For early stopping or λ selection | No rows are held out; only an eval_set gives validation data |
Sklearn composition |
bind_columns(feature_keys) |
For settings that name columns | Those settings take positions only | Column names in settings |
Pick an entry point
Two Protocols live in rieszreg.backends.base. Each backend implements one.
Backend.fit_augmented(aug_train, aug_valid, loss, …) is for learners whose loss decomposes naturally over augmented evaluation points. The orchestrator hands you an AugmentedDataset of pseudo-rows with is_original and potential_deriv_coef columns. The empirical Bregman-Riesz loss at \(\alpha\) is the sum over augmented rows \(r\) of \(D_r\,\tilde h(\alpha(Z_r)) + C_r\,h'(\alpha(Z_r))\), where \(D_r\) is is_original and \(C_r\) is potential_deriv_coef. Reference learners using this entry point: kernel ridge (KernelRidgeBackend), gradient boosting (XGBoostBackend, SklearnBackend), random forests (AugForestRieszBackend), single trees (RieszTreeBackend), and neural nets (TorchBackend, which groups the augmented rows by origin_index to minibatch over original rows).
MomentBackend.fit_rows(X_train, X_valid, estimand, loss, *, aug_train, aug_valid, …) is for learners that fit on the original rows and need the estimand itself. You receive the original-row feature arrays (columns in estimand.feature_keys order), the estimand, and the same augmented datasets fit_augmented would get. The augmented rows give every evaluation point \(a_j\) with coefficient \(c_j = -C_r\) and the original row it came from (origin_index), so each row’s moment is one vectorised group-by. Each row is a single training example to the learner. Reference learner using this entry point: the EconML-based random forest (ForestRieszBackend), which also reads the estimand to pick its basis functions.
Pick fit_augmented unless your learner needs the original-row matrix or the estimand: the augmented dataset already carries each row’s moment through origin_index. The orchestrator dispatches to the moment path only when fit_rows is the only method present.
Minimum viable wrapper
Three pieces: a backend class implementing one Protocol method, a predictor class with predict_eta and predict_alpha, and a one-line registration of the predictor’s loader. All three live in your own module.
Augmentation-style
from dataclasses import dataclass
import numpy as np
from sklearn.base import BaseEstimator
from rieszreg.backends import (
FitResult, register_predictor_loader,
)
class MyLearnerPredictor:
kind = "my-learner" # registry key, must be unique
def __init__(self, params, base_score, loss):
self.params = params # whatever your learner returns
self.base_score = base_score
self.loss = loss
def predict_eta(self, features):
return self.base_score + my_learner_predict(self.params, features)
def predict_alpha(self, features):
return self.loss.link_to_alpha(self.predict_eta(features))
def save(self, dir_path):
np.save(dir_path / "params.npy", self.params)
@classmethod
def load(cls, dir_path, *, base_score, loss, best_iteration):
params = np.load(dir_path / "params.npy")
return cls(params=params, base_score=base_score, loss=loss)
@dataclass(repr=False)
class MyLearnerBackend(BaseEstimator):
"""Augmentation-style wrapper around `my_learner_fit`."""
regularization: float = 1.0
def fit_augmented(self, aug_train, aug_valid, loss,
*, base_score, random_state):
# Each augmented row contributes D · h_tilde(α) + C · h'(α) to the loss.
# Translate (features, D, C) into whatever your learner consumes.
params = my_learner_fit(
features=aug_train.features,
is_original=aug_train.is_original,
potential_deriv_coef=aug_train.potential_deriv_coef,
regularization=self.regularization,
random_state=random_state,
)
predictor = MyLearnerPredictor(params, base_score=base_score, loss=loss)
return FitResult(predictor=predictor)
register_predictor_loader("my-learner", MyLearnerPredictor.load)Moment-style
Replace fit_augmented with fit_rows. The predictor stays the same.
@dataclass(repr=False)
class MyMomentBackend(BaseEstimator):
learning_rate: float = 1e-3
def fit_rows(self, X_train, X_valid, estimand, loss,
*, aug_train, aug_valid, base_score, random_state):
# m(α)(Z_i, Y_i) = sum_j c_j · α(a_j). The augmented rows carry every
# point a_j, its coefficient c_j = -C_r, and the row i it came from.
points = aug_train.features
coefs, row = -aug_train.potential_deriv_coef, aug_train.origin_index
params = my_moment_fit(
X_train, points, coefs, row,
learning_rate=self.learning_rate,
random_state=random_state,
)
predictor = MyLearnerPredictor(params, base_score=base_score, loss=loss)
return FitResult(predictor=predictor)Backend, MomentBackend, Predictor, and FitResult are all importable from rieszreg.backends. The Protocols are structural (typing.Protocol), so your wrapper does not need to inherit from anything as long as the method signatures match.
Worked example
The canonical linear-Gaussian DGP from rieszreg.testing.dgps is a binary treatment \(A \in \{0, 1\}\) with covariate \(X \in \mathbb R\) and propensity \(\pi(x) = \mathrm{logit}^{-1}(0.5\,x)\). The true representer for ATE is the inverse propensity weight \(\alpha_0(A, X) = (2A - 1) \,/\, [A\pi(X) + (1-A)(1-\pi(X))]\).
import numpy as np
from rieszreg import RieszEstimator, ATE
from rieszreg.testing.dgps import linear_gaussian_ate
dgp = linear_gaussian_ate()
df = dgp.sample(n=2000, rng=np.random.default_rng(0))
Z = df[["a", "x"]]
y = df["x"] + 2 * df["a"] + np.random.default_rng(1).normal(size=len(df)) # an outcome
est = RieszEstimator(
estimand=ATE(treatment="a", covariates=("x",)),
backend=MyLearnerBackend(regularization=0.1),
).fit(Z, y)
alpha_hat = est.predict(Z)
alpha_true = dgp.true_alpha(df)
print(f"Pearson(α̂, α₀) = {np.corrcoef(alpha_hat, alpha_true)[0, 1]:.3f}")RieszEstimator.fit reads feature_keys off the estimand, builds the augmented dataset by tracing \(m\) row by row, computes base_score from the loss and the empirical mean \(\bar m = \mathbb E_n[m(\alpha = 1)(Z, Y)]\), and dispatches to MyLearnerBackend.fit_augmented. The fitted predictor gets stored on est.predictor_. est.predict(Z) calls predictor.predict_alpha(features) on the rows you pass in.
y is the per-row outcome vector. Built-in estimands ignore it; pass it anyway so the call follows the sklearn convention. Custom \(Y\)-dependent estimands receive it inside m(\alpha)(z, y).
Sklearn composition
RieszEstimator already inherits from sklearn.base.BaseEstimator. Subclass BaseEstimator in your backend too. It gives the backend get_params and set_params, which GridSearchCV needs to reach backend settings. The examples on this page are dataclasses, so save keeps their settings; repr=False keeps sklearn’s printed form rather than the dataclass one. Composition with cross_val_predict, GridSearchCV, clone, and Pipeline works as long as the wrapper plays by sklearn’s two structural rules.
The constructor stores its arguments by name without modification. The __init__ that @dataclass writes for MyLearnerBackend sets self.regularization = regularization and stops. If you write __init__ yourself, do the same: no coercion, no validation, no derived attributes. clone(MyLearnerBackend(regularization=0.1)) then returns a fresh equivalent object, which sklearn relies on between folds.
Fit-time state lives on the predictor, not on the backend. The backend stays clone-clean across folds; fitted parameters live on the Predictor returned in FitResult, attached to est.predictor_ after fit.
With those two in place, the standard sklearn idioms compose:
from sklearn.model_selection import cross_val_predict, GridSearchCV, KFold
cv = KFold(n_splits=5, shuffle=True, random_state=0)
# Cross-fit α̂ for downstream DML / TMLE.
alpha_oof = cross_val_predict(est, Z, y, cv=cv)
# Tune the regularization on held-out Riesz loss.
grid = GridSearchCV(
estimator=est,
param_grid={"backend__regularization": [0.01, 0.1, 1.0, 10.0]},
cv=cv,
).fit(Z, y)
print(grid.best_params_)backend__regularization is sklearn’s nested-parameter syntax. It forwards to est.backend.regularization so any backend hyperparameter exposed as a constructor attribute is reachable.
If your backend uses held-out rows for fit-time logic such as early stopping or regularization-path selection, add a holdout_fraction() method. It returns the fraction of rows to hold out for this fit, or 0 when the fit won’t use them. The orchestrator calls it, splits the rows before augmentation, and passes the held-out part as aug_valid:
@dataclass(repr=False)
class MyEarlyStoppingBackend(BaseEstimator):
early_stopping_rounds: int | None = None
validation_fraction: float = 0.1
def holdout_fraction(self):
# Hold rows out only when early stopping will use them.
return self.validation_fraction if self.early_stopping_rounds is not None else 0.0An eval_set passed to fit replaces the split. A backend without the method gets no holdout, whatever attributes it has. Backends that compute their own internal CV or that don’t need a holdout define neither.
Column names in settings
If a backend setting names input columns, such as a list of categorical columns, add a bind_columns(feature_keys) method. It returns a copy of the backend with the names resolved to positions in feature_keys, the column order α̂ reads (treatment first). The orchestrator calls it once per fit and fits the copy, so the user’s backend keeps the names. rieszreg.backends.resolve_column_positions does the lookup and also accepts 0-based positions:
from dataclasses import dataclass, replace
from rieszreg.backends import resolve_column_positions
@dataclass(repr=False)
class MyCategoricalBackend(BaseEstimator):
categorical_features: tuple = ()
def bind_columns(self, feature_keys):
return replace(self, categorical_features=resolve_column_positions(
self.categorical_features, feature_keys, "categorical_features"))A backend without the method gets its settings unchanged, so they must be positions.
Save, load, conformance
RieszEstimator.save(path) writes metadata.json (loss spec, estimand factory_spec, base_score, your predictor.kind) and calls predictor.save(dir_path) for the binary payload. RieszEstimator.load(path) reads the metadata and looks up your registered predictor loader by kind. Built-in estimands round-trip automatically; custom estimands require RieszEstimator.load(path, estimand=...) so the user’s \(m\) is back in scope.
Run register_predictor_loader("my-learner", MyLearnerPredictor.load) at the top level of your wrapper module so import my_wrapper registers the loader.
save also stores the backend’s settings when the backend is a dataclass whose fields are all JSON-serializable. Otherwise the loaded estimator predicts as usual, but refitting it raises until you pass the backend again with set_params(backend=...).
rieszreg.testing.conformance ships small helpers (assert_clone_roundtrip, assert_get_params_round_trip) that verify the load-bearing sklearn properties on your wrapper:
from rieszreg.testing.conformance import assert_clone_roundtrip
def test_clone():
assert_clone_roundtrip(
lambda: RieszEstimator(
estimand=ATE(treatment="a", covariates=("x",)),
backend=MyLearnerBackend(regularization=0.1),
)
)For a private wrapper, those two helpers plus a fit/predict/score smoke test on linear_gaussian_ate cover the regression surface. The full suite (GridSearchCV, cross_val_predict, save/load on every built-in estimand, the estimator-consistency suite over the canonical DGPs) is the bar an in-tree contributed learner has to clear.