%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Example 1: Air Passengers data from Box and Jenkins (2015) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all % Load data AirPassengers = [112 118 132 129 121 135 148 148 136 119 104 ... 118 115 126 141 135 125 149 170 170 158 133 ... 114 140 145 150 178 163 172 178 199 199 184 ... 162 146 166 171 180 193 181 183 218 230 242 ... 209 191 172 194 196 196 236 235 229 242 264 ... 272 237 211 180 201 204 188 235 227 234 264 ... 302 293 259 229 203 229 242 233 267 269 270 ... 315 364 347 312 274 237 278 284 277 317 313 ... 318 374 413 405 355 306 271 306 315 301 356 ... 348 355 422 465 467 404 347 305 336 340 318 ... 362 348 363 435 491 505 404 359 310 337 360 ... 342 406 396 420 472 548 559 463 407 362 405 ... 417 391 419 461 472 535 622 606 508 461 390 432]'; y = log(AirPassengers); t = (1949 : 1/12 : 1961 - 1/12)'; % Plot of raw time series figure(1) plot(t, AirPassengers) %% Estimating Basic Structural Model m = UC(y, 12, 'model', 'llt/equal/arma(0,0)'); %% Full algorithm m = UC(y, 12); %% Stepwise with unit roots m = UC(y, 12, 'stepwise', true, 'tTest', true); %% Stepwise m = UC(y, 12, 'stepwise', true); %% Plotting components ind = (1 : 144); comp = m.comp(ind, :); figure(1) if exist('OCTAVE_VERSION', 'builtin') % Octave subplot(411) plot(t(ind), [comp(:, 1) y]), title('Series and Trend') subplot(412) plot(t(ind), comp(:, 2)), title('Slope') subplot(413) plot(t(ind), comp(:, 3)), title('Seasonal') subplot(414) plot(t(ind), comp(:, 4)), title('Irregular') else % Matlab stackedplot(comp) end %% Plotting forecasts orig = 132; m = UC(y(1 : orig), 12, 'h', 12); py = y(end - 23 : end); pt = t(end - 23 : end); tEnd = t(orig + 1 : end); forec = m.yFor; confUp = m.yFor + 2 * sqrt(m.yForV); confDown = m.yFor - 2 * sqrt(m.yForV); figure(1) clf plot(pt, py, 'k', ... tEnd, m.yFor, 'r', ... tEnd, confUp, 'r--', ... tEnd, confDown, 'r--') %% Table 5 in paper m = UC(y, 12); firstRow = m.criteria; m = UC(y, 12, 'criterion', 'bic'); secondRow = m.criteria; m = UC(y, 12, 'criterion', 'aicc'); secondRowBis = m.criteria; m = UC(y, 12, 'model', 'llt/equal/arma(0,0)'); thirdRow = m.criteria; [firstRow'; secondRow'; secondRowBis'; thirdRow'] %% Table 5 in paper stepwise m = UC(y, 12, 'stepwise', true); firstRow = m.criteria; m = UC(y, 12, 'criterion', 'bic', 'stepwise', true); secondRow = m.criteria; m = UC(y, 12, 'criterion', 'aicc', 'stepwise', true); secondRowBis = m.criteria; m = UC(y, 12, 'model', 'llt/equal/arma(0,0)', 'stepwise', true); thirdRow = m.criteria; [firstRow'; secondRow'; secondRowBis'; thirdRow'] %% Table 5 in paper stepwise with unit roots tests m = UC(y, 12, 'stepwise', true, 'tTest', true); firstRow = m.criteria; m = UC(y, 12, 'criterion', 'bic', 'stepwise', true, 'tTest', true); secondRow = m.criteria; m = UC(y, 12, 'criterion', 'aicc', 'stepwise', true, 'tTest', true); secondRowBis = m.criteria; m = UC(y, 12, 'model', 'llt/equal/arma(0,0)', 'stepwise', true, 'tTest', true); thirdRow = m.criteria; [firstRow'; secondRow'; secondRowBis'; thirdRow'] %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Example 2: Sales index for food in large retail stores in Spain %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all % Load data sales = [55.587,51.684,60.5,61.253,58.139,60.727,65.796,65.348,66.394,62.251, ... 62.449,94.558,57.99,56.334,64.14,61.911,62.963,63.184,68.551,68.354,62.622, ... 63.399,64.744,94.085,60.231,55.766,63.817,63.525,65.491,62.24,66.985, ... 69.597,66.065,68.928,66.704,102.011,64.983,58.653,64.828,68.958,67.308, ... 67.661,74.791,73.638,68.934,75.413,68.677,107.426,70.781,65.742,78.168, ... 72.317,70.799,74.797,85.55,81.51,78.252,82.986,78.539,119.978,74.714, ... 72.789,81.725,82.807,78.694,83.745,90.756,92.298,88.318,83.452,82.651, ... 127.228,80.834,78.886,94.056,89.172,89.818,97.283,97.182,102.585,93.512, ... 97.026,93.87,137.198,80.147,76.961,91.28,87.678,88.557,89.769,95.866, ... 100.824,86.99,92.783,94.748,138.712,87.211,80.458,90.777,90.628,93.097, ... 90.805,99.175,103.839,93.975,97.812,95.407,149.72,93.815,83.694,93.876, ... 96.834,94.755,98.444,108.069,102.83,95.295,104.658,98.396,156.37,94.731, ... 87.65,100.269,102.302,95.632,99.166,108.763,108.6,100.885,107.447,101.494, ... 166.701,98.574,91.761,105.062,106.276,100.487,104.33,114.002,113.86, ... 108.472,106.11,106.284,170.003,100.104,94.401,112.902,103.766,104.629, ... 111.269,113.212,115.667,107.245,113.332,113.569,174.818,105.848,104.337, ... 115.351,110.732,117.083,112.774,122.422,122.428,111.615,115.03,113.306, ... 166.181,109.956,95.511,102.91,106.695,103.379,105.681,110.53,108.421, ... 101.288,107.118,100.468,160.152,101.358,90.185,102.26,97.937,96.35,99.321, ... 110.585,101.858,95.74,104.142,100.239,156.003,97.343,88.299,98.594,102.577, ... 94.174,99.923,107.338,102.658,98.245,102.464,97.906,151.759,93.435,87.881, ... 97.933,94.717,91.348,99.194,99.89,103.448,94.917,97.053,96.76,147.646, ... 90.332,85.638,98.525,93.341,95.332,96.153,101.276,102.559,94.572,96.486, ... 100.286,146.287,94.466,85.742,96.119,94.52,96.093,92.773,99.479,98.906, ... 94.871,96.67,97.636,147.681,94.625,83.057,93.14,93.23,94.633,95.364,103.053, ... 102.284,96.334,100.291,96.275,147.714,93.894,87.486,95.046,95.498,92.235, ... 95.411,103.12,100.469,96.335,98.978,98.378,151.22,94.204,84.765,97.482, ... 96.351,93.439,99.422,104.737,102.522,100.124,96.775,101.05,156.047,94.835, ... 85.708,101.548,94.621,94.475,101.231,101.236,103.33,98.004,100.899,102.311, ... 154.454,93.92,84.067,96.034,96.364,95.707,99.731,105.513,106.697,96.986, ... 101.282,101.773,154.576]'; u = [0 -0.25 0 0 0 0 0 0 0 0 0 0 0 0.75 0 0 0 0 0 0 0 0 0 0 0 -0.25 0 0 0 0 0 0 ... 0 0 0 0 0 -0.25 0 0 0 0 0 0 0 0 0 0 0 -0.25 0 0 0 0 0 0 0 0 0 0 0 0.75 0 0 0 0 ... 0 0 0 0 0 0 0 -0.25 0 0 0 0 0 0 0 0 0 0 0 -0.25 0 0 0 0 0 0 0 0 0 0 0 -0.25 ... 0 0 0 0 0 0 0 0 0 0 0 0.75 0 0 0 0 0 0 0 0 0 0 0 -0.25 0 0 0 0 0 0 0 0 0 0 0 ... -0.25 0 0 0 0 0 0 0 0 0 0 0 -0.25 0 0 0 0 0 0 0 0 0 0 0 0.75 0 0 0 0 0 0 0 0 ... 0 0 0 -0.25 0 0 0 0 0 0 0 0 0 0 0 -0.25 0 0 0 0 0 0 0 0 0 0 0 -0.25 0 0 0 0 ... 0 0 0 0 0 0 0 0.75 0 0 0 0 0 0 0 0 0 0 0 -0.25 0 0 0 0 0 0 0 0 0 0 0 -0.25 0 ... 0 0 0 0 0 0 0 0 0 0 -0.25 0 0 0 0 0 0 0 0 0 0 0 0.75 0 0 0 0 0 0 0 0 0 0 0 ... -0.25 0 0 0 0 0 0 0 0 0 0 0 -0.25 0 0 0 0 0 0 0 0 0 0 0 -0.25 0 0 0 0 0 0 0 ... 0 0 0]'; t = (1995 : 1/12 : 2020 - 1/12)'; % Plot of raw time series figure(1) plot(t, sales) %% orig = 288; y = sales(1 : orig); yl = log(y); % Models estimation m1 = UC(yl, 12); m1 = UC(yl, 12, 'stepwise', true); m1 = UC(yl, 12, 'stepwise', true, 'tTest', true); m2 = UC(yl, 12, 'u', u); m2 = UC(yl, 12, 'u', u, 'stepwise', true); m2 = UC(yl, 12, 'u', u, 'stepwise', true, 'tTest', true); m3 = UC(yl, 12, 'u', u, 'outlier', 4); m3 = UC(yl, 12, 'u', u, 'outlier', 4, 'stepwise', true); m3 = UC(yl, 12, 'u', u, 'outlier', 4, 'stepwise', true, 'tTest', true); %% Plotting components m = UC(yl, 12, 'u', u, 'outlier', 4); ind = (1 : orig); comp = m.comp(ind, :); figure(1) if exist('OCTAVE_VERSION', 'builtin') % Octave subplot(411) plot(t(ind), [comp(:, 1) yl]), title('Series and Trend') subplot(412) plot(t(ind), comp(:, 2)), title('Slope') subplot(413) plot(t(ind), comp(:, 3)), title('Seasonal') subplot(414) plot(t(ind), comp(:, 4)), title('Irregular') else % Matlab stackedplot(comp) end %% Plotting forecasts m = UC(yl, 12, 'u', u, 'outlier', 4); py = log(sales(277 : end)); tEnd = t(orig + 1 : end); forec = m.yFor; confUp = m.yFor + 2 * sqrt(m.yForV); confDown = m.yFor - 2 * sqrt(m.yForV); figure(1) clf plot(t(277 : end), py, 'k', ... tEnd, m.yFor, 'r', ... tEnd, confUp, 'r--', ... tEnd, confDown, 'r--') %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Example 3: OECD Gross Domestic Product % with outliers %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all OECDgdp = [19.71 19.92 20.2 20.34 20.41 20.9 21.36 21.64 22.03 22.3 22.58 ... 22.76 23.13 23.4 23.77 24.18 24.58 24.89 25.14 25.3 25.58 25.77 26.11 26.38 ... 26.88 27.19 27.73 28.16 28.53 28.87 29.24 29.44 29.69 30.04 30.45 30.47 ... 30.74 31.12 31.53 31.7 32.19 32.77 33.19 33.77 34.55 34.94 35.06 35.46 ... 35.31 35.47 35.48 35.26 35.08 35.27 35.65 36.14 36.74 37.11 37.44 37.8 ... 38.14 38.47 38.83 39.15 39.46 40.24 40.61 41.14 41.38 41.89 42.09 42.38 ... 42.62 42.17 42.3 42.79 43.24 43.32 43.6 43.57 43.45 43.55 43.49 43.58 43.92 ... 44.41 45 45.65 46.31 46.62 47.19 47.49 47.95 48.46 49.02 49.43 49.65 49.96 ... 50.31 50.61 50.78 51.61 52.18 53.13 53.57 53.95 54.52 55.16 55.83 56.05 56.61 ... 57.15 57.7 58.23 58.44 58.59 58.65 59.01 59.18 59.55 60.07 60.26 60.56 60.73 ... 60.86 61.12 61.36 61.89 62.48 62.84 63.4 64.02 64.26 64.5 65.06 65.42 65.89 ... 66.66 67.14 67.77 68.17 69.07 69.69 70.34 70.61 70.97 71.54 72.15 72.62 73.16 ... 74.05 75.04 75.76 76.72 77.11 77.54 77.75 77.89 77.86 77.98 78.45 79.04 79.47 ... 79.71 79.88 80.29 81.15 82.01 82.64 83.2 83.7 84.23 84.9 85.46 86.26 86.94 ... 87.85 88.42 88.71 89.59 90.22 90.75 91.15 91.83 91.98 91.94 91.42 89.67 87.62 ... 87.74 88.23 89.07 89.62 90.57 91.31 91.82 92.06 92.4 92.93 93.36 93.76 93.89 ... 94.04 94.11 94.66 95.1 95.77 96.28 96.64 97.1 97.87 98.46 99.25 99.82 100.31 ... 100.59 101.1 101.53 101.9 102.77 103.49 104.17 104.96 105.81 106.34 106.94 ... 107.26 107.54 108.18 108.63 109.07 109.28]'; y = OECDgdp; t = (1962 : 0.25 : 2020 - 0.25)'; % Plot of raw time series figure(1) plot(t, y) %% Box-Cox transformation yl = log(y); % Models m1 = UC(yl, 4); m2 = UC(yl, 4, 'model', '?/?/?/?'); m3 = UC(yl, 4, 'model', '?/?/arma(2,0)'); m4 = UC(yl, 4, 'model', '?/?/arma(2,1)'); %% Plotting components ind = (1 : 232)'; figure(1) comp = m2.comp(ind, :); if exist('OCTAVE_VERSION', 'builtin') % Octave subplot(311) plot(t(ind), [comp(:, 1) yl]), title('Series and Trend') subplot(312) plot(t(ind), comp(:, 2)), title('Slope') subplot(313) plot(t(ind), comp(:, 5)), title('Cycle') else % Matlab stackedplot(comp) end %% Hodrick-Prescott filter hpCycle = UChp(yl, 4); %% All cycle estimations if exist('OCTAVE_VERSION', 'builtin') % Octave cycles = [hpCycle ... m2.comp(ind, 5) ... m3.comp(ind, 4) ... m4.comp(ind, 4)]; else cycles = [hpCycle ... table2array(m2.comp(ind, 5)) ... table2array(m3.comp(ind, 4)) ... table2array(m4.comp(ind, 4))]; end figure(1) clf plot(cycles) legend('HP Trend', 'BSM', 'AR(2)', 'ARMA(2,1)') %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Examples from other papers / books %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % UK drivers killed and seriously injured % Harvey (1989) pp. 82 UKDriverDeaths = [1687,1508,1507,1385,1632,1511,1559,1630,1579,1653,2152,2148,1752,1765,1717,1558,1575,1520, ... 1805,1800,1719,2008,2242,2478,2030,1655,1693,1623,1805,1746,1795,1926,1619,1992,2233,2192,2080,1768,1835, ... 1569,1976,1853,1965,1689,1778,1976,2397,2654,2097,1963,1677,1941,2003,1813,2012,1912,2084,2080,2118,2150, ... 1608,1503,1548,1382,1731,1798,1779,1887,2004,2077,2092,2051,1577,1356,1652,1382,1519,1421,1442,1543,1656, ... 1561,1905,2199,1473,1655,1407,1395,1530,1309,1526,1327,1627,1748,1958,2274,1648,1401,1411,1403,1394,1520, ... 1528,1643,1515,1685,2000,2215,1956,1462,1563,1459,1446,1622,1657,1638,1643,1683,2050,2262,1813,1445,1762, ... 1461,1556,1431,1427,1554,1645,1653,2016,2207,1665,1361,1506,1360,1453,1522,1460,1552,1548,1827,1737,1941, ... 1474,1458,1542,1404,1522,1385,1641,1510,1681,1938,1868,1726,1456,1445,1456,1365,1487,1558,1488,1684,1594, ... 1850,1998,2079,1494,1057,1218,1168,1236,1076,1174,1139,1427,1487,1483,1513,1357,1165,1282,1110,1297,1185, ... 1222,1284,1444,1575,1737,1763]'; y = log(UKDriverDeaths); % Model in Harvey (1989), pp. 83 m1 = UC(y(1 : 168), 12, 'model', 'llt/equal/arma(0,0)'); % Automatic identification m2 = UC(y(1 : 168), 12); % 'model' with 'outlier's and full sample m3 = UC(y, 12, 'outlier', 4, 'model', 'rw/equal/arma(0,0)'); % Optimal 'model' with 'outlier's m4 = UC(y, 12, 'outlier', 4); %% Rainfall in Fortaleza % Harvey (1989), pp. 86 y = [200.1, 85.2, 180.6, 135.6, 123.3, 159, 127.3, 177, 173.4, 145.7, 135.7, 171.6, ... 144.5, 146.8, 145.2, 109.8, 123.8, 247.8, 83.2, 128.9, 147, 162.8, 145.9, 225.6, 205.8, 148.7, 158.1, ... 156.9, 46.8, 50.3, 59.7, 153.9, 142.3, 124.6, 150.8, 104.7, 130.7, 139.9, 132, 73.6, 78.4, 153.4, 107.7, ... 121.1, 143, 250.5, 249.1, 214.4, 183.9, 86.3, 241.4, 94, 154.5, 87.8, 78.9, 113.6, 118.9, 143, 69.7, 83.4, ... 101.5, 205.1, 137.3, 244.6, 190.5, 151.2, 53, 132.8, 207.7, 131.9, 65.6, 184.7, 249.6, 159.5, 151.3, 184.7, ... 113.7, 157.1, 119.5, 99.5, 123, 110.7, 113.3, 87.9, 93.7, 188.8, 166.1, 82, 131.3, 158.6, 191.1, 144.7, 91.6, ... 78, 104.2, 109, 175, 172.4, 172.6, 138.4, 188.1, 111.4, 74.7, 137.8, 106.8, 103.2, 115.2, 80.6, 122.5, ... 50.4, 149.3, 101.1, 173.7, 125.8, 210.2, 242.8, 163, 128.8, 183.9, 138.5, 180.5, 119.2, 209.3, 129.9, ... 233.1, 251.2, 177.8, 141.7, 194.1, 175.2, 99.6]'; m1 = UC(y, 1, 'model', '?/-8/?/?'); m2 = UC(y, 1, 'model', '?/?/?/?'); %% Purse snatchings in Hyde Park, Chicago % Harvey (1989), pp. 89 y = [10, 15, 10, 10, 12, 10, 7, 17, 10, 14, 8, 17, 14, 18, 3, 9, ... 11, 10, 6, 12, 14, 10, 25, 29, 33, 33, 12, 19, 16, 19, 19, 12, ... 34, 15, 36, 29, 26, 21, 17, 19, 13, 20, 24, 12, 6, 14, 6, 12, 9, ... 11, 17, 12, 8, 14, 14, 12, 5, 8, 10, 3, 16, 8, 8, 7, 12, 6, 10, ... 8, 10, 5, 7]'; m1 = UC(y, 1, 'model', 'rw/none/arma(0,0)'); m2 = UC(y, 1); %% US GNP data % Harvey (1989), pp. 92 y = [116.8, 120.1, 123.2, 130.2, 131.4, 125.6, 124.5,134.3, 135.2, 151.8, ... 146.4, 139, 127.8, 147, 165.9, 165.5, 179.4, 190, 189.8,190.9,203.6, ... 183.5, 169.3, 144.2,141.5, 154.3, 169.5, 193, 203.2, 192.9, 209.4, 227.2, ... 263.7, 297.8, 337.1,361.3, 355.2, 312.6, 309.9, 323.7, 324.1, 355.3, ... 383.4, 395.1, 412.8, 406,438, 446.1, 452.5, 447.3, 475.9, 487.7, 497.2,... 529.8, 551, 581.1, 617.8,658.1, 675.2, 706.6, 724.7]'; m1 = UC(log(y), 1, 'model', 'llt/-8/none/arma(0,0)'); m2 = UC(log(y), 1, 'model', '?/?/?/?'); %% Quartely air passengers data % Harvey (1989), pp. 93 AirPassengers = [112 118 132 129 121 135 148 148 136 119 104 ... 118 115 126 141 135 125 149 170 170 158 133 ... 114 140 145 150 178 163 172 178 199 199 184 ... 162 146 166 171 180 193 181 183 218 230 242 ... 209 191 172 194 196 196 236 235 229 242 264 ... 272 237 211 180 201 204 188 235 227 234 264 ... 302 293 259 229 203 229 242 233 267 269 270 ... 315 364 347 312 274 237 278 284 277 317 313 ... 318 374 413 405 355 306 271 306 315 301 356 ... 348 355 422 465 467 404 347 305 336 340 318 ... 362 348 363 435 491 505 404 359 310 337 360 ... 342 406 396 420 472 548 559 463 407 362 405 ... 417 391 419 461 472 535 622 606 508 461 390 432]'; y = log(sum(reshape(AirPassengers, 3, 48)))'; m1 = UC(y, 4, 'model', 'llt/equal/arma(0,0)'); m2 = UC(y, 4); %% UK coal consumption in the UK % Harvey (1989), pp. 95 y = [303, 169, 152, 257, 247, 189, 146, 220, 248, 195, 141, 235, 278, ... 167, 150, 261, 244, 174, 104, 228, 243, 170, 113, 219, 237, 138, 114, ... 208, 190, 157, 93, 182, 183, 106, 86, 144, 226, 128, 62, 130, 169, 94, ... 91, 188, 148, 114, 62, 139, 104, 99, 76, 122, 107, 76, 51, 111, 95, 71, ... 63, 107, 65, 62, 39, 80, 86, 57, 37, 79, 83, 59, 42, 89, 84, 59, 43, 73, ... 90, 56, 40, 75, 80, 45, 32, 73, 72, 46, 38, 78, 77, 49, 41, 77, 78, 49, ... 34, 72, 68, 51, 23, 42, 72, 49, 39, 64, 63, 43, 45, 56]'; m1 = UC(log(y( 1 : 92)), 4, 'model', 'llt/eq/arma(0,0)'); m2 = UC(log(y( 1 : 92)), 4);