Definition: The statistical measures which show a relationship between two or more variables are called Measures of Relationship. Correlation and Regression are commonly used measures of relationship. In this blog, we will understand the Covariance measure and its calculations steps. Part 2 of this blog will explain the calculation of Correlation.
[Related read: Linear Regression Blog Series]
Covariance
Covariance is the measure of the joint variability of two random variables [X, Y]. For Example – Income and Expense of Households. The households having higher Income [say X] will have relatively higher Expenses [say Y] and vice-versa. This kind of relationship between two variables is called joint variability and is measured through Covariance and Correlation.
Covariance is represented as Cov[X, Y]. [Wikipedia link]. The covariance can Positive, Negative, or Zero.
Positive Covariance: If the variable[X] takes a higher value, the value of the corresponding variable[Y] is also higher and vice-versa.E.x. Income and Expense of Household. As X takes a higher value, the corresponding values of Y is on the higher side
Example: Price and Demand. As the Price of a commodity increases, its Demand decreases.
Note: The Zero Covariance means the covariance will be zero or near zero
Formula
Hands-on Example
To understand the concept of covariance, it is important to do some hands-on activity. A sample survey data of 15 households is given below. The fields are Monthly Income, Monthly Expense, and Annual Income details of the households.
Mthly_HH_IncomeMthly_HH_ExpenseAnnual_HH_Income50008000642006000700079920100004500112800100002000972001250012000147000140008000196560150001600016740018000200002160001900090002188802000090002208002000018000278400220002500027984023400500029203224000105003168002400010000244800
Scatter Plot
A scatter plot is best used to visually see the linear relationship between X and Y.
However, the linearity between Monthly Income and Annual Income appears to be much strong as compared to the relationship between Monthly Income and Monthly Expense.
The strength of the linear relationship between two continuous variables is measured by a statistical measure called Correlation
Covariance Calculations
Let us denote Monthly Household Income as X and Monthly Household Expense as Y. Then the covariance of Monthly Income and Expense is:
Cov[X,Y] = sum[ [X - mean[x]] * [Y - mean[y]] ] / [n - 1]
Mean calculation
# Calculating mean[X] mean[x] = [5000+6000+10000+10000+12500+14000+15000+18000+19000+20000+20000+22000+23400+24000+24000] / 15 mean[x] = 242900 / 15 mean[x] = 16193.33 # Calculating mean[Y] mean[y] = [8000+7000+4500+2000+12000+8000+16000+20000+9000+9000+18000+25000+5000+10500+10000] / 15 mean[y] = 164000 / 15 mean[y] = 10933.33
Intermediate covariance calculation steps
Monthly Inc.
[X]
[Y]X – mean[x]Y – mean[y][X – mean[x]]
* [Y – mean[y]]50008000-11193.33-2933.3332833777.7860007000-10193.33-3933.3340093777.78100004500-6193.33-6433.3339843777.78100002000-6193.33-8933.3355327111.111250012000-3693.331066.67-3939555.56140008000-2193.33-2933.336433777.781500016000-1193.335066.67-6046222.2218000200001806.679066.6716380444.441900090002806.67-1933.33-5426222.222000090003806.67-1933.33-7359555.5620000180003806.677066.6726900444.4422000250005806.6714066.6781680444.442340050007206.67-5933.33-42759555.5624000105007806.67-433.33-3382888.8924000100007806.67-933.33-7286222.22
Sum[X – mean[x]] * [Y – mean[y]]
Final covariance calculation step
n = 15
mean[x] = 16193.33
mean[y] = 10933.33
sum[ [X - mean[x]] * [Y - mean[y]] ] = 223293333.33
#Therefore the Covariance of Sample monthly Household Income and Expence is
Cov[X,Y] = sum[ [X - mean[x]] * [Y - mean[y]] ] / [n - 1]
Cov[X,Y] = 223293333.33 / [15 - 1] => 223293333.33 / 14
Cov[X,Y] = 15949523.81
Cov[Monthly Income , Monthly Expense] = 15949523.81
Interpretation of Covariance
- The covariance between the Monthly Income and the Monthly Expense is 15949523.81.
- It is a positive number, hence we conclude there is a positive relationship between Monthly Household Income and the Expense. i.e., when the Monthly Household Income takes a higher value, the corresponding Expense value is also likely to be higher and vice-versa.
Disadvantage of Covariance
- Covariance only measures the direction of the relationship, but it does not measure the strength of the relationship. In order to measure the strength, we need to calculate the normalized version of covariance, i.e., Correlation
Application of Variance-Covariance: Beta of Stock
The variance-covariance measures do not have any business meaning by themselves. However, these measures are used in calculations of other test statistics like ANOVA, R-Squared, hypothesis testing, statistical inference, and more. One practical application of Variance-Covariance is in calculating the Beta of Stock. Beta is a concept that measures the expected move in a stock relative to movements in the overall market. [Investopedia article on Beta of Stock]
Correlation
- Covariance only shows the direction of the linear relationship between two Variables [I.e., Positive, Negative, or No Covariance]. It cannot measure the strength of the relationship between the two variables.
- To measure both the strength and direction of the linear relationship between two variables, we use a statistical measure called correlation.
- The correlation only measures the association. The Association is not Causation.
Formula
- The Formula to Calculate the Correlation Coefficient [r] between Variable is
r = Covariance[x,y] / [[Standard deviation of X] * [Standard deviation of Y]]
- ‘r’ takes any value between -1 and 1
Correlation RangeInterpretationr = 1Perfectly Positive Linear Relationship between two variablesr = -1Perfectly Negative Linear Relationship between two variablesr = 0No Relationship between two variables
Positive, Negative, Zero Correlations
The two variables[X, Y] can have Positively Correlation, Negatively Correlation, or Zero correlation.
- If the Value in Variable [X] is high, the Corresponding Value of Variable [Y] is also high. Similarly, If the Value in Variable [X] is Low, the Corresponding Value of Variable [Y] is also Low. Then it is Positively Correlated.
- The Value of Correlation Coefficient [r] will be Positive.
- If the Value in Variable [X] is high, the Corresponding Value of Variable [Y] is low. Similarly, If the Value in Variable [X] is Low, the Corresponding Value of Variable [Y] is also high. Then it is Negatively Correlated.
- The Value of Correlation Coefficient [r] will be Negative.
- There will be no relationship between the two variables [X, Y].
- The Value of the Correlation Coefficient [r] will be Zero
Hands-on Example
Let’s calculate the correlation coefficient between two variables [monthly Income, Monthly Expense] for 15 sample household Survey data given in the below table.
Mthly_HH_IncomeMthly_HH_ExpenseAnnual_HH_Income50008000642006000700079920100004500112800100002000972001250012000147000140008000196560150001600016740018000200002160001900090002188802000090002208002000018000278400220002500027984023400500029203224000105003168002400010000244800
Correlation Calculations
- Let X be the Monthly Income and Y be Monthly Expense, Then the Correlation coefficient r is,
r = Cov[X,Y] / [Std[X] * Std[Y]]
- In the previous blog, We have already calculated the Covariance between the Variable Monthly Income and Monthly Expense. Refer to the Previous blog for Covariance calculations.
Cov[X,Y] = 15949523.81
- Mean Calculations
#Calculating mean[X] mean[x] = [5000+6000+10000+10000+12500+14000+15000+18000+19000+20000+20000+22000+23400+24000+24000] / 15 mean[x] = 242900 / 15 mean[x] = 16193.33 #Calculating mean[Y] mean[y] = [8000+7000+4500+2000+12000+8000+16000+20000+9000+9000+18000+25000+5000+10500+10000] / 15 mean[y] = 164000 / 15 mean[y] = 10933.33
- Intermediate Correlation Calculations
Mthly_HH_Income [X]Mthly_HH_Expense [Y]X – Mean[X][X – Mean[X]]^2Y – Mean[Y][Y – Mean[Y]]^25000.008000.00-11193.33125290711.11-2933.338604444.446000.007000.00-10193.33103904044.44-3933.3315471111.1110000.004500.00-6193.3338357377.78-6433.3341387777.7810000.002000.00-6193.3338357377.78-8933.3379804444.4412500.0012000.00-3693.3313640711.111066.671137777.7814000.008000.00-2193.334810711.11-2933.338604444.4415000.0016000.00-1193.331424044.445066.6725671111.1118000.0020000.001806.673264044.449066.6782204444.4419000.009000.002806.677877377.78-1933.333737777.7820000.009000.003806.6714490711.11-1933.333737777.7820000.0018000.003806.6714490711.117066.6749937777.7822000.0025000.005806.6733717377.7814066.67197871111.1123400.005000.007206.6751936044.44-5933.3335204444.4424000.0010500.007806.6760944044.44-433.33187777.7824000.0010000.007806.6760944044.44-933.33871111.11sum[[X – mean[X]]^2]573449333.33sum[[Y – mean[Y]]^2]554433333.33
- Standard deviation Calculations
Total Number of Observation, n = 15 #Standard deviation of X Std[X] = sqrt[sum[[X - mean[X]]^2] / [n - 1]] sum[[X - mean[X]]^2]= 573449333.33 #From the Above table Std[X] = sqrt[[573449333.33] / [15 - 1]] => sqrt[[573449333.33] / [14]] Std[X] = sqrt[40960666.67] Std[X] = 6400.05 #Standard deviation of Y
Std[Y] = sqrt[sum[[Y - mean[Y]]^2] / [n - 1]] sum[[Y - mean[Y]]^2]= 554433333.33 #From the Above table Std[Y] = sqrt[[554433333.33] / [15 - 1]] => sqrt[[554433333.33] / [14]] Std[Y] = sqrt[39602380.95] Std[Y] = 6293.04
- Final Correlation Calculation
Cov[X,Y] = 15949523.81 Std[X] = 6400.05 Std[Y] = 6293.04 #Correlation Coefficient r = Cov[X,Y] / [Std[X] * Std[Y]] r = 15949523.81 / [6400.05 * 6293.04] r = 15949523.81 / 40275770.65 r = 0.396
The correlation between monthly Income and monthly Expense is 0.396. Therefore, there is a Low Positive correlation between Monthly Household Income [X], and the Monthly Household Expense [Y].