Compute SS_xx, SP_xy, the slope and the intercept from the sums for the eight clients, and confirm against statistics.linear_regression.
Compute SS_xx, SP_xy, the slope and the intercept from the sums for the eight clients, and confirm against statistics.linear_regression.
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) xbar = sum(income) / n ybar = sum(score) / n ssxx = sum(x * x for x in income) - n * xbar ** 2 spxy = sum(x * y for x, y in zip(income, score)) - n * xbar * ybar b1 = spxy / ssxx b0 = ybar - b1 * xbar print(f"SSxx={ssxx} SPxy={spxy} b1={b1:.4f} b0={b0:.2f}") fit = st.linear_regression(income, score) print(f"statistics.linear_regression: slope={fit.slope:.4f} intercept={fit.intercept:.2f}")
IBS1 §13.4 (least squares), BCc ch 8, CC BY 4.0 — shape only; S21 W3 handout (three slope forms, rounding warning — shape only); original dataset