autoUniAr¶
Automatic selection and fitting of a univariate autoregressive time series model. The lag for the model is automatically selected using Akaike’s information criterion (AIC). Estimates of the autoregressive parameters for the model with minimum AIC are calculated using method of moments, method of least squares, or maximum likelihood.
Synopsis¶
autoUniAr (z, maxLag, p)
Required Arguments¶
- float
z[]
(Input) - Array of length
nObs
containing the stationary time series. - int
maxLag
(Input) - Maximum number of autoregressive parameters requested. It is required
that 1≤
maxLag
≤nObs/
2. - int
p
(Output) - Number of autoregressive parameters in the model with minimum AIC.
Return Value¶
Vector of length 1+ maxLag
containing the estimates for the constant and
the autoregressive parameters in the model with minimum AIC. The estimates
are located in the first 1+ p
locations of this array.
Optional Arguments¶
printLevel
(Input)Printing option:
printLevel
Action 0 No Printing. 1 Prints final results only. 2 Prints intermediate and final results. Default:
printLevel
= 0.maxIterations
(Input)Maximum number of estimation iterations.- Default:
maxIterations
= 300 method
, int (Input)Estimation method option:
method
Action 0 Method of moments. 1 Method of least squares realized through Householder transformations. 2 Maximum likelihood Default:
method
= 1.varNoise
(Output)- Estimate of innovation variance.
aic
(Output)- Minimum AIC.
meanEstimate
(Input/Output)Estimate of the mean of the time series
z
. On return,meanEstimate
contains an update of the mean.Default: Time series
z
is centered about its sample mean.
Description¶
Function autoUniAr
automatically selects the order of the AR model that
best fits the data and then computes the AR coefficients. The algorithm used
in autoUniAr
is derived from the work of Akaike, H., et. al (1979) and
Kitagawa and Akaike (1978). This code was adapted from the UNIMAR procedure
published as part of the TIMSAC-78 Library.
The best fit AR model is determined by successively fitting AR models with
0, 1, 2, …, maxLag
autoregressive coefficients. For each model,
Akaike’s Information Criterion (AIC) is calculated based on the formula
Function autoUniAr
uses the approximation to this formula developed by
Ozaki and Oda (1979),
where \(\hat{\sigma}^2\) is an estimate of the residual variance of the series, commonly known in time series analysis as the innovation variance. By dropping the constant
the calculation is simplified to
The best fit model is the model with minimum AIC. If the number of
parameters in this model is equal to the highest order autoregressive model
fitted, i.e., p=maxLag
, then a model with smaller AIC might exist for
larger values of maxLag
. In this case, increasing maxLag
to explore
AR models with additional autoregressive parameters might be warranted.
If method
= 0, estimates of the autoregressive coefficients for the
model with minimum AIC are calculated using method of moments. If method
=1, the coefficients are determined by the method of least squares applied
in the form described by Kitagawa and Akaike (1978). Otherwise, if
method
=2, the coefficients are estimated using maximum likelihood.
Example¶
Consider the Wolfer Sunspot data (Anderson 1971, p. 660) consisting of the
number of sunspots observed each year from 1770 through 1869. In this
example, autoUniAr
found the minimum AIC fit is an autoregressive model
with 3 lags:
where
μ the sample mean of the time series \(\left\{ w_t \right\}\). Defining the overall constant \(\phi_0\) by \(\phi_0 :=\mu\left( 1-\textstyle\sum_{i=1}^{3} \phi_i \right)\), we obtain the following equivalent representation:
The example computes estimates for \(\phi_0,\phi_1,\phi_2,\phi_3\) for each of the three parameter estimation methods available.
from __future__ import print_function
from numpy import *
from pyimsl.stat.autoUniAr import autoUniAr
from pyimsl.stat.dataSets import dataSets
from pyimsl.stat.writeMatrix import writeMatrix
maxlag = 20
n_obs = 100
p = []
aic = []
avar = []
z = empty(100)
# get wolfer sunspot data
w = dataSets(2)
for i in range(0, n_obs):
z[i] = w[21 + i][1]
# Compute AR parameters for minimum AIC by method of moments
print("AIC Automatic Order selection")
print("AR coefficients estimated using method of moments")
parameters = autoUniAr(z, maxlag, p,
varNoise=avar,
method=0,
aic=aic)
print("Order selected: %d" % p[0])
print("AIC = %11.4f, Variance = %11.4f" % (aic[0], avar[0]))
print("Constant estimate is %11.4f." % parameters[0])
writeMatrix("Final AR coefficients estimated by method of moments",
parameters[1:p[0] + 1], column=True)
# Compute AR parameters for minimum AIC by method of least squares
print("\nAIC Automatic Order selection")
print("AR coefficients estimated using method of least squares")
ar = autoUniAr(z, maxlag, p,
varNoise=avar,
method=1,
aic=aic)
print("Order selected: %d\n" % p[0])
print("AIC = %11.4f, Variance = %11.4f" % (aic[0], avar[0]))
print("Constant estimate is %11.4f." % ar[0])
writeMatrix("Final AR coefficients estimated by method of least squares",
ar[1:p[0] + 1], column=True)
# Compute AR parameters for minimum AIC by maximum likelihood estimation
print("\nAIC Automatic Order selection")
print("AR coefficients estimated using maximum likelihood")
ar = autoUniAr(z, maxlag, p,
varNoise=avar,
method=2,
aic=aic)
print("Order selected: %d" % p[0])
print("AIC = %11.4f, Variance = %11.4f" % (aic[0], avar[0]))
print("Constant estimate is %11.4f." % ar[0])
writeMatrix("Final AR coefficients estimated by maximum likelihood",
ar[1:p[0] + 1], column=True)
Output¶
AIC Automatic Order selection
AR coefficients estimated using method of moments
Order selected: 3
AIC = 633.0114, Variance = 287.2699
Constant estimate is 13.7098.
AIC Automatic Order selection
AR coefficients estimated using method of least squares
Order selected: 3
AIC = 633.0114, Variance = 144.7149
Constant estimate is 9.8934.
AIC Automatic Order selection
AR coefficients estimated using maximum likelihood
Order selected: 3
AIC = 633.0114, Variance = 218.8486
Constant estimate is 11.4218.
Final AR coefficients estimated by method of moments
1 1.368
2 -0.738
3 0.078
Final AR coefficients estimated by method of least squares
1 1.604
2 -1.024
3 0.209
Final AR coefficients estimated by maximum likelihood
1 1.551
2 -0.999
3 0.204