Compute the cross-product sum, the two means and the sample covariance of income and score by the shortcut form, and confirm it against statistics.covariance.
Compute the cross-product sum, the two means and the sample covariance of income and score by the shortcut form, and confirm it against statistics.covariance.
Answer
import statistics as st income = [35, 48, 52, 61, 70, 84, 95, 110] score = [610, 645, 640, 680, 700, 720, 745, 760] n = len(income) sxy = sum(x * y for x, y in zip(income, score)) xbar = sum(income) / n ybar = sum(score) / n cov = (sxy - n * xbar * ybar) / (n - 1) print(f"sumxy={sxy} xbar={xbar} ybar={ybar} cov={cov:.4f}") print(f"statistics.covariance={st.covariance(income, score):.4f}")
IBS1 §13.1 (scatter plots), IS1 §12.2, CC BY 4.0 — shape only; S21 W3 handout (shortcut sums, "sign only" — shape only); original dataset