🏠 Home
Correlation & Regression
Unit IV · Lecture 25

BDS113 — Introduction to Statistics

Fitting of Polynomials

Second-Degree Parabola · Normal Equations · Higher-Degree Polynomials · Choosing the Right Model


Unit IV · Lecture 25 of 40 · B.Sc. Data Science, 1st Year, Semester II

Correlation & Regression
Unit IV · Lecture 25

The Journey — Lecture 25


📐
When a Straight Line is Not Enough
Curved patterns · Residual analysis · Motivation for polynomials
📏
Fitting a Second-Degree Parabola
y = a + bx + cx² · Normal equations · Step-by-step method
✏️
Worked Examples
Parabola fitting · Interactive visualiser · Higher-degree overview
⚖️
Model Selection & Quiz
When to use which degree · Overfitting · Properties · Practice quiz
Correlation & Regression
Unit IV · Lecture 25

🤔 When a Straight Line is Not Enough


📖 The Problem

In the previous lecture, we fitted a straight line ŷ = a + bx using least squares. But many real-world relationships are not linear — they are curved. If we force a straight line through curved data, the fit will be poor and predictions unreliable.


💡 Analogy: Fitting Shoes

A straight line is like a one-size-fits-all shoe — it works for some feet (linear data) but pinches others (curved data). A polynomial is like a custom-moulded shoe — it adapts to the shape of your foot (the data pattern). The trick is to use the simplest shoe that fits well — you don't need a custom boot for a simple walk.


📖 Signs That You Need a Polynomial

1. Residual pattern: If residuals from a linear fit show a systematic U-shape or inverted U-shape, the data has curvature.

2. Scatter plot: If the scatter plot shows a clear curved trend (parabolic, S-shaped, etc.).

3. Domain knowledge: Physics, biology, economics often have inherent non-linear relationships (e.g., projectile motion is parabolic).

4. R² improvement: If adding a quadratic term significantly improves the fit (higher R², lower residual sum of squares).

📖 Common Polynomial Models

Degree 1 (Linear): y = a + bx
→ Straight line. 2 parameters.

Degree 2 (Quadratic/Parabola): y = a + bx + cx²
→ U-shape or inverted U. 3 parameters.

Degree 3 (Cubic): y = a + bx + cx² + dx³
→ S-shape or more complex curves. 4 parameters.

Degree k: y = a₀ + a₁x + a₂x² + … + aₖxᵏ
→ (k+1) parameters. Requires at least (k+1) data points.

Correlation & Regression
Unit IV · Lecture 25

📏 Fitting a Second-Degree Parabola: y = a + bx + cx²


📖 Problem Setup

We want to fit a second-degree polynomial (parabola) to n data points using the least squares criterion:

Minimise: S = Σ[yᵢ − (a + bxᵢ + cxᵢ²)]²

We need to find three unknowns: a, b, and c. This requires three normal equations.


📖 The Three Normal Equations

Taking ∂S/∂a = 0, ∂S/∂b = 0, ∂S/∂c = 0:

Σy = na + bΣx + cΣx²    … (1)

Σxy = aΣx + bΣx² + cΣx³    … (2)

Σx²y = aΣx² + bΣx³ + cΣx⁴    … (3)

Solving these three simultaneous equations gives the least squares estimates of a, b, and c.


💡 Pattern Recognition: Notice the pattern — each equation corresponds to multiplying the basic equation y = a + bx + cx² by 1, x, and x² respectively, then summing. For a degree-k polynomial, you multiply by 1, x, x², …, xᵏ to get (k+1) normal equations.
Correlation & Regression
Unit IV · Lecture 25

🪜 Step-by-Step: Fitting a Parabola


1 Create the computation table: Columns for x, y, xy, x²y, x², x³, x⁴.
2 Compute all sums: Σx, Σy, Σxy, Σx²y, Σx², Σx³, Σx⁴, and n.
3 Set up the three normal equations by substituting the sums.
4 Solve the three simultaneous equations for a, b, and c (by elimination, substitution, or matrix methods).
5 Write the equation: ŷ = a + bx + cx². Interpret the coefficients.
6 Verify: Check residuals and compute the residual sum of squares.

