Base Class¶
- class dro.linear_model.base.BaseLinearDRO(input_dim, model_type='svm', fit_intercept=True, solver='CLARABEL', kernel='linear')¶
Bases:
objectBase 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.
- Variables:
robust_obj – Robust empirical optimization objective from the most recent successful fit, or
Nonebefore it is available.- 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 = ‘CLARABEL’.
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: The metric to use when calculating kernel between instances in a feature array.kernel_gamma: Parameter gamma of the pairwise kernel specified by metric.n_components: Integer number of Nyström components, orNoneto use the full kernel representation on the next fit.
- Raises:
If
kernel_gammais not in scale, auto;If
metricis
TypeError – If
n_componentsis neither an integer norNone.
- 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)¶
Estimate performance with bias-corrected OLS or logistic loss.
Supported only for linear-kernel OLS and logistic models. SVM, LAD, and nonlinear-kernel models are not supported by this method.
- 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 or binary cross entropy for logistic.
- Return type:
- Raises:
NotImplementedError – For SVM, LAD, or nonlinear-kernel models.
If X and y have inconsistent sample sizes
If X feature dimension ≠ input_dim
LinAlgError – If the logistic information matrix is singular.
- 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
Use
score()for standard predictive metrics for SVM and LAD.OLS uses a pseudo-inverse; logistic evaluation requires an invertible information matrix.