All problems

The Regression Cost Function

mediumPythonLinear Regression

Kestrel Freight's training tool needs one number that says how badly a candidate rule fits the deliveries, so that a search can tell whether it is getting warmer or colder. That number is the regression cost. Here it is defined as the mean of the squared residuals, halved — a residual being the gap between what the rule says a delivery should have cost and what it actually cost. The halving is the usual convention in training code; it changes no comparison, and it makes the algebra of the search that comes later much tidier.

Task: Print the cost of the given rule on the given deliveries.

Input

The first line holds two numbers separated by a single space: b0, the rule's intercept, then b1, its slope. The second line holds one integer n, the number of deliveries, where 1 <= n <= 1000. Then come n lines, each holding a distance and the cost that delivery actually came to, separated by a single space.

Output

One line: the word cost, a single space, and the cost rounded to 4 decimal places.

Example:

Input:
1 2
3
1 3
2 5
3 7

Output:
cost 0.0000

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…