Implement median-of-halves quartiles and print them beside statistics.quantiles for the odd n = 11 set and the even n = 10 set, so the case where the library rule diverges is visible.
Implement median-of-halves quartiles and print them beside statistics.quantiles for the odd n = 11 set and the even n = 10 set, so the case where the library rule diverges is visible.
Answer
import statistics as st def hand_quartiles(data): """Median-of-halves: drop the median when n is odd.""" s = sorted(data) n = len(s) lower = s[: n // 2] upper = s[(n + 1) // 2 :] return st.median(lower), st.median(upper) for name, od in [("n=11", [1, 2, 4, 5, 7, 8, 10, 12, 15, 19, 41]), ("n=10", [6, 9, 11, 14, 16, 18, 19, 20, 24, 27])]: q1, q3 = hand_quartiles(od) lq1, _, lq3 = st.quantiles(od, n=4, method="exclusive") print(f"{name} hand: Q1={q1} Q3={q3} | quantiles(exclusive): Q1={lq1} Q3={lq3}")
IBS1 §2.2 (quartiles, fences), IS1 §2.4 (box-plot construction), CC BY 4.0 — shape only; original dataset