Las correlaciones son una gran herramienta para aprender cómo cambia una cosa con otra. Después de leer esto, debe comprender qué es la correlación, cómo pensar en las correlaciones en su propio trabajo y codificar una implementación mínima para calcular las correlaciones.
Una correlación se trata de cómo dos cosas cambian entre sí.
La correlación es un concepto matemático abstracto, pero probablemente ya tenga una idea de lo que significa. A continuación se muestran algunos ejemplos de las tres categorías generales de correlación.
A medida que coma más, probablemente terminará sintiéndose más lleno. Este es un caso en el que dos cosas están cambiando juntas de la misma manera. Uno sube (comiendo más comida), luego el otro también sube (sintiéndose lleno). Ésta es una correlación positiva .

Cuando esté en un automóvil y vaya más rápido, probablemente llegará a su destino más rápido y el tiempo total de viaje será menor. Este es un caso de dos cosas que cambian en la dirección opuesta (más velocidad, pero menos tiempo). Ésta es una correlación negativa .

También hay una tercera forma en que dos cosas pueden "cambiar". O mejor dicho, no cambiar. Por ejemplo, si aumentara de peso y observara cómo cambiaron los puntajes de sus exámenes, probablemente no habrá ningún patrón general de cambio en los puntajes de sus exámenes. Esto significa que no hay correlación.

Saber cómo cambian dos cosas juntas es el primer paso para la predicción
Ser capaz de describir lo que está sucediendo en nuestros ejemplos anteriores es genial y todo. Pero cual es el punto? La razón es aplicar este conocimiento de manera significativa para ayudar a predecir lo que sucederá a continuación.
En nuestro ejemplo de alimentación, podemos registrar cuánto comemos durante toda una semana y luego anotar qué tan llenos nos sentimos después. Como descubrimos antes, cuanto más comemos, más llenos nos sentimos.
Después de recopilar toda esta información, podemos hacer más preguntas sobre por qué sucede esto para comprender mejor esta relación. Aquí, podemos comenzar a preguntarnos qué tipo de alimentos nos sacian más, o si la hora del día también afecta qué tan llenos nos sentimos.
También se puede aplicar un pensamiento similar a su trabajo o negocio. Si nota que las ventas u otras métricas importantes aumentan o disminuyen con otra medida de su negocio (en otras palabras, las cosas están correlacionadas positivamente o negativamente), puede valer la pena explorar y aprender más sobre esa relación para mejorar su negocio.
Las correlaciones pueden tener diferentes niveles de fuerza
Hemos cubierto algunas correlaciones generales como
- positivo,
- negativo, o
- inexistente
Aunque esas descripciones están bien, no todas las correlaciones positivas y negativas son iguales.
Estas descripciones también se pueden traducir a números. Un valor de correlación puede tomar cualquier valor decimal entre uno negativo, \ (- 1 \), y uno positivo, \ (+ 1 \).
Los valores decimales entre \ (- 1 \) y \ (0 \) son correlaciones negativas, como \ (- 0.32 \).
Los valores decimales entre \ (0 \) y \ (+ 1 \) son correlaciones positivas, como \ (+ 0.63 \).
Una correlación cero perfecta significa que no hay correlación.
Para cada tipo de correlación, existe un rango de correlaciones fuertes y correlaciones débiles. Los valores de correlación más cercanos a cero son correlaciones más débiles , mientras que los valores más cercanos a uno positivo o negativo son correlaciones más fuertes .
Las correlaciones fuertes muestran tendencias más obvias en los datos, mientras que las débiles se ven más desordenadas. Por ejemplo, la correlación positiva alta más fuerte a continuación se parece más a una línea en comparación con la correlación positiva más débil y más baja.

De manera similar, las correlaciones fuertemente negativas tienen una tendencia más obvia que la correlación negativa más débil y más baja.

