ACF和PACF公式


18

我想创建一个用于从时间序列数据中绘制ACF和PACF的代码。就像从minitab生成的图一样(如下)。

ACF绘图

PACF绘图

我已经尝试搜索该公式,但是我仍然不太了解它。 您介意告诉我该公式以及如何使用它吗? 上面的ACF和PACF图上的水平红线是什么?公式是什么?

谢谢,


1
@javlacalle您提供的公式正确吗?它不会工作如果 Ñ Σ= 1ÿ - ˉ Ý< 0
ρ(k)=1nkt=k+1n(yty¯)(ytky¯)1nt=1n(yty¯)1nkt=k+1n(ytky¯),
正确?应该像下面吗?$$ \ rho(k)= \ frac {\ frac {1} {nk} \ sum_ {t = k + 1} ^ n(y_t-\ bar {y})(y_ {tk}-\ bar {y} )} {\ sqrt {\ frac {1} {n} \ sum_ {t = 1} ^ n(y_t-\ bar {y})^ 2} \ sqrt {\ frac {1} {nk} \ sum_ {t = k + 1} ^ n(y_ {tk}-\ bar {y})^ 2}} \ ,,
t=1n(yty¯)<0and/ort=k+1n(ytky¯)<0
conighion

@conighion你说得对,谢谢。我以前没看过 我已经解决了。
javlacalle

Answers:


33

自相关

两个变量y1,y2之间的相关性定义为:

ρ=E[(y1μ1)(y2μ2)]σ1σ2=Cov(y1,y2)σ1σ2,

其中,E是期望算子,μ1μ2是所述装置分别为y1y2σ1,σ2是它们的标准偏差。

在单个变量(即自相关)的上下文中,y1是原始序列,而y2是其滞后形式。在上述定义中,顺序的样本自相关k=0,1,2,...可以通过与观察到的一系列计算下面的表达式来获得ytt=1,2,...,n

ρ(k)=1nkt=k+1n(yty¯)(ytky¯)1nt=1n(yty¯)21nkt=k+1n(ytky¯)2,

其中y¯是数据的样本均值。

部分自相关

部分自相关在消除一个变量对两个变量的影响后,测量一个变量的线性相关性。例如,在去除了y t - 1y ty t - 2的影响之后,阶次部分自相关测量yt2yt的影响(线性相关性)。yt1ytyt2

每个部分自相关可以通过一系列形式的回归获得:

y~t=ϕ21y~t1+ϕ22y~t2+et,

在那里y~t是原系列减去样本均值,yty¯ϕ22的估计将给出阶次2的部分自相关的值。通过增加k附加滞后来扩展回归,最后一项的估计将给出k阶的部分自相关。

计算样本偏自相关的另一种方法是为每个阶k求解以下系统:

(ρ(0)ρ(1)ρ(k1)ρ(1)ρ(0)ρ(k2)ρ(k1)ρ(k2)ρ(0))(ϕk1ϕk2ϕkk)=(ρ(1)ρ(2)ρ(k)),

其中ρ()是样本自相关。样本自相关和部分自相关之间的这种映射称为 Durbin-Levinson递归。这种方法相对容易实现以进行说明。例如,在R软件中,我们可以获得阶数为5的部分自相关,如下所示:

# sample data
x <- diff(AirPassengers)
# autocorrelations
sacf <- acf(x, lag.max = 10, plot = FALSE)$acf[,,1]
# solve the system of equations
res1 <- solve(toeplitz(sacf[1:5]), sacf[2:6])
res1
# [1]  0.29992688 -0.18784728 -0.08468517 -0.22463189  0.01008379
# benchmark result
res2 <- pacf(x, lag.max = 5, plot = FALSE)$acf[,,1]
res2
# [1]  0.30285526 -0.21344644 -0.16044680 -0.22163003  0.01008379
all.equal(res1[5], res2[5])
# [1] TRUE

置信带

置信带可以被计算为样品的自相关的值±z1α/2nz1α/21α/2

±z1α/21n(1+2i=1kρ(i)2).


1
(+1) Why the two different confidence bands?
Scortchi - Reinstate Monica

2
@Scortchi Constant bands are used when testing for independence, while the increasing bands are sometimes used when identifying an ARIMA model.
javlacalle

1
The two methods for calculating confidence bands are explained in a little more detail here.
Scortchi - Reinstate Monica

Perfect explanation!
Jan Rothkegel

1
@javlacalle, does the expression for ρ(k) miss squares in the denominator?
Christoph Hanck

9

