This example fits a VAR(1) to a 2-dimensional vector time series. Then, forecasts for up to 4 steps ahead are requested, illustrating the default forecasting behavior.
import com.imsl.math.*; import com.imsl.stat.*; public class VectorAutoregressionEx1 { public static void main(String args[]) { double data1[] = { 0, 0, -0.148105526820657, 0.0507420782620461, -1.13674727366735, 0.862892721126079, 1.54366183541037, -1.13804802266371, -0.0923211737957721, 1.65649055346038, 0.521764564424907, -2.11208211206815, 0.843683397890515, 2.56656798707681, -2.70330819114831, -2.83452914666041, 4.93683704766295, 3.95965377457266, -4.78314880243807, -2.23136673998374, 6.24911547448071, 0.40543051970714, -6.76582567372196, 0.816818897274206, 6.21142944093012, -4.247694573354, -5.29817270483491, 5.08246614505046, 4.19557583927549, -5.35697380907112, -3.21572784971078, 7.89716072829777, 0.485717553966479, -8.25930665413043, 2.69490292018773, 10.9017252520684, -5.41090143363388, -10.400477539227, 8.29423327234419, 9.10321370415527 }; TimeSeries ts = new TimeSeries(); // Set the data for the two-dimensional time series ts.setSeriesValues(data1, 2); // Set up the vector autoregression model. Use default AR lag = 1. VectorAutoregression VAR1 = new VectorAutoregression(ts); // Get estimates of coefficient matrix A1 double[][] coefs = VAR1.getEstimates(); // Print estimates PrintMatrix pm = new PrintMatrix("Estimated Coefficient Matrix A1"); pm.print(coefs); // Get h =1, 2, 3, 4 step ahead forecasts for all time points past // the default presample value (nT = 6) double[][] forecasts = VAR1.getForecasts(); // Print forecasts pm = new PrintMatrix("VAR Forecasts"); PrintMatrixFormat pmf = new PrintMatrixFormat(); String[] colLabels = {"(h=1,k=1)", "(h=1,k=2)", "(2,1)", "(2,2)", "(3,1)", "(3,2)", "(4,1)", "(4,2)"}; pmf.setColumnLabels(colLabels); pmf.setNoRowLabels(); pm.print(pmf, forecasts); } }
Estimated Coefficient Matrix A1 0 1 0 -1.017 -0.296 1 0.273 -1.053 VAR Forecasts (h=1,k=1) (h=1,k=2) (2,1) (2,2) (3,1) (3,2) (4,1) (4,2) 0.094 2.366 -0.795 -2.466 1.538 2.381 -2.269 -2.088 -1.617 -2.473 2.377 2.163 -3.058 -1.63 3.593 0.883 3.589 2.248 -4.316 -1.389 4.802 0.286 -4.97 1.007 -6.194 -2.824 7.137 1.286 -7.642 0.592 7.6 -2.706 5.526 1.046 -5.932 0.405 5.916 -2.043 -5.415 3.764 -6.478 1.276 6.214 -3.11 -5.403 4.969 4.028 -6.705 6.642 -2.704 -5.959 4.659 4.685 -6.53 -2.837 8.154 -5.064 6.166 3.329 -7.874 -1.059 9.199 -1.642 -9.976 3.888 -6.796 -1.946 8.217 -0.449 -9.183 3.172 9.548 -2.685 6.785 0.726 -7.877 1.591 8.492 -4.129 -8.509 0.937 -9.193 1.765 9.936 -4.733 -9.982 7.767 9.221 1.948 8.83 -4.593 -8.767 7.265 7.981 -9.752 -6.424 -5.965 -10.746 9.247 9.69 -12.273 -7.683 14.759 4.746 8.58 9.477 -11.532 -7.641 13.993 4.903 -15.687 -1.349Link to Java source.