Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Covariance | Covariance vs Correlation
Learning Statistics with Python
course content

Course Content

Learning Statistics with Python

Learning Statistics with Python

1. Basic Concepts
2. Mean, Median and Mode with Python
3. Variance and Standard Deviation
4. Covariance vs Correlation
5. Confidence Interval
6. Statistical Testing

bookCovariance

Covariance is a measure of the joint variability of two random variables.

The value of covarianceMeaning
PositiveTwo variables move in the same direction
0Two variables no linear relationship
NegativeTwo variables move in opposite directions

The formulas are different for the sample and population, but we will not dive deeper into them. In this chapter, we will discuss covariances of the following dataset:

Store_IDStore_AreaItems_AvailableDaily_Customer_CountStore_Sales
001659196153066490
111461175221039820
221340160972054010
331451174862053730
441770211145046620
  • Store_ID - The unique id of the store;
  • Store_Area - The area of the store;
  • Items_Available - The number of items that are available in the store;
  • Daily_Customer_Count - The daily number of customers in the store;
  • Store_Sales - The number of sales in the store.

Calculating Covariance with Python:

To compute covariance in Python, you can use the np.cov() function from the NumPy library. It requires two parameters: the sequences of data for which you want to calculate the covariance.

The result is the value at index [0,1]. This course won't cover the other values in the output, refer to the example:

123456789
import pandas as pd import numpy as np df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a849660e-ddfa-4033-80a6-94a1b7772e23/update/Stores.csv') # Calculating covariance cov = np.cov(df['Store_Area'], df['Items_Available'])[0,1] print(round(cov, 2))
copy

This indicates that the values move in the same direction. This makes sense because a larger store area corresponds to a greater number of items. One significant drawback of covariance is that the value can be infinite.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 1
some-alt