Losses

A Loss describes the Bregman-Riesz loss the learner minimizes. rieszreg ships four built-in losses (SquaredLoss, KLLoss, BernoulliLoss, BoundedSquaredLoss) and a Loss base class for defining your own.

Bregman-Riesz Losses

Hines & Miles (2025) show that for a convex, differentiable function \(h: \mathbb R \to \mathbb R\) (called \(F\) in their paper), the minimizer of a population loss based on \(h\) (the average Bregman divergence between \(\alpha\) and \(\alpha_0\) with potential \(h\)) can be expressed in a form that does not involve the true Riesz representer \(\alpha_0\).

\[ \begin{aligned} \arg\min_\alpha \mathcal L_h(\alpha) &= \arg\min_\alpha \mathbb E[h(\alpha_0(Z)) - h(\alpha(Z)) - h'(\alpha(Z))(\alpha_0(Z) - \alpha(Z))] \\ &= \arg\min_\alpha \mathbb E[\tilde h(\alpha(Z)) - m(h' \circ \alpha)(Z,Y)] \end{aligned} \]

where \(\tilde h(t) = t h'(t) - h(t)\). Kato (2026) and Hines & Miles both give some intuition for why this loss makes sense and what sensible choices of \(h\) are in different cases. When \(h(t) = t^2\) this reduces to the standard Riesz loss of Chernozhukov, Newey, Quintas-Martínez, and Syrgkanis (2021). rieszreg implements several Bregman-Riesz losses and also allows users to define their own by defining the function \(h\).

Loss table

The “Potential \(h\)” column is \(h\) in α-space — the function the user passes as potential= (or overrides). At fit time the learner produces an unconstrained \(\eta\), the link maps \(\eta\) to \(\alpha = g(\eta)\) in the domain, and \(h\) is evaluated at \(\alpha\). The “Effective potential \(h \circ g\)” column is what actually gets evaluated in \(\eta\)-space — useful when comparing losses to gradient-boosting objectives stated in \(\eta\).

Loss Potential \(h(\alpha)\) Effective \(h(g(\eta))\) \(\alpha\) domain Use for
SquaredLoss \(\alpha^2\) \(\eta^2\) \(\mathbb{R}\) Default.
KLLoss \(\alpha \log \alpha\) \(\eta \cdot e^\eta\) \((0, \infty)\) Density-ratio estimands (TSM, IPSI). Guarantees \(\hat\alpha > 0\).
BernoulliLoss \(\alpha \log \alpha + (1-\alpha)\log(1-\alpha)\) \(\eta\,\sigma(\eta) - \log(1+e^\eta)\) \((0, 1)\) \(\alpha_0\) known to lie in \((0, 1)\).
BoundedSquaredLoss(lo, hi) \(\alpha^2\) \(\big(\mathrm{lo}+(\mathrm{hi}-\mathrm{lo})\sigma(\eta)\big)^2\) \((\mathrm{lo}, \mathrm{hi})\) Squared-loss fit with prior bounds (e.g. trimmed-propensity ratios).

Choosing a loss for your estimand

Pick a loss whose α-range contains the true α of your estimand. Where α leaves the range, the fit is pinned at the edge of it, and α̂ is wrong there. SquaredLoss has no range limit, so it fits every estimand; use one of the others only when you know α stays inside its range, for example α > 0 for a density ratio under KLLoss.

fit can check one thing from the data: the mean of α, \(E[\alpha_0] = E[m(Z, 1)]\). When that mean lies outside the loss’s range, α can’t stay inside it, and fit warns before fitting. It still fits, so you can go ahead with that loss if you mean to:

  • ATE, ATT, AdditiveShift and LocalShift have mean 0, because their functional subtracts, e.g. \(\alpha(0, x)\) in ATE. So their α takes negative values, and under KLLoss, BernoulliLoss or BoundedSquaredLoss with lo ≥ 0, α̂ is pinned at the lower edge wherever α is negative. Some learners return α̂ ≡ 0 everywhere, for example RieszBooster for ATE under KLLoss.
  • TSM has mean 1, so under BernoulliLoss the fit is pinned at 1.

The mean can lie inside the range while α leaves it somewhere, for example TSM under BoundedSquaredLoss(0, 3) when some propensities are below 1/3. fit can’t detect that, because it depends on α itself, so check that the range you pick holds the values you expect α to take.

Among the built-in estimands, only OutcomeRegNormSq with a binary outcome has α in \((0, 1)\): there α is \(P(Y = 1 \mid X)\). Otherwise BernoulliLoss is for custom estimands.

KLLoss needs good overlap. Where some covariate values have almost no treated rows, the empirical KL loss has no lower bound there. A flexible learner (SklearnBackend, RieszNet) can then push α̂ toward infinity at those points. Use SquaredLoss, or RieszBooster’s default XGBoost backend, when propensities get close to 0 or 1.

Pass the loss instance to any learner via loss=:

import numpy as np, pandas as pd
from rieszboost import RieszBooster
from rieszreg import ATE, TSM, KLLoss, BoundedSquaredLoss

rng = np.random.default_rng(0)
n = 500
x = rng.uniform(0, 1, n)
pi = 1 / (1 + np.exp(-(4 * x - 2)))
a = rng.binomial(1, pi)
df = pd.DataFrame({"a": a.astype(float), "x": x})

# Squared loss (default)
RieszBooster(estimand=ATE(), n_estimators=100).fit(df).predict(df)[:3]
array([1.57342625, 2.52326083, 3.59552908])
# KL loss for a density-ratio estimand
RieszBooster(estimand=TSM(level=1), loss=KLLoss(), n_estimators=100).fit(df).predict(df)[:3]
array([1.6228152 , 2.49242063, 4.05755093])
# Bounded squared with prior bounds
RieszBooster(estimand=ATE(),
             loss=BoundedSquaredLoss(lo=-5.0, hi=5.0),
             n_estimators=100).fit(df).predict(df)[:3]
array([1.57167941, 2.50903554, 3.74554245])
set.seed(0)
n  <- 500
x  <- runif(n)
pi <- 1 / (1 + exp(-(4 * x - 2)))
a  <- rbinom(n, 1, pi)
df <- data.frame(a = as.numeric(a), x = x)

# Squared loss (default)
RieszBooster$new(estimand = ATE(), n_estimators = 100L)$fit(df)$predict(df)[1:3]
[1]  1.169958 -1.336773  1.965481
# KL loss for a density-ratio estimand
RieszBooster$new(estimand = TSM(level = 1), loss = KLLoss(),
                 n_estimators = 100L)$fit(df)$predict(df)[1:3]
[1] 1.169545675 0.009841453 1.751067194
# Bounded squared with prior bounds
RieszBooster$new(estimand = ATE(),
                 loss = BoundedSquaredLoss(lo = -5.0, hi = 5.0),
                 n_estimators = 100L)$fit(df)$predict(df)[1:3]
[1]  1.173893 -1.336104  2.007336

Custom losses

NotePython-only

Custom losses are Python-only. From R, define the loss in Python via reticulate::py_run_string and pass the instance via loss=.

Define your own loss the same way the built-ins are defined: subclass Loss, override potential(alpha) (the math symbol \(h\)), potential_deriv(alpha) (\(h'\)), and (if the loss has a non-trivial domain) the link methods. The example below is a pseudo-Huber potential \(h(\alpha) = \sqrt{1 + \alpha^2} - 1\) with the identity link — strictly convex, bounded gradient, no domain restriction.

import numpy as np
from rieszreg import Loss, ATE
from rieszboost import RieszBooster

class PseudoHuberLoss(Loss):
    name = "pseudo-huber"

    def potential(self, alpha):
        return np.sqrt(1.0 + alpha ** 2) - 1.0

    def potential_deriv(self, alpha):
        return alpha / np.sqrt(1.0 + alpha ** 2)

booster = RieszBooster(
    estimand=ATE(),
    loss=PseudoHuberLoss(),
    n_estimators=200, learning_rate=0.05, max_depth=3,
).fit(df)
booster.predict(df)[:5]
array([ 1.57145417,  2.95149994,  3.59623194, -1.2756505 ,  1.20341575])

For a quick experiment without writing a class, instantiate Loss directly with the potential and link as constructor arguments:

loss = Loss(
    potential=lambda a: np.sqrt(1.0 + a ** 2) - 1.0,
    potential_deriv=lambda a: a / np.sqrt(1.0 + a ** 2),
    link="identity",
)

If you skip potential_deriv, the framework will try to derive it via jax.grad when potential is written using jax.numpy ops. If jax isn’t available or your potential uses stock numpy, it falls back to a central-difference numerical derivative. Subclassing with an analytic potential_deriv is fastest; jax autograd is the next-best option.

import jax.numpy as jnp

# potential_deriv resolved automatically by jax.grad
loss = Loss(potential=lambda a: jnp.sqrt(1.0 + a ** 2) - 1.0, link="identity")
loss.potential_deriv(np.array([0.5, 1.0, 2.0]))
array([0.4472136 , 0.70710677, 0.8944272 ], dtype=float32)

When the loss has a restricted α-domain — say you want \(\alpha > 0\) — set the link to constrain \(\hat\alpha\):

class KLLikeLoss(Loss):
    name = "kl-like"

    def potential(self, alpha):
        return alpha * np.log(alpha)

    def potential_deriv(self, alpha):
        return np.log(alpha) + 1.0

    def link_to_alpha(self, eta):
        return np.exp(np.clip(eta, -50.0, 50.0))

    def alpha_to_eta(self, alpha):
        return np.log(alpha)

Loss from a constructor or a one-off subclass is not save/load round-trippable — the user’s potential callable doesn’t survive the JSON metadata path. Pass the loss explicitly when reloading the estimator. The four built-ins do round-trip.

SquaredLoss and KLLoss are short reference implementations.

Backend support

NoteWhich backends accept which losses
  • Boosting (rieszboost): all four losses.
  • Kernel ridge (krrr): SquaredLoss only — non-quadratic losses need a Newton iteration on the kernel system.
  • Random forest, augmentation-style (forestriesz.AugForestRieszRegressor): all four losses. Each tree picks splits by the gain in the loss you chose, and each leaf stores that loss’s closed-form optimum.
  • Random forest, moment-style (forestriesz.ForestRieszRegressor): SquaredLoss only.
  • Neural network (riesznet): all four losses. Each loss’s per-row Bregman form is implemented in autograd-friendly torch ops; gradient parity vs the analytic loss is checked at 1e-8.

Pass an unsupported loss and the backend raises at fit time with a clear error.