{"schemaVersion":2,"courseId":"ols","module":{"id":"ols-line","title":"1 · The line of best fit","summary":"What OLS does, and recovering a known slope from data.","stages":[{"type":"read","title":"Modelling y with a line","html":"\n            <h2>The line of best fit</h2>\n            <p>Econometrics starts here. We model an outcome <em>y</em> as a straight-line\n            function of a regressor <em>x</em>, plus an error <em>ε</em> for everything we\n            didn't measure:</p>\n            <p class=\"katexish\">y = β₀ + β₁·x + ε</p>\n            <p>β₀ is the intercept and β₁ is the slope — the effect on <em>y</em> of a\n            one-unit change in <em>x</em>. <strong>Ordinary Least Squares (OLS)</strong>\n            chooses the β's that make the line as close to the points as possible.</p>\n            <div class=\"callout\"><b>Key idea.</b> OLS minimises the sum of <em>squared</em>\n            vertical gaps between the points and the line: Σ(yᵢ − ŷᵢ)².</div>","id":"ols-line-01","skillIds":["ols.least-squares-geometry"],"estimatedMinutes":6,"difficulty":"core","prerequisiteStageIds":[]},{"type":"code","title":"Estimate it","note":"We simulate data with a true intercept of 2 and slope of 3, then check that statsmodels recovers them.","code":"\nimport numpy as np, statsmodels.api as sm\nimport matplotlib.pyplot as plt\n\nrng = np.random.default_rng(0)\nn = 200\nx = rng.normal(0, 1, n)\ny = 2 + 3 * x + rng.normal(0, 1, n)        # truth: β0=2, β1=3\n\nres = sm.OLS(y, sm.add_constant(x)).fit()\nprint(res.summary())\n\nplt.figure(figsize=(6, 3.7))\nplt.scatter(x, y, s=12, color=\"#da9100\", alpha=0.5)\nxs = np.linspace(x.min(), x.max(), 50)\nplt.plot(xs, res.params[0] + res.params[1] * xs, color=\"#c9c6ac\", lw=2)\nplt.xlabel(\"x\"); plt.ylabel(\"y\")\nplt.title(f\"intercept={res.params[0]:.2f}, slope={res.params[1]:.2f}\")\n","id":"ols-line-02","skillIds":["ols.coefficient-interpretation"],"estimatedMinutes":10,"difficulty":"core","prerequisiteStageIds":["ols-line-01"]},{"type":"interactive","title":"Feel the fit","note":"Drag the sliders. Watch the line, the estimated slope, and its standard error react in real time — more noise means a fuzzier, less certain estimate.","params":[{"name":"slope","label":"True slope β₁","min":-5,"max":5,"step":0.1,"value":3},{"name":"noise","label":"Noise σ","min":0.1,"max":4,"step":0.1,"value":1}],"template":"\nimport numpy as np, statsmodels.api as sm, matplotlib.pyplot as plt\nrng = np.random.default_rng(0); n = 200\nx = rng.normal(0, 1, n)\ny = 2 + {{slope}} * x + rng.normal(0, {{noise}}, n)\nres = sm.OLS(y, sm.add_constant(x)).fit()\nplt.figure(figsize=(6, 3.7))\nplt.scatter(x, y, s=12, color=\"#da9100\", alpha=0.5)\nxs = np.linspace(x.min(), x.max(), 50)\nplt.plot(xs, res.params[0] + res.params[1]*xs, color=\"#c9c6ac\", lw=2)\nplt.title(f\"beta1_hat = {res.params[1]:.2f}   SE = {res.bse[1]:.3f}\")\n","id":"ols-line-03","skillIds":["ols.least-squares-geometry"],"estimatedMinutes":8,"difficulty":"core","prerequisiteStageIds":["ols-line-02"]},{"type":"quiz","title":"Check","prompt":"Holding n fixed, doubling the noise σ does what to the slope's standard error?","choices":["halves it","leaves it unchanged","roughly doubles it","quadruples it"],"answer":2,"hint":"The standard error of β̂₁ is proportional to σ.","explain":"SE(β̂₁) ∝ σ, so doubling σ roughly doubles the standard error — the slider shows exactly this.","id":"ols-line-04","skillIds":["ols.coefficient-interpretation"],"estimatedMinutes":5,"difficulty":"core","prerequisiteStageIds":["ols-line-03"],"variantId":"ols-line-04-core"},{"type":"truefalse","title":"Does a great fit prove the cause?","prompt":"You just fit the line and read off the summary. Claim: <b>a regression with R² = 0.92 has clearly captured the causal effect</b> of the regressor on <em>y</em>.","answer":false,"hint":"R² measures in-sample fit. Does fit say anything about whether x is correlated with the error?","explain":"This is the <b>high-R²-is-causal trap</b>. R² is only the fraction of <em>in-sample</em> variance the line reproduces — it says nothing about bias, omitted variables, or causation. A confounded regression (ability omitted from wage~schooling) can sit at R²≈0.9, while a perfectly unbiased causal estimate can have R²≈0.1. Boundary: R² and t-stats describe <em>fit and noise</em>; a causal claim needs E[ε|x]=0, which no in-sample statistic can certify. Whenever a defence of <em>causality</em> cites R² or a t-stat, suspect this error.","points":10,"id":"ols-line-05","skillIds":["ols.least-squares-geometry"],"estimatedMinutes":3,"difficulty":"core","prerequisiteStageIds":["ols-line-04"],"variantId":"ols-line-05-core"}]}}
