Exercises

Apply the complete project workflow through tracing, numerical calculation, debugging, implementation, inspection, recording, and reporting. The final exercise joins seeded data generation, finite slope search, structured evidence, and a verified rerun.

These exercises treat the chapter as one connected project. Work through them in order when possible: trace the dependencies, implement the numerical stages, inspect failure modes, preserve the run, and finish with an integrated program.

Trace the project

Exercise: Order the complete project workflow

Which sequence respects the information dependencies in the noisy-line project?

Choose the dependency order

Select one choice, then check.

HintSeparate choices from evidence

Configuration must exist before data generation. A report can describe a result only after fitting and inspection.

SolutionMove from question to report

The dependency order is question → configuration → data → fitting → inspection → record → report. The run record preserves the computation, while the report explains its evidence and limitations.

Generate and fit the data

Exercise: Generate paired repeatable measurements

Complete make_data so it creates five evenly spaced inputs from 0.0 through 4.0, draws one normal noise value per input, and adds the noise to a line with the supplied slope. Two calls with the same arguments must print:

shape: (5,)
paired: True
repeatable: True

Implement seeded synthetic data generation

Ready to run.

HintMatch the noise size to x

Use np.linspace(0.0, 4.0, num=5) and set the normal draw's size to x.size.

SolutionGenerate one noisy y value per x value

np.linspace creates the coordinates, and the generator creates one noise value for each coordinate. Recreating the generator inside the function makes the same arguments reproduce the arrays.

Exercise: Calculate a mean squared error by hand

For these arrays, calculate the mean squared error:

prediction: [0, 2, 4]
measured y: [0, 1, 5]

Answer it first, then check.

HintPreserve all three errors

The errors are [0, 1, -1]. Square before taking the mean.

SolutionAverage the squared pointwise errors

The squared errors are [0, 1, 1]. Their sum is 2, and 2 / 3 is approximately 0.6667.

Exercise: Fit a slope by exhaustive search

Complete fit_slope so it evaluates every candidate and keeps the candidate with the smallest mean squared error. The program must print:

best slope: 2.0
best loss: 0.0075

Search every candidate slope

Ready to run.

HintUpdate slope and loss as one pair

If loss < best_loss, assign the current slope to best_slope and the current loss to best_loss.

SolutionCompare each candidate with the best so far

Each loop iteration creates predictions and one MSE. Updating both best values together preserves the correspondence between slope and loss. Slope 2.0 gives MSE 0.0075 for the supplied measurements.

Inspect limitations and residual evidence

Exercise: Diagnose a candidate-grid boundary result

A search evaluates slopes from 1.0 through 2.0. The loss decreases at every candidate, and slope 2.0 has the smallest loss. What should you investigate next?

Choose the next investigation

Select one choice, then check.

HintInspect beyond the winning edge

The losses were still decreasing when the search reached its largest candidate.

SolutionExpand the search range

Evaluate slopes above 2.0. The current result is only the best candidate inside the searched interval, not proof that the minimum occurs at its boundary.

Exercise: Interpret a curved residual pattern

A residual plot shows negative residuals at small x, positive residuals near the middle, and negative residuals again at large x. What is the strongest interpretation supported by this pattern?

Choose the interpretation

Select one choice, then check.

HintLook for systematic structure

Random scatter around zero and a repeated curved pattern carry different information.

SolutionThe line may miss curvature

A negative-positive-negative pattern is systematic rather than an unstructured spread around zero. It suggests that a straight line does not capture all of the relation in these measurements.

Preserve and explain the run

Exercise: Choose a sufficient run record

Which record contains the information needed to understand and attempt to rerun the fitting computation?

Choose the sufficient record

Select one choice, then check.

HintFollow every dependency of the result

The fitted slope depends on more than generator state.

SolutionPreserve configuration, results, and context

Save the project version, complete configuration, results, relevant environment information, and observation. The seed is necessary for this generated data but is not sufficient by itself.

Exercise: Explain a changed seeded rerun

An original run and a rerun both use seed 7, but the rerun generates one extra normal value before creating y. Why can the measurements differ?

Choose the explanation

Select one choice, then check.

HintTrack call order

Every random draw consumes values and changes the generator's current state.

SolutionThe extra draw changes the later sequence position

Both generators begin at the same state, but the rerun advances once more before generating y. Its measurements therefore use a different part of the pseudorandom sequence.

Exercise: Choose a bounded report conclusion

Which conclusion is supported by a low-loss fit to this chapter's synthetic measurements?

Choose the supported conclusion

Select one choice, then check.

HintMatch the claim to the test

No real dataset or independent unseen data was evaluated.

SolutionLimit the conclusion to the controlled run

The project supports a claim about recovery under this synthetic process and finite search. It does not establish universal linearity or generalization.

Integrate the project

Exercise: Complete an end-to-end numerical run

Complete the missing stages so the program generates paired noisy data, fits the lowest-loss candidate, builds a structured record, and verifies a rerun. It must print:

paired: True
fitted slope: 2.0
record complete: True
rerun matches: True

Generate, fit, record, and rerun

Ready to run.

HintComplete one dependency at a time

First create noise and y. Then compute a loss for each candidate and use np.argmin. Store the original result under results, and pass the saved configuration back to run_project.

SolutionConnect configuration to reproducible evidence

The seeded generator creates paired measurements, exhaustive search selects slope 2.0, and the record separates the copied configuration from computed results. Reusing that configuration recreates the same generator operations and measurements.