Introduction
Reinforcement learning (RL) is increasingly deployed in high-stakes domains like healthcare and finance to optimize long-term outcomes. However, standard RL algorithms are often "fairness-unaware," maximizing rewards based on state variables that may be tainted by sensitive attributes like race or gender. PyCFRL is the first Python library dedicated to achieving counterfactual fairness in offline RL by stripping away these causal influences through sequential data preprocessing.
Library Architecture and Methods
The core of PyCFRL is a sequential data preprocessing algorithm based on recent theoretical work by Wang et al. (2025). Instead of redesigning RL agents, the library transforms the data the agent "sees," making it a plug-and-play solution for existing RL workflows.
As shown in the architecture overview, the library is organized into five functional modules:
- reader: Handles trajectory data ingestion and export.
- preprocessor: Implements the counterfactual state construction algorithm.
- agents: Provides implementations like Fitted Q-Iteration (FQI) that can internally handle preprocessing.
- environment: Creates synthetic or simulated environments (digital twins) to estimate transition dynamics from real-world data.
- evaluation: Measures policy value and counterfactual unfairness levels.
The Preprocessing Pipeline
The library constructs counterfactual states by estimating what an individual's variables would have been if their sensitive attribute had differed, while holding historical exogenous factors constant. These fair state vectors are then concatenated with observed states to train ethically robust policies.
Experimental Evaluation
The authors demonstrated PyCFRL using a synthetic environment ($N=500$, $T=10$) to simulate real-world offline trajectory data where underlying transition rules are unknown.
Key Results
PyCFRL ("Ours") was compared against three baselines: Random (equal probability actions), Full (uses all data including sensitive attributes), and Unaware (ignores sensitive attributes without preprocessing).
As illustrated in the results table, PyCFRL reduced counterfactual unfairness by approximately 90% compared to the "Full" and "Unaware" models. While there is a "fairness tax"—a reduction in cumulative reward (Value) from 8.606 to 7.358—this trade-off is a critical metric for stakeholders seeking ethically robust decision-making.
Key Insights and Future Work
- The "Unaware" Trap: Simply removing a sensitive attribute is insufficient; bias remains "baked into" other state variables. PyCFRL's causal framework addresses this directly.
- Preprocessing as a Bridge: By focusing on data transformation, PyCFRL allows engineers to use standard RL algorithms (like PPO or DQN) while still meeting fairness requirements.
Future versions of the library aim to support variable episode lengths, integrate with the gymnasium ecosystem, and handle more complex, non-additive causal relationships.
Summary updated