"I want to create a code for plotting ACF and PACF from time-series data".

Although the OP is a bit vague, it may possibly be more targeted to a "recipe"-style coding formulation than a linear algebra model formulation.


The ACF is rather straightforward: we have a time series, and basically make multiple "copies" (as in "copy and paste") of it, understanding that each copy is going to be offset by one entry from the prior copy, because the initial data contains t data points, while the previous time series length (which excludes the last data point) is only t1. We can make virtually as many copies as there are rows. Each copy is correlated to the original, keeping in mind that we need identical lengths, and to this end, we'll have to keep on clipping the tail end of the initial data series to make them comparable. For instance, to correlate the initial data to tst3 we'll need to get rid of the last 3 data points of the original time series (the first 3 chronologically).

Example:

We'll concoct a times series with a cyclical sine pattern superimposed on a trend line, and noise, and plot the R generated ACF. I got this example from an online post by Christoph Scherber, and just added the noise to it:

x=seq(pi, 10 * pi, 0.1)
y = 0.1 * x + sin(x) + rnorm(x)
y = ts(y, start=1800)

enter image description here

Ordinarily we would have to test the data for stationarity (or just look at the plot above), but we know there is a trend in it, so let's skip this part, and go directly to the de-trending step:

model=lm(y ~ I(1801:2083))
st.y = y - predict(model)

enter image description here

Now we are ready to takle this time series by first generating the ACF with the acf() function in R, and then comparing the results to the makeshift loop I put together:

ACF = 0                  # Starting an empty vector to capture the auto-correlations.
ACF[1] = cor(st.y, st.y) # The first entry in the ACF is the correlation with itself (1).
for(i in 1:30){          # Took 30 points to parallel the output of `acf()`
  lag = st.y[-c(1:i)]    # Introducing lags in the stationary ts.
  clipped.y = st.y[1:length(lag)]    # Compensating by reducing length of ts.
  ACF[i + 1] = cor(clipped.y, lag)   # Storing each correlation.
}
acf(st.y)                            # Plotting the built-in function (left)
plot(ACF, type="h", main="ACF Manual calculation"); abline(h = 0) # and my results (right).

enter image description here


OK. That was successful. On to the PACF. Much more tricky to hack... The idea here is to again clone the initial ts a bunch of times, and then select multiple time points. However, instead of just correlating with the initial time series, we put together all the lags in-between, and perform a regression analysis, so that the variance explained by the previous time points can be excluded (controlled). For example, if we are focusing on the PACF ending at time tst4, we keep tst, tst1, tst2 and tst3, as well as tst4, and we regress tsttst1+tst2+tst3+tst4 through the origin and keeping only the coefficient for tst4:

PACF = 0          # Starting up an empty storage vector.
for(j in 2:25){   # Picked up 25 lag points to parallel R `pacf()` output.
  cols = j        
  rows = length(st.y) - j + 1 # To end up with equal length vectors we clip.

  lag = matrix(0, rows, j)    # The storage matrix for different groups of lagged vectors.

for(i in 1:cols){
  lag[ ,i] = st.y[i : (i + rows - 1)]  #Clipping progressively to get lagged ts's.
}
  lag = as.data.frame(lag)
  fit = lm(lag$V1 ~ . - 1, data = lag) # Running an OLS for every group.
  PACF[j] = coef(fit)[j - 1]           # Getting the slope for the last lagged ts.
}

And finally plotting again side-by-side, R-generated and manual calculations:

enter image description here

That the idea is correct, beside probable computational issues, can be seen comparing PACF to pacf(st.y, plot = F).


code here.


1

Well, in the practise we found error (noise) which is represented by et the confidence bands help you to figure out if a level can be considerate as only noise (because about the 95% times will be into the bands).


Welcome to CV, you might want to consider adding some more detailed information on how OP would go about do this specifically. Maybe also add some information on what each line represents?
Repmat

1

Here is a python code to compute ACF:

def shift(x,b):
    if ( b <= 0 ):
        return x
    d = np.array(x);
    d1 = d
    d1[b:] = d[:-b]
    d1[0:b] = 0
    return d1

# One way of doing it using bare bones
# - you divide by first to normalize - because corr(x,x) = 1
x = np.arange(0,10)
xo = x - x.mean()

cors = [ np.correlate(xo,shift(xo,i))[0]  for i in range(len(x1)) ]
print (cors/cors[0] )

#-- Here is another way - you divide by first to normalize
cors = np.correlate(xo,xo,'full')[n-1:]
cors/cors[0]

Hmmm Code formatting was bad:
Sada
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.