The Correlation Coefficient
Wrenfield Agronomy records two numbers for every trial plot: the kilograms of fertiliser applied and the tonnes of grain harvested. The agronomists want one number saying how tightly those two move together along a straight line — Pearson's correlation coefficient, r. It runs from -1, where the plots sit exactly on a downward line, through 0, where there is no straight-line relationship at all, up to +1 for an exact upward line. Unlike raw covariance, r is unchanged if the fertiliser is recorded in grams instead of kilograms, which is why it is the figure that gets quoted in the report.
Task: Print Pearson's r for the two columns.
Input
The first line holds one integer n, how many trial plots follow. Each of the next n lines holds two plain numbers separated by a single space: the fertiliser applied, then the grain harvested.
Output
One line holding r with exactly three digits after the decimal point. A perfect downward relationship prints as -1.000, and the minus sign is part of the answer.
Example:
Input:
5
1 2
2 4
3 6
4 8
5 10
Output:
1.000
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