Ad spend versus sales: fit the line
A mobile-game studio in Seoul wants to know how much extra revenue each extra thousand dollars of advertising brings. You will fit a straight line by ordinary least squares and report how tightly the points follow it.
Task: line 1 is the number of data points n (at least 3). Each of the next n lines holds x y (ad spend and revenue, numbers). The x values are never all equal and the y values are never all equal. Using slope = Sxy / Sxx, intercept = mean(y) - slope * mean(x) and the correlation r = Sxy / sqrt(Sxx * Syy) (where Sxy = sum((x - mx) * (y - my)), Sxx = sum((x - mx)^2), Syy = sum((y - my)^2)) print one line: slope=A intercept=B r=C, each with 3 decimals. Print 0.000 instead of -0.000 when a value rounds to zero.
Sample tests
Input
5 10 120 20 180 30 260 40 310 50 390
Expected output
slope=6.700 intercept=51.000 r=0.998
Input
4 1 9 2 7 3 4 4 2
Expected output
slope=-2.400 intercept=11.500 r=-0.997
Your answer is also checked against 3 hidden tests.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
