lackOfFit

Performs lack-of-fit test for a univariate time series or transfer function given the appropriate correlation function.

Synopsis

lackOfFit (nObservations, cf, lagmax, npfree)

Required Arguments

int nObservations (Input)
Number of observations of the stationary time series.
float cf[] (Input)
Array of length lagmax +1 containing the correlation function.
int lagmax (Input)
Maximum lag of the correlation function.
int npfree (Input)
Number of free parameters in the formulation of the time series model. npfree must be greater than or equal to zero and less than lagmax. Woodfield (1990) recommends npfree = p + q for an ARMA(p, q) model.

Return Value

An array of length 2 with the test statistic, Q, and its p‑value, p. Under the null hypothesis, Q has an approximate chi-squared distribution with lagmax-lagmin+1-npfree degrees of freedom.

Optional Arguments

lagmin, int (Input)
Minimum lag of the correlation function. lagmin corresponds to the lower bound of summation in the lack of fit test statistic.

Default value is 1.

Description

Function lackOfFit may be used to diagnose lack of fit in both ARMA and transfer function models. Typical arguments for these situations are:

Model LAGMIN LAGMAX NPFREE
ARMA (p, q) 1 \(\sqrt{\mathtt{n\_observations}}\) p + q
Transfer function 0 \(\sqrt{\mathtt{n\_observations}}\) r + s

Function lackOfFit performs a portmanteau lack of fit test for a time series or transfer function containing n observations given the appropriate sample correlation function

\[\hat{\rho}(k)\]

for \(k=L,L+1,\ldots,K\) where L = lagmin and K = lagmax.

The basic form of the test statistic Q is

\[Q = n(n+2) \sum_{k=L}^{K} (n-k)^{-1} \hat{\rho} (k)\]

with \(L=1\) if

\[\hat{\rho}(k)\]

is an autocorrelation function. Given that the model is adequate, Q has a chi-squared distribution with KL + 1 − m degrees of freedom where m = npfree is the number of parameters estimated in the model. If the mean of the time series is estimated, Woodfield (1990) recommends not including this in the count of the parameters estimated in the model. Thus, for an ARMA(p, q) model set npfree= p + q regardless of whether the mean is estimated or not. The original derivation for time series models is due to Box and Pierce (1970) with the above modified version discussed by Ljung and Box (1978). The extension of the test to transfer function models is discussed by Box and Jenkins (1976, pages 394–395).

Example

Consider the Wölfer Sunspot Data (Anderson 1971, page 660) consisting of the number of sunspots observed each year from 1749 through 1924. The data set for this example consists of the number of sunspots observed from 1770 through 1869. An ARMA(2,1) with nonzero mean is fitted using function arma. The autocorrelations of the residuals are estimated using function autocorrelation. A portmanteau lack of fit test is computed using 10 lags with lackOfFit.

from __future__ import print_function
from numpy import *
from pyimsl.stat.arma import arma
from pyimsl.stat.autocorrelation import autocorrelation
from pyimsl.stat.dataSets import dataSets
from pyimsl.stat.lackOfFit import lackOfFit

p = 2
q = 1
n_observations = 100
max_itereations = 0
lagmin = 1
lagmax = 10
npfree = 4
x = empty(100)
residuals = []

# Get sunspot data for 1770 through 1869, store it in x[].
data = dataSets(2)
for i in range(0, n_observations):
    x[i] = data[21 + i, 1]

# Get residuals from ARMA(2,1) for autocorrelation/lack of fit
parameters = arma(x, p, q,
                  leastSquares=True,
                  residual=residuals)

# Get autocorrelations from residuals for lack of fit test
#     NOTE:  number of OBS is equal to number of residuals
correlations = autocorrelation(residuals, lagmax)

#  Get lack of fit test statistic and p-value
#     NOTE:  number of OBS is equal to original number of data
result = lackOfFit(n_observations, correlations, lagmax, npfree)

#  Print parameter estimates, test statistic, and p-value
#     NOTE: Test Statistic Q follows a Chi-squared dist.
print("Lack of Fit Statistic, Q = \t%3.5f\n             P-value of Q = \t %1.5f"
      % (result[0], result[1]))

Output

Lack of Fit Statistic, Q = 	23.98888
             P-value of Q = 	 0.00052