CounterfactualExplanations

A package for Counterfactual Explanations and Algorithmic Recourse in Julia.

https://github.com/juliatrustworthyai/counterfactualexplanations.jl

Science Score: 77.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 16 DOI reference(s) in README
  • Academic publication links
    Links to: arxiv.org, zenodo.org
  • Committers with academic emails
    2 of 14 committers (14.3%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.2%) to scientific vocabulary

Keywords

counterfactual-explanations deep-learning explainable-ai julia machine-learning

Keywords from Contributors

predictive-uncertainty parallel conformal-prediction numerical probability-distribution pdes simulator programming-language adapters physics
Last synced: 6 months ago · JSON representation ·

Repository

A package for Counterfactual Explanations and Algorithmic Recourse in Julia.

Basic Info
Statistics
  • Stars: 122
  • Watchers: 6
  • Forks: 6
  • Open Issues: 70
  • Releases: 0
Topics
counterfactual-explanations deep-learning explainable-ai julia machine-learning
Created about 4 years ago · Last pushed 7 months ago
Metadata Files
Readme Changelog License Code of conduct Citation

README.md

CounterfactualExplanations

Counterfactual Explanations and Algorithmic Recourse in Julia.

Stable Dev Build Status Coverage Code Style: Blue License Package Downloads Aqua QA

CounterfactualExplanations.jl is a package for generating Counterfactual Explanations (CE) and Algorithmic Recourse (AR) for black-box algorithms. Both CE and AR are related tools for explainable artificial intelligence (XAI). While the package is written purely in Julia, it can be used to explain machine learning algorithms developed and trained in other popular programming languages like Python and R. See below for a short introduction and other resources or dive straight into the docs.

There is also a corresponding paper, Explaining Black-Box Models through Counterfactuals, which has been published in JuliaCon Proceedings. Please consider citing the paper, if you use this package in your work:

DOI DOI

