Keyword: 多変量時系列, 多入力モデル, 予測
概要
本サンプルは多入力モデルからの時系列の予測の計算を行うC#によるサンプルプログラムです。 本サンプルは以下に示される時系列データを分析し、時系列の予測値を出力します。
※本サンプルはnAG Library for .NETに含まれる関数 g13bj() のExampleコードです。本サンプル及び関数の詳細情報は g13bj のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はg13bj のマニュアルページを参照)| このデータをダウンロード |
g13bj Example Program Data
1 40 8 6 1
1 0 0 0 0 1 4
0 0 0 0 1 0
0 0 0 0 0 0
0 0 0 0 1 0
1 1 1 1 3 0
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 0 0 2 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 4 0
0.0 0.0 0.0 0.0 1.6743 0.0
0.0 0.0 0.0 0.0 -0.9505 0.0
0.0 0.0 0.0 0.0 1.4605 0.0
0.0 0.0 0.0 0.0 -0.4862 0.0
0.0 0.0 0.0 0.0 0.8993 0.0
- 1行目はタイトル行で読み飛ばされます。
- 2行目の1番目のパラメータ(kfc)はモデルをフィットする際に定数が推定されたか固定値なのかを指定しています。"1"は推定されたことを意味します。2番目のパラメータ(nev)は入力時系列と出力時系列の値の数を指定しています。3番目のパラメータ(nfv)は出力時系列の予測値の数を指定しています。4番目のパラメータ(nser)は入力時系列と出力時系列の数を指定しています。5番目のパラメータ(kzef)は入力時系列と出力ノイズの値で時系列データの配列xxyを上書きするかどうかを指定しています。"1"は上書きすることを意味します。
- 3行目はARIMAモデルの次数ベクトル(mr:自己回帰の数、非季節階差の次数、移動平均、季節自己回帰、季節階差の次数、季節移動平均と季節期間)を指定しています。
- 4〜7行目は伝達関数モデルの次数(mt)を指定しています。
- 8〜9行目は多入力モデルの引数の推定値(para)を指定しています。
- 10〜49行目に入力時系列と出力時系列の過去値(xxy)を指定しています。
- 50〜57行目に出力時系列の予測に必要な入力時系列の将来値(xxy)を指定しています。
- 58行目に入力時系列ARIMAモデルの推定残差分散(rmsxy)を指定しています。
- 59〜65行目に入力時系列ARIMAモデルの次数の配列(mrx)を指定しています。
- 66〜70行目に入力時系列ARIMAモデルの引数の値(parx)を指定しています。
出力結果
(本関数の詳細はg13bj のマニュアルページを参照)| この出力例をダウンロード |
g13bj Example Program Results
After processing 40 sets of observations
6 values of the state set are derived
6.0530 193.8741 2.0790 -2.8580 -3.5906 -2.5203
The residual mean square for the output
series is also derived and its value is 20.7599
The forecast values and their standard errors are
I FVA FSD
1 93.398 4.5563
2 96.958 6.2172
3 86.046 7.0933
4 77.589 7.3489
5 82.139 7.3941
6 96.276 7.5823
7 98.345 8.1445
8 93.577 8.8536
The values of z(t) and n(t) are
I z1 z2 z3 z4 z5 n
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
- 3〜4行目には40組の観測値が処理され、6つの状態集合が得られたことが示されています。
- 6行目には状態集合が出力されています。
- 13行目には出力時系列の残差平均平方が出力されています。
- 15から22行目には予測値とその標準誤差が出力されています。
- 28〜75行目にはZ値、ノイズが出力されています。
ソースコード
(本関数の詳細はg13bj のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g13bj Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
namespace NagDotNetExamples
{
public class G13BJE
{
static bool defaultdata = true;
static string datafile = "";
static void Main(String[] args)
{
if (args.Length == 1)
{
defaultdata = false;
datafile = args[0];
}
StartExample();
}
public static void StartExample()
{
try
{
DataReader sr = null;
if (defaultdata)
{
sr = new DataReader("exampledata/g13bje.d");
}
else
{
sr = new DataReader(datafile);
}
int i, isttf, j, kfc, kzef, ldparx, n, ncf=0, nev, nfv, npara, nparx, nser, nsttf; int[] mr = new int[7];
int ifail;
Console.WriteLine("g13bj Example Program Results");
// Skip heading in data file
sr.Reset();
sr.Reset();
kfc = int.Parse(sr.Next());
nev = int.Parse(sr.Next());
nfv = int.Parse(sr.Next());
nser = int.Parse(sr.Next());
kzef = int.Parse(sr.Next());
double[] fsd = new double[nfv];
double[] fva = new double[nfv];
double[] rmsxy = new double[nser];
int[,] mrx = new int[7, nser];
int[,] mt = new int[4, nser];
if (nser > 0 && nfv > 0 && nev > 0)
{
sr.Reset();
for (i = 1; i <= 7; i++)
{
mr[i - 1] = int.Parse(sr.Next());
}
for (i = 1; i <= 4; i++)
{
sr.Reset();
for (j = 1; j <= nser; j++)
{
mt[i - 1, j - 1] = int.Parse(sr.Next());
}
}
npara = 0;
for (i = 1; i <= nser-1; i++)
{
npara = npara + mt[1, i - 1] + mt[2, i - 1];
}
npara = npara + mr[0] + mr[2] + mr[3] + mr[5] + nser;
double [] para = new double[npara];
for(i = 1; i<=nser-1; i++)
{
if (mt[4-1,i-1]>1)
{
ncf = mt[0, i-1] + mt[1, i-1] + mt[2,i-1];
}
}
isttf = mr[1-1]*mr[7-1] + mr[2-1] + mr[5-1]*mr[7-1] + mr[3-1] +
Math.Max(mr[1-1],mr[6-1]*mr[7-1]) + ncf;
double [] sttf = new double [isttf];
if (npara > 0)
{
sr.Reset();
for (i = 1; i <= npara; i++)
{
para[i - 1] = double.Parse(sr.Next());
}
n = nev + nfv;
double [, ] xxy = new double[n, nser]; for (i = 1; i <= n; i++)
{
sr.Reset();
for (j = 1; j <= nser; j++)
{
xxy[i - 1, j - 1] = double.Parse(sr.Next());
}
}
sr.Reset();
for (i = 1; i <= nser; i++)
{
rmsxy[i - 1] = double.Parse(sr.Next());
}
for (i = 1; i <= 7; i++)
{
sr.Reset();
for (j = 1; j <= nser; j++)
{
mrx[i - 1, j - 1] = int.Parse(sr.Next());
}
}
ldparx = 0;
for (i=1; i<=nser-1; i++)
{
if (rmsxy[i-1] != 0.0E0)
{
nparx = mrx[1-1,i-1] + mrx[3-1,i-1] + mrx[4-1,i-1] + mrx[6-1,i-1];
ldparx = Math.Max(ldparx,nparx);
}
}
double[,] parx = new double[ldparx, nser];
for (i = 1; i <= 5; i++)
{
sr.Reset();
for (j = 1; j <= nser; j++)
{
parx[i - 1, j - 1] = double.Parse(sr.Next());
}
}
//
G13.g13bj(mr, nser, mt, para, npara, kfc, nev, nfv, xxy, kzef, rmsxy, mrx, parx, fva, fsd,
sttf, out nsttf, out ifail);
//
Console.WriteLine("");
if (ifail < 0)
{
Console.WriteLine("");
Console.WriteLine("** g13bj failed with ifail = {0,5}", ifail);
}
else
{
if (ifail != 0)
{
Console.WriteLine(" {0}{1,3}", ("g13bj fails.") + (" ifail ="), ifail);
}
if ((((ifail == 0) || (ifail == 8)) || (ifail == 9)) || (ifail == 11))
{
Console.WriteLine(" {0}{1,3}{2}", "After processing", nev, " sets of observations");
Console.WriteLine(" {0,3}{1}", nsttf, " values of the state set are derived");
Console.WriteLine("");
for (i = 1; i <= nsttf; i++)
{
Console.Write(" {0, 8:f4}", sttf[i - 1], i % 7 == 0 || i == nsttf ? "\n" : " ");
}
Console.WriteLine(""); ;
Console.WriteLine("");
Console.WriteLine(" {0}", ("The residual mean square for the") + (" output"));
Console.WriteLine(" {0}{1,10:f4}", "series is also derived and its value is", rmsxy[nser - 1]);
Console.WriteLine("");
Console.WriteLine(" {0}", "The forecast values and their standard errors are");
Console.WriteLine("");
Console.WriteLine(" {0}", " I FVA FSD");
Console.WriteLine("");
for (i = 1; i <= nfv; i++)
{
Console.WriteLine(" {0,4}{1,10:f3}{2,10:f4}", i, fva[i - 1], fsd[i - 1]);
}
Console.WriteLine("");
Console.WriteLine(" {0}", "The values of z(t) and n(t) are");
Console.WriteLine("");
Console.WriteLine(" {0}", " I z1 z2 z3 z4 z5 n");
Console.WriteLine("");
for (i = 1; i <= n; i++)
{
Console.Write(" {0,3}", i);
for (j = 1; j <= nser; j++)
{
Console.Write(" {0,10:f3}", xxy[i - 1, j - 1]);
}
Console.WriteLine(""); ;
}
}
}
}
}
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Exception Raised");
}
}
}
}
