PyCFRL: A Python library for counterfactually fair offline reinforcement learning via sequential data preprocessing

Jianhan Zhang, Jitao Wang, Chengchun Shi, John D. Piette, Donglin Zeng, Zhenke WuarXiv 2025 · DOI

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.

The modular architecture of PyCFRL includes components for data reading, preprocessing, agent training, environment simulation, and evaluation.

As shown in the architecture overview, the library is organized into five functional modules:

  1. reader: Handles trajectory data ingestion and export.
  2. preprocessor: Implements the counterfactual state construction algorithm.
  3. agents: Provides implementations like Fitted Q-Iteration (FQI) that can internally handle preprocessing.
  4. environment: Creates synthetic or simulated environments (digital twins) to estimate transition dynamics from real-world data.
  5. 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).

Quantitative comparison showing that PyCFRL significantly reduces counterfactual unfairness compared to Full and Unaware models, with a manageable trade-off in cumulative reward.

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

BibTeX
@misc{zhang2025pycfrl,
  author = {Jianhan Zhang and Jitao Wang and Chengchun Shi and John D. Piette and Donglin Zeng and Zhenke Wu},
  title = {{PyCFRL: A Python library for counterfactually fair offline reinforcement learning via sequential data preprocessing}},
  year = {2025},
  eprint = {2510.06935},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2510.06935},
}
Figure