2016-09-22 24 views
1

私は2つの比較的小さな時系列間の相互相関に取り組んでいますが、達成しようとすると私は自分自身を調整できない問題にぶつかっています。まず、plt.xcorrnp.correlateの間の依存関係を理解し​​ます。しかし、plt.xcorrとの差を調整するのに問題がありますか?0ラグでのノーマルplt.xcorrとnp.corrcoefとの差

a = np.array([ 7.35846410e+08, 8.96271634e+08, 6.16249222e+08, 
    8.00739868e+08, 1.06116376e+09, 9.05690167e+08, 
    6.31383600e+08]) 
b = np.array([ 1.95621617e+09, 2.06263134e+09, 2.27717015e+09, 
    2.27281916e+09, 2.71090116e+09, 2.84676385e+09, 
    3.19578883e+09]) 

np.corrcoef(a,b) 
# returns: 
array([[ 1.  , 0.02099573], 
     [ 0.02099573, 1.  ]]) 

plt.xcorr(a,b,normed=True, maxlags=1) 
# returns: 
array([-1, 0, 1]), 
array([ 0.90510941, 0.97024415, 0.79874158]) 

私はこれらが同じ結果を返すと予想しました。私は明らかにどのようにplt.xcorrが規範されているのか理解できません。

答えて

0

私はノルムhttp://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xcorr

を使用:ブール、オプション、デフォルト:true

真は、0番目の遅れで自己相関することによって、データを正規化した場合。

次のコードでは、plt_corrnp_corrになります。

plt_corr = plt.xcorr(a, b, normed=True, maxlags=6) 

c = np.correlate(a, a) # autocorrelation of a 
d = np.correlate(b, b) # autocorrelation of b 
np_corr = np.correlate(a/np.sqrt(c), b/np.sqrt(d), 'full') 
1

標準的な「ピアソン積 - 相関係数」の計算は、平均値でシフトされたサンプルを使用しています。 相互相関係数は正規化されたサンプルを使用しません。 それ以外は計算が似ています。しかし、それらの係数は異なる式と異なる意味を持っています。彼らは、サンプルabの平均値は、平均値でシフトするサンプルを変更していない場合(0に等しい場合にのみ等しい

import numpy as np 
import matplotlib.pyplot as plt 

a = np.array([7.35846410e+08, 8.96271634e+08, 6.16249222e+08, 
    8.00739868e+08, 1.06116376e+09, 9.05690167e+08, 6.31383600e+08]) 
b = np.array([1.95621617e+09, 2.06263134e+09, 2.27717015e+09, 
    2.27281916e+09, 2.71090116e+09, 2.84676385e+09, 3.19578883e+09]) 

y = np.corrcoef(a, b) 
z = plt.xcorr(a, b, normed=True, maxlags=1) 
print("Pearson product-moment correlation coefficient between `a` and `b`:", y[0][1]) 
print("Cross-correlation coefficient between `a` and `b` with 0-lag:", z[1][1], "\n") 


# Calculate manually: 

def pearson(a, b): 
    # Length. 
    n = len(a) 

    # Means. 
    ma = sum(a)/n 
    mb = sum(b)/n 

    # Shifted samples. 
    _ama = a - ma 
    _bmb = b - mb 

    # Standard deviations. 
    sa = np.sqrt(np.dot(_ama, _ama)/n) 
    sb = np.sqrt(np.dot(_bmb, _bmb)/n) 

    # Covariation. 
    cov = np.dot(_ama, _bmb)/n 

    # Final formula. 
    # Note: division by `n` in deviations and covariation cancel out each other in 
    #  final formula and could be ignored. 
    return cov/(sa * sb) 

def cross0lag(a, b): 
    return np.dot(a, b)/np.sqrt(np.dot(a, a) * np.dot(b, b)) 

pearson_coeff = pearson(a, b) 
cross_coeff = cross0lag(a, b) 

print("Manually calculated coefficients:") 
print(" Pearson =", pearson_coeff) 
print(" Cross =", cross_coeff, "\n") 


# Normalized samples: 
am0 = a - sum(a)/len(a) 
bm0 = b - sum(b)/len(b) 
pearson_coeff = pearson(am0, bm0) 
cross_coeff = cross0lag(am0, bm0) 
print("Coefficients for samples with means = 0:") 
print(" Pearson =", pearson_coeff) 
print(" Cross =", cross_coeff) 

出力:

Pearson product-moment correlation coefficient between `a` and `b`: 0.020995727082 
Cross-correlation coefficient between `a` and `b` with 0-lag: 0.970244146831 

Manually calculated coefficients: 
    Pearson = 0.020995727082 
    Cross = 0.970244146831 

Coefficients for samples with means = 0: 
    Pearson = 0.020995727082 
    Cross = 0.020995727082 
関連する問題