2020年4月27日 星期一

Calculating Covariance using Excel and Python

Excel covariance

https://support.microsoft.com/en-us/office/covariance-p-function-6f0e1e6d-956d-4e4b-9943-cfef0bf9edfc

The formula =COVARIANCE.S(A1:A5,B1:B5) gives 6.5

3 9
2 7
4 12
5 15
6 17 6.5


Python 3 Jupyter Notebook

import pandas as pd, numpy as np
df = pd.DataFrame(list(zip([3,2,4,5,6], [9,7,12,15,17])), columns =['a', 'b'])

ab
039
127
2412
3515
4617

df.cov()
ab
a2.56.5
b6.517.0

df.cov().iloc[0, 1]

6.5

沒有留言:

張貼留言