Now that we understand the idea of covariance, we want to transition into talking about correlation
and specifically the Pearson correlation coefficient, which we often indicate with this letter R or
sometimes the Greek letter rho.
But either way, whether we indicate it with R or rho, that Pearson correlation coefficient is just
referring to the correlation between the variables X and Y, or between these two data series X and
Y, and we calculate it.
And this is why we had to learn covariance first as the covariance divided by the product of the standard
deviations of X, And why, given this formula, we can see that there are several ways we could go
about actually calculating correlation.
Of course, we could first calculate a complete covariance value and then we could calculate both standard
deviations and we could divide covariance by the product of the standard deviations.
Or we could break these values down using the formulas for each of them individually.
And it would look something like this.
So here in the numerator we have the formula for covariance between x and Y, and then in the denominator
we have each of the standard deviation values.
So we could go off of this formula as well.
Sometimes we'll see the correlation formula as a simplified version of this one.
It looks like this.
And basically what we've recognized is that in the denominator here, when we have the square root of
the product of one over N and this sum, we can take the square root of each of those individually.
So this here, this four square root is equivalent to the square root of one over n multiplied by the
square root of this whole sum.
Essentially what that means is that we can kind of pull out in front this square root of one over N
and this square root of one over N, And if we multiply those together, then they simplify to just
one over.
N.
If we pull out this and this from underneath the square root, we can write them separately as the square
root of one over n times the square root of one over n.
Of course, when we multiply those square roots together, the roots cancel and we're just left with
one over.
N.
So this whole thing here is equivalent to one over N And now we can see that we get this one over N
to cancel with this one over N giving us this formula right here, which means we can calculate correlation
either way.
We can calculate it using this formula or using this formula.
We just have to be careful that we're not dividing by n here in the numerator and then leaving out the
one over n as part of both roots in the denominator or vice versa.
Because realize that this value we have here in the numerator is not the covariance of x and Y because
it doesn't include this one over n value.
So if we calculate covariance and then use this square root here in the denominator, we'll get an incorrect
value for correlation.
The only reason this formula works is because we cancel this one over n from every part of our formula,
including the covariance formula.
And so this is not the full covariance formula.
So we just have to be really careful with which pieces we're using to calculate correlation.
But other than that, actually calculating this value is pretty straightforward because we already know
how to calculate covariance.
We already know how to calculate standard deviations.
Again, just like with covariance, especially as our data sets get larger and larger, we're definitely
going to want to use a calculator or software to find all of these values, to sum up all of these values.
And so here we've extended the table that we used to look at covariance to include values for correlation.
So we've already seen this table when we talked about covariance up to this part right here and now
we've just added these two extra columns.
So we already have our covariance value and we're essentially going to be using this formula right here.
So let's start by getting rid of all this cancellation we did.
So working from this formula, we essentially just need to calculate the standard deviation with respect
to X and Y.
So if we look at our standard deviation formula here, we have x ABI minus x bar.
What we already calculated that value when we found covariance, we have it here in our table.
Now all we have to do is square it.
So in this column here, we're just squaring the value from this column.
So we square that value.
We're also going to need the squared Y sub B minus Y bar value, which we have here.
And then this summation notation here just tells us to sum up all of those squared values.
So we find all those values and then here we take their sum.
So we have the sum of all the x sub by minus x bar quantity squared values, and that's about 10.8.
And then this 49.4 is this sum here, the sum of all the Y sub by minus y bar, quantity squared values.
And once we have those two sums, we just have to divide by n the number of data points, which in this
case is seven we have here and equals seven.
So we have to divide by seven.
That's these values here, which means that these two values are the variance, not the covariance,
but the variance of X and Y individually.
So the variance of X, the variance of Y, and then we take their square roots and we get the standard
deviations.
So these here, this is the standard deviation for X, and this is the standard deviation for Y, which
means that if we round these values, what we're getting here is approximately the covariance, which
we said was about 3.2449 divided by and I'm just rounding here, but divided by the standard deviation
with respect to X, So that's about 1.245, so 1.2, four, five.
And then the standard deviation with respect to Y, which is about 2.657, so 2.657.
So this is the calculation we're doing to find our correlation value right here.
We're just taking this covariance, dividing it by the product of these two standard deviations.
And the correlation value we get is about 0.9805 rounded to four decimal places.
So approximately 0.9805.
Now, the reason that correlation is so helpful to us is because it's a standardized version of covariance.
If we think back to when we learned about variance and standard deviation for a single variable, we
can kind of think about it this way.
So when we were talking about variance in standard deviation, we sort of had this business need to
measure the dispersion or the spread of a single variable.
And so we would calculate variance, and variance is a statistical measure of spread, but it's not
as intuitive or helpful because it's not in the same units as the original data.
If the original data was given in meters, then variance would always be in meters squared.
So if we take the square root of variance, we get here standard deviation and the units of standard
deviation match the units of the original data.
And so standard deviation is a standardized, more intuitive measure of spread or dispersion when we're
talking about just one variable.
Well, covariance and correlation sort of act in the same way.
Our goal here is to get a measure of the relationship between two variables instead of the measure of
dispersion or measure of spread of one variable.
That's sort of our business requirement or our real world goal.
And we can calculate covariance as a statistical measure of the relationship between two variables.
And that's helpful to a degree, but for the same reason that variance was only marginally helpful.
The units of covariance don't match the original units, so if the original units of X are in.
Meters and the original units of Y are in meters, then the units of covariance are going to be in meters
squared, which gives us some idea of the measure of the relationship but isn't super intuitive.
The correlation coefficient by dividing by the two standard deviations removes these units entirely,
and the correlation coefficient are just correlation actually has no units at all.
Because if you think about it, if we said here that covariance was in meters squared, well, the units
of standard deviation for these two measures are going to be in meters and in meters.
And so when we divide meter squared by meters and meters, all the units will cancel and we're left
with a unit list measurement of correlation.
And what that means in this case is that correlation is this standardized value that always falls between
negative one and positive one.
So in the same way that maybe probability exists on a scale of 0 to 1, because we can only have a 0%
chance that something happens all the way up to a 100% chance that something happens, but nothing less
than zero and nothing greater than 100.
So probability is locked on this 0 to 1 scale.
In that same way correlation is locked on this -1 to 1 scale.
And so it sort of gives us a measure of power or this particular scale on which to understand the strength
of a relationship.
So if covariance only gave us the direction of the relationship, correlation not only gives us the
direction of the relationship, but also the strength of the relationship.
So we want to think about covariance as just direction, but correlation as direction and.
Strength, and that's where the standardized scale comes in.
So we said with covariance, if we got a positive value, it told us that our two variables are two
measures.
X and Y moved in the same direction.
They would always increase together or they would decrease together, whereas a negative value for covariance
told us that our variables moved in the opposite direction.
One would decrease while the other would increase or vice versa.
But it didn't give us any indication of the strength of the relationship.
Whereas with correlation here on this -1 to 1 scale, if we think about here, the line or this is negative
one, this is one, and then we have zero in the middle here.
If we find a value for correlation between zero and one, it means that the direction of the relationship
is positive and so X and Y move in the same direction.
They both increase or they both decrease.
Whereas if we get a value for correlation between zero and negative one, it tells us that the variables
X and Y move in the opposite direction.
One increases, while the other decreases or vice versa.
But the value we find for correlation also gives us the strength of the relationship in the sense that
if we are much closer to positive one over here or to negative one all the way over here on the left,
we know that we have a strong positive relationship on the right or a strong negative relationship on
the left, meaning that the two variables are highly correlated or closely correlated.
Contrast that with finding a value for correlation that's close to zero, whether that's positive or
negative.
So something like 0.1 or -0.1, some value that's close to zero either on the positive side or the negative
side, that would tell us that there is a very weak relationship in the data.
So a weak correlation or a low correlation and visually we can think about that as just how tightly
clustered the data is around a trend.
So in really super simple terms, if we have a data set and that data set.
Looks.
Like this.
We already know that that's a positive correlation.
We know that we would calculate a positive value for covariance, for correlation.
Because of the positive covariance, we know that correlation will also be positive, but because all
of the data is perfectly in alignment along this one line, we know that that's a very strong correlation
in this particular case, a very strong positive correlation.
And so we know that we're going to get a correlation very close to one.
And in fact, that's what we got here with this data set.
We were looking at an extremely strong positive correlation, an extremely strong positive relationship.
And so our data probably looks something much like this.
If we had a very strong negative correlation that would look like this.
So again, a very strong relationship where the data is all tightly clustered close to this line, but
just in the negative direction with a negative slope.
So here this was blue.
Let's say that this is red.
And then if we call a positive but weak correlation or weak relationship, something that looks like
this, so we might have.
Data.
That looks something like this so we can see the positive direction, but it's not tightly pulled to
this middle trend line that's a weaker positive relationship or a weak positive correlation.
And then a weak negative correlation will do in purple and that.
Would look something.
Like this where we see that negative direction of the data, but the correlation is weaker.
Covariance doesn't give us any intuition, any insight whatsoever about how tightly the data is packed
around this trend line.
It just gives us direction.
But obviously when we're answering real world questions, we really want to know, is the trend super
strong?
Is it super highly correlated?
Do the two variables match up in lockstep with each other?
Whereas one variable changes the other, one changes predictably along this line, and we can say that
the two variables are very highly correlated.
That's something we want to know.
And standardising covariance by dividing by the product of the standard deviations gives us correlation,
which does give us insight into how strong or weak the relationship is.
If this blue data set here is the data that we get about sales compared to how much money we spend on
marketing.
Then this data here is telling us very clearly that as we spend more money on marketing, as our marketing
advertising budget increases, sales also increase in lockstep.
Then we absolutely want to pour more money into marketing and advertising so that we can see sales go
up.
But if this is the data set we're getting instead this green data set, we can see that the data does
have a positive correlation, but it's not necessarily always the case that as we increase our marketing
and advertising budget that sales will go up.
The relationship between those two things is not nearly as strong as the relationship we see up here.
And so as a business, we might choose to make slightly different decisions based on whether we get
this blue data set or this green data set.
But we only have insight into how strong this relationship is if we standardize our covariance statistic
by dividing by the product of the standard deviations in order to turn it into this correlation measure.