Inspecting the Fitted Result

A fitted slope and one loss value summarize the result but do not explain it completely. This lesson checks the fitted parameter, recomputes predictions, examines pointwise residuals, and uses plots to find patterns that mean squared error can hide.

The search returns a slope and its mean squared error, but two numbers are not enough to understand the result. Inspection asks whether the fitted value is credible, whether its predictions follow the measurements, and whether the remaining errors have a pattern that the loss hides.

For this synthetic project, we can use evidence that would not normally be available with real data: the true generating slope.

Compare the fitted slope with the known target

Suppose the data was generated with:

true slope:   2.00
fitted slope: 1.95

The signed slope error is:

fitted slope - true slope
= 1.95 - 2.00
= -0.05

The negative sign says that the fitted slope is smaller than the known target. Its absolute error is 0.05.

This comparison is possible because the data is synthetic. With ordinary observations, the true parameter is usually unknown; estimating it is the reason for fitting the model.

Do not require exact equality in this project. Noise perturbs the measurements, and the finite candidate grid may not contain the true slope. A close value is evidence that the fitting process behaves reasonably under the controlled conditions.

Recompute predictions from the fitted slope

The fitted slope should reproduce one prediction for every input:

prediction = fitted_slope * x

Before interpreting the plot, check the numerical structure:

Then recompute the loss rather than trusting a stale value:

np.isclose compares floating-point numbers with a small tolerance. It is more appropriate than exact equality when equivalent calculations may differ by tiny rounding amounts.

Inspect one residual per measurement

For inspection, define a residual as:

ri=yiy^ir_i = y_i - \hat{y}_i

Read this as: “residual sub i equals measured y sub i minus predicted y sub i.”

The fitting lesson defined prediction error in the opposite direction, prediction - measurement. Both produce the same squared error. Here the residual convention is useful because:

  • a positive residual places the measurement above the fitted line;
  • a negative residual places it below the fitted line.

For a small dataset, print a table:

x     measured    predicted    residual
0.0      0.10         0.00         0.10
1.0      2.00         2.00         0.00
2.0      4.10         4.00         0.10
3.0      5.90         6.00        -0.10

The loss summarizes the squared residuals, but this table preserves their locations and signs.

One loss can hide different error patterns

Consider two sets of residuals:

case A: [ 1.0, -1.0]
case B: [ 1.4142, 0.0]

Their approximate mean squared errors are both 1.0:

case A: (1² + (-1)²) / 2 = 1
case B: (1.4142² + 0²) / 2 ≈ 1

Case A spreads the error across both points. Case B concentrates nearly all of it in one point. The same loss therefore does not imply the same fit pattern.

The locations of errors can reveal problems such as:

  • one unusual measurement dominating the loss;
  • mostly positive residuals followed by mostly negative residuals;
  • increasing residual size as x grows;
  • a curved pattern that a straight line cannot represent;
  • paired arrays that were shuffled independently.

Plot measurements, predictions, and residuals

A useful figure shows the measured points and fitted line on the same axes. A second panel can show residuals around zero.

Inspect a fitted line and its residuals

The upper panel compares measurements with predictions. The lower panel preserves the sign and location of every remaining error.

Ready to run.

The vertical segments in the upper panel join each prediction to its measurement. Their lengths are the absolute residuals. The residual panel makes direction visible: points above zero lie above the fitted line, while points below zero lie below it.

Read the figure as evidence

For this project, ask:

  1. Do the measurements and fitted line use the same horizontal and vertical variables?
  2. Does the line pass through the general centre of the points?
  3. Are residuals present on both sides of zero?
  4. Does one point have a much larger residual than the others?
  5. Do residuals form a curve or another systematic pattern?
  6. Are the axes and legend clear enough to identify the evidence?

A useful interpretation names what is visible:

The fitted line follows the overall linear pattern. Residuals are small relative to the vertical scale and appear on both sides of zero, with no obvious curved pattern.

Avoid conclusions such as “the model is correct.” A plot can support or weaken a claim about fit; it cannot prove that the chosen model is the only possible explanation.

Compare the winner with nearby candidates

The fitted slope should have no larger loss than any candidate evaluated by the search. Inspecting nearby values shows whether the minimum is well distinguished:

slope    loss
1.75     0.7425
2.00     0.0317
2.25     0.5392

Here, 2.00 is clearly better than its neighbours on this grid. If several nearby slopes have almost equal losses, report that the data does not strongly distinguish among them at the current noise level and coordinate range.

Also inspect whether the winner lies at the edge of the grid. An edge result can indicate that a lower-loss slope lies outside the searched range.

This is an in-sample inspection

The same measurements were used to select and inspect the fitted slope. This is an in-sample result.

The project demonstrates that the code can recover a sensible slope for a controlled dataset. It does not test how well the fitted model predicts new, unseen measurements. Later machine-learning work will separate training data from validation or test data when generalization is the question.

Exercise: Choose the evidence for a fitted line

Which figure provides the most useful first inspection of this project?

Choose the useful figure

Select one choice, then check.

HintPreserve the comparison

A line cannot be judged visually when the observations it is meant to fit are absent.

SolutionShow measurements and predictions together

Plot measured points and the fitted line on the same labelled axes. A line or loss alone removes the pointwise evidence needed to inspect the fit.

Exercise: Calculate the signed slope error

The true slope is 2.0 and the fitted slope is 1.9. What is fitted_slope - true_slope?

Answer it first, then check.

HintUse fitted minus true

Calculate 1.9 - 2.0.

SolutionThe fitted slope is below the target

1.9 - 2.0 = -0.1. The magnitude is 0.1, and the negative sign indicates that the fitted slope is smaller than the known generating slope.

Exercise: Interpret a residual sign

Using residual = measured - predicted, what does a positive residual mean?

Choose the interpretation

Select one choice, then check.

HintUse a small numerical pair

If the measurement is 4.2 and the prediction is 4.0, the residual is 0.2.

SolutionA positive residual lies above the line

A positive value means measured > predicted, so the plotted measurement lies above the fitted line at that input.

Exercise: Recognize what MSE can hide

Two fits have the same mean squared error. What can still differ?

Choose what may differ

Select one choice, then check.

HintCompare a summary with its components

The mean does not preserve the order, sign, or location of individual residuals.

SolutionEqual loss does not imply equal residuals

Residual signs, magnitudes, and locations may differ even when their squared values have the same mean. Inspect the residuals or plot to retain that information.

Exercise: Compute fitted-result diagnostics

Complete the program so it calculates predictions, residuals, mean squared error, and the largest absolute residual. It must print:

loss: 0.01
largest residual: 0.1
residual signs: [1.0, 0.0, 1.0, -1.0]

Inspect loss and pointwise residuals

Ready to run.

HintKeep the residual convention consistent

Use residual = y - prediction. Then square for MSE and use np.max(np.abs(residual)) for the largest magnitude.

SolutionCompute summaries from the residual array

The predictions are [0, 2, 4, 6], so the residuals are [0.1, 0.0, 0.1, -0.1]. Their squared mean is 0.01, and the largest absolute residual is 0.1.

Inspect the result at several levels

Use four connected views:

parameter: fitted slope and known target
summary: mean squared error
points: predictions and residuals
figure: spatial pattern across x

Agreement across these views is stronger evidence than any one view alone. Disagreement is a reason to inspect the data, implementation, candidate grid, or model assumptions before reporting a conclusion.