Keyword: 多変量時系列, 多入力モデル, 予測
概要
本サンプルは多入力モデルからの時系列の予測の計算を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される時系列データを分析し、時系列の予測値を出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_tsa_multi_inp_model_forecast() のExampleコードです。本サンプル及び関数の詳細情報は nag_tsa_multi_inp_model_forecast のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_tsa_multi_inp_model_forecast のマニュアルページを参照)このデータをダウンロード |
nag_tsa_multi_inp_model_forecast (g13bjc) Example Program Data 40 8 6 1 0 0 0 0 1 4 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 3 0.4950 0.2380 -0.3670 -3.8760 4.5160 2.4740 8.6290 0.6880 -82.8580 1.0 1.0 0.0 0.0 8.075 105.0 1.0 0.0 1.0 0.0 7.819 119.0 1.0 0.0 0.0 1.0 7.366 119.0 1.0 -1.0 -1.0 -1.0 8.113 109.0 2.0 1.0 0.0 0.0 7.380 117.0 2.0 0.0 1.0 0.0 7.134 135.0 2.0 0.0 0.0 1.0 7.222 126.0 2.0 -1.0 -1.0 -1.0 7.768 112.0 3.0 1.0 0.0 0.0 7.386 116.0 3.0 0.0 1.0 0.0 6.965 122.0 3.0 0.0 0.0 1.0 6.478 115.0 3.0 -1.0 -1.0 -1.0 8.105 115.0 4.0 1.0 0.0 0.0 8.060 122.0 4.0 0.0 1.0 0.0 7.684 138.0 4.0 0.0 0.0 1.0 7.580 135.0 4.0 -1.0 -1.0 -1.0 7.093 125.0 5.0 1.0 0.0 0.0 6.129 115.0 5.0 0.0 1.0 0.0 6.026 108.0 5.0 0.0 0.0 1.0 6.679 100.0 5.0 -1.0 -1.0 -1.0 7.414 96.0 6.0 1.0 0.0 0.0 7.112 107.0 6.0 0.0 1.0 0.0 7.762 115.0 6.0 0.0 0.0 1.0 7.645 123.0 6.0 -1.0 -1.0 -1.0 8.639 122.0 7.0 1.0 0.0 0.0 7.667 128.0 7.0 0.0 1.0 0.0 8.080 136.0 7.0 0.0 0.0 1.0 6.678 140.0 7.0 -1.0 -1.0 -1.0 6.739 122.0 8.0 1.0 0.0 0.0 5.569 102.0 8.0 0.0 1.0 0.0 5.049 103.0 8.0 0.0 0.0 1.0 5.642 89.0 8.0 -1.0 -1.0 -1.0 6.808 77.0 9.0 1.0 0.0 0.0 6.636 89.0 9.0 0.0 1.0 0.0 8.241 94.0 9.0 0.0 0.0 1.0 7.968 104.0 9.0 -1.0 -1.0 -1.0 8.044 108.0 10.0 1.0 0.0 0.0 7.791 119.0 10.0 0.0 1.0 0.0 7.024 126.0 10.0 0.0 0.0 1.0 6.102 119.0 10.0 -1.0 -1.0 -1.0 6.053 103.0 11.0 1.0 0.0 0.0 5.941 0.0 11.0 0.0 1.0 0.0 5.386 0.0 11.0 0.0 0.0 1.0 5.811 0.0 11.0 -1.0 -1.0 -1.0 6.716 0.0 12.0 1.0 0.0 0.0 6.923 0.0 12.0 0.0 1.0 0.0 6.939 0.0 12.0 0.0 0.0 1.0 6.705 0.0 12.0 -1.0 -1.0 -1.0 6.914 0.0 0.0 0.0 0.0 0.0 0.1720 0.0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 4 0.0 0.0 0.0 0.0 1.6743 0.0 0.0 0.0 0.0 -0.9505 0.0 0.0 0.0 0.0 1.4605 0.0 0.0 0.0 0.0 -0.4862 0.0 0.0 0.0 0.0 0.8993
- 1行目はタイトル行で読み飛ばされます。
- 2行目に入力時系列と出力時系列の値の数(nev)、出力時系列の予測値の数(nfv)、入力時系列と出力時系列の数(nseries)を指定しています。
- 3行目はARIMAモデルの次数ベクトル(自己回帰の数、非季節階差の次数、移動平均、季節自己回帰、季節階差の次数、季節移動平均と季節期間)を指定しています。
- 4〜7行目は伝達関数モデルの次数(transfv)を指定しています。
- 8〜9行目は多入力モデルの引数の推定値(para)を指定しています。
- 10〜49行目に入力時系列と出力時系列の過去値(xxy)を指定しています。
- 50〜57行目に出力時系列の予測に必要な入力時系列の将来値(xxy)を指定しています。
- 58行目に入力時系列ARIMAモデルの推定残差分散(rmsxy)を指定しています。
- 59〜65行目に入力時系列ARIMAモデルの次数の配列(mrx)を指定しています。
- 66〜70行目に入力時系列ARIMAモデルの引数の値(parx)を指定しています。
出力結果
(本関数の詳細はnag_tsa_multi_inp_model_forecast のマニュアルページを参照)この出力例をダウンロード |
nag_tsa_multi_inp_model_forecast (g13bjc) Example Program Results Parameters to g13bjc ____________________ nseries...................... 6 cfixed.................. Nag_TRUE outfile................ stdout 40 sets of observations were processed. The residual mean square for the output series is 20.0902 The forecast values and their standard errors are i fva fsd 1 93.398 4.4822 2 96.958 6.1498 3 86.046 7.0315 4 77.589 7.2885 5 82.139 7.3327 6 96.276 7.5220 7 98.345 8.0883 8 93.577 8.8020 The values of z(t) and noise(t) are i z1 z2 z3 z4 z5 noise 1 -0.339 -3.889 0.000 0.000 188.603 -79.375 2 -0.339 -0.000 4.514 0.000 199.438 -84.613 3 -0.339 -0.000 0.000 2.479 204.683 -87.823 4 -0.339 3.889 -4.514 -2.479 204.383 -91.940 5 -0.678 -3.889 0.000 0.000 210.623 -89.056 6 -0.678 -0.000 4.514 0.000 208.591 -77.426 7 -0.678 -0.000 0.000 2.479 205.070 -80.870 8 -0.678 3.889 -4.514 -2.479 203.407 -87.624 9 -1.017 -3.889 0.000 0.000 206.974 -86.068 10 -1.017 -0.000 4.514 0.000 206.132 -87.628 11 -1.017 -0.000 0.000 2.479 201.920 -88.381 12 -1.017 3.889 -4.514 -2.479 194.819 -75.698 13 -1.356 -3.889 0.000 0.000 203.974 -76.729 14 -1.356 -0.000 4.514 0.000 209.884 -75.041 15 -1.356 -0.000 0.000 2.479 210.705 -76.828 16 -1.356 3.889 -4.514 -2.479 210.373 -80.912 17 -1.695 -3.889 0.000 0.000 205.942 -85.358 18 -1.695 -0.000 4.514 0.000 194.575 -89.394 19 -1.695 -0.000 0.000 2.479 185.866 -86.650 20 -1.695 3.889 -4.514 -2.479 185.509 -84.709 21 -2.035 -3.889 0.000 0.000 191.606 -78.682 22 -2.035 -0.000 4.514 0.000 193.194 -80.673 23 -2.035 -0.000 0.000 2.479 199.896 -77.340 24 -2.035 3.889 -4.514 -2.479 203.497 -76.358 25 -2.374 -3.889 0.000 0.000 214.552 -80.290 26 -2.374 -0.000 4.514 0.000 213.770 -79.910 27 -2.374 -0.000 0.000 2.479 216.796 -76.901 28 -2.374 3.889 -4.514 -2.479 206.780 -79.302 29 -2.713 -3.889 0.000 0.000 200.416 -91.814 30 -2.713 -0.000 4.514 0.000 185.941 -84.742 31 -2.713 -0.000 0.000 2.479 171.495 -82.261 32 -2.713 3.889 -4.514 -2.479 166.673 -83.857 33 -3.052 -3.889 0.000 0.000 173.418 -77.477 34 -3.052 -0.000 4.514 0.000 176.573 -84.035 35 -3.052 -0.000 0.000 2.479 192.594 -88.021 36 -3.052 3.889 -4.514 -2.479 201.261 -87.105 37 -3.391 -3.889 0.000 0.000 207.879 -81.599 38 -3.391 -0.000 4.514 0.000 210.249 -85.372 39 -3.391 -0.000 0.000 2.479 205.262 -85.350 40 -3.391 3.889 -4.514 -2.479 193.874 -84.379 41 -3.730 -3.889 0.000 0.000 185.617 -84.600 42 -3.730 0.000 4.514 0.000 178.969 -82.795 43 -3.730 0.000 0.000 2.479 169.607 -82.309 44 -3.730 3.889 -4.514 -2.479 166.832 -82.409 45 -4.069 -3.889 0.000 0.000 172.733 -82.636 46 -4.069 0.000 4.514 0.000 178.579 -82.748 47 -4.069 0.000 0.000 2.479 182.739 -82.804 48 -4.069 3.889 -4.514 -2.479 183.582 -82.831
- 6行目に適用されている時系列の数が出力されています。
- 8〜12行目には以下に示すプログラムのオプション引数が出力されています。
cfixed 定数 c を初期値に固定されたままにするか推定するかを指定します。"Nag_TRUE"は固定されたままにすることを意味します。 outfile 結果が出力されるファイル名。 - 11行目には40組の観測値が処理されたことが示されています。
- 13行目には出力時系列の残差平方和が出力されています。
- 15から27行目には予測値と標準誤差が出力されています。
- 29〜80行目にはZ値、ノイズが出力されています。
ソースコード
(本関数の詳細はnag_tsa_multi_inp_model_forecast のマニュアルページを参照)
※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法
このソースコードをダウンロード |
/* nag_tsa_multi_inp_model_forecast (g13bjc) Example Program. * * CLL6I261D/CLL6I261DL Version. * * Copyright 2017 Numerical Algorithms Group. * * Mark 26.1, 2017. */ #include <nag.h> #include <stdio.h> #include <string.h> #include <nag_string.h> #include <nag_stdlib.h> #include <nagg13.h> #define PARX(I, J) parx[(I) *tdparx + J] #define XXY(I, J) xxy[(I) *tdxxy + J] #define MRX(I, J) mrx[(I) *tdmrx + J] int main(void) { Integer exit_status = 0; Integer i, inser, j, ldparx, *mrx = 0, n, nev, nfv, npara; Integer nseries, tdmrx, tdparx, tdxxy; Nag_ArimaOrder arimav; Nag_G13_Opt options; Nag_TransfOrder transfv; double *fsd = 0, *fva = 0, *para = 0, *parx = 0, *rmsxy = 0; double *xxy = 0; NagError fail; INIT_FAIL(fail); printf("nag_tsa_multi_inp_model_forecast (g13bjc) Example Program " "Results\n"); scanf(" %*[^\n]"); /* Skip heading in data file */ #define ZT(I, J) options.zt[(J)+(I) *options.tdzt] /* * Initialize the option-setting function. */ /* nag_tsa_options_init (g13bxc). * Initialization function for option setting */ nag_tsa_options_init(&options); scanf("%ld%ld%ld", &nev, &nfv, &nseries); if (nseries > 0 && nev > 0 && nfv > 0) { /* * Set option variable to the desired value. */ options.cfixed = Nag_TRUE; /* * Allocate memory to the arrays in structure transfv containing * the transfer function model orders of the input series. */ /* nag_tsa_transf_orders (g13byc), see above. */ nag_tsa_transf_orders(nseries, &transfv, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_tsa_transf_orders (g13byc).\n%s\n", fail.message); exit_status = 1; goto END; } /* * Read the orders vector of the ARIMA model for the output noise * component into structure arimav. */ scanf("%ld%ld%ld%ld%ld" "%ld%ld", &arimav.p, &arimav.d, &arimav.q, &arimav.bigp, &arimav.bigd, &arimav.bigq, &arimav.s); /* * Read the transfer function model orders of the input series into * structure transfv. */ inser = nseries - 1; for (j = 0; j < inser; ++j) scanf("%ld", &transfv.b[j]); for (j = 0; j < inser; ++j) scanf("%ld", &transfv.q[j]); for (j = 0; j < inser; ++j) scanf("%ld", &transfv.p[j]); for (j = 0; j < inser; ++j) scanf("%ld", &transfv.r[j]); npara = 0; for (i = 0; i < inser; ++i) npara = npara + transfv.q[i] + transfv.p[i]; npara = npara + arimav.p + arimav.q + arimav.bigp + arimav.bigq + nseries; ldparx = 8; if (npara >= 1) { if (!(fsd = nAG_ALLOC(nfv, double)) || !(fva = nAG_ALLOC(nfv, double)) || !(para = nAG_ALLOC(npara, double)) || !(parx = nAG_ALLOC(ldparx * (nseries - 1), double)) || !(rmsxy = nAG_ALLOC(nseries, double)) || !(xxy = nAG_ALLOC((nev + nfv) * (nseries), double)) || !(mrx = nAG_ALLOC(7 * (nseries - 1), Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } tdmrx = nseries - 1; tdparx = nseries - 1; tdxxy = nseries; for (i = 0; i < npara; ++i) scanf("%lf", ¶[i]); n = nev + nfv; for (i = 0; i < n; ++i) for (j = 0; j < nseries; ++j) scanf("%lf", &XXY(i, j)); for (i = 0; i < nseries; ++i) scanf("%lf", &rmsxy[i]); for (i = 0; i < 7; ++i) for (j = 0; j < inser; ++j) scanf("%ld", &MRX(i, j)); for (i = 0; i < 5; ++i) for (j = 0; j < inser; ++j) scanf("%lf", &PARX(i, j)); /* nag_tsa_multi_inp_model_forecast (g13bjc), see above. */ fflush(stdout); nag_tsa_multi_inp_model_forecast(&arimav, nseries, &transfv, para, npara, nev, nfv, xxy, tdxxy, rmsxy, mrx, tdmrx, parx, ldparx, tdparx, fva, fsd, &options, &fail); if (fail.code == NE_NOERROR || fail.code == NE_SOLUTION_FAIL_CONV || fail.code == NE_MAT_NOT_POS_DEF) { printf("%1ld sets of observations were processed.\n", nev); printf("\nThe residual mean square for the output "); printf("series is %10.4f\n\n", rmsxy[nseries - 1]); printf("The forecast values and their standard errors are\n\n"); printf("\n i fva fsd\n\n"); for (i = 0; i < nfv; ++i) printf("%4ld%10.3f%10.4f\n", i + 1, fva[i], fsd[i]); printf("\nThe values of z(t) and noise(t) are\n\n"); printf(" i z1 z2 z3 z4" " z5 noise\n\n"); for (i = 0; i < n; ++i) { printf("%4ld", i + 1); for (j = 0; j < nseries - 1; ++j) printf("%10.3f ", ZT(i, j)); printf("%10.3f\n", options.noise[i]); } } else { printf("Error from nag_tsa_multi_inp_model_forecast (g13bjc)." "\n%s\n", fail.message); exit_status = 1; goto END; } } else { printf("npara is out of range: npara = %-3ld\n", npara); /* nag_tsa_free (g13xzc). * Freeing function for use with g13 option setting */ nag_tsa_free(&options); /* nag_tsa_trans_free (g13bzc), see above. */ nag_tsa_trans_free(&transfv); exit_status = 1; goto END; } } else { printf("One or more of nseries, nev and nfv are out of range:" " nseries = %-3ld, nv = %-3ld while " "nfv = %-3ldq\n", nseries, nev, nfv); exit_status = 1; goto END; } /* nag_tsa_free (g13xzc), see above. */ nag_tsa_free(&options); /* nag_tsa_trans_free (g13bzc), see above. */ nag_tsa_trans_free(&transfv); END: nAG_FREE(fsd); nAG_FREE(fva); nAG_FREE(para); nAG_FREE(parx); nAG_FREE(rmsxy); nAG_FREE(xxy); nAG_FREE(mrx); return exit_status; }