12. Simple Linear Regression

Course Administration and Examination Guidelines

  • Proctoring software behavior and environment controls:

    • Automated examination proctoring systems (such as the Lockdown Browser / "Rotter Brothers" environment) flag visual irregularities during monitoring.

    • Backlighting scenarios—where intense illumination originates from behind the student—darken facial skin tones on camera, triggering system warnings that the test-taker cannot be seen or has been cut out of frame.

    • Incidental room decor can trigger false violations; an incident occurred where a wall poster displaying a face-like mask shape was flagged by the software as an unauthorized secondary person in the room before manual verification cleared the incident.

    • Optimal examination setups require a well-lit testing environment with direct, front-facing illumination to eliminate automated software flags.

  • Examination curriculum structure:

    • Future examination questions and formats will be reshuffled from prior course offerings and historical question banks.

    • The scope of the final examination is non-cumulative regarding earlier foundational content; all topics evaluated on the midterm are completed, and the final exam exclusively tests concepts introduced from simple linear regression onward.

Fundamentals of Simple Linear Regression

  • Motivation and predictive modeling objectives:

    • Observed trends—such as plotting midterm exam grades against total hours invested in studying—illustrate that increased study duration corresponds to higher exam point totals.

    • The goal of statistical modeling is to summarize empirical data into a formal framework capable of producing deterministic predictions (e.g., forecasting an exact score or grade based on a prospective student's planned study hours).

  • Model definition and specifications:

    • Simple linear regression models the relationship between a single independent predictor variable xx and a continuous quantitative response variable yy.

    • The population relationship is formulated as a rigid straight-line equation:     y=β0+β1xy = \beta_0 + \beta_1 x

    • Predictor variable (xx): The known input measurement.

    • Response variable (yy): The quantitative output to be estimated.

    • Parameters (β0,β1\beta_0, \beta_1): Unknown population coefficients that dictate the geometric location and slope of the regression line in Euclidean space.

  • Application to advertising data:

    • Sales forecasting can be structured by linking revenue to marketing investments:     Sales=β0+β1×TV Budget\text{Sales} = \beta_0 + \beta_1 \times \text{TV Budget}

    • If the television advertising budget is known alongside estimates of β0\beta_0 and β1\beta_1, an exact sales prediction can be calculated.

  • Parameter estimation framework:

    • The true parameters β0\beta_0 and β1\beta_1 represent the unobservable underlying data-generating relationship.

    • Empirical estimates are designated as β^0\hat{\beta}_0 and β^1\hat{\beta}_1, yielding the fitted prediction value y^\hat{y}:     y^=β^0+β^1x\hat{y} = \hat{\beta}_0 + \hat{\beta}_1 x

    • Training datasets consist of discrete paired observations: (x1,y1),(x2,y2),…,(xn,yn)(x_1, y_1), (x_2, y_2), \dots, (x_n, y_n).

    • Parameter discovery approaches include:

    • Heuristic search / iterative fitting: Initializing arbitrary coefficient values, computing prediction errors, and updating coefficients iteratively until convergence.

    • Analytical closed-form solutions: Applying mathematical minimization techniques via the least squares criterion.

Least Squares Optimization and Coefficient Derivation

  • Residual definition and Residual Sum of Squares (RSS):

    • The prediction discrepancy for each observation is termed the residual (eie_i):     ei=yi−y^ie_i = y_i - \hat{y}_i

    • The least squares approach identifies parameter values that minimize the Residual Sum of Squares (RSS):     RSS=∑i=1nei2=∑i=1n(yi−y^i)2=∑i=1n(yi−β0−β1xi)2\text{RSS} = \sum_{i=1}^n e_i^2 = \sum_{i=1}^n (y_i - \hat{y}_i)^2 = \sum_{i=1}^n (y_i - \beta_0 - \beta_1 x_i)^2

  • Generative data framework with stochastic noise:

    • The empirical generative relationship includes a stochastic error component:     y=β0+β1x+ϵy = \beta_0 + \beta_1 x + \epsilon

    • The random disturbance ϵ\epsilon represents intrinsic system noise drawn from a normal distribution with a mean of zero and variance σ2\sigma^2:     ϵ∼N(0,σ2)\epsilon \sim N(0, \sigma^2)

  • Derivation of the intercept through expectations:

    • Applying the mathematical expectation operator across the generative model yields:     E(y)=E(β0+β1x+ϵ)=E(β0)+E(β1x)+E(ϵ)E(y) = E(\beta_0 + \beta_1 x + \epsilon) = E(\beta_0) + E(\beta_1 x) + E(\epsilon)

    • Because β0\beta_0 is a constant parameter, its expected value is itself (E(β0)=β0E(\beta_0) = \beta_0). By comparison, rolling a balanced six-sided die with faces {1,2,3,4,5,6}\{1, 2, 3, 4, 5, 6\} produces variable outcomes with an expected value equal to the mean:     1+2+3+4+5+66=3.5\frac{1 + 2 + 3 + 4 + 5 + 6}{6} = 3.5     A deterministic constant sequence consistently evaluates to that constant value.

    • Because the random error ϵ\epsilon has a mean of zero, its expected value vanishes:     E(ϵ)=0E(\epsilon) = 0

    • Factoring the constant slope β1\beta_1 out of the expectation gives:     E(β1x)=β1E(x)E(\beta_1 x) = \beta_1 E(x)

    • The expectation identity simplifies to:     E(y)=β0+β1E(x)E(y) = \beta_0 + \beta_1 E(x)

    • Rearranging provides the formula for the intercept:     β0=E(y)−β1E(x)\beta_0 = E(y) - \beta_1 E(x)

  • Derivation of the slope through covariance:

    • Expanding the covariance between the predictor xx and the response yy:     Cov(x,y)=Cov(x,β0+β1x+ϵ)=Cov(x,β0)+Cov(x,β1x)+Cov(x,ϵ)\text{Cov}(x, y) = \text{Cov}(x, \beta_0 + \beta_1 x + \epsilon) = \text{Cov}(x, \beta_0) + \text{Cov}(x, \beta_1 x) + \text{Cov}(x, \epsilon)

    • Covariance evaluates how two variables vary together; because β0\beta_0 is an invariant constant, it exhibits zero covariance with xx:     Cov(x,β0)=β0Cov(x,1)=0\text{Cov}(x, \beta_0) = \beta_0 \text{Cov}(x, 1) = 0

    • The random noise ϵ\epsilon is an intrinsic systemic component independent of the predictor xx, resulting in zero covariance:     Cov(x,ϵ)=0\text{Cov}(x, \epsilon) = 0

    • Factoring out β1\beta_1 leaves the covariance of xx with itself, which equals the variance of xx:     Cov(x,β1x)=β1Cov(x,x)=β1Var(x)\text{Cov}(x, \beta_1 x) = \beta_1 \text{Cov}(x, x) = \beta_1 \text{Var}(x)

    • Equating terms yields:     Cov(x,y)=β1Var(x)\text{Cov}(x, y) = \beta_1 \text{Var}(x)

    • Solving for the optimal slope parameter β1\beta_1:     β1=Cov(x,y)Var(x)\beta_1 = \frac{\text{Cov}(x, y)}{\text{Var}(x)}

  • Sample-level estimator formulas:

    • For a sample of nn observations, the sample variance of xx is:     Var(x)=1n∑i=1n(xi−xˉ)2\text{Var}(x) = \frac{1}{n} \sum_{i=1}^n (x_i - \bar{x})^2

    • The sample covariance between xx and yy is:     Cov(x,y)=1n∑i=1n(xi−xˉ)(yi−yˉ)\text{Cov}(x, y) = \frac{1}{n} \sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y})

    • Taking the ratio cancels the factor 1n\frac{1}{n}, yielding the standard least squares estimators:     β^1=∑i=1n(xi−xˉ)(yi−yˉ)∑i=1n(xi−xˉ)2\hat{\beta}_1 = \frac{\sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^n (x_i - \bar{x})^2}     β^0=yˉ−β^1xˉ\hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x}

    • In these equations, xˉ\bar{x} and yˉ\bar{y} denote the empirical sample means of the predictor and response variables.