💡 Minimum Data Points: To fit a degree-k polynomial, you need at least k+1 data points. For a parabola (k=2), you need at least 3 points. In practice, you should have significantly more points than parameters for a reliable fit.
Correlation & Regression
Unit IV · Lecture 25

✏️ Example 1: Fitting a Parabola


📝 Problem

Fit a second-degree parabola y = a + bx + cx² to the following data:

x12345
y26122030

📖 Solution — Step 1–2: Computation Table

xyxyx²yx²x³x⁴
1222111
2612244816
3123610892781
420803201664256
53015075025125625
1570280120455225979

📖 Step 3: Normal Equations

n = 5, Σx = 15, Σy = 70, Σxy = 280, Σx²y = 1204, Σx² = 55, Σx³ = 225, Σx⁴ = 979

(1): 70 = 5a + 15b + 55c
(2): 280 = 15a + 55b + 225c
(3): 1204 = 55a + 225b + 979c


📖 Step 4: Solving the System

From (1): 14 = a + 3b + 11c   →   a = 14 − 3b − 11c   … (1')

Substitute (1') into (2): 280 = 15(14−3b−11c) + 55b + 225c
280 = 210 − 45b − 165c + 55b + 225c
70 = 10b + 60c   →   7 = b + 6c   … (4)

Substitute (1') into (3): 1204 = 55(14−3b−11c) + 225b + 979c
1204 = 770 − 165b − 605c + 225b + 979c
434 = 60b + 374c   … (5)

From (4): b = 7 − 6c. Substitute into (5):
434 = 60(7−6c) + 374c = 420 − 360c + 374c
14 = 14c   →   c = 1
b = 7 − 6(1) = 1
a = 14 − 3(1) − 11(1) = 0


Fitted Parabola

ŷ = x + x²

Or equivalently: ŷ = x(1 + x). The data follows a perfect quadratic pattern! (This is because y = x + x² generates the values 2, 6, 12, 20, 30 exactly.)

Correlation & Regression
Unit IV · Lecture 25

✏️ Example 1: Verification


xy (observed)ŷ = x + x²Residual
121 + 1 = 20
262 + 4 = 60
3123 + 9 = 120
4204 + 16 = 200
5305 + 25 = 300
SSE =0

Perfect Fit!

All residuals are zero. SSE = 0.

This happens because the data was generated by a perfect quadratic function. In real-world data, residuals will never be exactly zero — but a good polynomial fit makes them small and randomly scattered.


💡 Comparison with Linear Fit: If we had fitted a straight line to this data, we would get ŷ = −2 + 6.4x, with SSE = 11.2 — a much worse fit. The parabola captures the curvature that the line misses.
Correlation & Regression
Unit IV · Lecture 25

✏️ Example 2: Production Cost vs Output


📝 Problem

A factory records output (x, in hundreds of units) and total cost (y, in ₹ thousands). Fit a parabola y = a + bx + cx².

x123456
y152022253038

xyxyx²yx²x³x⁴
1151515111
22040804816
3226619892781
4251004001664256
53015075025125625
6382281368362161296
211505992811914412275

📖 Normal Equations

(1): 150 = 6a + 21b + 91c
(2): 599 = 21a + 91b + 441c
(3): 2811 = 91a + 441b + 2275c

Solving (by elimination):
a ≈ 14.29, b ≈ −1.57, c ≈ 0.89


Fitted Parabola

ŷ = 14.29 − 1.57x + 0.89x²

The positive coefficient of x² (c = 0.89) indicates increasing marginal cost — as output increases, each additional unit costs more to produce. This is a classic pattern in manufacturing economics.

Correlation & Regression
Unit IV · Lecture 25

🎬 Interactive: Parabola Fitter


Enter data to fit both a straight line and a parabola, and compare visually:






Correlation & Regression
Unit IV · Lecture 25

📐 General Polynomial Fitting


📖 General Form

A polynomial of degree k has the form:

y = a₀ + a₁x + a₂x² + a₃x³ + … + aₖxᵏ

This has (k+1) unknown coefficients and requires (k+1) normal equations.


📖 The (k+1) Normal Equations

The j-th normal equation (j = 0, 1, 2, …, k) is obtained by multiplying y = a₀ + a₁x + … + aₖxᵏ by xʲ and summing:

Σxʲy = a₀Σxʲ + a₁Σxʲ⁺¹ + a₂Σxʲ⁺² + … + aₖΣxʲ⁺ᵏ

This gives a system of (k+1) linear equations in (k+1) unknowns, which can be written in matrix form as:

[Σx⁰ Σx¹ … Σxᵏ ] [a₀] [Σy ]
[Σx¹ Σx² … Σxᵏ⁺¹] [a₁] = [Σxy ]
[ ⋮ ⋮ ⋱ ⋮ ] [⋮ ] [ ⋮ ]
[Σxᵏ Σxᵏ⁺¹ … Σx²ᵏ] [aₖ] [Σxᵏy]


💡 Matrix Notation: This is X'Xa = X'y, where X is the design matrix, a is the coefficient vector, and y is the response vector. The solution is a = (X'X)⁻¹X'y — the standard formula from linear regression.
Correlation & Regression
Unit IV · Lecture 25

📐 Cubic Fit: y = a + bx + cx² + dx³


📖 Four Normal Equations

Σy = na + bΣx + cΣx² + dΣx³
Σxy = aΣx + bΣx² + cΣx³ + dΣx⁴
Σx²y = aΣx² + bΣx³ + cΣx⁴ + dΣx⁵
Σx³y = aΣx³ + bΣx⁴ + cΣx⁵ + dΣx⁶


📖 When to Use Cubic

1. The scatter plot shows an S-shape or inflection point.

2. The quadratic fit leaves systematic residual patterns.

3. The data has a change in curvature (concave up then concave down, or vice versa).

4. Domain knowledge suggests a cubic relationship (e.g., volume vs. side length).

📖 Computation Requirements

Table columns needed: x, y, xy, x²y, x³y, x², x³, x⁴, x⁵, x⁶

Sums needed: Σx through Σx⁶, Σy, Σxy, Σx²y, Σx³y

Minimum data points: 4 (but 10+ recommended)

Equations to solve: 4 simultaneous equations

In practice, cubic and higher fits are computed using software (Python, R, Excel) rather than by hand.

Correlation & Regression
Unit IV · Lecture 25

🎯 Choosing the Right Polynomial Degree


📖 The Overfitting Problem

A polynomial of degree (n−1) can pass through all n data points exactly (SSE = 0). But this "perfect" fit is useless for prediction — it captures noise, not the true relationship. This is called overfitting.


✅ Guidelines for Choosing Degree

1. Start with degree 1 (linear). If residuals show no pattern, stop.

2. Try degree 2 (quadratic) if residuals show curvature. If the improvement is significant and c is statistically significant, use it.

3. Try degree 3 only if degree 2 still shows systematic residuals.

4. Prefer the simplest model that adequately fits the data. This is the principle of parsimony (Occam's Razor).

5. Never use degree ≥ (n−1) — it will overfit.

⚠️ Signs of Overfitting

1. The fitted curve oscillates wildly between data points.

2. Adding more degree barely reduces SSE but makes the curve more complex.

3. Predictions outside the data range are wildly unreasonable.

4. The coefficients are very large with alternating signs.

5. The model performs poorly on new, unseen data.


💬 Rule of Thumb: In most practical applications, a degree 1 or 2 polynomial is sufficient. Degree 3 and higher are rarely needed unless the data clearly demands it. When in doubt, prefer simplicity.
Correlation & Regression
Unit IV · Lecture 25

⚖️ Linear vs Quadratic vs Cubic — Comparison


FeatureLinear (Degree 1)Quadratic (Degree 2)Cubic (Degree 3)
Equationy = a + bxy = a + bx + cx²y = a + bx + cx² + dx³
Parameters2 (a, b)3 (a, b, c)4 (a, b, c, d)
Normal equations234
ShapeStraight lineU-shape or inverted US-shape or complex curve
Min. data points234
ComputationEasy (by hand)Moderate (by hand)Hard (use software)
Overfitting riskLowModerateHigher
Best forLinear trendsCurved trends with one bendTrends with two bends (inflection)

💡 Parsimony Principle: Always prefer the simplest model (lowest degree) that adequately captures the data pattern. A model with fewer parameters is easier to interpret, less likely to overfit, and more robust for predictions.
Correlation & Regression
Unit IV · Lecture 25

🔑 Properties of Polynomial Least Squares


Correlation & Regression
Unit IV · Lecture 25

🔑 Summary: Fitting of Polynomials


Correlation & Regression
Unit IV · Lecture 25

🧠 Quick Quiz — Lecture 25


Q1: To fit a second-degree polynomial, you need at least:

A) 2 data points
B) 3 data points
C) 4 data points
D) 5 data points

Q2: How many normal equations are needed for a cubic polynomial?

A) 2
B) 3
C) 4
D) 6

