Skip to content

Cost-weighted service loss regressor

This section documents the cost-weighted service loss (CWSL) regressor used in model selection workflows.

The CWSL regressor supports learning and ranking models based on asymmetric service risk preferences.

eb_evaluation.model_selection.cwsl_regressor

scikit-learn-style estimator wrapper for cost-aware model selection.

This module defines CWSLRegressor, a lightweight wrapper around ElectricBarometer that exposes a familiar fit / predict / score API.

The estimator selects among a set of candidate models using Cost-Weighted Service Loss (CWSL) as the selection criterion. Candidate models are trained using their native objectives (e.g., squared error), but selection is performed using asymmetric operational costs.

The primary cost preference can be summarized by the cost ratio:

\[ R = \frac{c_u}{c_o} \]

where \(c_u\) is the underbuild (shortfall) cost per unit and \(c_o\) is the overbuild (excess) cost per unit.

CWSLRegressor

scikit-learn-style estimator that selects among candidate models using CWSL.

This class wraps ElectricBarometer and exposes:

  • fit(X, y): perform cost-aware model selection
  • predict(X): predict using the selected best estimator
  • score(X, y): return a sklearn-style score based on negative CWSL

Parameters:

Name Type Description Default
models dict[str, Any]

Mapping of candidate model name to an unfitted estimator implementing:

  • fit(X, y)
  • predict(X)

Models may be scikit-learn regressors, pipelines, or EB adapters.

required
cu float

Underbuild (shortfall) cost per unit. Must be strictly positive.

2.0
co float

Overbuild (excess) cost per unit. Must be strictly positive.

1.0
tau float

Tolerance parameter forwarded to ElectricBarometer for optional diagnostics (e.g., HR@tau).

2.0
training_mode selection_only

Training behavior; currently ElectricBarometer supports selection-only mode.

"selection_only"
refit_on_full bool

Refit behavior after selection.

  • In holdout mode: if True, refit the winning model on train + validation.
  • In CV mode: the winning model is refit on the full dataset.
True
selection_mode (holdout, cv)

How selection is performed:

  • "holdout": split internally using validation_fraction.
  • "cv": perform K-fold selection inside ElectricBarometer.
"holdout"
cv int

Number of folds when selection_mode="cv".

3
validation_fraction float

Fraction of samples used for validation when selection_mode="holdout". Must be in (0, 1).

0.2
random_state int | None

Seed used for internal shuffling and CV splitting.

None

Attributes:

Name Type Description
selector_ ElectricBarometer | None

Underlying selector instance used in the most recent fit.

best_name_ str | None

Name of the winning model.

best_estimator_ Any | None

Fitted winning estimator.

results_ Any

Comparison table produced by ElectricBarometer (typically a pandas DataFrame).

validation_cwsl_ float | None

CWSL score of the winning model on validation (holdout) or mean CV.

validation_rmse_ float | None

RMSE score of the winning model on validation or mean CV.

validation_wmape_ float | None

wMAPE score of the winning model on validation or mean CV.

n_features_in_ int | None

Number of features observed during fit.

Notes

score returns negative CWSL to align with sklearn conventions (higher is better).

r_ property

Cost ratio.

Returns:

Type Description
float

The ratio \(R = c_u / c_o\).

fit(X, y, sample_weight=None)

Fit CWSLRegressor on (X, y) by delegating selection to ElectricBarometer.

Parameters:

Name Type Description Default
X array-like of shape (n_samples, n_features)

Feature matrix.

required
y array-like of shape (n_samples,)

Target vector.

required
sample_weight numpy.ndarray of shape (n_samples,)

Optional per-sample weights. In CV mode these are passed through to ElectricBarometer so validation folds are cost-weighted. In holdout mode, they are passed through to ElectricBarometer (behavior depends on EB).

None

Returns:

Type Description
CWSLRegressor

Fitted instance.

Raises:

Type Description
ValueError

If X/y shapes are incompatible or sample_weight length mismatches.

predict(X)

Generate predictions from the selected best estimator.

Parameters:

Name Type Description Default
X array-like of shape (n_samples, n_features)
required

Returns:

Type Description
numpy.ndarray of shape (n_samples,)

Predicted values.

Raises:

Type Description
RuntimeError

If called before fit.

score(X, y, sample_weight=None)

Compute a sklearn-style score using negative CWSL.

Parameters:

Name Type Description Default
X array-like of shape (n_samples, n_features)

Feature matrix.

required
y array-like of shape (n_samples,)

Target vector.

required
sample_weight numpy.ndarray of shape (n_samples,)

Optional per-sample weights passed to CWSL.

None

Returns:

Type Description
float

Negative CWSL on the provided data (higher is better).

get_params(deep=True)

Minimal sklearn-compatible get_params implementation.

Parameters:

Name Type Description Default
deep bool

Included for sklearn compatibility. Nested parameter extraction is not performed in this lightweight implementation.

True

Returns:

Type Description
dict[str, Any]

Parameter mapping.

set_params(**params)

Minimal sklearn-compatible set_params implementation.

Parameters:

Name Type Description Default
**params

Parameters to set on this instance.

{}

Returns:

Type Description
CWSLRegressor

Updated instance.

Raises:

Type Description
ValueError

If an invalid parameter name is provided.