Interpretation of Regression Coefficients

  • Empirical results from the advertising dataset:

    • Estimated intercept (β^0\hat{\beta}_0): 7.037.03

    • Estimated slope (β^1\hat{\beta}_1): 0.04750.0475

  • Physical interpretation of the intercept (β^0\hat{\beta}_0):

    • Represents the baseline expected value of yy when the predictor variable is zero (x=0x = 0).

    • In the context of marketing expenditure, investing 0 dollars0\,\text{dollars} in television advertising results in a baseline sales volume of 7.03 thousand units7.03\,\text{thousand units} (i.e., 7,030 units7{,}030\,\text{units} sold by default).

  • Physical interpretation of the slope (β^1\hat{\beta}_1):

    • Represents the rate of change or marginal difference in yy resulting from a single unit increase in xx.

    • Because expenditure is measured in thousands of dollars, an additional 1 unit1\,\text{unit} of investment (1,000 dollars1{,}000\,\text{dollars}, such as shifting budget from 50,000 dollars50{,}000\,\text{dollars} to 51,000 dollars51{,}000\,\text{dollars}, or from 250,000 dollars250{,}000\,\text{dollars} to 251,000 dollars251{,}000\,\text{dollars}) results in an increase in sales of 0.0475 thousand units0.0475\,\text{thousand units}.

    • Expressed in individual product quantities, every additional 1,000 dollars1{,}000\,\text{dollars} invested yields an increase of 47.5 units47.5\,\text{units} sold.

  • Structural constraints of linear modeling:

    • Linear regression is a rigid model that imposes a straight-line assumption on data distributions.

    • Real-world data points inevitably scatter around this straight line, producing unavoidable residual errors.

