package com.imsl.test.example.stat; import java.text.*; import com.imsl.stat.*; import com.imsl.math.*; import java.util.logging.*; /** *

* Finds the minimum AIC autoregressive model for the Canadian lynx data.

*

* Using the Canadian Lynx data included in TIMSAC-78, * ARAutoUnivariate is used to find the minimum AIC autoregressive * model using a maximum number of lags of maxlag=20.

* *

* This example compares the three different methods for estimating the * autoregressive coefficients, and it illustrates the relationship between * these estimates and those calculated within the TIMSAC UNIMAR code. As * illustrated, the UNIMAR code estimates the coefficients and innovation * variance using only the last \(N\)-maxlag values in the time * series. The other estimation methods use all \(N-k\) values, where \(k\) is * the number of lags with minimum AIC selected by * ARAutoUnivariate. *

* *

* This example also illustrates how to generate forecasts for the observed * series values and beyond by setting the backward origin for the * forecasts.

* * @see Code * @see Output */ public class ARAutoUnivariateEx2 { public static void main(String args[]) throws Exception { /* THE CANDIAN LYNX DATA AS USED IN TIMSAC 1821-1934 */ double[] y = { 0.24300e01, 0.25060e01, 0.27670e01, 0.29400e01, 0.31690e01, 0.34500e01, 0.35940e01, 0.37740e01, 0.36950e01, 0.34110e01, 0.27180e01, 0.19910e01, 0.22650e01, 0.24460e01, 0.26120e01, 0.33590e01, 0.34290e01, 0.35330e01, 0.32610e01, 0.26120e01, 0.21790e01, 0.16530e01, 0.18320e01, 0.23280e01, 0.27370e01, 0.30140e01, 0.33280e01, 0.34040e01, 0.29810e01, 0.25570e01, 0.25760e01, 0.23520e01, 0.25560e01, 0.28640e01, 0.32140e01, 0.34350e01, 0.34580e01, 0.33260e01, 0.28350e01, 0.24760e01, 0.23730e01, 0.23890e01, 0.27420e01, 0.32100e01, 0.35200e01, 0.38280e01, 0.36280e01, 0.28370e01, 0.24060e01, 0.26750e01, 0.25540e01, 0.28940e01, 0.32020e01, 0.32240e01, 0.33520e01, 0.31540e01, 0.28780e01, 0.24760e01, 0.23030e01, 0.23600e01, 0.26710e01, 0.28670e01, 0.33100e01, 0.34490e01, 0.36460e01, 0.34000e01, 0.25900e01, 0.18630e01, 0.15810e01, 0.16900e01, 0.17710e01, 0.22740e01, 0.25760e01, 0.31110e01, 0.36050e01, 0.35430e01, 0.27690e01, 0.20210e01, 0.21850e01, 0.25880e01, 0.28800e01, 0.31150e01, 0.35400e01, 0.38450e01, 0.38000e01, 0.35790e01, 0.32640e01, 0.25380e01, 0.25820e01, 0.29070e01, 0.31420e01, 0.34330e01, 0.35800e01, 0.34900e01, 0.34750e01, 0.35790e01, 0.28290e01, 0.19090e01, 0.19030e01, 0.20330e01, 0.23600e01, 0.26010e01, 0.30540e01, 0.33860e01, 0.35530e01, 0.34680e01, 0.31870e01, 0.27230e01, 0.26860e01, 0.28210e01, 0.30000e01, 0.32010e01, 0.34240e01, 0.35310e01 }; double[][] printOutput; double timsacAR[], mmAR[], mleAR[], lsAR[]; double forecasts[], residuals[]; double timsacConstant, mmConstant, mleConstant, lsConstant; double timsacVar, timsacEquivalentVar, mmVar, mleVar, lsVar; int maxlag = 20; String[] colLabels = {"TIMSAC", "Method of Moments", "Least Squares", "Maximum Likelihood"}; String[] colLabels2 = {"Observed", "Forecast", "Residual"}; PrintMatrixFormat pmf = new PrintMatrixFormat(); PrintMatrix pm = new PrintMatrix(); NumberFormat nf = NumberFormat.getNumberInstance(); pm.setColumnSpacing(4); nf.setMinimumFractionDigits(4); pmf.setNumberFormat(nf); pmf.setColumnLabels(colLabels); System.out.println("Automatic Selection of Minimum AIC AR Model"); System.out.println(""); ARAutoUnivariate autoAR = new ARAutoUnivariate(maxlag, y); // logging to console Logger logger = autoAR.getLogger(); ConsoleHandler ch = new ConsoleHandler(); ch.setLevel(Level.ALL); // default ConsoleHandler Level is INFO logger.setLevel(Level.FINE); logger.addHandler(ch); ch.setFormatter(new com.imsl.IMSLFormatter()); autoAR.compute(); int orderSelected = autoAR.getOrder(); System.out.println("Minimum AIC Selected=" + autoAR.getAIC() + " with an optimum lag of k= " + autoAR.getOrder()); System.out.println(""); timsacAR = autoAR.getTimsacAR(); timsacConstant = autoAR.getTimsacConstant(); timsacVar = autoAR.getTimsacVariance(); lsAR = autoAR.getAR(); lsConstant = autoAR.getConstant(); lsVar = autoAR.getInnovationVariance(); autoAR.setEstimationMethod(ARAutoUnivariate.METHOD_OF_MOMENTS); autoAR.compute(); mmAR = autoAR.getAR(); mmConstant = autoAR.getConstant(); mmVar = autoAR.getInnovationVariance(); autoAR.setEstimationMethod(ARAutoUnivariate.MAX_LIKELIHOOD); autoAR.compute(); mleAR = autoAR.getAR(); mleConstant = autoAR.getConstant(); mleVar = autoAR.getInnovationVariance(); printOutput = new double[orderSelected + 1][4]; printOutput[0][0] = timsacConstant; for (int i = 0; i < orderSelected; i++) { printOutput[i + 1][0] = timsacAR[i]; } printOutput[0][1] = mmConstant; for (int i = 0; i < orderSelected; i++) { printOutput[i + 1][1] = mmAR[i]; } printOutput[0][2] = lsConstant; for (int i = 0; i < orderSelected; i++) { printOutput[i + 1][2] = lsAR[i]; } printOutput[0][3] = mleConstant; for (int i = 0; i < orderSelected; i++) { printOutput[i + 1][3] = mleAR[i]; } pm.setTitle("Comparison of AR Estimates"); pm.print(pmf, printOutput); /* calculation of equivalent innovation variance using TIMSAC coefficients. The Timsac innovation variance is calculated using only N-maxlag observations in the series. The following code calculates the innovation variance using N-k observations in the series with the Timsac coefficient. This illustrates that the least squares Timsac coefficients will not have the least value for the sum of squared residuals, which is calculated using all N-k observations. */ ARMA armaLS = new ARMA(orderSelected, 0, y); armaLS.setArmaInfo(autoAR.getTimsacConstant(), autoAR.getTimsacAR(), new double[0], autoAR.getTimsacVariance()); armaLS.setBackwardOrigin(y.length - orderSelected); forecasts = armaLS.getForecast(1); double sumResiduals = 0.0; for (int i = 0; i < y.length - orderSelected; i++) { sumResiduals += (y[i + orderSelected] - forecasts[i]) * (y[i + orderSelected] - forecasts[i]); } timsacEquivalentVar = sumResiduals / (y.length - orderSelected - 1); printOutput = new double[1][4]; printOutput[0][0] = timsacEquivalentVar; /* the method of moments variance */ printOutput[0][1] = mmVar; /* the least squares variance */ printOutput[0][2] = lsVar; /* the maximum likelihood estimate of the variance */ printOutput[0][3] = mleVar; nf.setMinimumFractionDigits(5); pmf.setNumberFormat(nf); pm.setTitle("Comparison of Equivalent Innovation Variances"); pm.print(pmf, printOutput); /* FORECASTING - An example of forecasting using the maximum * likelihood estimates for the minimum AIC AR model. In this example, * forecasts are returned for the last 10 values in the series followed * by the forecasts for the next 5 values. */ autoAR.setBackwardOrigin(10); forecasts = autoAR.getForecast(15); residuals = autoAR.getResiduals(); printOutput = new double[15][3]; for (int i = 0; i < 10; i++) { printOutput[i][0] = y[y.length - 10 + i]; printOutput[i][1] = forecasts[i]; printOutput[i][2] = residuals[i]; } for (int i = 10; i < 15; i++) { printOutput[i][0] = Double.NaN; printOutput[i][1] = forecasts[i]; printOutput[i][2] = Double.NaN; } nf.setMaximumFractionDigits(3); pmf.setFirstRowNumber(105); pmf.setNumberFormat(nf); pmf.setColumnLabels(colLabels2); pm.setTitle("Maximum Likelihood Forecasts of Last 10 Observations & " + "the Next 5"); pm.print(pmf, printOutput); } }