Milestone 2 of 8
Implement and hand-check line prediction and score
Keep prediction and mean squared difference independent, validate their arrays, and reproduce one small calculation by hand.
Keep the two supplied numerical rules independent. A search is easier to trust when prediction and scoring can be checked without running the whole project.
Goal
Implement predict_line and mean_squared_difference, verify their inputs and
outputs, and reproduce one supplied calculation by hand before adding search.
Inputs
Use the line contract:
prediction = slope * x + intercept
Use the score contract:
mean((prediction - observed) ** 2)
Work with one three-value input, prediction, and observed example whose values are small enough to expand on paper. Arrays must be one-dimensional, finite, equal in length, and non-empty wherever the function requires both arrays.
Deliverables
Implement the two independent functions in src/line.py. The prediction
function returns one value per input. The score function returns one finite
number and does not change either input array.
Record the three-value expansion and its result in report.md. Keep the name
mean_squared_difference in the public boundary; do not replace it with a
statistical interpretation that the project does not teach.
Checks
Check a scalar or one-element prediction, a three-value hand calculation, and a larger finite array. Reject empty score inputs, mismatched lengths, non-one-dimensional arrays, and non-finite values with a clear result or exception.
Check that prediction and observed inputs remain unchanged, that the score is non-negative, and that a zero difference gives zero. Verify the compact NumPy calculation against the expanded three-value calculation.
Workspace
Put both functions in src/line.py and keep configuration loading in
src/config.py. The later search module should call these functions rather
than copy their formulas into a loop. Keep the hand-check in report.md.
Hints
HintExpand before reducing
HintShape is part of the contract
Review
Compare the hand calculation with the function result. Explain why the score is a supplied comparison value for this project, not a claim about how well a line will work on future data.
How to check your work
Checks compare the functions and edge-case behavior with the supplied fixture. The supplied fixture makes the numerical boundary visible without requiring a particular NumPy expression.