Causal Effect API¶
Causal effect regularization detector for shortcut detection via causal effect estimation.
Class Reference¶
CausalEffectDetector
¶
CausalEffectDetector(
*,
effect_estimator: str = "direct",
spurious_threshold: float = 0.1,
random_state: int = 42
)
Bases: DetectorBase
Detect shortcut attributes via causal effect estimation.
Estimates the causal effect of each candidate attribute on the task label. Attributes with near-zero estimated effect are flagged as spurious (shortcuts), since changing them should not change the true label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
effect_estimator
|
str
|
Estimator for causal effect ("direct" supported). |
'direct'
|
spurious_threshold
|
float
|
Attributes with |TE_a| < threshold are flagged as spurious. Default 0.1. |
0.1
|
random_state
|
int
|
Random seed for reproducibility. |
42
|
Source code in shortcut_detect/causal/causal_effect/src/detector.py
Functions¶
fit
¶
fit(
*,
embeddings: ndarray,
labels: ndarray,
attributes: dict[str, ndarray],
counterfactual_pairs: ndarray | list | None = None
) -> CausalEffectDetector
Fit causal effect estimator and detect spurious attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
ndarray
|
(n_samples, n_features) representation space. |
required |
labels
|
ndarray
|
(n_samples,) task labels (binary or multi-class). |
required |
attributes
|
dict[str, ndarray]
|
Dict of attribute_name -> (n_samples,) values per sample. Binary (0/1) or categorical; multi-valued attributes are binarized. |
required |
counterfactual_pairs
|
ndarray | list | None
|
Optional. For interventional data (Phase 2). Not used in current Direct estimator. |
None
|
Returns:
| Type | Description |
|---|---|
CausalEffectDetector
|
self |
Source code in shortcut_detect/causal/causal_effect/src/detector.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
Loader Integration Example¶
from shortcut_detect import ShortcutDetector
loader_data = {
"embeddings": embeddings, # (n, d)
"labels": labels, # (n,)
"attributes": {
"race": race_labels, # (n,) binary or categorical
"color": color_labels,
},
}
detector = ShortcutDetector(
methods=["causal_effect"],
causal_effect_spurious_threshold=0.1,
)
detector.fit_from_loaders({"causal_effect": loader_data})
print(detector.get_results()["causal_effect"]["metrics"])