Curve Fitting · Least Squares Criterion · Fitting a Straight Line · Normal Equations · Worked Examples
Unit IV · Lecture 24 of 40 · B.Sc. Data Science, 1st Year, Semester II
Correlation & Regression
Unit IV · Lecture 24
The Journey — Lecture 24
📐
What is Curve Fitting? Motivation · Scatter plots · Finding the "best" curve through data
⚖️
The Principle of Least Squares Why squared errors? · Minimisation · Historical context
📏
Fitting a Straight Line y = a + bx Normal equations · Derivation of a and b · Shortcut formulas
✏️
Worked Examples & Interactive Tools Step-by-step problems · Visual line fitting · Quiz
Correlation & Regression
Unit IV · Lecture 24
🤔 What is Curve Fitting?
📖 Formal Definition
Curve fitting is the process of finding a mathematical function (curve) that best fits a set of observed data points. The goal is to capture the underlying relationship between two variables (x and y) in a compact mathematical form, so we can describe, predict, and understand the data.
💡 Analogy: Connecting the Dots
Imagine you have plotted several points on a graph paper — each point represents an observation (xᵢ, yᵢ). The points don't fall on a perfect line or curve. Curve fitting is like drawing the "best possible line" through these scattered points — the line that passes as close as possible to all points simultaneously.
📖 Why Do We Fit Curves?
1. Description: Summarise the relationship between variables in a simple equation.
2. Prediction: Estimate y for values of x not observed in the data.
3. Interpolation: Estimate values between observed data points.
4. Extrapolation: Estimate values beyond the observed range (with caution).
5. Understanding: Identify trends, patterns, and the nature of the relationship.
📖 Common Curves to Fit
Straight line: y = a + bx (most common)
Parabola: y = a + bx + cx²
Exponential: y = abˣ
Power curve: y = axᵇ
Logarithmic: y = a + b·ln(x)
Today we focus on the straight line — the simplest and most widely used.
Correlation & Regression
Unit IV · Lecture 24
🎯 The Problem: Which Line is "Best"?
📖 The Challenge
Given n data points (x₁,y₁), (x₂,y₂), …, (xₙ,yₙ), there are infinitely many straight lines that can be drawn through or near the data. We need a criterion to select the one "best" line. Different criteria give different "best" lines.
📖 Possible Criteria
1. Minimise Σ|yᵢ − ŷᵢ| (sum of absolute deviations)
→ Gives the MAD-based line. Hard to solve analytically.
2. Minimise Σ(yᵢ − ŷᵢ)² (sum of squared deviations)
→ Least Squares! Easy to solve, elegant mathematics.
3. Minimise max|yᵢ − ŷᵢ| (maximum deviation)
→ Minimax criterion. Used in some engineering applications.
4. Minimise Σ|yᵢ − ŷᵢ|ᵖ for other values of p
→ Generalised criterion. p = 2 is the most common.
💡 Why Squares Win
The least squares criterion (option 2) dominates because:
1. Mathematical elegance: Squaring makes the function differentiable everywhere (unlike |x|), allowing us to use calculus to find the minimum.
2. Unique solution: The least squares problem always has a unique, closed-form solution.
3. Statistical optimality: Under certain conditions (normal errors), least squares gives the best possible estimates (Gauss-Markov theorem).
4. Penalises large errors: Squaring gives more weight to large deviations, which is often desirable.
Correlation & Regression
Unit IV · Lecture 24
⚖️ The Principle of Least Squares — Stated
📖 Formal Statement
The Principle of Least Squares states that, among all possible curves of a given type, the best-fitting curve is the one for which the sum of the squares of the residuals (vertical deviations of observed values from the curve) is minimum.
Minimise: S = Σeᵢ² = Σ(yᵢ − ŷᵢ)²
Where:
• yᵢ = observed value of y for the i-th data point
• ŷᵢ = predicted (fitted) value from the curve
• eᵢ = yᵢ − ŷᵢ = residual (error) for the i-th point
• S = sum of squared residuals (to be minimised)
📖 Historical Note
The method of least squares was independently developed by Adrien-Marie Legendre (1805) and Carl Friedrich Gauss (1809, though he claimed to have used it since 1795). Gauss used it to predict the orbit of the asteroid Ceres. The method became the foundation of modern regression analysis and remains the most widely used curve-fitting technique in science and engineering.
💬 Famous Quote:"The method of least squares is the jewel in the crown of statistics." — It combines mathematical elegance with practical utility in a way few other methods can match.
Correlation & Regression
Unit IV · Lecture 24
🎬 Visual: Least Squares in Action
Click "Fit Line" to see the least squares line drawn through sample data points. The residuals (vertical red lines) show the errors being minimised:
Blue dots = observed data | Red line = least squares fit | Gray lines = residuals (errors)
Correlation & Regression
Unit IV · Lecture 24
📏 Fitting a Straight Line: y = a + bx
📖 Problem Setup
We want to fit a straight line ŷ = a + bx to n data points (x₁,y₁), (x₂,y₂), …, (xₙ,yₙ) using the least squares criterion.
The residual for the i-th point is: eᵢ = yᵢ − (a + bxᵢ)
We need to find a (intercept) and b (slope) that minimise:
S = Σ[yᵢ − (a + bxᵢ)]²
📖 What Do a and b Mean?
b (slope): For every one-unit increase in x, y changes by b units on average. If b > 0, y increases with x (positive relationship). If b < 0, y decreases with x (negative relationship).
a (intercept): The predicted value of y when x = 0. It is the point where the line crosses the y-axis. (Note: a may not always have a meaningful interpretation if x = 0 is outside the data range.)
Correlation & Regression
Unit IV · Lecture 24
📐 Derivation: Normal Equations
📖 Minimisation Using Calculus
To minimise S = Σ[yᵢ − a − bxᵢ]², we take partial derivatives with respect to a and b, and set them to zero:
∂S/∂a = −2Σ(yᵢ − a − bxᵢ) = 0 ∂S/∂b = −2Σxᵢ(yᵢ − a − bxᵢ) = 0
These two equations in two unknowns (a and b) are called the normal equations. Solving them simultaneously gives the least squares estimates.
💡 Why "Normal" Equations? The term "normal" here means "perpendicular" — the equations arise from requiring the residual vector to be orthogonal (normal) to the column space of the design matrix. This is a geometric interpretation from linear algebra.
For every unit increase in x, y increases by 0.6 units on average. When x = 0, the predicted y is 2.2.
Correlation & Regression
Unit IV · Lecture 24
✏️ Example 1: Verification & Predictions
📖 Verification
Check 1: Residuals sum to zero
x
y
ŷ = 2.2+0.6x
Residual (y−ŷ)
1
2
2.8
−0.8
2
4
3.4
+0.6
3
5
4.0
+1.0
4
4
4.6
−0.6
5
5
5.2
−0.2
Sum of residuals =
0.0 ✓
📖 Check 2: Line passes through (x̄, ȳ)
At x = x̄ = 3: ŷ = 2.2 + 0.6(3) = 2.2 + 1.8 = 4.0 = ȳ ✓
Predictions:
At x = 6: ŷ = 2.2 + 0.6(6) = 2.2 + 3.6 = 5.8 (extrapolation)
At x = 2.5: ŷ = 2.2 + 0.6(2.5) = 2.2 + 1.5 = 3.7 (interpolation)
💡 Sum of Squared Residuals: S = (−0.8)² + (0.6)² + (1.0)² + (−0.6)² + (−0.2)² = 0.64 + 0.36 + 1.0 + 0.36 + 0.04 = 2.4. This is the minimum possible sum of squared errors for any straight line through this data.
Correlation & Regression
Unit IV · Lecture 24
✏️ Example 2: Advertising vs Sales
📝 Problem
A company records advertising expenditure (₹ lakhs) and sales (₹ lakhs). Fit a regression line and predict sales when advertising = ₹7 lakhs.
When x = 7: ŷ = 6.6 + 3.83(7) = 6.6 + 26.81 = ₹33.41 lakhs
For every ₹1 lakh increase in advertising, sales increase by approximately ₹3.83 lakhs.
Correlation & Regression
Unit IV · Lecture 24
🎬 Interactive: Least Squares Line Fitter
Enter paired (x, y) data to compute the least squares line, see the equation, and visualise the fit:
Correlation & Regression
Unit IV · Lecture 24
🔑 Properties of Least Squares Estimates
The line always passes through (x̄, ȳ): Substituting x = x̄ into ŷ = a + bx gives ŷ = ȳ. This is a fundamental property of the least squares line.
Residuals sum to zero: Σ(yᵢ − ŷᵢ) = 0. The positive and negative residuals cancel out exactly.
Residuals are uncorrelated with x: Σxᵢ(yᵢ − ŷᵢ) = 0. The residuals have no linear relationship with the predictor.
Unique solution: As long as not all x values are identical (i.e., Σ(xᵢ−x̄)² ≠ 0), the solution for a and b is unique.
Unbiased estimates: If the true relationship is y = α + βx and errors have zero mean, then E[b] = β and E[a] = α. The estimates are centred on the true values.
Minimum variance (Gauss-Markov): Under certain conditions, least squares estimates have the smallest variance among all linear unbiased estimates — they are BLUE (Best Linear Unbiased Estimator).
Sensitive to outliers: Because errors are squared, a single outlier can dramatically shift the fitted line. This is both a strength (detects influential points) and a weakness.
Only minimises vertical deviations: Least squares minimises the sum of squared vertical distances (yᵢ − ŷᵢ), not perpendicular distances. If you want to minimise perpendicular distances, use orthogonal regression (total least squares).
Correlation & Regression
Unit IV · Lecture 24
🔑 Summary: Principle of Least Squares
Curve fitting finds the mathematical function that best represents the relationship in observed data.
The Principle of Least Squares selects the curve that minimises the sum of squared residuals: S = Σ(yᵢ − ŷᵢ)².
For a straight line ŷ = a + bx, the normal equations are: Σy = na + bΣx and Σxy = aΣx + bΣx².
b = [nΣxy − ΣxΣy] / [nΣx² − (Σx)²] and a = ȳ − bx̄.
The line always passes through (x̄, ȳ) and residuals always sum to zero.
Least squares estimates are BLUE — Best Linear Unbiased Estimators (Gauss-Markov theorem).
The method was developed by Legendre (1805) and Gauss (1809).
Limitation: Sensitive to outliers because squaring amplifies large errors.
Correlation & Regression
Unit IV · Lecture 24
🧠 Quick Quiz — Lecture 24
Q1: The least squares method minimises the sum of:
A) Absolute deviations
B) Squared deviations
C) Maximum deviation
D) Percentage errors
Q2: The least squares line always passes through:
A) The origin (0, 0)
B) The point (x̄, ȳ)
C) The point (a, b)
D) The median point
Q3: If b = 0 in the fitted line ŷ = a + bx, this means:
A) The line passes through the origin
B) There is no linear relationship between x and y
C) All y values are zero
D) The fit is perfect
Q4: The sum of residuals in a least squares fit is always:
A) Positive
B) Zero
C) Negative
D) Equal to S
Q5: Who is credited with publishing the method of least squares first?
A) Carl Friedrich Gauss
B) Adrien-Marie Legendre
C) Karl Pearson
D) Francis Galton
Q6: The slope b in ŷ = a + bx represents:
A) The value of y when x = 0
B) The change in y for a one-unit change in x
C) The correlation between x and y
D) The mean of y
Q7: Least squares estimates are BLUE. This stands for:
A) Basic Linear Unbiased Estimate
B) Best Linear Unbiased Estimator
C) Biased Linear Unbiased Error
D) Best Least Unit Estimate
Q8: A limitation of least squares is:
A) It does not give a unique solution
B) It is sensitive to outliers
C) It can only fit curved lines
D) It requires the data to be normally distributed
Correlation & Regression
Unit IV · Lecture 24
📝 Homework & Practice
📝 Practice Tasks
1. Fit a least squares line to: x = 2, 4, 6, 8, 10 and y = 3, 7, 5, 11, 9. Predict y when x = 7.
2. The data below shows study hours and exam scores. Fit a regression line and interpret the slope.
Hours (x): 1, 2, 3, 4, 5, 6
Score (y): 45, 50, 55, 60, 65, 70
3. For the fitted line ŷ = 10 + 2.5x, interpret the values 10 and 2.5 in the context of production cost (y) and units produced (x).
4. Verify that the least squares line from Problem 1 passes through (x̄, ȳ).
5. Explain why the least squares method uses squared deviations instead of absolute deviations. List two mathematical advantages.
6. If Σx = 30, Σy = 50, Σxy = 200, Σx² = 150, and n = 6, find the least squares estimates a and b.
7. BONUS: Show that the least squares line ŷ = a + bx always passes through (x̄, ȳ). (Hint: Substitute x = x̄ into the equation and use a = ȳ − bx̄.)
Correlation & Regression
Unit IV · Lecture 24
Thank You!
Upcoming
Lecture 25: Fitting of Polynomials — Second-degree parabola and higher Lecture 26: Correlation — Karl Pearson's Coefficient Lecture 27: Rank Correlation — Spearman's Coefficient
Unit IV · Lecture 24 · BDS113 Introduction to Statistics · B.Sc. Data Science, Semester II