Reproduce Anscombe's finding: compute the usual summary of several datasets and report which ones cannot be told apart by their numbers.
What to build
A program that summarises paired datasets and groups those whose summaries match.
Requirements
- For each dataset print
NAME: mean x MX, mean y MY, r R, line y = A + Bx, all numbers with two decimals, where R is the correlation and the line is the least-squares fit; when B is negative writey = A - |B|x. - Then, for each group of two or more datasets whose printed summaries are identical, print
same summary: N1, N2, ..., groups in the order their first member appeared; if there is no such group printevery summary is different.
Input
Line 1: k. Then, for each dataset, three lines: its name, its x values and its y values (separated by spaces).
Output
k lines, then the group lines.
Constraints
1 ≤ k ≤ 10; each dataset has 2 to 50 points; neither the x values nor the y values of a dataset are all equal.
Example
Input:
3
A
1 2 3
2 4 6
B
1 2 3
6 4 2
C
3 2 1
6 4 2
Output:
A: mean x 2.00, mean y 4.00, r 1.00, line y = 0.00 + 2.00x
B: mean x 2.00, mean y 4.00, r -1.00, line y = 8.00 - 2.00x
C: mean x 2.00, mean y 4.00, r 1.00, line y = 0.00 + 2.00x
same summary: A, C
Why it works
Rounded summaries are what a report shows, so datasets with identical rounded summaries are indistinguishable on paper, and only plotting them reveals the difference.
Examples
3 A 1 2 3 2 4 6 B 1 2 3 6 4 2 C 3 2 1 6 4 2
A: mean x 2.00, mean y 4.00, r 1.00, line y = 0.00 + 2.00x B: mean x 2.00, mean y 4.00, r -1.00, line y = 8.00 - 2.00x C: mean x 2.00, mean y 4.00, r 1.00, line y = 0.00 + 2.00x same summary: A, C
2 I 10 8 13 9 11 14 6 4 12 7 5 8.04 6.95 7.58 8.81 8.33 9.96 7.24 4.26 10.84 4.82 5.68 II 10 8 13 9 11 14 6 4 12 7 5 9.14 8.14 8.74 8.77 9.26 8.10 6.13 3.10 9.13 7.26 4.74
I: mean x 9.00, mean y 7.50, r 0.82, line y = 3.00 + 0.50x II: mean x 9.00, mean y 7.50, r 0.82, line y = 3.00 + 0.50x same summary: I, II
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Compute each summary as a tuple of the rounded strings you print; equal tuples mean the same summary.
Hint 2
The least-squares slope is the covariance of x and y divided by the variance of x.
Solution
Show one correct solution (try it yourself first)
def mean(values):
return sum(values) / len(values)
k = int(input())
summaries = []
for _ in range(k):
name = input()
xs = [float(v) for v in input().split()]
ys = [float(v) for v in input().split()]
mx, my = mean(xs), mean(ys)
sxy = sum((a - mx) * (b - my) for a, b in zip(xs, ys))
sxx = sum((a - mx) ** 2 for a in xs)
syy = sum((b - my) ** 2 for b in ys)
r = sxy / (sxx * syy) ** 0.5
slope = sxy / sxx
intercept = my - slope * mx
sign = "+" if slope >= 0 else "-"
text = f"mean x {mx:.2f}, mean y {my:.2f}, r {r:.2f}, line y = {intercept:.2f} {sign} {abs(slope):.2f}x"
print(f"{name}: {text}")
summaries.append((name, text))
groups = []
for name, text in summaries:
for group in groups:
if group[0] == text:
group[1].append(name)
break
else:
groups.append((text, [name]))
shown = [names for _, names in groups if len(names) > 1]
for names in shown:
print("same summary: " + ", ".join(names))
if not shown:
print("every summary is different")