¿De dónde proviene el valor r ? ¿Y qué valores puede tomar?
El " valor r " es una forma común de indicar un valor de correlación. Más específicamente, se refiere a la correlación (muestra) de Pearson, o r de Pearson . La nota de "muestra" es para enfatizar que solo puede reclamar la correlación para los datos que tiene, y debe tener cuidado al hacer afirmaciones más amplias más allá de sus datos.
The table below summarizes what we've covered about correlations so far.
Pearson's r value | Correlation between two things is... | Example |
---|---|---|
r = -1 | Perfectly negative | Hour of the day and number of hours left in the day |
r < 0 | Negative | Faster car speeds and lower travel time |
r = 0 | Independent or uncorrelated | Weight gain and test scores |
r > 0 | Positive | More food eaten and feeling more full |
r = 1 | Perfectly positive | Increase in my age and increase in your age |
In the next few sections, we will
- Break down the math equation to calculate correlations
- Use example numbers to use this correlation equation
- Code up the math equation in Python and JavaScript
Breaking down the math to calculate correlations
As a reminder, correlations can only be between \(-1\) and \(1\). Why is that?
The quick answer is that we adjust the amount of change in both variables to a common scale. In more technical terms, we normalize how much the two variables change together by how much each of the two variables change by themselves.
From Wikipedia, we can grab the math definition of the Pearson correlation coefficient. It looks very complicated, but let's break it down together.
\[ \textcolor{lime}{r} _{ \textcolor{#4466ff}{x} \textcolor{fuchsia}{y} } = \frac{ \sum_{i=1}^{n} (x_i - \textcolor{green}{\bar{x}})(y_i - \textcolor{olive}{\bar{y}}) }{ \sqrt{ \sum_{i=1}^{n} (x_i - \textcolor{green}{\bar{x}})^2 \sum_{i=1}^{n} (y_i - \textcolor{olive}{\bar{y}})^2 } }\]
From this equation, to find the \(\textcolor{lime}{\text{correlation}}\) between an \( \textcolor{#4466ff}{\text{x variable}} \) and a \( \textcolor{fuchsia}{\text{y variable}} \), we first need to calculate the \( \textcolor{green}{\text{average value for all the } x \text{ values}} \) and the \( \textcolor{olive}{ \text{average value for all the } y \text{ values}} \).
Let's focus on the top of the equation, also known as the numerator. For each of the \( x\) and \(y\) variables, we'll then need to find the distance of the \(x\) values from the average of \(x\), and do the same subtraction with \(y\).
Intuitively, comparing all these values to the average gives us a target point to see how much change there is in one of the variables.
This is seen in the math form, \(\textcolor{#800080}{\sum_{i=1}^{n}}(\textcolor{#000080}{x_i - \overline{x}})\), \(\textcolor{#800080}{\text{adds up all}}\) the \(\textcolor{#000080}{\text{differences between}}\) your values with the average value for your \(x\) variable.
In the bottom of the equation, also known as the denominator, we do a similar calculation. However, before we add up all of the distances from our values and their averages, we will multiple them by themselves (that's what the \((\ldots)^2\) is doing).
This denominator is what "adjusts" the correlation so that the values are between \(-1\) and \(1\).
Using numbers in our equation to make it real
To demonstrate the math, let's find the correlation between the ages of you and your siblings last year \([1, 2, 6]\) and your ages for this year \([2, 3, 7]\). Note that this is a small example. Typically you would want many more than three samples to have more confidence in your correlation being true.
Looking at the numbers, they appear to increase the same. You may also notice they are the same sequence of numbers but the second set of numbers has one added to it. This is as close to a perfect correlation as we'll get. In other words, we should get an \(r = 1\).
First we need to calculate the averages of each. The average of \([1, 2, 6]\) is \((1+2+6)/3 = 3\) and the average of \([2, 3, 7]\) is \((2+3+7)/3 = 4\). Filling in our equation, we get
\[ r _{ x y } = \frac{ \sum_{i=1}^{n} (x_i - 3)(y_i - 4) }{ \sqrt{ \sum_{i=1}^{n} (x_i - 3)^2 \sum_{i=1}^{n} (y_i - 4)^2 } }\]
Looking at the top of the equation, we need to find the paired differences of \(x\) and \(y\). Remember, the \(\sum\) is the symbol for adding. The top then just becomes
\[ (1-3)(2-4) + (2-3)(3-4) + (6-3)(7-4) \]
\[= (-2)(-2) + (-1)(-1) + (3)(3) \]
\[= 4 + 1 + 9 = 14\]
So the top becomes 14.
\[ r _{ x y } = \frac{ 14 }{ \sqrt{ \sum_{i=1}^{n} (x_i - 3)^2 \sum_{i=1}^{n} (y_i - 4)^2 } }\]
In the bottom of the equation, we need to do some very similar calculations, except focusing on just the \(x\) and \(x\) separately before multiplying.
Let's focus on just \( \sum_{i=1}^n (x_i - 3)^2 \) first. Remember, \(3\) here is the average of all the \(x\) values. This number will change depending on your particular data.
\[ (1-3)^2 + (2-3)^2 + (6-3)^2 \]
\[= (-2)^2 + (-1)^2 + (3)^2 = 4 + 1 + 9 = 14 \]
And now for the \(y\) values.
\[ (2-4)^2 + (3-4)^2 + (7-4)^2 \]
\[ (-2)^2 + (-1)^2 + (3)^2 = 4 + 1 + 9 = 14\]
We those numbers filled out, we can put them back in our equation and solve for our correlation.
\[ r _{ x y } = \frac{ 14 }{ \sqrt{ 14 \times 14 }} = \frac{14}{\sqrt{ 14^2}} = \frac{14}{14} = 1\]
We've successfully confirmed that we get \(r = 1\).
Although this was a simple example, it is always best to use simple examples for demonstration purposes. It shows our equation does indeed work, which will be important when coding it up in the next section.
Python and JavaScript code for the Pearson correlation coefficient
Math can sometimes be too abstract, so let's code this up for you to experiment with. As a reminder, here is the equation we are going to code up.
\[ r _{ x y } = \frac{ \sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y}) }{ \sqrt{ \sum_{i=1}^{n} (x_i - \bar{x})^2 \sum_{i=1}^{n} (y_i - \bar{y})^2 } }\]
After going through the math above and reading the code below, it should be a bit clearer on how everything works together.
Below is the Python version of the Pearson correlation.
import math def pearson(x, y): """ Calculate Pearson correlation coefficent of arrays of equal length. Numerator is sum of the multiplication of (x - x_avg) and (y - y_avg). Denominator is the squart root of the product between the sum of (x - x_avg)^2 and the sum of (y - y_avg)^2. """ n = len(x) idx = range(n) # Averages avg_x = sum(x) / n avg_y = sum(y) / n numerator = sum([(x[i] - avg_x)*(y[i] - avg_y) for i in idx]) denom_x = sum([(x[i] - avg_x)**2 for i in idx]) denom_y = sum([(y[i] - avg_y)**2 for i in idx]) denominator = math.sqrt(denom_x * denom_y) return numerator / denominator
Here's an example of our Python code at work, and we can double check our work using a Pearson correlation function from the SciPy package.
import numpy as np import scipy.stats # Create fake data x = np.arange(5, 15) # array([ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) y = np.array([24, 0, 58, 26, 82, 89, 90, 90, 36, 56]) # Use a package to calculate Pearson's r # Note: the p variable below is the p-value for the Pearson's r. This tests # how far away our correlation is from zero and has a trend. r, p = scipy.stats.pearsonr(x, y) r # 0.506862548805646 # Use our own function pearson(x, y) # 0.506862548805646
Below is the JavaScript version of the Pearson correlation.
function pearson(x, y) { let n = x.length; let idx = Array.from({length: n}, (x, i) => i); // Averages let avgX = x.reduce((a,b) => a + b) / n; let avgY = y.reduce((a,b) => a + b) / n; let numMult = idx.map(i => (x[i] - avg_x)*(y[i] - avg_y)); let numerator = numMult.reduce((a, b) => a + b); let denomX = idx.map(i => Math.pow((x[i] - avgX), 2)).reduce((a, b) => a + b); let denomY = idx.map(i => Math.pow((y[i] - avgY), 2)).reduce((a, b) => a + b); let denominator = Math.sqrt(denomX * denomY); return numerator / denominator; };
Here's an example of our JavaScript code at work to double check our work.
x = Array.from({length: 10}, (x, i) => i + 5) // Array(10) [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ] y = [24, 0, 58, 26, 82, 89, 90, 90, 36, 56] pearson(x, y) // 0.506862548805646
Feel free to translate the formula into either Python or JavaScript to better understand how it works.
In conclusion
Correlations are a helpful and accessible tool to better understand the relationship between any two numerical measures. It can be thought of as a start for predictive problems or just better understanding your business.
Correlation values, most commonly used as Pearson's r, range from \(-1\) to \(+1\) and can be categorized into negative correlation (\(-1 \lt r \lt 0\)), positive (\(0 \lt r \lt 1\)), and no correlation (\(r = 0\)).
A glimpse into the larger world of correlations
There is more than one way to calculate a correlation. Here we have touched on the case where both variables change at the same way. There are other cases where one variable may change at a different rate, but still have a clear relationship. This gives rise to what's called, non-linear relationships.
Note, correlation does not imply causation. If you need quick examples of why, look no further.
Below is a list of other articles I came across that helped me better understand the correlation coefficient.
- If you want to explore a great interactive visualization on correlation, take a look at this simple and fantastic site.
- Using Python, there multiple ways to implement a correlation and there are multiple types of correlation. This excellent tutorial shows great examples of Python code to experiment with yourself.
- A blog post by Sabatian Sauer goes over correlations using "average deviation rectangles", where each point creates a visual rectangle from each point using the mean, and illustrating it using the R programming language.
- Y para las personas profundamente curiosas, echen un vistazo a este documento que muestra 13 formas de ver el coeficiente de correlación (PDF).
Sígueme en Twitter y echa un vistazo a mi blog personal, donde comparto otras ideas y recursos útiles para la programación, las estadísticas y el aprendizaje automático.
¡Gracias por leer!