Enter the two sets of data whose relationship you want to check, each separated by commas (,). The correlation coefficient r, the covariance and the equation of the regression line are calculated, and the regression line is drawn over a scatter plot.
Table of Contents
-
What you can do on this page
-
What is this calculation used for?
-
How to Use
-
Formulas and figures
-
Symbols and terms
-
Good to know before you start
-
How to calculate it in Excel
-
How to calculate it in Google Sheets
-
How to calculate it in Python
-
How to write it in LaTeX and other math languages (copy and paste)
-
How to have ChatGPT do the calculation
-
DataChef Features
-
Related Features
-
NumberChef Calculators List
What you can do on this page
- Enter paired \(x\) and \(y\) data (two variables) and you get the correlation coefficient \(r\) and the covariance \(s_{xy}\) on the spot
- The mean, variance and standard deviation of \(x\) and of \(y\), and the equation of the regression line (least squares) \(y = ax + b\) are calculated at the same time
- A scatter plot is drawn with the regression line and the mean point on top, so you can see at a glance how the two sets of data are related (rising to the right, falling to the right, or scattered)
- A rule-of-thumb reading of the value, such as "strong positive correlation" or "little or no correlation", is shown too
- A plain-language explanation of the formulas and copy-and-paste formulas for Excel, Google Sheets and Python are all on this page
What is this calculation used for?
A classic use of the correlation coefficient is checking "do grades go up when you study more?" with data instead of gut feeling. Calculate \(r\) from pairs of study time and score for each student, and one number tells you how strong the relationship is.
Schools and tutoring centers sometimes look at the correlation between subjects on practice tests (do students who are good at math also do well in science?) to guide their teaching. But keep in mind that a correlation does not prove the cause, as in "just studying longer will always raise the score".
Sales of ice cream and cold drinks are known to have a positive correlation with temperature, and sales of hot soup and hot coffee a negative one. Stores and restaurants check the correlation in past "temperature and units sold" data and change how much they order and what they display according to the weather forecast.
Go one step further and find the regression line, and you can make a concrete estimate such as "if tomorrow's forecast high is 90°F, we will sell about this many".
In investing, the correlation coefficient between the price movements of assets such as stocks, bonds and gold is a basic tool of diversification. Combining assets with low (or negative) correlation means that when one goes down, the other is less likely to go down too, which keeps the ups and downs of the whole portfolio smaller.
Pension funds and mutual funds always analyze the correlations between assets before deciding how to divide their money among them.
When defects increase in a factory, checking the correlation coefficient between the defect rate and conditions such as processing temperature, humidity and machine running time helps narrow down which conditions move together with the defects. This is such a basic method that the scatter plot used for it is one of the "seven basic tools of quality".
Once a condition with a strong correlation is found, the next step is an experiment that changes the condition to confirm cause and effect, and improvements follow from there.
Research on lifestyle and health, such as "exercise and blood pressure" or "smoking and the risk of a disease", also starts by checking the correlation in the data.
It is also where people learn not to jump from correlation to cause. A famous example is that ice cream sales and drownings have a positive correlation. Both simply rise with a common factor, hot weather. Medical research uses many methods to remove the effect of such third factors and get closer to real cause and effect.
Formulas and figures
Symbols and terms
Symbols
| \(x_i,\ y_i\) | x sub i, y sub i | The \(x\) value and the \(y\) value of the \(i\)th pair. (Example - the study time \(x_3\) and the score \(y_3\) of the 3rd student.) \(i\) is a letter often used for an index (position number). |
| \(n\) | n | The number of pairs, from the first letter of "number". (Example - for pairs from 5 students, \(n = 5\)) |
| \(\bar{x},\ \bar{y}\) | x-bar, y-bar | The means of \(x\) and of \(y\). A bar over a letter is the customary way to show a mean. |
| \(x_i - \bar{x}\) | x sub i minus x-bar | The deviation of \(x\). It shows how far each value is from the mean of \(x\). \(y_i - \bar{y}\) is the deviation of \(y\). |
| \(s_{xy}\) | s sub x y | The covariance of \(x\) and \(y\), the mean of the products of deviations. It shows the tendency of the two sets of data to move together. The letter \(s\) is customary for statistics in the same family as the standard deviation. |
| \(s_x,\ s_y\) | s sub x, s sub y | The standard deviations of \(x\) and of \(y\) (how spread out each is). They are the square roots of the variances. |
| \(s_x^2,\ s_y^2\) | s sub x squared, s sub y squared | The variances of \(x\) and of \(y\), the mean of the squared deviations (divided by \(n\)). |
| \(r\) | r | The correlation coefficient. The letter is said to come from the \(r\) in "correlation" or "relation". It is always between \(-1\) and \(1\). |
| \(\sum\) | sigma (summation sign) | A symbol that means "add them all up". \(\sum_{i=1}^{n}\) is read "the sum from \(i = 1\) to \(n\)". |
| \(a,\ b\) | a, b | The slope and the intercept of the regression line \(y = ax + b\). They play the same roles as \(m\) and \(b\) in \(y = mx + b\). |
Terms
| correlation | A straight-line tendency between two sets of data, where one increases as the other increases (or decreases). The stronger it is, the closer the points on a scatter plot lie to a straight line. |
| positive correlation | The tendency for \(y\) to increase as \(x\) increases. The scatter plot rises to the right, and the correlation coefficient is positive. |
| negative correlation | The tendency for \(y\) to decrease as \(x\) increases. The scatter plot falls to the right, and the correlation coefficient is negative. |
| correlation coefficient | A single number from \(-1\) to \(1\) that shows the strength and direction of a correlation (also called the Pearson correlation coefficient). It is found by dividing the covariance by the product of the standard deviations of \(x\) and \(y\). Because it does not depend on the units, you can compare its strength across different data. |
| covariance | The mean of the products of the deviations of \(x\) and \(y\). Positive means a tendency to rise to the right, and negative means a tendency to fall to the right. This page divides by the number of pairs \(n\) (the population version). |
| scatter plot | A graph that plots paired data \((x,\ y)\) as points on a coordinate plane. From the pattern of the points you can read the direction and strength of a correlation at a glance. |
| variable | A quantity you measure as data, such as height, score or temperature. This page looks at the relationship between two variables, \(x\) and \(y\). |
| deviation | The difference between a value and the mean. Positive means above the mean and negative means below it. The covariance, the variance and the standard deviation are all built from deviations. |
| variance | The mean of the squared deviations. It shows how large the spread of the data is. It appears in the denominator of the slope of the regression line. |
| standard deviation | The square root of the variance. Its unit is the same as the data, which makes it easy to use as a measure of spread. It appears in the denominator of the correlation coefficient. |
| regression line (line of best fit) | The straight line \(y = ax + b\) that fits the cloud of points on a scatter plot best. It is used to predict a \(y\) value from an \(x\) value. |
| least squares method | A way to choose the slope and the intercept of a line so that the vertical gaps (errors) between each point and the line, squared and added up, are as small as possible. The regression line is the line chosen this way. |
| outlier | A value far away from the other points. The correlation coefficient is strongly affected by outliers, so always check it together with the scatter plot. |
| association | A relationship where two quantities move together (as one increases, the other increases or decreases). Even with a correlation, one is not necessarily the cause of the other (causation). There may simply be a common cause behind both (a third factor, also called a lurking variable). |
| causation (cause and effect) | A relationship where one thing is the cause and the other is its result. An association (moving together) alone does not show causation; there may simply be a common cause behind both (a third factor). |
Good to know before you start
Here is what helps you use the calculation on this page with real understanding, not just by pressing the button.
If you get stuck, going back to review these topics is the fastest way forward.
| The mean (Grade 6) |
|
| Negative numbers (Grades 6–7) |
|
| Coordinates and linear functions (Grades 6–8) |
|
| Square roots (Grade 8) |
|
| Data analysis (high school statistics) |
|
How to calculate it in Excel
| x of pair 1 | 1 |
| x of pair 2 | 2 |
| x of pair 3 | 3 |
| x of pair 4 | 4 |
| x of pair 5 | 5 |
| y of pair 1 | 2 |
| y of pair 2 | 4 |
| y of pair 3 | 4 |
| y of pair 4 | 4 |
| y of pair 5 | 6 |
| Covariance s_xy (divide by n) | =COVARIANCE.P(B1:B5,B6:B10) |
| (For reference) sample covariance, divide by n−1 | =COVARIANCE.S(B1:B5,B6:B10) |
| x of pair 1 | 1 |
| x of pair 2 | 2 |
| x of pair 3 | 3 |
| x of pair 4 | 4 |
| x of pair 5 | 5 |
| y of pair 1 | 2 |
| y of pair 2 | 4 |
| y of pair 3 | 4 |
| y of pair 4 | 4 |
| y of pair 5 | 6 |
| Correlation coefficient r | =CORREL(B1:B5,B6:B10) |
| (For reference) the same value with PEARSON | =PEARSON(B1:B5,B6:B10) |
| x of pair 1 | 1 |
| x of pair 2 | 2 |
| x of pair 3 | 3 |
| x of pair 4 | 4 |
| x of pair 5 | 5 |
| y of pair 1 | 2 |
| y of pair 2 | 4 |
| y of pair 3 | 4 |
| y of pair 4 | 4 |
| y of pair 5 | 6 |
| Slope a | =SLOPE(B6:B10,B1:B5) |
| Intercept b | =INTERCEPT(B6:B10,B1:B5) |
| x of pair 1 | 1 |
| x of pair 2 | 2 |
| x of pair 3 | 3 |
| x of pair 4 | 4 |
| x of pair 5 | 5 |
| y of pair 1 | 2 |
| y of pair 2 | 4 |
| y of pair 3 | 4 |
| y of pair 4 | 4 |
| y of pair 5 | 6 |
| Mean of x x̄ | =AVERAGE(B1:B5) |
| Mean of y ȳ | =AVERAGE(B6:B10) |
| Variance of x s_x² | =VARP(B1:B5) |
| Standard deviation of x s_x | =STDEVP(B1:B5) |
| Variance of y s_y² | =VARP(B6:B10) |
| Standard deviation of y s_y | =STDEVP(B6:B10) |
The covariance in the first table is 1.6 (the sample covariance is 2), the correlation coefficient in the second table is about 0.8944, and the third table gives a slope of 0.8 and an intercept of 1.6.
Be careful: there are two covariance functions. COVARIANCE.P is the covariance that divides by n (the same as this calculator), and COVARIANCE.S is the sample covariance that divides by n − 1. In old versions of Excel (2007 and earlier), use COVAR instead of COVARIANCE.P.
CORREL and PEARSON give the same correlation coefficient (r is the same whether you divide by n or by n − 1, so there is no choice to make).
SLOPE and INTERCEPT take "the y range, the x range" in that order (note that x does not come first).
To use a different number of pairs, add (or remove) rows of numbers, then change "B1:B5" and "B6:B10" in the formulas to your actual data ranges.
How to calculate it in Google Sheets
| x of pair 1 | 1 |
| x of pair 2 | 2 |
| x of pair 3 | 3 |
| x of pair 4 | 4 |
| x of pair 5 | 5 |
| y of pair 1 | 2 |
| y of pair 2 | 4 |
| y of pair 3 | 4 |
| y of pair 4 | 4 |
| y of pair 5 | 6 |
| Covariance s_xy (divide by n) | =COVAR(B1:B5,B6:B10) |
| x of pair 1 | 1 |
| x of pair 2 | 2 |
| x of pair 3 | 3 |
| x of pair 4 | 4 |
| x of pair 5 | 5 |
| y of pair 1 | 2 |
| y of pair 2 | 4 |
| y of pair 3 | 4 |
| y of pair 4 | 4 |
| y of pair 5 | 6 |
| Correlation coefficient r | =CORREL(B1:B5,B6:B10) |
| x of pair 1 | 1 |
| x of pair 2 | 2 |
| x of pair 3 | 3 |
| x of pair 4 | 4 |
| x of pair 5 | 5 |
| y of pair 1 | 2 |
| y of pair 2 | 4 |
| y of pair 3 | 4 |
| y of pair 4 | 4 |
| y of pair 5 | 6 |
| Slope a | =SLOPE(B6:B10,B1:B5) |
| Intercept b | =INTERCEPT(B6:B10,B1:B5) |
In Google Sheets, the covariance that divides by n is found with the COVAR function (the same definition as this calculator; the name COVARIANCE.P calls the same function). The correlation coefficient uses CORREL, the same as Excel.
How to calculate it in Python
import statistics
xs = [1, 2, 3, 4, 5] # x data (example: study time)
ys = [2, 4, 4, 4, 6] # y data (example: quiz score)
n = len(xs)
mean_x = statistics.mean(xs) # mean of x
mean_y = statistics.mean(ys) # mean of y
# covariance s_xy (population version: divide by n)
covariance = sum((x - mean_x) * (y - mean_y) for x, y in zip(xs, ys)) / n
sd_x = statistics.pstdev(xs) # standard deviation of x (divide by n)
sd_y = statistics.pstdev(ys) # standard deviation of y (divide by n)
r = covariance / (sd_x * sd_y) # correlation coefficient
slope = covariance / statistics.pvariance(xs) # slope a of the regression line
intercept = mean_y - slope * mean_x # intercept b of the regression line
print(f"Covariance s_xy: {covariance}")
print(f"Correlation coefficient r: {r}")
# the intercept can show a floating-point error (1.5999...), so round it to 10 significant digits
print(f"Regression line: y = {slope:.10g}x + {intercept:.10g}")
How to write it in LaTeX and other math languages (copy and paste)
s_xy = {(x₁ − x̄)(y₁ − ȳ) + … + (xₙ − x̄)(yₙ − ȳ)} ÷ n
s_{xy} = \dfrac{1}{n}\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mrow>
<msub><mi>s</mi><mrow><mi>x</mi><mi>y</mi></mrow></msub>
<mo>=</mo>
<mfrac><mn>1</mn><mi>n</mi></mfrac>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow>
<mi>n</mi>
</munderover>
<mrow>
<mo>(</mo><msub><mi>x</mi><mi>i</mi></msub><mo>−</mo><mover><mi>x</mi><mo>¯</mo></mover><mo>)</mo>
<mo>(</mo><msub><mi>y</mi><mi>i</mi></msub><mo>−</mo><mover><mi>y</mi><mo>¯</mo></mover><mo>)</mo>
</mrow>
</mrow>
</math>
s_(xy) = (1/n) sum_(i=1)^n (x_i - bar x)(y_i - bar y)
Covariance[xdata, ydata]*(Length[xdata] - 1)/Length[xdata] (* Covariance divides by n-1, so this converts it to the value divided by n *)
s_xy := add((x[i] - x_bar)*(y[i] - y_bar), i = 1 .. n)/n;
s_xy = mean((x - mean(x)).*(y - mean(y)));
s_xy = (1/n) ∑_(i=1)^n (x_i − x̄)(y_i − ȳ)
r = s_xy ÷ (s_x × s_y)
r = \dfrac{s_{xy}}{s_x\, s_y}
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mrow>
<mi>r</mi>
<mo>=</mo>
<mfrac>
<msub><mi>s</mi><mrow><mi>x</mi><mi>y</mi></mrow></msub>
<mrow><msub><mi>s</mi><mi>x</mi></msub><msub><mi>s</mi><mi>y</mi></msub></mrow>
</mfrac>
</mrow>
</math>
r = s_(xy) / (s_x s_y)
Correlation[xdata, ydata]
r := s_xy/(s_x*s_y);
R = corrcoef(x, y); r = R(1, 2);
r = s_xy/(s_x s_y)
y = ax + b, a = s_xy ÷ s_x², b = ȳ − a·x̄
y = ax + b, \quad a = \dfrac{s_{xy}}{s_x^{2}}, \quad b = \bar{y} - a\bar{x}
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mrow>
<mi>y</mi><mo>=</mo><mi>a</mi><mi>x</mi><mo>+</mo><mi>b</mi>
<mo>,</mo><mspace width="1em"/>
<mi>a</mi><mo>=</mo>
<mfrac>
<msub><mi>s</mi><mrow><mi>x</mi><mi>y</mi></mrow></msub>
<msubsup><mi>s</mi><mi>x</mi><mn>2</mn></msubsup>
</mfrac>
<mo>,</mo><mspace width="1em"/>
<mi>b</mi><mo>=</mo>
<mover><mi>y</mi><mo>¯</mo></mover>
<mo>−</mo>
<mi>a</mi><mover><mi>x</mi><mo>¯</mo></mover>
</mrow>
</math>
y = a x + b, a = s_(xy)/s_x^2, b = bar y - a bar x
lm = LinearModelFit[Transpose[{xdata, ydata}], t, t]; lm["BestFitParameters"]
a := s_xy/s_x^2; b := y_bar - a*x_bar;
p = polyfit(x, y, 1); % p(1) = slope a, p(2) = intercept b
y = ax + b, a = s_xy/s_x^2, b = ȳ − ax̄
How to have ChatGPT do the calculation
You are a statistics calculation assistant. Do the following calculation by actually running Python code, and base your answer only on the numbers from the execution result (do not answer by mental math or guessing). For the following paired data, find each of the values below. x: 1, 2, 3, 4, 5 y: 2, 4, 4, 4, 6 1. The means of x and y, and their variances and standard deviations dividing by n 2. The covariance s_xy (the sum of the products of deviations divided by the number of pairs n; do not divide by n − 1) 3. The correlation coefficient r 4. The slope a and the intercept b of the regression line y = ax + b (least squares) Show the formulas you used and the numbers from the execution result.
How to Use
-
1Enter your numbersType the numbers you want to calculate with into the input fields
-
2CalculatePress the "Calculate" button
-
3Check the resultThe result appears on the spot. The same page also explains the idea behind the calculation and the formula
DataChef Features
No technical knowledge required.
Intuitive and user-friendly operation.
Can be used without registering personal information.
Automatic file deletion by clicking "download".
and rapid file conversion.
No attribution required.
No need to contact us for commercial use permission.