Optimization Landscapes and Gradient Descent

  • Geometric properties of the loss surface:

    • In an iterative optimization framework, all possible parameter pairings of (β0,β1)(\beta_0, \beta_1) form a two-dimensional domain plane.

    • Computing the Residual Sum of Squares (RSS) for each coordinate defines a third dimension, creating a three-dimensional bowl-shaped surface.

  • Optimization mechanics of gradient descent:

    • Parameters are initialized arbitrarily (e.g., β0=0\beta_0 = 0, β1=500\beta_1 = 500), which initially produces high RSS values.

    • The algorithm calculates the negative gradient using differential functions to establish the steepest path down the surface.

    • Iterative parameter updates move the system downward until settling at the global minimum (the base of the bowl), identifying the optimal coefficients β^0\hat{\beta}_0 and β^1\hat{\beta}_1.

Sampling Variability, Standard Errors, and Confidence Intervals

  • Population line versus sample fits:

    • The theoretical population regression line represents the true underlying data-generating model (e.g., f(x)=2+3xf(x) = 2 + 3x).

    • Real-world datasets represent finite samples from this population; fitting a model to a sample produces an estimated line that deviates from the true population line.

    • Drawing different random subsets of data yields an ensemble of distinct regression lines.

  • Analogy to the Central Limit Theorem:

    • Consider estimating a population bivariate mean vector μ=(0,0)\mu = (0, 0) using three separate random samples (e.g., sample 1 marked by red crosses, sample 2 marked by blue squares, and sample 3 marked by yellow diamonds).

    • Each individual sample yields a different sample mean, but the average across many sample means converges to the population mean.

  • Estimator variance and standard errors:

    • The dispersion of an estimator around the true population mean μ\mu is captured by its standard error squared:     Var(μ^)=SE(μ^)2=σ2n\text{Var}(\hat{\mu}) = \text{SE}(\hat{\mu})^2 = \frac{\sigma^2}{n}

    • The sampling variance of the regression coefficients is given by:     SE(β^0)2=σ2(1n+xˉ2∑i=1n(xi−xˉ)2)\text{SE}(\hat{\beta}_0)^2 = \sigma^2 \left( \frac{1}{n} + \frac{\bar{x}^2}{\sum_{i=1}^n (x_i - \bar{x})^2} \right)     SE(β^1)2=σ2∑i=1n(xi−xˉ)2\text{SE}(\hat{\beta}_1)^2 = \frac{\sigma^2}{\sum_{i=1}^n (x_i - \bar{x})^2}

  • Residual Standard Error (RSE) as an estimate of σ\sigma:

    • The true variance of the error term (σ2\sigma^2) is typically unknown and is estimated from the sample data using the Residual Standard Error (RSE):     RSE=σ=RSSn−2\text{RSE} = \sigma = \sqrt{\frac{\text{RSS}}{n - 2}}     σ2=RSSn−2\sigma^2 = \frac{\text{RSS}}{n - 2}

    • The denominator n−2n - 2 accounts for the degrees of freedom lost by estimating two parameters (β0\beta_0 and β1\beta_1).

  • Construction and interpretation of confidence intervals:

    • Coefficient estimates are commonly reported across a range rather than as isolated point values.

    • The 95% confidence interval for the slope parameter β1\beta_1 is approximated by:     [β^1−2×SE(β^1),  β^1+2×SE(β^1)][\hat{\beta}_1 - 2 \times \text{SE}(\hat{\beta}_1), \; \hat{\beta}_1 + 2 \times \text{SE}(\hat{\beta}_1)]

    • An example confidence interval for β0\beta_0 is [6.130,7.935][6.130, 7.935].

    • Interpretation: If 100 independent models were constructed using distinct samples from the population, 95 of the resulting confidence intervals would contain the true parameter value.

