2016-04-07 45 views
0

Bazı basit veriler üzerinde bazı analizler yapıyorum ve otomatik korelasyon ve kısmi oto-korelasyon çizmeye çalışıyorum. Bu grafikleri kullanarak ARIMA modelimde çizmek için P ve Q değerini bulmaya çalışıyorum.Python - python çiziminde eksenel çizgiyi nerede çizdiğinizi bulun

Grafikler üzerinde görebiliyorum, ama açıkçası, her bir grafik için, arsanın eksen çizgisini geçtiğini açıkça söyleyebilir miyim? Yukarıdaki kodda

plt.subplot(122) 
plt.plot(lag_pacf) 
plt.axhline(y=0, linestyle = '--', color = 'grey') 
plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--',color = 'red') 
plt.axhline(y=1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--', color = 'green') 
plt.title('Partial Autocorelation Function') 

Yani

, ben bulabilirsiniz ve gösteri, nerede lag_pacf arsa Ben önceden belirlenmiş olan axhlines haçlar?

Teşekkür

+0

Alex, önerilen çözüm sorununuzu çözmedi mi? – armatita

+0

Özür dilerim, son derece meşguldüm. Şimdi doldururum :) –

cevap

3

Satır lag_pacf segmentlerinin ve y en arasındaki kesişme noktalarını hesaplamak gerekir:

Autocorrelation plot with hlines

I: Bu Tamamen rastgele örneğin

from matplotlib import pyplot as plt 
import numpy as np 
lag_pacf = np.random.randint(-10,10,30) 
log_moving_average_difference = [i for i in range(30)] 
#plt.subplot(122) 
plt.plot(lag_pacf) 
plt.axhline(y=0, linestyle = '--', color = 'grey') 
plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--',color = 'red') 
plt.axhline(y=1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--', color = 'green') 
plt.title('Partial Autocorelation Function') 
plt.xlim(0,30) 
plt.ylim(-10,10) 
plt.show() 

def line_intersection(line1, line2): 
    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0]) 
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1]) #Typo was here 

    def det(a, b): 
     return a[0] * b[1] - a[1] * b[0] 

    div = det(xdiff, ydiff) 
    if div == 0: 
     return None 

    d = (det(*line1), det(*line2)) 
    x = det(d, xdiff)/div 
    y = det(d, ydiff)/div 
    return x, y 

def near(a, b, rtol=1e-5, atol=1e-8): 
    return abs(a - b) < (atol + rtol * abs(b)) 
def crosses(line1, line2): 
    """ 
    Return True if line segment line1 intersects line segment line2 and 
    line1 and line2 are not parallel. 
    """ 
    (x1,y1), (x2,y2) = line1 
    (u1,v1), (u2,v2) = line2 
    (a,b), (c,d) = (x2-x1, u1-u2), (y2-y1, v1-v2) 
    e, f = u1-x1, v1-y1 
    denom = float(a*d - b*c) 
    if near(denom, 0): 
     # parallel 
     return False 
    else: 
     t = (e*d - b*f)/denom 
     s = (a*f - e*c)/denom 
     # When 0<=t<=1 and 0<=s<=1 the point of intersection occurs within the 
     # line segments 
     return 0<=t<=1 and 0<=s<=1 

plt.plot(lag_pacf) 
plt.axhline(y=0, linestyle = '--', color = 'grey') 
plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--',color = 'red') 
plt.axhline(y=1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--', color = 'green') 
plt.title('Partial Autocorelation Function') 

yys = [0,-1.96/np.sqrt(len(log_moving_average_difference)),1.96/np.sqrt(len(log_moving_average_difference))] 
xx, yy = [],[] 
xo,yo = [k for k in range(30)],lag_pacf 
d = 20 
for i in range(1,len(lag_pacf)): 
    for k in yys: 
     p1 = np.array([xo[i-1],yo[i-1]],dtype='float') 
     p2 = np.array([xo[i],yo[i]],dtype='float') 
     k1 = np.array([xo[i-1],k],dtype='float') 
     k2 = np.array([xo[i],k],dtype='float') 
     if crosses((p2,p1),(k1,k2)): 
      seg = line_intersection((p2,p1),(k1,k2)) 
      if seg is not None: 
       xx.append(seg[0]) 
       yy.append(seg[1]-d) 
       plt.scatter(seg[0],seg[1],c='red') 
plt.xlim(0,30) 
plt.ylim(-10,10) 
plt.show() 

bunu elde etti:

Plot with lines interceptions

+0

Teşekkürler! Geri dönüşte gecikme için özür dilerim. Yukarıdaki kod çalışır, ama bunu yazmanın yetenekli olacağını düşünmüyorum! –