Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Activity 8.1

This page is an ipython notebook, meaning you can download it and run the executable cells. It directly follows Activity 8.1. Reference answers appear after the questions, challenges, predictions, and reflections. They show one reasonable solution or interpretation, but other correct approaches may exist.

Ethics, Fairness, Accountability & Transparency

Tools: Python, pandas, NumPy, matplotlib/seaborn, Jupyter/VS Code

Learning objectives

By the end of this tutorial, you should be able to:

  • identify several forms of bias in a data workflow;

  • evaluate group representation;

  • calculate group-specific classification metrics;

  • compute demographic parity, true-positive rates, and false-positive rates;

  • investigate threshold tradeoffs;

  • quantify uncertainty from small subgroup samples;

  • identify proxy and measurement concerns;

  • write a compact model card;

  • document intended and out-of-scope uses;

  • design a basic monitoring plan;

  • distinguish technical performance from responsible deployment.


Part 0 — Set up

Create:

notebooks/week15_ethics.ipynb

Import:

Create:


Part 1 — Create a synthetic screening dataset

We will use a fictional screening system.

Create:

Inspect:

Important

This dataset is synthetic.

The purpose is to study fairness concepts without making claims about any real population.


Part 2 — Representation

Calculate:

and:

Questions

  1. Are the groups equally represented?

  2. Is unequal representation automatically unfair?

  3. What additional context would you need?


Part 3 — Outcome prevalence

Calculate:

Question

Do base rates differ across groups?

Why could that matter when interpreting fairness metrics?

Reference answer

Unequal representation is not automatically unfair. Interpretation needs context about the target population, sampling process, intended use, historical selection, and uncertainty for smaller groups.


Part 4 — Apply a classification threshold

Set:

Create:

Inspect:


Reference answer

Different base rates matter because fairness metrics condition on different events. The same classifier can have different TPR, FPR, PPV, and positive rates across groups even with one threshold.

Part 5 — Overall accuracy

Calculate:

Question

Does this value tell you whether performance is similar across groups?


Part 6 — Group-specific accuracy

Calculate:

Reflection

How does this change your understanding of model performance?

Reference answer

Overall accuracy does not show whether groups have similar performance. Group-specific metrics are needed because aggregate accuracy can hide subgroup disparities.


Part 7 — Confusion matrix components

Write:

Test on each group.

Reference answer

Confusion counts separate true positives, false positives, true negatives, and false negatives, revealing which error types differ across groups.


Part 8 — Define fairness-relevant metrics

Write:


Reference answer

Positive prediction rate, TPR, FPR, PPV, and accuracy use different denominators and answer different operational questions.

Part 9 — Calculate group metrics

Run:

Interpret each column.


Part 10 — Demographic parity difference

Extract positive prediction rates.

Calculate:

Question

What would a value near zero mean?

Does that prove the classifier is fair?

Reference answer

Demographic parity compares positive prediction rates. A difference near zero means similar selection rates, not that all other fairness criteria are satisfied.


Part 11 — Equal opportunity difference

Calculate:

Interpret:

Among truly positive cases, how differently are groups treated?

Reference answer

Equal opportunity compares TPR. A difference near zero means truly positive cases are detected at similar rates, but it does not establish overall fairness.


Part 12 — False-positive-rate difference

Calculate:

Question

Why might this matter even if true-positive rates are equal?

Reference answer

FPR difference asks how differently truly negative cases are incorrectly labeled positive. This matters because false positives can carry substantial costs.


Part 13 — Predictive parity comparison

Compare:

with:

Question

What does PPV answer that TPR does not?

Reference answer

Predictive parity compares PPV: among predicted-positive cases, how often is the prediction correct? It can differ even when TPR is similar.


Part 14 — Metric table

Create a DataFrame containing:

group
n
base_rate
positive_rate
accuracy
TPR
FPR
PPV

Round only for display.

Do not round intermediate calculations.

