knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.5, warning = FALSE, message = FALSE ) library(AIBias)
Standard fairness audits treat bias as a snapshot — a single-point disparity measurement at one moment in time. But in sequential decision systems (loan approvals, parole reviews, hiring pipelines, credit scoring), decisions at time $t$ feed back into the features available at time $t+1$.
AIBias treats algorithmic bias as a longitudinal process. It tracks:
The package ships with lending_panel, a 600-applicant × 6-year panel of
synthetic loan decisions across three racial groups.
data(lending_panel) head(lending_panel)
# Pooled approval rates by group tapply(lending_panel$approved, lending_panel$race, mean) |> round(3)
Even from this simple tabulation we see a gap. But it misses the dynamic story: are disadvantaged groups less able to recover from a denial? Are gaps widening over time?
obj <- aib_build( data = lending_panel, id = "applicant_id", time = "year", group = "race", decision = "approved" ) print(obj)
aib_describe() computes:
obj <- aib_describe(obj, ref_group = "White") obj$bias$cumulative
The cumulative burden (CB_normalized) summarizes the average disparity
experienced across all waves — a single policy-facing number.
plot(obj, type = "trajectory")
Both groups show a persistent negative disparity from wave 1. The gap is relatively stable. The decomposition in Step 5 shows what keeps it stable.
plot(obj, type = "heatmap")
The heatmap displays the full group × time disparity surface. Red cells indicate disadvantaged periods.
The key question for compounding bias: are disadvantaged groups less likely to recover after a denial, and less likely to retain approval?
obj <- aib_transition(obj, ref_group = "White") # Recovery and retention gaps obj$transitions$recovery_gap obj$transitions$retention_gap
plot(obj, type = "transition")
After a denial, Black and Hispanic applicants are approved the next year less often than White applicants (the recovery gap), and after an approval they keep it less often (the retention gap).
By default, transitions link each decision to the decision at the
immediately preceding scheduled occasion (lag = "scheduled"). A unit that
misses an occasion contributes no transition into the next one. Use
lag = "recorded" to link each decision to the unit's previous data row
instead, which is what AIBias 0.1.1 did. The two agree on balanced panels.
The Markov amplification operator $A^{state}_{g,r}(T) = \sum_t \|v_g(t) - v_r(t)\|$ quantifies cumulative divergence in state distributions:
obj$transitions$amp_state
The amplification index contrasts the gap among units with a favorable prior decision and the gap among units with an unfavorable one:
$$A_{g,r}(t) = B_{g,r}(t \mid 1) - B_{g,r}(t \mid 0).$$
Under the default sign = "mechanism", aib_amplify() reports
$-A_{g,r}(t)$, which equals the recovery gap minus the retention gap.
Negative values mean the focal group's shortfall after a denial is larger
than its shortfall after an approval.
obj <- aib_amplify(obj, ref_group = "White") obj$amplification$cumulative
plot(obj, type = "amplification")
The index says whether the conditional gap depends on the prior decision. It does not say whether the overall gap is growing. The next section shows a case where the index is exactly zero and the gap still widens.
Suppose both groups start with 50% favorable decisions. The reference group recovers with probability 0.2 and retains with probability 0.8; the focal group recovers with 0.1 and retains with 0.7. Both conditional gaps equal $-0.10$, so the amplification index is zero. Yet the approval gap goes from 0 to $-0.10$ to $-0.16$.
sim <- aib_simulate(n = 20000, n_times = 3, groups = c("ref", "focal"), init = c(0.5, 0.5), p01 = c(0.2, 0.1), p11 = c(0.8, 0.7), seed = 2026) zobj <- aib_build(sim, "id", "time", "group", "decision", verbose = FALSE) zobj <- aib_transition(zobj, ref_group = "ref", verbose = FALSE) zobj <- aib_amplify(zobj, ref_group = "ref", verbose = FALSE) round(zobj$amplification$index$A_index, 3) zobj <- aib_decompose(zobj, ref_group = "ref", verbose = FALSE) zobj$decomposition$steps[, c("time_from", "time_to", "gap_prev", "gap_curr", "carry", "recovery", "retention")]
The gap widens because the focal group enters the second step with fewer
approvals, and approvals persist (carry), on top of the recovery and
retention gaps.
aib_decompose() makes this exact for any panel. For each pair of
consecutive occasions it takes the units with a recorded decision at both
(the paired units) and splits the current gap among them as
$$\Delta^P_t = \bar s\,\Delta^P_{t-1} + (1-\bar q)\,\Delta p_{01} + \bar q\,\Delta p_{11},$$
where $\bar q$ is the two groups' average share favorable at $t-1$, $\bar s$ their average retention-minus-recovery rate, and $\Delta$ means focal minus reference. Two more terms, $S_{t-1}$ and $S_t$, record how far the full observed gap at each occasion differs from the paired gap. The change in the observed gap is then
$$\Delta_t - \Delta_{t-1} = \underbrace{(\bar s - 1)\Delta^P_{t-1}}{\text{decay}} + \underbrace{(1-\bar q)\Delta p{01}}{\text{recovery}} + \underbrace{\bar q\,\Delta p{11}}{\text{retention}} + \underbrace{(S_t - S{t-1})}_{\text{sample change}}.$$
The decay term is the part of the prior gap that does not carry forward. The identity is exact for the recorded data; it needs no assumption that prior decisions cause later ones.
obj <- aib_decompose(obj, ref_group = "White") obj$decomposition$totals
plot(obj, type = "decomposition")
In the lending panel the gap for Black applicants barely moves, but not because it carries forward. Approval persists only weakly ($\bar s$ is small), so each year most of the prior gap decays. The recovery and retention gaps recreate it at every decision. A remedy aimed only at past denials would leave most of this gap in place.
The panel is complete, so the sample terms are zero. When decisions are
missing, aib_decompose() reports how many units were present, how many
had a decision and how many were paired, and flags steps where a
prior-state cell is empty (supported = FALSE) or smaller than min_cell
(sparse = TRUE).
To separate "case mix" differences from residual disparity, fit a covariate-adjusted model:
obj <- aib_adjust( obj, formula = ~ income + credit_score, method = "glm", ref_group = "White" ) # Adjusted trajectory head(obj$adjusted$trajectory)
aib_bootstrap() resamples whole applicant histories and recomputes every
component already in the object, including the decomposition.
obj <- aib_bootstrap(obj, B = 500, seed = 2024, conf = 0.95) subset(obj$bootstrap$intervals, component == "decomposition_total") plot(obj, type = "decomposition") # adds intervals for the observed change
aib_audit()Run the full pipeline in one call:
result <- aib_audit( lending_panel, id = "applicant_id", time = "year", group = "race", decision = "approved", ref_group = "White", decompose = TRUE, bootstrap = TRUE, B = 200, seed = 42 ) summary(result)
If at every occasion $p_g^{11}(t) \le p_r^{11}(t)$, $p_g^{01}(t) \le p_r^{01}(t)$ and $p_r^{11}(t) \ge p_r^{01}(t)$, and both groups start at the same rate, then the focal group's favorable-decision rate never exceeds the reference group's. The gap need not widen at every step, however, and it can level off. The decomposition shows which terms hold it where it is.
All quantities here describe recorded decisions. Reading the transition gaps or the decomposition as effects of earlier decisions on later ones requires assumptions about how decisions and observation are generated.
| Estimand | Function | Formula |
|---|---|---|
| Bias trajectory | aib_describe() | $B_{g,r}(t)$ |
| Standardized trajectory | aib_describe() | $B^*{g,r}(t)$ |
| Cumulative burden | aib_describe() | $CB{g,r}(T)$ |
| Recovery gap | aib_transition() | $\Delta^{01}{g,r}$ |
| Retention gap | aib_transition() | $\Delta^{11}{g,r}$ |
| Amplification index | aib_amplify() | $A_{g,r}(t)$ |
| Gap decomposition | aib_decompose() | decay, recovery, retention, sample change |
| Bias persistence | aib_persistence() | $PB_{g,r}(c)$ |
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.