Hypothesis Testing and Statistical Significance

  • Formulation of statistical hypotheses:

    • Null Hypothesis (H0H_0): Predictor variable xx has no linear relationship with response variable yy:     H0:β1=0H_0: \beta_1 = 0

    • Alternative Hypothesis (HaH_a): A non-zero relationship exists between xx and yy:     Ha:β1≠0H_a: \beta_1 \neq 0

  • Mathematical implication of the null hypothesis:

    • Setting β1=0\beta_1 = 0 cancels the predictor term (β1x=0\beta_1 x = 0), reducing the model to a constant intercept (y=β0+ϵy = \beta_0 + \epsilon).

    • Under H0H_0, variation in xx does not influence yy.

  • The tt-statistic:

    • Evaluates how many standard errors the estimated slope β^1\hat{\beta}_1 lies away from zero:     t=β^1−0SE(β^1)t = \frac{\hat{\beta}_1 - 0}{\text{SE}(\hat{\beta}_1)}

    • When β^1\hat{\beta}_1 is close to zero, the numerator approaches zero, driving tt toward zero. Larger deviations from zero increase the magnitude of tt.

  • Derivation of the pp-value and decision thresholds:

    • The pp-value represents the probability of observing a test statistic equal to or greater in magnitude than ∣t∣|t| under the assumption that the null hypothesis (β1=0\beta_1 = 0) holds true.

    • Lower pp-values indicate a low probability that the observed coefficient occurred by chance under the null hypothesis, providing evidence to reject H0H_0.

    • Standard significance cutoffs include α=0.05\alpha = 0.05, α=0.005\alpha = 0.005, or α=0.001\alpha = 0.001.

    • When p<0.005p < 0.005, the null hypothesis is rejected, establishing that the linear relationship between xx and yy is statistically significant.

    • A significance level of 0.0050.005 implies that if 100 models were evaluated under H0H_0, an estimate of that magnitude would occur purely by chance only 0.5%0.5\% of the time (or 5 times out of 1,000).

