compileml.fairness

counterfactual

def counterfactual(artifact, X, protected, protected_feature: str | None, *, labels=None) -> dict

from compileml.fairness import counterfactual

§11 — flip the protected attribute and see whether decisions move.

Three outcomes, reported rather than skipped, and kept distinct because they mean different things:

- status="not_named" — protected_feature is None. The audit was not told which input, if any, is the attribute, so it has nothing to flip. That is **not** evidence the model ignores the attribute, and the reason says so. If a column of X matches the protected attribute value for value, the reason names it. - status="not_an_input" — a name was given and the artifact has no such feature, so the model cannot be using it directly. That is the desired outcome and appears as a positive statement. - status="flipped" — the attribute is an input and was flipped.

Parameters#

NameTypeDefaultKind
artifact—requiredpositional
X—requiredpositional
protected—requiredpositional
protected_featurestr | Nonerequiredpositional
labels—Nonekeyword-only

Returns#

dict