1

I want to calculate corr between two series. I defined them as:

s = pd.Series([1,2,3,4,5,6,7])
s2 = pd.Series([2,3,4])

what i want is a correlation series, that the values is the corr between s.rolling(3) with s2

For example: the first element of result should be [1,2,3].corr(s2), the second should be [2,3,4].corr(s2), .....

I read the usage example of rolling, but i think it inner method rolling(3).corr cant solve this, is there any good methods to do this?

xyhuang
  • 414
  • 3
  • 11

2 Answers2

1

Can you do a rolling apply:

from scipy.stats import pearsonr
s.rolling(3).apply(lambda x: pearsonr(x,s2)[0])

Output:

0    NaN
1    NaN
2    1.0
3    1.0
4    1.0
5    1.0
6    1.0
dtype: float64
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
0

Why not just use a for loop?

import pandas as pd
s = pd.Series([1,2,3,4,5,6,7])
s2 = pd.Series([2,3,4])
window_size = len(s2)

output = []
for i in range(len(s)-window_size):
    output.append(s[i:window_size+i].corr(s2))
William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33