Keyword: 一変量時系列, 季節階差, 非季節階差
概要
本サンプルは一変量時系列の季節及び非季節階差の計算を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される時系列データを分析し、季節及び非季節階差を出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_tsa_diff() のExampleコードです。本サンプル及び関数の詳細情報は nag_tsa_diff のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_tsa_diff のマニュアルページを参照)| このデータをダウンロード |
nag_tsa_diff (g13aac) Example Program Data 20 2 1 4 120.0 108.0 98.0 118.0 135.0 131.0 118.0 125.0 121.0 100.0 82.0 82.0 89.0 88.0 86.0 96.0 108.0 110.0 99.0 105.0
- 1行目はタイトル行で読み飛ばされます。
- 2行目に時系列データの数(nx)、非季節階差の次数(d)、季節階差の次数(ds)、季節性(s)を指定しています。
- 3〜6行目に時系列データ(x)を指定しています。
出力結果
(本関数の詳細はnag_tsa_diff のマニュアルページを参照)| この出力例をダウンロード |
nag_tsa_diff (g13aac) Example Program Results
Non-seasonal differencing of order 2 and seasonal differencing
of order 1 with seasonality 4 are applied
The output array holds 20 values, of which the first 14 are differenced values
-11.0 -10.0 -8.0 4.0 12.0
-2.0 18.0 9.0 -4.0 -6.0
-5.0 -2.0 -12.0 5.0 2.0
-10.0 -13.0 17.0 6.0 105.0
- 3〜4行目に2次の非季節階差と1次の季節階差が適用されていることが示されています。
- 6行目に、出力結果の配列には20個の値があり最初の14個は階差であることが示されています。
- 8〜11行目には階差の値(最初の14個)と再構成データ(残り6個)が出力されています。
ソースコード
(本関数の詳細はnag_tsa_diff のマニュアルページを参照)
※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
/* nag_tsa_diff (g13aac) Example Program.
*
* CLL6I261D/CLL6I261DL Version.
*
* Copyright 2017 Numerical Algorithms Group.
*
* Mark 26.1, 2017.
*/
#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg13.h>
int main(void)
{
/* Scalars */
Integer exit_status, i, d, ds, s, nx, nxd;
NagError fail;
/* Arrays */
double *x = 0, *xd = 0;
INIT_FAIL(fail);
exit_status = 0;
printf("nag_tsa_diff (g13aac) Example Program Results\n");
/* Skip heading in data file */
scanf("%*[^\n] ");
scanf("%ld%ld%ld%ld%*[^\n] ", &nx,
&d, &ds, &s);
if (nx > 0) {
/* Allocate memory */
if (!(x = nAG_ALLOC(nx, double)) || !(xd = nAG_ALLOC(nx, double)))
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
for (i = 1; i <= nx; ++i)
scanf("%lf", &x[i - 1]);
scanf("%*[^\n] ");
printf("\n");
printf("Non-seasonal differencing of order %ld "
"and seasonal differencing\nof order %ld "
"with seasonality %ld are applied\n", d, ds, s);
/* nag_tsa_diff (g13aac).
* Univariate time series, seasonal and non-seasonal
* differencing
*/
nag_tsa_diff(x, nx, d, ds, s, xd, &nxd, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_tsa_diff (g13aac).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
printf("\n");
printf("The output array holds %2ld values, of which the "
"first %2ld are differenced values\n\n", nx, nxd);
for (i = 1; i <= nx; ++i) {
printf("%10.1f", xd[i - 1]);
if (i % 5 == 0 || i == nx)
printf("\n");
}
}
END:
nAG_FREE(x);
nAG_FREE(xd);
return exit_status;
}