Q3: The second normal equation for a parabola y = a + bx + cx² is:

A) Σy = na + bΣx + cΣx²
B) Σxy = aΣx + bΣx² + cΣx³
C) Σx²y = aΣx² + bΣx³ + cΣx⁴
D) Σxy = a + bx + cx²

Q4: Overfitting occurs when:

A) The polynomial degree is too low
B) The polynomial degree is too high, capturing noise
C) There are too few data points
D) The data is linear

Q5: A polynomial of degree (n−1) through n data points will:

A) Always underfit
B) Pass through all points exactly (SSE = 0)
C) Give the best predictions
D) Be a straight line

Q6: The principle of parsimony suggests:

A) Always use the highest degree polynomial
B) Use the simplest model that adequately fits the data
C) Always use a linear model
D) Use as many parameters as data points

Q7: If the coefficient c in y = a + bx + cx² is positive, the parabola opens:

A) Upward (U-shape)
B) Downward (inverted U)
C) To the right
D) It's a straight line

Q8: Why is extrapolation with polynomials dangerous?

A) Polynomials are always linear
B) Polynomials can behave wildly outside the data range
C) Polynomials cannot make predictions
D) Polynomials only work for integer values
Correlation & Regression
Unit IV · Lecture 25

📝 Homework & Practice


