THINK FIRST·CODE LATER

← All labs

Detect Simpson's paradox

Problem

Input: a CSV group,option,successes,total.

Print the overall success rate of each option (alphabetical): overall: app_routing 78.0%, manual 82.6%; then for each group (alphabetical) weekday: app_routing 93.1%, manual 86.7% (app_routing n=87, manual n=270). If one option wins (has the highest rate) in every group but is not the overall winner, print SIMPSON'S PARADOX: app_routing is better in every group, but manual looks better overall and reason: the options are used in different proportions across groups with different base rates; otherwise no reversal between the overall and the per-group comparison.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v14-3-data-1.csv

group,option,successes,total
weekday,app_routing,81,87
weekday,manual,234,270
weekend,app_routing,192,263
weekend,manual,55,80
What your program must print for v14-3-data-1.csv
overall: app_routing 78.0%, manual 82.6%
weekday: app_routing 93.1%, manual 86.7% (app_routing n=87, manual n=270)
weekend: app_routing 73.0%, manual 68.8% (app_routing n=263, manual n=80)
SIMPSON'S PARADOX: app_routing is better in every group, but manual looks better overall
reason: the options are used in different proportions across groups with different base rates

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v14-3-data-1.csv

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v14-3-data-1.csv | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v14-3-data-1.csv", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.