@article{Altmeyer2023,
  doi = {10.21105/jcon.00130},
  url = {https://doi.org/10.21105/jcon.00130},
  year = {2023},
  publisher = {The Open Journal},
  volume = {1},
  number = {1},
  pages = {130},
  author = {Patrick Altmeyer and Arie van Deursen and Cynthia C. S. Liem},
  title = {Explaining Black-Box Models through Counterfactuals},
  journal = {Proceedings of the JuliaCon Conferences}
}

🚩 Installation

You can install the stable release from Julia’s General Registry as follows:

julia using Pkg Pkg.add("CounterfactualExplanations")

CounterfactualExplanations.jl is under active development. To install the development version of the package you can run the following command:

julia using Pkg Pkg.add(url="https://github.com/juliatrustworthyai/CounterfactualExplanations.jl")

🤔 Background and Motivation

Machine learning models like Deep Neural Networks have become so complex, opaque and underspecified in the data that they are generally considered Black Boxes. Nonetheless, such models often play a key role in data-driven decision-making systems. This creates the following problem: human operators in charge of such systems have to rely on them blindly, while those individuals subject to them generally have no way of challenging an undesirable outcome:

“You cannot appeal to (algorithms). They do not listen. Nor do they bend.”

— Cathy O’Neil in Weapons of Math Destruction, 2016

🔮 Enter: Counterfactual Explanations

Counterfactual Explanations can help human stakeholders make sense of the systems they develop, use or endure: they explain how inputs into a system need to change for it to produce different decisions. Explainability benefits internal as well as external quality assurance.

Counterfactual Explanations have a few properties that are desirable in the context of Explainable Artificial Intelligence (XAI). These include:

  • Full fidelity to the black-box model, since no proxy is involved.
  • No need for (reasonably) interpretable features as opposed to LIME and SHAP.
  • Clear link to Algorithmic Recourse and Causal Inference.
  • Less susceptible to adversarial attacks than LIME and SHAP.

Simple Usage Example

To get started, try out this simple usage example with synthetic data:

``` julia using CounterfactualExplanations using CounterfactualExplanations.Models using Plots using TaijaData using TaijaPlotting

Data and Model:

data = loadlinearlyseparable() counterfactualdata = CounterfactualData(data...) M = fitmodel(counterfactual_data, :Linear)

Choose factual:

target = 2 factual = 1 chosen = findall(predictlabel(M, counterfactualdata) .== factual) |> rand x = selectfactual(counterfactualdata, chosen)

Generate counterfactuals

generator = WachterGenerator() ce = generatecounterfactual( x, # factual target, # target counterfactualdata, # data M, # model generator # counterfactual generator ) plot(ce) ```

[ Info: No target label supplied, using first.

Example: Give Me Some Credit

Consider the following real-world scenario: a retail bank is using a black-box model trained on their clients’ credit history to decide whether they will provide credit to new applicants. To simulate this scenario, we have pre-trained a binary classifier on the publicly available Give Me Some Credit dataset that ships with this package (Kaggle 2011).

The figure below shows counterfactuals for 10 randomly chosen individuals that would have been denied credit initially.

Example: MNIST

The figure below shows a counterfactual generated for an image classifier trained on MNIST: in particular, it demonstrates which pixels need to change in order for the classifier to predict 3 instead of 8.

Since v0.1.9 counterfactual generators are fully composable. Here we have composed a generator that combines ideas from Wachter, Mittelstadt, and Russell (2017) and Altmeyer et al. (2023):

``` julia

Compose generator:

using CounterfactualExplanations.Objectives: distancemad, distancefromtarget generator = GradientBasedGenerator() @chain generator begin @objective logitcrossentropy + 0.2distancemad + 0.1distancefromtarget @with_optimiser Adam(0.1)
end ```

🔍 Usage example

Generating counterfactuals will typically look like follows. Below we first fit a simple model to a synthetic dataset with linearly separable features and then draw a random sample:

``` julia

Data and Classifier:

counterfactualdata = CounterfactualData(loadlinearlyseparable()...) M = fitmodel(counterfactual_data, :Linear)

Select random sample:

target = 2 factual = 1 chosen = rand(findall(predictlabel(M, counterfactualdata) .== factual)) x = selectfactual(counterfactualdata, chosen) ```

To this end, we specify a counterfactual generator of our choice:

``` julia

Counterfactual search:

generator = DiCEGenerator(λ=[0.1,0.3]) ```

Here, we have chosen to use the CounterfactualExplanations.Generators.GradientBasedGenerator to move the individual from its factual label 1 to the target label 2.

With all of our ingredients specified, we finally generate counterfactuals using a simple API call:

julia conv = conv = CounterfactualExplanations.Convergence.GeneratorConditionsConvergence() ce = generate_counterfactual( x, target, counterfactual_data, M, generator; num_counterfactuals=3, convergence=conv, )

The plot below shows the resulting counterfactual path:

[ Info: No target label supplied, using first.

☑️ Implemented Counterfactual Generators

Currently, the following counterfactual generators are implemented:

  • ClaPROAR (Altmeyer et al. 2023)
  • CLUE (Antorán et al. 2020)
  • DiCE (Mothilal, Sharma, and Tan 2020)
  • ECCCo (Altmeyer et al. 2024)
  • FeatureTweak (Tolomei et al. 2017)
  • Generic
  • GravitationalGenerator (Altmeyer et al. 2023)
  • Greedy (Schut et al. 2021)
  • MINT (Karimi et al. 2020) (causal CE)
  • PROBE (Pawelczyk et al. 2023)
  • REVISE (Joshi et al. 2019)
  • T-CREx (Bewley et al. 2024) (global CE)
  • Wachter (Wachter, Mittelstadt, and Russell 2017)

🎯 Goals and limitations

The goal of this library is to contribute to efforts towards trustworthy machine learning in Julia. The Julia language has an edge when it comes to trustworthiness: it is very transparent. Packages like this one are generally written in pure Julia, which makes it easy for users and developers to understand and contribute to open-source code. Eventually, this project aims to offer a one-stop-shop of counterfactual explanations.

Our ambition is to enhance the package through the following features:

  1. Support for all supervised machine learning models trained in MLJ.jl.
  2. Support for regression models.

🛠 Contribute

Contributions of any kind are very much welcome! Take a look at the issue to see what things we are currently working on. If you have an idea for a new feature or want to report a bug, please open a new issue.

Development

If your looking to contribute code, it may be helpful to check out the Explanation section of the docs.

Testing

Please always make sure to add tests for any new features or changes.

Documentation

If you add new features or change existing ones, please make sure to update the documentation accordingly. The documentation is written in Documenter.jl and is located in the docs/src folder.

Log Changes

As of version 1.1.1, we have tried to be more stringent about logging changes. Please make sure to add a note to the CHANGELOG.md file for any changes you make. It is sufficient to add a note under the Unreleased section.

General Pointers

There are also some general pointers for people looking to contribute to any of our Taija packages here.

Please follow the SciML ColPrac guide.

🎓 Citation

If you want to use this codebase, please consider citing the corresponding paper:

@article{Altmeyer2023,
  doi = {10.21105/jcon.00130},
  url = {https://doi.org/10.21105/jcon.00130},
  year = {2023},
  publisher = {The Open Journal},
  volume = {1},
  number = {1},
  pages = {130},
  author = {Patrick Altmeyer and Arie van Deursen and Cynthia C. s. Liem},
  title = {Explaining Black-Box Models through Counterfactuals},
  journal = {Proceedings of the JuliaCon Conferences}
}

📚 References

Altmeyer, Patrick, Giovan Angela, Aleksander Buszydlik, Karol Dobiczek, Arie van Deursen, and Cynthia CS Liem. 2023. “Endogenous Macrodynamics in Algorithmic Recourse.” In 2023 IEEE Conference on Secure and Trustworthy Machine Learning (SaTML), 418–31. IEEE.

Altmeyer, Patrick, Mojtaba Farmanbar, Arie van Deursen, and Cynthia CS Liem. 2024. “Faithful Model Explanations Through Energy-Constrained Conformal Counterfactuals.” In Proceedings of the AAAI Conference on Artificial Intelligence, 38:10829–37. 10.

Antorán, Javier, Umang Bhatt, Tameem Adel, Adrian Weller, and José Miguel Hernández-Lobato. 2020. “Getting a Clue: A Method for Explaining Uncertainty Estimates.” https://arxiv.org/abs/2006.06848.

Bewley, Tom, Salim I. Amoukou, Saumitra Mishra, Daniele Magazzeni, and Manuela Veloso. 2024. “Counterfactual Metarules for Local and Global Recourse.” https://arxiv.org/abs/2405.18875.

Joshi, Shalmali, Oluwasanmi Koyejo, Warut Vijitbenjaronk, Been Kim, and Joydeep Ghosh. 2019. “Towards Realistic Individual Recourse and Actionable Explanations in Black-Box Decision Making Systems.” https://arxiv.org/abs/1907.09615.

Kaggle. 2011. “Give Me Some Credit, Improve on the State of the Art in Credit Scoring by Predicting the Probability That Somebody Will Experience Financial Distress in the Next Two Years.” https://www.kaggle.com/c/GiveMeSomeCredit; Kaggle. https://www.kaggle.com/c/GiveMeSomeCredit.

Karimi, Amir-Hossein, Julius Von Kügelgen, Bernhard Schölkopf, and Isabel Valera. 2020. “Algorithmic Recourse Under Imperfect Causal Knowledge: A Probabilistic Approach.” https://arxiv.org/abs/2006.06831.

Mothilal, Ramaravind K, Amit Sharma, and Chenhao Tan. 2020. “Explaining Machine Learning Classifiers Through Diverse Counterfactual Explanations.” In Proceedings of the 2020 Conference on Fairness, Accountability, and Transparency, 607–17. https://doi.org/10.1145/3351095.3372850.

Pawelczyk, Martin, Teresa Datta, Johannes van-den-Heuvel, Gjergji Kasneci, and Himabindu Lakkaraju. 2023. “Probabilistically Robust Recourse: Navigating the Trade-Offs Between Costs and Robustness in Algorithmic Recourse.” https://arxiv.org/abs/2203.06768.

Schut, Lisa, Oscar Key, Rory Mc Grath, Luca Costabello, Bogdan Sacaleanu, Yarin Gal, et al. 2021. “Generating Interpretable Counterfactual Explanations By Implicit Minimisation of Epistemic and Aleatoric Uncertainties.” In International Conference on Artificial Intelligence and Statistics, 1756–64. PMLR.

Tolomei, Gabriele, Fabrizio Silvestri, Andrew Haines, and Mounia Lalmas. 2017. “Interpretable Predictions of Tree-Based Ensembles via Actionable Feature Tweaking.” In Proceedings of the 23rd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 465–74. https://doi.org/10.1145/3097983.3098039.

Wachter, Sandra, Brent Mittelstadt, and Chris Russell. 2017. “Counterfactual Explanations Without Opening the Black Box: Automated Decisions and the GDPR.” Harv. JL & Tech. 31: 841. https://doi.org/10.2139/ssrn.3063289.

Owner

  • Name: Taija
  • Login: JuliaTrustworthyAI
  • Kind: organization
  • Location: Netherlands

Home for repositories of the Taija (Trustworthy Artifical Intelligence in Julia) project.

Citation (CITATION.bib)

@article{Altmeyer2023,
  doi = {10.21105/jcon.00130},
  url = {https://doi.org/10.21105/jcon.00130},
  year = {2023},
  publisher = {The Open Journal},
  volume = {1},
  number = {1},
  pages = {130},
  author = {Patrick Altmeyer and Arie van Deursen and Cynthia C. s. Liem},
  title = {Explaining Black-Box Models through Counterfactuals},
  journal = {Proceedings of the JuliaCon Conferences}
}

GitHub Events

Total
  • Create event: 26
  • Commit comment event: 18
  • Release event: 8
  • Issues event: 20
  • Watch event: 2
  • Delete event: 4
  • Issue comment event: 25
  • Push event: 179
  • Pull request event: 26
Last Year
  • Create event: 26
  • Commit comment event: 18
  • Release event: 8
  • Issues event: 20
  • Watch event: 2
  • Delete event: 4
  • Issue comment event: 25
  • Push event: 179
  • Pull request event: 26

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 1,779
  • Total Committers: 14
  • Avg Commits per committer: 127.071
  • Development Distribution Score (DDS): 0.323
Past Year
  • Commits: 295
  • Committers: 2
  • Avg Commits per committer: 147.5
  • Development Distribution Score (DDS): 0.193
Top Committers
Name Email Commits
pat-alt a****t@g****m 1,205
kmariuszk k****m@g****m 210
Rauno Arike R****e@s****l 135
JorgeLuizFranco 6****o 57
Vincent Pikand p****t@g****m 54
CompatHelper Julia c****y@j****g 35
Lauri 6****l 25
Simon Kasdorp s****p@s****l 22
lkeskull L****l@s****l 20
Pietro Monticone 3****e 8
A-Salmon 9****n 4
Jafar Isbarov c****v@g****m 2
Ikko Eltociear Ashimine e****r@g****m 1
Patrick Altmeyer p****r@P****x 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 237
  • Total pull requests: 188
  • Average time to close issues: 2 months
  • Average time to close pull requests: 7 days
  • Total issue authors: 12
  • Total pull request authors: 13
  • Average comments per issue: 0.84
  • Average comments per pull request: 1.29
  • Merged pull requests: 150
  • Bot issues: 0
  • Bot pull requests: 21
Past Year
  • Issues: 23
  • Pull requests: 30
  • Average time to close issues: 4 days
  • Average time to close pull requests: 1 day
  • Issue authors: 2
  • Pull request authors: 3
  • Average comments per issue: 0.3
  • Average comments per pull request: 1.0
  • Merged pull requests: 22
  • Bot issues: 0
  • Bot pull requests: 5
Top Authors
Issue Authors
  • pat-alt (156)
  • RaunoArike (40)
  • VincentPikand (12)
  • kmariuszk (5)
  • izagorac (4)
  • JorgeLuizFranco (4)
  • laurikskl (3)
  • drobiu (2)
  • abuszydlik (2)
  • lazarusA (1)
  • feldob (1)
  • JuliaTagBot (1)
Pull Request Authors
  • pat-alt (119)
  • github-actions[bot] (24)
  • RaunoArike (20)
  • kmariuszk (17)
  • VincentPikand (10)
  • pitmonticone (9)
  • laurikskl (7)
  • JorgeLuizFranco (6)
  • checkdgt (3)
  • drobiu (3)
  • A-Salmon (2)
  • ceferisbarov (2)
  • eltociear (1)
Top Labels
Issue Labels
enhancement (104) easy (56) students (36) medium (33) difficult (24) must have (24) bug (24) could have (14) students :dart: (9) documentation (9) priority :dart: (6) help wanted (5) CCE :100: (3) students :star: (3) should have (3) good first issue (2) question (2) wontfix (1) breaking (1)
Pull Request Labels
priority :dart: (3) easy (2) enhancement (1) students (1) bug (1)

Packages

  • Total packages: 1
  • Total downloads:
    • julia 1 total
  • Total dependent packages: 3
  • Total dependent repositories: 0
  • Total versions: 58
juliahub.com: CounterfactualExplanations

A package for Counterfactual Explanations and Algorithmic Recourse in Julia.

  • Versions: 58
  • Dependent Packages: 3
  • Dependent Repositories: 0
  • Downloads: 1 Total
Rankings
Dependent repos count: 9.9%
Stargazers count: 13.1%
Average: 28.9%
Dependent packages count: 38.9%
Forks count: 53.5%
Last synced: 6 months ago

Dependencies

.github/workflows/CI.yml actions
  • actions/checkout v2 composite
  • codecov/codecov-action v2 composite
  • julia-actions/cache v1 composite
  • julia-actions/julia-buildpkg v1 composite
  • julia-actions/julia-docdeploy v1 composite
  • julia-actions/julia-processcoverage v1 composite
  • julia-actions/julia-runtest v1 composite
  • julia-actions/setup-julia v1 composite
.github/workflows/TagBot.yml actions
  • JuliaRegistries/TagBot v1 composite
.github/workflows/CompatHelper.yml actions
.github/workflows/FormatCheck.yml actions
  • actions/checkout v1 composite
  • julia-actions/setup-julia latest composite
  • reviewdog/action-suggester v1 composite