📝 Practice Tasks

1. Fit a parabola y = a + bx + cx² to: x = 1, 2, 3, 4, 5 and y = 1, 4, 9, 16, 25. What do you notice?

2. Fit a second-degree polynomial to the following data and predict y at x = 6:
   x: 1, 2, 3, 4, 5
   y: 5, 8, 13, 20, 29

3. For the data in Problem 2, also fit a straight line. Compare the SSE of both models. Which is better?

4. Write down the four normal equations needed to fit a cubic polynomial y = a + bx + cx² + dx³.

5. Explain why a polynomial of degree (n−1) always gives SSE = 0 for n data points, and why this is not desirable.

6. A scatter plot shows data that curves upward on both sides (U-shape). What degree polynomial would you try first? Why?

7. BONUS: For the parabola y = a + bx + cx², show that the vertex (minimum or maximum point) is at x = −b/(2c). Use this to find the vertex of ŷ = 14.29 − 1.57x + 0.89x² from Example 2.

Correlation & Regression
Unit IV · Lecture 25

Thank You!


Upcoming

Lecture 26: Correlation — Karl Pearson's Coefficient of Correlation
Lecture 27: Rank Correlation — Spearman's Coefficient
Lecture 28: Regression Lines & Their Properties

Unit IV · Lecture 25 · BDS113 Introduction to Statistics · B.Sc. Data Science, Semester II