The chi-square statistic

Before deciding anything, a chi-square test adds up how far each observed count is from its expected count. Show each category's contribution and the total.

What to build

A program that prints each category's contribution and the chi-square statistic.

Requirements

  • For each category, in order, print category I: C, with I counting from 1 and C to 3 decimal places.
  • Then print chi-square X to 3 decimal places.

Input

Two lines: the observed counts, then the expected counts, in the same order.

Output

One line per category, then the total.

Constraints

2 to 20 categories; every expected count is over 0.

Example

Input:

8 9 12 11 6 14
10 10 10 10 10 10

Output:

category 1: 0.400
category 2: 0.100
category 3: 0.400
category 4: 0.100
category 5: 1.600
category 6: 1.600
chi-square 4.200

Why it works

Dividing each squared gap by its expected count scales it to the size of the category, so the contributions can be added into one measure of misfit.

Examples

Example 1 · Input
8 9 12 11 6 14
10 10 10 10 10 10
Expected output
category 1: 0.400
category 2: 0.100
category 3: 0.400
category 4: 0.100
category 5: 1.600
category 6: 1.600
chi-square 4.200
Example 2 · Input
30 20
25 25
Expected output
category 1: 1.000
category 2: 1.000
chi-square 2.000

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Read both lines into lists of floats and walk through them together with zip.

Hint 2

Each contribution is (o − e)² ÷ e.

Solution

Show one correct solution (try it yourself first)
obs = list(map(float, input().split()))
exp = list(map(float, input().split()))
total = 0
for i, (o, e) in enumerate(zip(obs, exp), 1):
    c = (o - e) ** 2 / e
    total += c
    print(f"category {i}: {c:.3f}")
print(f"chi-square {total:.3f}")

Lesson: Chi-square tests for counts →