Skip to main content

PyCFRL: A Python Library for Counterfactually Fair Reinforcement Learning

PyPI version PyPI total downloads PyPI monthly downloads PyPI weekly downloads License

Documentation: PyCFRL Documentation.

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

Note: This library was originally named CFRL, but we later changed the name to PyCFRL.

Installation

$ pip install pycfrl

A Brief Introduction to Counterfactual Fairness

Counterfactual fairness is one variation of fairness metrics. However, unlike well-known group fairness metrics such as demographic parity and equal opportunity, counterfactual fairness defines fairness based on causal reasoning and enforces it at the individual level. In short, a reinforcement learning policy is counterfactually fair if, at every time step, it would assign the same decisions with the same probabilities for an individual had the individual belong to a different subgroup defined by some sensitive attribute (such as race and gender). At its core, counterfactual fairness views the observed states and rewards as biased proxies of the (unobserved) true underlying states and rewards, where the bias is often a result of the observed sensitive attribute. Thus, to ensure counterfactual fairness, we want the policy to be based on the true underlying states and rewards rather than their biased proxies.

We refer interested readers to Kusner et al. (2017) for a detailed discussion of counterfactual fairness in the single-stage predictions setting, and to Wang et al. (2025) for a detailed discussion of counterfactual fairness in the reinforcement learning setting.

Key Functionalities

PyCFRL is designed with two main functionalities:

  1. Provide algorithms that enforce counterfactual fairness for reinforcement learning policies. The current version of PyCFRL implements the sequential data preprocessing algorithm proposed by Wang et al. (2025) for offline reinforcement learning. The algorithm takes in an offline RL trajectory and outputs a preprocessed, bias-free trajectory. The preprocessed trajectory can then be passed to any existing offline reinforcement learning algorithms for training, and the learned policy should be approximately counterfactually fair.

  2. Provide a platform to evaluate RL policies based on counterfactual fairness. After passing in their policy and a trajectory dataset from the target environment, users can assess how well their policies perform in the target environment in terms of the discounted cumulative reward and counterfactual fairness metric.

High-level Design

Module Functionalities
reader Implements functions that read tabular trajectory data from either a .csv file or a pandas.Dataframe into a format required by PyCFRL. Also implements functions that export trajectory data to either a .csv file or a pandas.Dataframe.
preprocessor Implements the data preprocessing algorithm introduced in Wang et al. (2025).
agents Implements a fitted Q-iteration (FQI) algorithm, which learns RL policies and makes decisions based on the learned policy. Users can also pass a preprocessor to the FQI; in this case, the FQI will be able to take in unpreprocessed trajectories and directly output counterfactually fair policies.
environment Implements a synthetic environment that produces synthetic data as well as a simulated environment that simulates the transition dynamics of the environment underlying some real-world RL trajectory data. Also implements functions for sampling trajectories from the synthetic and simulated environments.
evaluation Implements functions that evaluate the value and counterfactual fairness of a trained policy. Depending on the user's needs, the evaluation can be done either in a synthetic environment or in a simulated environment.

Workflow Chart

A general PyCFRL workflow is as follows: First, simulate a trajectory using environment or read in a trajectory using reader. Then, train a preprocessor using preprocessor to remove the bias in the trajectory data. After that, pass the preprocessed trajectory into the FQI algorithm in agents to learn a counterfactually fair policy. Finally, use functions in evaluation to evaluate the value and counterfactual fairness of the trained policy. See ... for more detailed workflow examples.

Examples

We provide a few short code examples showcasing some key use cases of PyCFRL.

1. Trajectory Preprocessing Only: See here.

2. Preprocessing + Policy Learning: See here.

3. Assessing Preprocessors using Synthetic Data: See here.

4. Assessing Policies using Real Data: See here.

Testing

We provide unit tests as well as integration tests for the main functions of the PyCFRL. The tests can be found in the tests folder of the PyCFRL repository. To run the test, first install pytest:

$ pip install pytest

Then run

$ python -m pytest

Note that this library relies heavily on machine learning and deep learning methods such as neural networks. The outputs from these methods are not exactly predictable because of the stochasticity involved. Therefore, most of the tests focus on testing the shape and data type of the outputs rather than the value of the outputs.

Note: Currently, the 'lm' model type is not supported. Thus, pytest will skip the test cases that involve the 'lm' model type.

Metadata

Release files for pycfrl 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycfrl 0.3.2
File Size Uploaded Provenance Actions
pycfrl-0.3.2.tar.gz 2.6 MB Without provenance Details for pycfrl-0.3.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pycfrl 0.3.2
File Interpreter ABI Platform Actions
pycfrl-0.3.2-py3-none-any.whl Python 3 none any Details for pycfrl-0.3.2-py3-none-any.whl

Total release size: 2.7 MB

Release files / pycfrl-0.3.2.tar.gz

Download URL pycfrl-0.3.2.tar.gz
Size 2.6 MB
Tags Source
SHA-256 checksum
How to use checksums
7204e15e3c4e4dbe2d3cdeab06ead86524c8db2e6b3a763238ec05354e480a35
BLAKE2b-256 checksum
How to use checksums
eba33fa8d69e7b07b015972e54b946bf66b5e873f6115809a05fe29e1362b74c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.8

Release files / pycfrl-0.3.2-py3-none-any.whl

Download URL pycfrl-0.3.2-py3-none-any.whl
Size 52.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6178e07836230d0a72c97b8f177dad722ae67354f98b91efceb4f3fe9c2c68fe
BLAKE2b-256 checksum
How to use checksums
1a67d94c41245c75d724d4f3f8b864b7f7f783db0d9275c14e86d7c69cf92a01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.8

Security for pycfrl 0.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page