Linear DRO Base Class¶
- class dro.src.linear_model.base.BaseLinearDRO(input_dim, model_type='svm', fit_intercept=True, solver='MOSEK', kernel='linear')¶
Bases:
object
Base class for Linear Distributionally Robust Optimization (DRO) models.
This class supports both regression and binary classification tasks. To ensure convex optimization, this class only supports linear models or kernelized models, e.g., SVM, Linear Regression, and Logistic Regression.
- Parameters:
input_dim (int) – Dimensionality of the input features.
model_type (str) – Model type indicator (‘svm’ for SVM, ‘logistic’ for Logistic Regression, ‘ols’ for Linear Regression for OLS, ‘lad’ for Linear Regression for LAD), default = ‘svm’.
fit_intercept (bool) – Whether to calculate the intercept for this model. If set to False, no intercept will be used in calculations (i.e. data is expected to be centered), default = True.
solver (str) – Optimization solver to solve the problem, default = ‘MOSEK’.
kernel (str) – the kernel type to be used in the optimization model, default = ‘linear’
- update_kernel(config)¶
Update model class (kernel parameters) based on configuration
- Parameters:
Configuration dictionary with keys:
'metric'
:
- Raises:
- update(config)¶
Update model parameters based on configuration.
- Parameters:
config (dict) – The model configuration
- fit(X, y)¶
Fit model to data by solving an optimization problem.
- Parameters:
X (
numpy.ndarray
) – input covariatesy (
numpy.ndarray
) – input labels
- load(config)¶
Load model parameters from a configuration dictionary.
- Parameters:
config (dict) – The model configuration to load
- predict(X)¶
Predict output based on input data and model parameters.
- Parameters:
X (
numpy.ndarray
) – Input covariates- Returns:
the predicted labels
- Return type:
- score(X, y, weights=None)¶
Compute accuracy and F1 score for classification tasks, or MSE for regression.
- Parameters:
X (
numpy.ndarray
) – Input feature matrix of shape (n_samples, n_features)y (
numpy.ndarray
) – Target labels/values of shape (n_samples,)weights (Optional[
numpy.ndarray
], defaults to None) – Sample weight vector of shape (n_samples,), None indicates equal weights
- Returns:
For classification: Tuple containing (accuracy, F1-score)
For regression: Mean Squared Error (MSE)
- Return type:
- Raises:
ValueError – If task type is not properly configured
- Example:
>>> Classification model: Returns (0.95, 0.93) >>> Regression model: Returns 3.1415
- evaluate(X, y, fast=True)¶
Evaluate model performance with bias-corrected mean squared error (MSE).
Specifically designed for OLS models to compute an unbiased performance estimate by adjusting for the covariance structure of the features. This implementation accelerates evaluation by avoiding full retraining.
- Parameters:
X (numpy.ndarray) – Feature matrix of shape (n_samples, n_features). Must match the model’s input_dim (n_features).
y (numpy.ndarray) – Target values of shape (n_samples,).
fast (bool)
- Returns:
Bias-corrected MSE for OLS models. Returns raw MSE for other model types.
- Return type:
- Raises:
If X and y have inconsistent sample sizes
If X feature dimension ≠ input_dim
LinAlgError – If feature covariance matrix is singular (non-invertible)
- Example:
>>> model = Chi2DRO(input_dim=5, model_type='ols') >>> X_test = np.random.randn(50, 5) >>> y_test = np.random.randn(50) >>> score = model.evaluate(X_test, y_test) >>> print(f"Corrected MSE: {score:.4f}")
Note
Currently, bias correction is only implemented for
model_type='ols'
; other model types return the raw MSE.Covariance matrix computation uses pseudo-inverse to handle high-dimensional data (
n_features > n_samples
).