<jupyter_start><jupyter_text>What is Covariance?In mathematics and statistics, covariance is a measure of the relationship between two random variables. The metric evaluates how much – to what extent – the variables change together. In other words, it is essentially a measure of the variance between two variables. However, the metric does not assess the dependency between variables. Unlike the correlation coefficient, covariance is measured in units. The units are computed by multiplying the units of the two variables. The variance can take any positive or negative values. The values are interpreted as follows:    Positive covariance: Indicates that two variables tend to move in the same direction.    Negative covariance: Reveals that two variables tend to move in inverse directions.![covariance1.jpg](attachment:covariance1.jpg) For a sample covariance, the formula is slightly adjusted:![covarianceSample.jpg](attachment:covarianceSample.jpg)Where:    Xi – the values of the X-variable    Yj – the values of the Y-variable    X̄ – the mean (average) of the X-variable    Ȳ – the mean (average) of the Y-variable    n – the number of data points  Covariance vs. CorrelationCovariance and correlation both primarily assess the relationship between variables. The closest analogy to the relationship between them is the relationship between the variance and standard deviation.Covariance measures the total variation of two random variables from their expected values. Using covariance, we can only gauge the direction of the relationship (whether the variables tend to move in tandem or show an inverse relationship). However, it does not indicate the strength of the relationship, nor the dependency between the variables.On the other hand, correlation measures the strength of the relationship between variables. Correlation is the scaled measure of covariance. It is dimensionless. In other words, the correlation coefficient is always a pure value and not measured in any units.The relationship between the two concepts can be expressed using the formula below:![covariance3.png](attachment:covariance3.png)Where:    ρ(X,Y) – the correlation between the variables X and Y    Cov(X,Y) – the covariance between the variables X and Y    σX – the standard deviation of the X-variable    σY – the standard deviation of the Y-variable  For Example:-1. Obtain the data.First, John obtains the figures for both ABC Corp. stock and the S&P 500. The prices obtained are summarized in the table below:![CovarianceEx1.png](attachment:CovarianceEx1.png) 2. Calculate the mean (average) prices for each asset.![CovarianceEx2.png](attachment:CovarianceEx2.png) 3. For each security, find the difference between each value and mean price.![CovarianceEx3.png](attachment:CovarianceEx3.png) 4. Multiply the results obtained in the previous step.5. Using the number calculated in step 4, find the covariance.![CovarianceEx4.webp](attachment:CovarianceEx4.webp)In such a case, the positive covariance indicates that the price of the stock and the S&P 500 tend to move in the same direction.<jupyter_code>import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
A = [1692,1978,1884,2151,2519]
B = [68,102,110,112,154]

data = np.array([A,B])

covMatrix = np.cov(data)
print (covMatrix)
sns.heatmap(covMatrix, annot=True, fmt='g')
plt.show()<jupyter_output>[[97732.7  9107.3]
 [ 9107.3   941.2]]<jupyter_text>Two variable thats why 2*2 matrix    Cov = [[Caa , Cab]          [Cba , Cbb]]Where,    Caa(97732.7):- It represents the variance of variable A with degree of freedom n-1     Cab(9107.3):- It represents the covariance of variable A and B with degree of freedom n-1     Cba(9107.3):- It represents the covariance of variable B and A with degree of freedom n-1     Cbb(941.2):-It represents the variance of variable B with degree of freedom n-1<jupyter_code>import numpy as np

A = [1692,1978,1884,2151,2519]
B = [68,102,110,112,154]

data = np.array([A,B])

covMatrix = np.cov(data, bias = True) # Degree of freedom is n
print (covMatrix)
sns.heatmap(covMatrix, annot=True, fmt='g')
plt.show()<jupyter_output>[[78186.16  7285.84]
 [ 7285.84   752.96]]<jupyter_text>Two variable thats why 2*2 matrix    Cov = [[Caa , Cab]          [Cba , Cbb]]Where,    Caa(97732.7):- It represents the variance of variable A with degree of freedom n    Cab(9107.3):- It represents the covariance of variable A and B with degree of freedom n    Cba(9107.3):- It represents the covariance of variable B and A with degree of freedom n    Cbb(941.2):-It represents the variance of variable B with degree of freedom n<jupyter_code>import numpy as np

A = [45,37,42,35,39]
B = [38,31,26,28,33]
C = [10,15,17,21,12]

data = np.array([A,B,C])

covMatrix = np.cov(data)
print (covMatrix)
sns.heatmap(covMatrix, annot=True, fmt='g')
plt.show()<jupyter_output>[[ 15.8    9.6  -12.  ]
 [  9.6   21.7  -17.25]
 [-12.   -17.25  18.5 ]]
{'fraction_non_alphanumeric': 0.08191056910569106, 'fraction_numerical': 0.04857723577235772, 'mean_word_length': 4.566742081447964, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '52935', 'license': 'Apache-2.0', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1403, 'n_words': 635, 'path': 'Statistics/Covariance.ipynb', 'repo_name': 'himanshu-1205/Statistics', 'size': '4920'}