Correlation and Covariance Calculator
Calculate Pearson correlation, Spearman rank correlation, and covariance between two data sets.
Correlation and Covariance Calculator
Calculator
Adjust values & calculateEnter your values below. Every result is computed in your browser โ no data is sent to any server.
Formula: r = Sum[(xi - x_mean)(yi - y_mean)] / sqrt[Sum(xi - x_mean)^2 * Sum(yi - y_mean)^2]
Worked example โ r = 0.9956 (Very Strong Positive) | Sample Cov = 75 | R-squared = 99.12%
Formula
r = Sum[(xi - x_mean)(yi - y_mean)] / sqrt[Sum(xi - x_mean)^2 * Sum(yi - y_mean)^2]
The Pearson correlation coefficient is calculated by dividing the sum of the products of deviations from the means by the geometric mean of the sums of squared deviations. Covariance uses the same numerator but divides by N (population) or N-1 (sample).
Worked Examples
Example 1: Height and Weight Correlation
Problem:Given heights (cm): 160, 165, 170, 175, 180 and weights (kg): 55, 62, 68, 72, 80, calculate the correlation and covariance.
Solution:Mean X = 170, Mean Y = 67.4 Deviations: (-10,-12.4), (-5,-5.4), (0,0.6), (5,4.6), (10,12.6) Sum of dx*dy = 124+27+0+23+126 = 300 Sum dx^2 = 100+25+0+25+100 = 250 Sum dy^2 = 153.76+29.16+0.36+21.16+158.76 = 363.2 Sample Cov = 300/4 = 75 Pearson r = 300 / sqrt(250 x 363.2) = 300/301.33 = 0.9956
Result:r = 0.9956 (Very Strong Positive) | Sample Cov = 75 | R-squared = 99.12%
Example 2: Study Hours vs Exam Score
Problem:Study hours: 2, 4, 6, 8, 10 and exam scores: 50, 55, 70, 80, 90. Find correlation.
Solution:Mean X = 6, Mean Y = 69 Sum dx*dy = (-4)(-19)+(-2)(-14)+(0)(1)+(2)(11)+(4)(21) = 76+28+0+22+84 = 210 Sum dx^2 = 16+4+0+4+16 = 40 Sum dy^2 = 361+196+1+121+441 = 1120 Pearson r = 210 / sqrt(40 x 1120) = 210/211.66 = 0.9922
Result:r = 0.9922 (Very Strong Positive) | More study hours strongly predict higher scores
Frequently Asked Questions
What is the difference between correlation and covariance?
Correlation and covariance both measure the relationship between two variables, but they differ in scale and interpretation. Covariance measures the directional relationship between two variables and can take any value from negative infinity to positive infinity. Its magnitude depends on the units of measurement, making it difficult to compare across different datasets. Correlation, specifically Pearson correlation, is a standardized version of covariance that always falls between -1 and +1. It is calculated by dividing the covariance by the product of the two standard deviations. This normalization makes correlation unitless and directly comparable across any pair of variables regardless of their scales. A correlation of +1 means perfect positive linear relationship, -1 means perfect negative, and 0 means no linear relationship.
How do I interpret the Pearson correlation coefficient?
The Pearson correlation coefficient r ranges from -1 to +1 and measures the strength and direction of a linear relationship between two variables. Values close to +1 indicate a strong positive relationship where both variables increase together. Values close to -1 indicate a strong negative relationship where one variable increases as the other decreases. Values near 0 suggest no linear relationship. Common interpretation thresholds are: 0.9 to 1.0 is very strong, 0.7 to 0.9 is strong, 0.5 to 0.7 is moderate, 0.3 to 0.5 is weak, and below 0.3 is very weak or negligible. However, context matters greatly. In physics, correlations below 0.95 might be considered poor, while in social sciences, correlations above 0.5 are often considered strong. Remember that correlation does not imply causation.
What is the difference between population and sample covariance?
The difference between population and sample covariance lies in the denominator used for calculation. Population covariance divides the sum of products of deviations by N (the total number of data points), assuming you have measured every member of the population. Sample covariance divides by N-1 instead, applying what is known as Bessel correction. This correction compensates for the fact that a sample tends to underestimate the true population variance because the sample mean is closer to the sample data points than the true population mean would be. When working with data from experiments, surveys, or any subset of a larger group, you should use the sample covariance (N-1). Population covariance is only appropriate when you have data for the entire population. For large datasets, the difference becomes negligible.
What is the coefficient of determination (R-squared) and what does it tell us?
The coefficient of determination, commonly written as R-squared, is the square of the Pearson correlation coefficient. It represents the proportion of variance in one variable that is explained or predicted by the other variable. R-squared ranges from 0 to 1 (or 0% to 100%). For example, an R-squared of 0.85 means that 85% of the variation in variable Y can be explained by its linear relationship with variable X, while the remaining 15% is unexplained variation due to other factors or random noise. In regression analysis, R-squared is a key metric for assessing model fit. Higher values indicate the regression line fits the data more closely. However, a high R-squared alone does not validate a model since it can be artificially inflated by adding more variables.
When should I use Spearman rank correlation instead of Pearson correlation?
Spearman rank correlation should be used instead of Pearson correlation in several situations. First, when the relationship between variables is monotonic but not necessarily linear, Spearman captures this better because it measures rank-order association. Second, when your data contains significant outliers, Spearman is more robust because converting values to ranks reduces the influence of extreme values. Third, when variables are measured on ordinal scales (like satisfaction ratings from 1 to 5), Spearman is more appropriate since it does not assume interval-level measurement. Fourth, when the data violates normality assumptions required by Pearson, Spearman provides a non-parametric alternative. Pearson is preferred when the relationship is truly linear and data is normally distributed with no major outliers, as it uses more information from the data and is statistically more powerful in those conditions.
How is the least-squares regression line derived from these same sums?
The slope reuses the covariance numerator: b = Sum(dx*dy) / Sum(dx^2), and the intercept is a = y_mean - b * x_mean, which guarantees the line passes through the point of means. For the study-hours data, b = 210 / 40 = 5.25 and a = 69 - 5.25 * 6 = 37.5, so the fitted line is y = 37.5 + 5.25x, predicting about 5.25 extra score points per study hour. An equivalent form is b = r * (sy / sx), which shows why the slope and the correlation always share the same sign.
Why does changing the units of measurement alter the covariance but not the correlation?
Covariance carries the product of both variables' units, so it scales with them. Re-express heights in inches instead of centimetres and every dx is divided by 2.54, so a covariance of 75 kg*cm becomes 29.53 kg*in even though the underlying relationship is untouched. The correlation divides that covariance by both standard deviations, which shrink by the identical factor, so r stays exactly 0.9956. This scale invariance is what makes r comparable across datasets, and it is also why a large covariance on its own tells you nothing about the strength of a relationship.
Can a correlation of zero still hide a perfect relationship?
Yes, because Pearson's r only detects the linear component. Take x = -2, -1, 0, 1, 2 with y = x^2, giving y = 4, 1, 0, 1, 4. The deviation products are -4, 1, 0, -1, and 4, which sum to exactly zero, so r = 0 even though y is completely determined by x. Any symmetric U-shaped or cyclical pattern behaves this way. Always plot the scatter before trusting a near-zero coefficient, and consider Spearman's rank correlation or a curve fit when the pattern is monotonic but bent, or curved and non-monotonic.
What can make a strong correlation misleading?
A high r establishes co-movement, not a mechanism. A lurking confounder can drive both variables, as when ice cream sales and drowning rates both rise with summer temperature. Two unrelated series that each trend over time will correlate strongly simply because both are increasing, which is the classic spurious correlation. Causality can also run backwards from the direction you assume. A restricted sample range flattens r, while a single extreme outlier can manufacture one. Only a controlled experiment, or careful adjustment for the confounders, supports a causal reading.
How do tied values affect the Spearman rank correlation?
The shortcut formula rho = 1 - 6 * Sum(d^2) / (n(n^2 - 1)) is exact only when every value in each list is distinct. Tied observations should share a midrank, the average of the positions they occupy, and once midranks appear the shortcut becomes biased. The rigorous approach is to compute Pearson's r on the two rank vectors, which reduces to the shortcut when there are no ties. Watch for this with coarse ordinal data such as 1 to 5 ratings, where ties are the norm rather than the exception.
References
Reviewed for accuracy by Manoj Kumar, Mathematics Educator ยท Editorial policy
Related Calculators
๐งฎCorrelation Calculator
Calculate correlation with inputs, formulas, and instant results.
๐งฎScatter Plot Correlation Calculator
Calculate Pearson and Spearman correlation coefficients from paired data with significance test.
๐งฎCovariance Calculator
covariance calculator. Get instant, accurate results.
๐งฎAnnulus Area Calculator
Calculate annulus area with inputs, formulas, and instant results.
๐งฎArea Calculator
Calculate area with inputs, formulas, and instant results.
๐งฎArea of a Rectangle Calculator
Calculate the area, perimeter, and diagonal of a rectangle. Find missing sides from known area. Convert between metric and imperial area units.
๐งฎArea of Crescent Calculator
Calculate area of crescent with inputs, formulas, and instant results.
๐งฎCenter of Mass Calculator
Calculate center of mass with inputs, formulas, and instant results.