#59 Pandas (Part 36): Rolling Covariance and Correlation in Python | Tutorial

preview_player
Показать описание
The videos discusses methods to calculate rolling covariance and correlation in Python using timeseries data.

Timeline & Data
(Python 3.7)

00:00 - Welcome
00:09 - Outline of video
00:29 - Open Jupyter notebook
00:41 - Data
01:53 - Rolling Covariance: DataFrame: .rolling().cov()
03:53 - Rolling Correlation: DataFrame: .rolling().corr()
04:38 - Rolling Correlation: DataFrame: with a specific column
06:00 - Rolling Covariance: DataFrame: between groups of columns
09:30 - Unstack rolling Covariance output
11:00 - Unstack rolling Covariance output: access multi-index columns from DataFrame
11:51 - Unstack rolling Covariance output: .plot()
12:36 - Covariance between two different DataFrames: Create DataFrame
13:00 - Rename columns of DataFrame
13:10 - Covariance between two different DataFrames:
13:59 - Ending notes

###########
# Data
###########
df = pd.DataFrame({
'a': [1,2,3,4,5,6,6,5,4,3,2,1,2,2,2,2,2,2],
'b': [1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6],
'c': [0,9]*9
},
)

##########
Рекомендации по теме
Комментарии
Автор

When triggering a rolling regression (or for that matter, a simple mean) with a window size of 10, I wonder if you could show us how I can start the rolling process at a particular time date, let's say, '2024-01-02 9:31:00' .

tomrhee
Автор

Thank You very much. Clear and concise explanation. Question: i have a dataframe with returns of benchmark on col1 and returns of stocks in columns adjacent. I want to calculate a rolling Beta (rolling covariance) of all returns versus benchmark. How could we do this?

laurentbernut
Автор

what if my rows are every month since 1980 (492 rows). and my columns are 2400 stocks.
If i compute a rolling covariance with window=12, My computer crashes after 4 hours of computing. Is there any way to solve this problem? for example, do all computations needed with the first covariance, and then erase that value from the memory and do the same for the next one in memory etc? I have no idea how to solve this problem : (

duartepombo
Автор

Great lesson. One doubt.. the mean used by the computed covariance is related to the window mean or all the samples mean? Thanks in advance

deniscandido
visit shbcf.ru