All problems

Fitting the Least-Squares Line

mediumPythonLinear Regression

Kestrel Freight bills fuel separately from mileage, and the finance team wants one straight-line rule that turns a delivery's distance into an expected fuel cost. They have a handful of past deliveries to learn from. No straight line will pass through every one of them, so the team wants the least-squares line: the single line for which the gaps between the recorded costs and the line's own values, each squared first, add up to the smallest total any line could manage. The line is free in both respects — its tilt and its height are both fitted, and neither is pinned to zero.

Task: Fit the least-squares line to the recorded deliveries and print its slope and its intercept. The slope is how much the cost moves when the distance goes up by one; the intercept is where the line sits when the distance is zero.

Input

The first line holds one integer n, the number of deliveries, where 2 <= n <= 1000. Then come n lines, each holding two numbers separated by a single space: the distance and the fuel cost of one delivery. Not all distances are the same.

Output

Two lines, in this order. First the word slope, a single space, and the slope rounded to 2 decimal places. Then the word intercept, a single space, and the intercept rounded to 2 decimal places. Round once, at printing time.

Example:

Input:
5
1 3
2 5
3 7
4 9
5 11

Output:
slope 2.00
intercept 1.00

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…