
Custom LightGBM objective: MSE with a squared covariance penalty
Source:R/objectives.R
make_objective_mse_cov.RdBuild a custom LightGBM objective callback that minimizes a
standard squared-error loss plus a soft penalty on the covariance between
the per-sample residual r = y_pred - y_true and the (centered) labels
y_true. The penalty pushes the model toward "vertical equity" by
discouraging residuals that systematically scale with y.
This is an R port of the LGBCovPenalty objective from an active
collaboration (see the
soft-vertical-equity-constrained-mass-appraissal
repository). It is intended to be used when the model is trained in
log-space (so the "diff" residual is equivalent to a log-ratio).
Penalty (using mean-centered labels y_centered = y_true - mean(y_true)):
$$\mathrm{cov} = \frac{1}{n} \sum_{i=1}^{n} r_i \tilde{y}_i$$
$$\mathrm{penalty} = \frac{\rho}{2} \, n \, \mathrm{cov}^2$$
where \(\tilde{y}\) is the vector of mean-centered labels.
A diagonal Hessian approximation is used (matches the reference Python
implementation).
Value
A function with signature function(preds, dtrain) suitable for
passing as the obj argument of lightgbm::lgb.train.