Reference answer

TPR conditions on actual positives; PPV conditions on predicted positives. They answer different questions and respond differently to prevalence.


Part 15 — Visualize the disparities

Create a long-form metric table.

Plot:

Reflection

Which differences are most visually substantial?


Part 16 — Sample size beside metrics

Add group counts to your metric report.

Question

Why should a fairness metric never be interpreted without knowing how many observations support it?

Reference answer

Metric differences should be interpreted with group sample sizes and uncertainty. Smaller groups produce noisier estimates, so a fairness metric without its supporting n can mislead.


Part 17 — Bootstrap uncertainty for TPR

Use Week 11’s bootstrap logic.

For each group:

  1. select the group’s rows;

  2. bootstrap rows with replacement;

  3. calculate TPR for each bootstrap sample;

  4. calculate a 95% percentile interval.

Important

Handle bootstrap samples where a denominator becomes zero.

Reflection

How much uncertainty is there around each group’s TPR?


Part 18 — Small subgroup experiment

Create a smaller subgroup:

Calculate TPR and bootstrap interval.

Question

How did uncertainty change?


Part 19 — Threshold sweep

Create:

For each threshold:

  1. generate predictions;

  2. calculate TPR by group;

  3. calculate FPR by group;

  4. calculate positive rate by group.

Store results.


Reference answer

Smaller samples yield wider bootstrap intervals. Increasing a threshold generally lowers positive prediction rate, TPR, and FPR, exposing tradeoffs rather than a universally best threshold.

Part 20 — Plot threshold tradeoffs

Plot:

threshold vs. TPR
threshold vs. FPR
threshold vs. positive rate

by group.

Questions

  1. Does one threshold optimize every metric?

  2. What tradeoffs become visible?

  3. Which decisions are technical, and which are policy choices?


Part 21 — Compare overall and subgroup optimization

Find the threshold with highest overall accuracy.

Then inspect group-specific metrics at that threshold.

Reflection

Does maximizing aggregate accuracy guarantee acceptable subgroup performance?

Reference answer

Threshold selection combines technical evidence with policy/value judgments. An overall optimum can mask subgroup tradeoffs, so inspect both overall and group-specific objectives.


Part 22 — Calibration by group

Create probability bins:

Calculate by:

group
probability bin

the:

mean predicted probability
observed positive rate
count

Plot predicted vs. observed.


Part 23 — Calibration interpretation

Question

If both groups are calibrated, does that guarantee:

equal TPR?
equal FPR?
equal positive rates?

Explain why fairness definitions are not interchangeable.

Reference answer

Calibration asks whether cases assigned a given probability experience the outcome at approximately that rate. Calibration can differ by group even when discrimination metrics are similar.


Part 24 — Proxy-variable thought experiment

Suppose the dataset includes:

region_code
school_id
neighborhood_score

and group membership is removed.

Discuss:

  1. Could these variables still contain group information?

  2. Would removing group make auditing easier or harder?

  3. Why is “fairness through unawareness” incomplete?

No code is required.

Reference answer

Fairness definitions are not interchangeable because they condition on different events and represent different harms/goals. With unequal base rates, several criteria may be impossible to satisfy simultaneously.


Part 25 — Measurement-bias thought experiment

Suppose the outcome variable is:

prior system approval

rather than:

true future success

Questions

  1. What exactly would the model learn?

  2. Could historical bias enter the label?

  3. Would excellent predictive accuracy solve this problem?

Reference answer

Removing group membership does not remove information about group membership if other variables act as proxies. Measurement choices can also encode historical inequalities.


Part 26 — Missingness audit

Introduce:

Calculate missingness by group.

Question

What process might produce unequal missingness in a real system?

Reference answer

A model trained on historically biased labels can learn that bias accurately. High predictive accuracy does not prove that the target label or decision process is ethically appropriate.


Part 27 — Data minimization exercise

Your hypothetical data source contains:

name
email
date_of_birth
ZIP code
group
score
outcome
device ID
full browsing history

The model requires only a subset.

Write:

Keep:
Do not keep:
Need justification:

for each field.

Reflection

Why is data minimization an ethical and technical principle?

Reference answer

Unequal missingness can arise from access, measurement, reporting, workflow, or follow-up differences. Audit missingness by group because missing-data handling can change the represented population.


Part 28 — Intended use

Write a short description:

## Intended use

This model is intended to...

Include:

  • who should use it;

  • what decision it supports;

  • what population it was evaluated on.

Reference answer

Data minimization means collecting only information necessary for the stated purpose, reducing privacy risk, misuse opportunities, storage burden, and proxy-variable exposure.


Part 29 — Out-of-scope use

Write:

## Out-of-scope use

Give at least three examples of uses that are not supported by the analysis.

Question

Why is documenting misuse risk important?

Reference answer

Intended use should state the supported decision, users, population, setting, outputs, and evaluation context.


Part 30 — Performance section

Create a model-card table with:

overall accuracy
group-specific accuracy
TPR by group
FPR by group
PPV by group
positive rate by group
sample size by group

Include uncertainty where possible.

Reference answer

Document out-of-scope uses because technically possible use is not the same as validated use. This makes foreseeable misuse and unsupported extrapolation explicit.


Part 31 — Limitations section

Write at least five limitations.

Possible categories:

synthetic data
sample representativeness
measurement validity
small subgroup uncertainty
threshold dependence
distribution shift
proxy variables

Be specific.

Reference answer

Limitations can include representativeness, label/measurement quality, subgroup sample sizes, uncertainty, distribution shift, proxy variables, and limits to generalization.


Part 32 — Accountability section

Write:

## Accountability

Model owner:
Decision owner:
Monitoring owner:
Escalation path:
Appeal/review mechanism:

Reflection

Why should these roles be explicit before deployment?

Reference answer

Accountability should specify who owns deployment decisions, monitors outcomes, investigates failures, can halt/override the system, and handles appeals or review.


Part 33 — Transparency section

Write:

## Transparency

Describe:

  • what the model outputs;

  • what information users should see;

  • what limitations should be communicated;

  • what should not be implied.

Reference answer

Roles must be explicit before deployment so monitoring, escalation, maintenance, and responsibility do not fall between teams.


Part 34 — Monitoring plan

Create:

Metric
Frequency
Population/subgroup
Alert condition
Owner
Action

Include at least:

overall performance
TPR/FPR by group
missingness
calibration
data drift

Reference answer

Transparency should explain what the model outputs, what evidence/uncertainty users can see, known limitations, and what the output should not be interpreted to mean.

Part 35 — Distribution-shift simulation

Create a future dataset where:

Apply the same model formula and threshold.

Compare current vs. future:

probability distribution
accuracy
positive rate
group metrics

Question

Why can a previously validated model become inappropriate?

Reference answer

Distribution shift occurs when future data differ from training/evaluation data. Compare distributions and performance over time to detect degradation.


Part 36 — Feedback-loop discussion

Consider:

model labels individuals high risk
      ↓
they receive more scrutiny
      ↓
more negative events are recorded
      ↓
future training data contain more negatives

Answer:

  1. Is the future label independent of model deployment?

  2. What monitoring could reveal this?

  3. How might this affect retraining?

Reference answer

A previously validated model can become inappropriate because populations, measurement systems, policies, incentives, and base rates change over time.


Part 37 — NIST RMF mapping exercise

Use the four functions:

Govern
Map
Measure
Manage

For your fictional screening system, write one concrete action for each.

Example structure:

Govern:
Map:
Measure:
Manage:

Reference answer

Map actions to Identify, Measure, Manage, and Govern: document context/risks; quantify performance/fairness/uncertainty; implement mitigations/monitoring; assign responsibility/review processes.

