THINK FIRST·CODE LATER

← All labs

A reproducible bootstrap confidence interval

Problem

Input: a line seed=S resamples=B, then a CSV district,pm25 with two districts.

Let a and b be the readings of the alphabetically first and second district. With rng = np.random.default_rng(S), repeat B times: resample a and b with replacement (rng.choice(x, x.size), a first then b) and record median(a*) − median(b*). The 95 % CI is the 2.5th and 97.5th percentiles of these differences (np.percentile). Print Lucheng median 46, Ouhai median 38, difference 8, bootstrap 95% CI of the difference (2000 resamples, seed 42): [3, 13.5] (values in :g format) and the CI excludes 0 or the CI includes 0: the difference may be due to chance.

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-4-data-1.txt — first 14 lines of 62

seed=42 resamples=2000
district,pm25
Lucheng,42
Lucheng,37
Lucheng,46
Lucheng,51
Lucheng,57
Lucheng,49
Lucheng,44
Lucheng,42
Lucheng,54
Lucheng,61
Lucheng,50
Lucheng,38
What your program must print for v14-4-data-1.txt
Lucheng median 46, Ouhai median 38, difference 8
bootstrap 95% CI of the difference (2000 resamples, seed 42): [3, 13.5]
the CI excludes 0

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-4-data-1.txt

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

Get-Content v14-4-data-1.txt | 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-4-data-1.txt", 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.