2014-03-13 92 views
4

matplotlib中自動關聯的計算方式與pandas.tools.plotting,sm.graphics.tsa.plot_acf等其他庫不同的是什麼?matplotlib中的自動關聯和pandas.tools.plotting中的自動關聯有什麼區別?

從下面的代碼我們可以注意到,這兩個庫返回的自動關聯值不同,就像matplotlib返回大於零的所有自動關聯值,並且pandas.tools.plotting返回一些-ve自動關聯值(除了置信區間,負x軸)。

import matplotlib.pyplot as plt 
import statsmodels.api as sm 
import pandas as pd 
from pandas.tools.plotting import autocorrelation_plot 

dta = sm.datasets.sunspots.load_pandas().data 
dta.index = pd.Index(sm.tsa.datetools.dates_from_range('1700', '2008')) 
del dta["YEAR"] 

plt.acorr(dta['SUNACTIVITY'],maxlags = len(dta['SUNACTIVITY']) -1, linestyle = "solid", usevlines = False, marker='') 
plt.show() 

autocorrelation_plot(dta['SUNACTIVITY']) 
plt.show() 

回答

2

熊貓繪圖和statsmodel圖形中的自動關聯在計算自相關之前標準化數據。這些庫將平均值和除以數據的標準偏差。

使用標準化時,他們假定您的數據是用高斯定律(具有一定的均值和標準差)生成的。事實可能並非如此。

相關性很敏感。這兩種功能(matplotlib和熊貓繪圖)都有其缺點。通過使用matplotlib下面的代碼生成

圖將等同於圖中由繪製大熊貓或statsmodels圖形

dta['SUNACTIVITY_2'] = dta['SUNACTIVITY'] 
dta['SUNACTIVITY_2'] = (dta['SUNACTIVITY_2'] - dta['SUNACTIVITY_2'].mean())/ (dta['SUNACTIVITY_2'].std()) 
plt.acorr(dta['SUNACTIVITY_2'],maxlags = len(dta['SUNACTIVITY_2']) -1, linestyle = "solid", usevlines = False, marker='') 
plt.show() 

源代碼生成:

Matplotlib

Pandas

相關問題