Part 38 — Responsible deployment decision

Choose one:

Deploy
Deploy with safeguards
Do not deploy yet

Write a one-paragraph justification based on:

  • technical performance;

  • subgroup metrics;

  • uncertainty;

  • limitations;

  • accountability;

  • monitoring readiness.

There is no automatically correct answer.

The reasoning matters.


Part 39 — Final ethics audit

Answer:

What problem is the system solving?
Who is affected?
What assumptions are being made?
What data limitations exist?
What groups may experience different errors?
Which fairness metric matters most here, and why?
What metric conflicts exist?
How uncertain are subgroup estimates?
What should users be told?
Who is accountable?
How will harm be detected after deployment?
What would trigger suspension or retraining?

Reference answer

A defensible ethics audit ties conclusions to the problem, assumptions, data limitations, subgroup errors, uncertainty, relevant fairness criteria, accountability, and monitoring readiness.

Part 40 — Git checkpoint

Run:

git status

Commit:

git add .
git commit -m "Complete Week 15 responsible data science exercises"
git push

Part 41 — Final course reflection

Answer in Markdown.

1. Technical correctness

Why is correct code not sufficient for responsible data science?

2. Bias

What is the difference among selection, historical, and measurement bias?

3. Fairness

Why is there no single universally correct fairness metric?

4. Error rates

Why must false positives and false negatives be interpreted in context?

5. Transparency

What information should users know about a model?

6. Accountability

Why should responsibility remain with people and organizations rather than “the algorithm”?

7. Documentation

How do model cards and data documentation improve responsible practice?

8. Monitoring

Why must responsible evaluation continue after deployment?

9. Reproducibility

How does reproducibility support accountability?

10. STA 556

Which technical practice from this course most improves your ability to build trustworthy statistical workflows?


Completion checklist

  • Created Week 15 notebook

  • Generated a synthetic classification dataset

  • Audited group representation

  • Compared base rates

  • Applied a classification threshold

  • Calculated overall accuracy

  • Calculated subgroup accuracy

  • Built confusion-matrix counts

  • Calculated TPR

  • Calculated FPR

  • Calculated PPV

  • Calculated positive prediction rate

  • Calculated demographic-parity difference

  • Calculated equal-opportunity difference

  • Compared false-positive rates

  • Compared predictive parity

  • Built a group metric table

  • Visualized subgroup metrics

  • Reported subgroup sample sizes

  • Bootstrapped uncertainty for a fairness metric

  • Investigated a small subgroup

  • Swept classification thresholds

  • Visualized threshold tradeoffs

  • Compared overall vs. subgroup optimization

  • Audited calibration by group

  • Discussed proxy variables

  • Discussed label/measurement bias

  • Audited missingness by group

  • Completed a data-minimization exercise

  • Wrote intended-use documentation

  • Wrote out-of-scope-use documentation

  • Created a model-card performance section

  • Documented limitations

  • Defined accountability roles

  • Wrote a transparency section

  • Designed a monitoring plan

  • Simulated distribution shift

  • Discussed feedback loops

  • Mapped the project to Govern/Map/Measure/Manage

  • Made a responsible deployment recommendation

  • Completed the final ethics audit

  • Committed Week 15 work to Git

  • Pushed work to GitHub


What you should now understand

Technically correct workflow
      ↓
Ethical problem definition
      ↓
Representative + appropriate data
      ↓
Performance + subgroup evaluation
      ↓
Fairness tradeoffs
      ↓
Transparency + documentation
      ↓
Accountability
      ↓
Monitoring
      ↓
Responsible statistical practice

This completes STA 556: from computational foundations and data engineering through reproducibility, numerical methods, testing, performance, and responsible data science.

Reference answer

Correct code is necessary but not sufficient: responsible data science also requires valid targets, representative data, contextual interpretation of errors, justified fairness criteria, transparency, accountability, data minimization, and ongoing monitoring.