~/ learn/ afm-113/ cards/ The least-squares line
1 of 5

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

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/d0141991-95f7-4764-ae11-e34096337700/flashcard utf-8 LF