Problem 361501 · easy · Level 03 Linear Management & Searching

Is the Line Better Than a Guess?

linear regression · least squares · test error · baseline · mean squared error

A phone maker ran battery tests at different screen brightness levels and wants to predict battery life. Before trusting a straight line, it wants to know how much better the line does on new tests than the simplest possible model, which always predicts the average battery life of the training tests.

Write line_vs_mean(x_train, y_train, x_test, y_test) that returns a tuple (a, b, mse_line, mse_mean, skill):

  • a and b are the intercept and slope of the least-squares line y = a + b·x through the training points (the line that minimises the sum of squared differences between y_train and the line);
  • mse_line is the mean squared error of the line's predictions on the test points;
  • mse_mean is the mean squared error on the test points of the constant prediction mean(y_train);
  • skill is 1 - mse_line / mse_mean, or None when mse_mean is 0.

If all training x are equal, no line is best: return None.

The setup provides battery_tests(n, seed), which returns (brightness, hours) for n random tests.

Examples

Input:  x_train = [0, 1, 2, 3], y_train = [1, 3, 5, 7], x_test = [4, 5], y_test = [9, 12]
Output: (1.0, 2.0, 0.5, 44.5, 0.9887640449438202)
Explanation: the training points lie on y = 1 + 2x. On the test points the line predicts 9 and 11,
errors 0 and 1, so mse_line = 0.5. The training mean is 4, with errors 5 and 8: mse_mean = 44.5.

Constraints

  • 2 <= len(x_train) == len(y_train) <= 10**5, 1 <= len(x_test) == len(y_test) <= 10**5
  • values are between -10**4 and 10**4; floats are compared with a tolerance of 1e-6

Goals

  • Fit the least-squares line on training data only
  • Measure its mean squared error on test data it has not seen
  • Compare it with the constant prediction by the training mean
Starting Python…