Model Evaluation Metrics: RSE and R-squared

  • Residual Standard Error (RSE) evaluation:

    • Measures the absolute average deviation between observed outcomes and the fitted regression line:     RSE=RSSn−2\text{RSE} = \sqrt{\frac{\text{RSS}}{n - 2}}

    • In the TV advertising dataset, an RSE=3.26\text{RSE} = 3.26 indicates that model predictions deviate from actual sales by an average of 3.26 thousand units3.26\,\text{thousand units} (3,260 units3{,}260\,\text{units}).

    • Scale dependence and contextual interpretation of RSE:

    • For a small firm selling 5,0005{,}000 to 10,000 units10{,}000\,\text{units} monthly, a forecasting error of 3,260 units3{,}260\,\text{units} represents an error rate of roughly 30%30\% to 65%65\%, which indicates poor predictive performance.

    • For large-scale enterprises (e.g., Amazon) moving millions or billions of units, an error of 3,260 units3{,}260\,\text{units} is negligible, making an RSE=3.26\text{RSE} = 3.26 an acceptable fit.

    • Because RSE is measured in the units of the response variable, its magnitude cannot be evaluated without direct knowledge of the scale of the dataset.

  • Coefficient of Determination (R2R^2 Statistic):

    • Provides a standardized, unitless metric bounded strictly between 00 and 11:     R2=1−RSSTSSR^2 = 1 - \frac{\text{RSS}}{\text{TSS}}

    • Total Sum of Squares (TSS) measures the total inherent variability in the response variable around its mean:     TSS=∑i=1n(yi−yˉ)2\text{TSS} = \sum_{i=1}^n (y_i - \bar{y})^2

    • Informational interpretation:

    • Variance represents information; an invariant variable contains no information.

    • TSS represents the total variance present in the response variable, while RSS represents the variance left unexplained by the regression model.

    • If the model performs poorly, the residual errors approach the total variability (RSS≈TSS\text{RSS} \approx \text{TSS}), driving the ratio RSSTSS\frac{\text{RSS}}{\text{TSS}} toward 11 and yielding R2≈0R^2 \approx 0.

    • If the model fits the data well, the residual sum of squares is small relative to total variation (RSS≪TSS\text{RSS} \ll \text{TSS}), driving the ratio toward 00 and yielding R2≈1R^2 \approx 1.

    • An R2R^2 value of 0.80.8 indicates that the regression model explains 80%80\% of the variance present in the response variable.

Questions & Discussion

  • Prompt regarding parameter nullity:

    • Question: If β1=0\beta_1 = 0 while β0\beta_0 remains a non-zero value, what does this state about the relationship between xx and yy?

  • Student proposal 1:

    • Student Response: Does it mean that the covariance of xx and yy is zero?

    • Instructor Clarification: Covariance is not the direct answer; the inquiry focuses on the model equation itself.

  • Student proposal 2:

    • Student Response: There is no relationship between them.

    • Instructor Explanation: Setting β1=0\beta_1 = 0 removes the predictor term (0×x=00 \times x = 0), meaning xx contributes nothing to the computation of yy. Consequently, yy equals the constant intercept β0\beta_0 regardless of the value taken by xx, establishing that yy is completely independent of xx.

Computational Implementation and Laboratory Work

  • Scope of the laboratory assignment:

    • Part 1: Implement simple linear regression from scratch by coding closed-form formulas to compute β^0\hat{\beta}_0 and β^1\hat{\beta}_1.

    • Part 2: Extend the manual implementation to multiple linear regression, supporting multiple simultaneous predictor variables (x1,x2,…,xpx_1, x_2, \dots, x_p) and their corresponding coefficients.

  • Integration with scikit-learn:

    • Models must also be fitted using the LinearRegression implementation in the scikit-learn library.

    • The automated library output reports model parameters, the R2R^2 score, tt-statistics, and pp-values.

    • Students are required to compare and contrast the numerical outputs of their manual implementations against the fitted results produced by scikit-learn.