Fitting a Multiple Linear Regression Model Using the Normal Equation

Last updated

Problem Setup

Scenario:Estimate a restaurant's weekly revenue based on its seating capacity and average customer rating.

Given:Three historical restaurant records containing seating capacity, average rating, and the corresponding weekly revenue in hundreds of pounds.

Task:Use multiple linear regression to model weekly revenue from both seating capacity and average rating, then estimate the weekly revenue for a restaurant with 45 seats and an average rating of 4.1.

Need a quick refresher? Review Multiple Linear theory

Restaurant Capacity, Rating & Revenue

Each row contains a restaurant's seating capacity, average rating, and weekly revenue in hundreds of pounds. These observations are used to fit a regression model with two input features.

Data Point
Seating_Capacity✎
Avg_Rating✎
Weekly_Revenue_hundreds
P1
35
4
14
P2
40
3.8
15
P3
25
3.2
10
Target
45
4.1
?

Step-by-Step Solution

Step 1: Extract X and Y Matrices

Formula: X=[1x11x121x21x22…],Y=[y1y2…]X = \begin{bmatrix} 1 & x_{11} & x_{12} \\ 1 & x_{21} & x_{22} \\ \dots \end{bmatrix}, \quad Y = \begin{bmatrix} y_1 \\ y_2 \\ \dots \end{bmatrix}
Matrix X (with 1s)
1354
1403.8
1253.2
Matrix Y
14
15
10

Step 2: Calculate Transpose (XTX^T)

XTX^T
111
354025
43.83.2

Step 3: Multiply (XT⋅XX^T \cdot X)

(XT⋅X)(X^T \cdot X)
310011
1003450372
1137240.68

Step 4: Find Inverse (XT⋅X)−1(X^T \cdot X)^{-1}

(XT⋅X)−1(X^T \cdot X)^{-1}
54.50.667-20.833
0.6670.029-0.444
-20.833-0.4449.722

Step 5: Multiply Inverse by XTX^T

(XT⋅X)−1⋅XT(X^T \cdot X)^{-1} \cdot X^T
-5.524.5
-0.10.133-0.033
2.5-1.667-0.833

Step 6: Calculate Regression Coefficients

Formula: B=[(XT⋅X)−1⋅XT]⋅YB = [(X^T \cdot X)^{-1} \cdot X^T] \cdot Y
Matrix BB
-2
0.267
1.667

Intercept (b0b_0) : -2

Weight for X1X_{1} (b1b_{1}) : 0.267

Weight for X2X_{2} (b2b_{2}) : 1.667

Step 7: Calculate Target Prediction

Formula: Y=b0+b1X1+b2X2+⋯+bnXnY = b_0 + b_1X_1 + b_2X_2 + \dots + b_nX_n
Calculating the Target Prediction

Line: Y≈−2+(0.267×45)+(1.667×4.1)Y \approx -2 + (0.267 \times 45) + (1.667 \times 4.1)

Y≈16.833Y \approx 16.833

* Note: Intermediate coefficients are displayed in rounded form. The final prediction is calculated using full precision.

Final Answer

Prediction:16.833 hundred GBP

Interpretation:The model estimates weekly revenue using both seating capacity and average rating. This estimate is based on only three historical records, so it should not be treated as a reliable forecast of actual revenue.

Exam Takeaway

  • Keep coefficient order consistent: Each coefficient corresponds to a specific column of the design matrix. The first coefficient belongs to the intercept column, and the remaining coefficients follow the order of the input features. Mixing this order produces incorrect predictions.
  • Check when the normal equation is applicable: The inverse-based formula β=(XTX)−1XTY\beta = (X^TX)^{-1}X^TY requires XTXX^TX to be invertible. If the feature columns are linearly dependent, the inverse does not exist, and another method such as the pseudoinverse is needed.
  • A perfect fit does not guarantee reliable predictions: This example has three observations and three regression coefficients, allowing an exact fit because the design matrix is invertible. However, the model has no residual degrees of freedom, so its ability to generalize to unseen data cannot be established from these records alone.