dro: Distributionally Robust Optimization for Machine Learning

repository·main·Indexed 41 days ago

https://github.com/namkoong-lab/dro

A Python package for implementing Distributionally Robust Optimization (DRO) methods. It supports various distance metrics and base models, including linear models solved via cvxpy, neural networks via Pytorch, and tree-based ensembles via xgboost and lightgbm. Features include f-divergence DRO, Wasserstein DRO, MMD-DRO, and Group DRO for supervised learning tasks.

Tokens
51.8K
Snippets
136
Records
212
Agent score
50%

What's inside dro

  1. Overview of the dro package

    main

    dro is a Python package designed for Distributionally Robust Optimization (DRO) in machine learning. It implements DRO methods for supervised learning tasks using linear losses (such as SVM, logistic regression, and linear regression).

    Key features include:

    • Optimization Engine: Built on top of the cvxpy convex optimization solver.
    • Distance Metrics: Supports various distance metrics $d(\cdot, \cdot)$ to define ambiguity sets.
    • Model Support: Compatible with linear models, neural networks, and tree-based ensembles.
    • Data Generation: Includes integrated synthetic data generation mechanisms based on recent research.

    The core optimization problem solved is: $$\min_{\theta} \max_{P: P \in U} E_{(X,Y) \sim P}[\ell(\theta;(X, Y))]$$ where $U$ is the ambiguity set, typically defined as $U = {P: d(P, \hat P_n) \leq \epsilon}$ using the empirical distribution $\hat P_n$ and a hyperparameter $\epsilon$.

  2. Explore Linear DRO Methods

    main

    The dro package provides a variety of Linear Distributionally Robust Optimization (DRO) methods. These are categorized into typical linear methods and advanced linear methods. Use these methods to build models that are robust against distribution shifts in machine learning tasks.

    Typical Linear DRO Methods

    • base_linear: Base linear DRO implementations.
    • chi2dro_linear: Chi-square DRO.
    • cvardro_linear: Conditional Value-at-Risk (CVaR) DRO.
    • groupdro_linear: Group DRO.
    • kldro_linear: Kullback-Leibler (KL) divergence DRO.
    • tvdro: Total Variation (TV) DRO.
    • mmddro: Maximum Mean Discrepancy (MMD) DRO.
    • wdro_linear: Wasserstein DRO.

    Advanced Linear DRO Methods

    • conditionaldro: Conditional DRO.
    • hrdro: Hierarchical DRO.
    • marginaldro: Marginal DRO.
    • motdro: Multi-Objective Transport DRO.
    • or_wdro: Optimal Transport Wasserstein DRO.
    • sinkhorn_dro: Sinkhorn-based DRO.
    • bayesian_dro: Bayesian DRO.
  3. Use the Robust Satisficing version of WDRO

    main

    The Robust Satisficing version of WDRO solves a constrained optimization problem where the goal is to maximize the ambiguity size $\epsilon$ subject to a performance constraint.

    For regression and classification, this is equivalent to maximizing the norm of the parameters subject to an empirical performance constraint: $$ \max {|\theta|{\Sigma^{-1/2},p},\quad \text{s.t.}~ E{(X,Y) \sim \widehat P}[\ell_{tr}(\theta;(X, Y))] \leq \tau}. $$

    In this mode, $\epsilon$ is an optimization goal rather than a fixed hyperparameter. You must provide $\Sigma$, $p$, $\kappa$, and $\tau$.

  4. Understand the `dro` package pipeline

    main

    The dro package is designed around a consistent three-stage pipeline for Distributionally Robust Optimization (DRO):

    1. Data: Generate or load data using the Data Module.
    2. Model: Fit a model using the Model Module (Linear, Kernel, Neural Network, or Tree-based).
    3. Evaluation / Diagnostics: Assess model performance and inspect worst-case scenarios.

    DRO optimizes the worst-case loss over an ambiguity set $\mathcal{P}$ defined by a distance $d$ from the empirical distribution $\hat{P}$ and a radius $\epsilon$.

  5. Define groups using `group_idx`

    main

    To use Group DRO, you must specify which column in your feature matrix X contains the group membership information using the group_idx parameter.

    • Format: The index must be a zero-based column index.
    • Values: The column must contain finite numeric categories.
    • Flexibility: Categories do not need to be consecutive (e.g., values 0, 2, and 10 will define three distinct groups).
    • Updating: You can change the group feature before refitting a model using model.update({"group_idx": new_index}).
  6. Understand Bayesian (Parametric) DRO

    main

    Bayesian (Parametric) Distributionally Robust Optimization (DRO) addresses stochastic optimization by incorporating uncertainty about the underlying distribution parameters. Instead of optimizing against a single fixed distribution, it optimizes against a posterior distribution $\zeta_N$ of the parameters $\zeta$ given the observed data.

    In practice, dro approximates the outer expectation over the posterior distribution using finite samples of $\zeta$. For each sampled parameter $\zeta$, Monte Carlo sampling is used to handle the continuous integral of the parametric distribution $P_{\zeta}$.

    Supported parametric families include:

    • Exponential: Uses a rate parameter $\zeta$ with a default $\Gamma(1, 1)$ prior.
    • Gaussian: Uses a Normal-Inverse-Wishart (NIW) prior on the mean $\mu$ and covariance $\Sigma$, which is conjugate to the multivariate Gaussian likelihood, allowing for closed-form posterior updates.
  7. Understand f-divergence DRO models and hyperparameters

    main

    The dro package implements several $f$-divergence Distributionally Robust Optimization (DRO) models. These models use a distance metric $d(P, Q)$ to define an ambiguity set around a reference distribution.

    Supported Models and Hyperparameters

    ModelDivergence TypeHyperparameterHyperparameter Range
    KL-DROKullback-Leiblereps$\epsilon \geq 0$ (0 reduces to ERM)
    $\chi^2$-DROChi-squareeps$\epsilon \geq 0$ (0 reduces to ERM)
    TV-DROTotal Variationeps$\epsilon \in [0, 1]$
    CVaR-DROConditional Value-at-Riskalpha$\alpha \in (0, 1]$ (0 reduces to ERM)

    Key Concepts

    • Ambiguity Size (eps): Controls the size of the uncertainty set. When eps = 0, the model reduces to Empirical Risk Minimization (ERM).
    • Worst-case Illustration: In $\chi^2$-DRO, the model finds a perturbed probability distribution by solving an optimization problem that maximizes loss subject to a constraint on the distance from the empirical distribution.
  8. Understand Wasserstein DRO Hyperparameters

    main

    When using Wasserstein Distributionally Robust Optimization (WDRO) in dro, you can control the robustness and perturbation characteristics using the following hyperparameters:

    • Σ: The feature importance perturbation matrix (dimension: dimX x dimX).
    • p: Norm parameter for controlling the perturbation moment of the features $X$.
    • κ: Robustness parameter for the perturbation of the targets $Y$. For classification (svm or logistic), a finite $\kappa$ allows label flips at a cost of $\kappa$. Setting $\kappa = \infty$ forbids label changes.
    • ε: The ambiguity size of the Wasserstein ball.
    • τ: (Used in the Robust Satisficing Version) A threshold where $\tau \geq 1$ is typically set as a multiple of the best empirical performance $E_{(X, Y)\sim \hat P_n}[\ell(\theta_{ERM};(X, Y))]$.
  9. How Group DRO for Linear Models works

    main

    Group Distributionally Robust Optimization (Group DRO) is designed for scenarios where training data is divided into known groups and you want to prevent poor performance in any single subgroup.

    Instead of minimizing the average loss across all samples, Group DRO minimizes the largest empirical mean loss among all observed groups. It achieves this by solving a convex optimization problem where an epigraph variable $t$ is minimized subject to the constraint that the average loss for every group $g$ is less than or equal to $t$.

    Key characteristics:

    • Group Weighting: Every category present in the training data contributes one group-average constraint. Small groups have the same influence on the objective as large groups and are not down-weighted by their sample count.
    • Feature Inclusion: The group column (specified by group_idx) remains in the design matrix during fitting and prediction. This means the model may use the group identity as a predictive feature, which is an important consideration if the group represents a sensitive attribute.
  10. How MMD-DRO works and its kernel options

    main

    MMD-DRO (Maximum Mean Discrepancy Distributionally Robust Optimization) defines an ambiguity set $\mathcal{P}(d, \eta) = {Q: d(Q, \hat P) \leq \eta}$ based on a kernel distance $d(P, Q)$. The choice of kernel determines which differences between distributions are emphasized.

    Supported kernels include:

    • rbf (default): Gaussian RBF kernel $\exp(-\gamma|x-y|_2^2)$. If kernel_gamma is None, $\gamma$ is the reciprocal of the median nonzero squared pairwise distance.
    • laplacian: $\exp(-\gamma|x-y|_1)$. If kernel_gamma is None, $\gamma$ is the reciprocal of the median nonzero $\ell_1$ distance. Useful for less smooth similarity.
    • polynomial (alias poly): $(\gamma x^\top y + c_0)^d$. Defaults are degree $d=2$, offset $c_0=1$, and $\gamma=1/p$ (where $p$ is the dimension of the joint sample).
    • linear: $x^\top y$. Captures differences in first moments only.

    Note: It is highly recommended to standardize continuous features and targets before fitting, especially when using polynomial kernels, because the kernel is applied to the joint samples $(X, Y)$.

    from dro.linear_model.mmd_dro import MMD_DRO
    
    # Default RBF kernel usage
    model = MMD_DRO(input_dim=X.shape[1], model_type="svm")
    model.update({"eta": 0.1, "sampling_method": "hull"})
    params = model.fit(X, y)
  11. Supported Neural Network Architectures in NN-DRO

    main

    The dro.neural_model implementations support several built-in backbone architectures. You can specify these using the model_type parameter.

    Supported architectures:

    • linear: Linear Model
    • mlp: Multi-Layer Perceptron
    • alexnet: AlexNet
    • resnet: ResNet-18

    Users can also use their own custom model architectures by calling the update() method on an existing model instance.