Keyword: 多変量時系列, 多入力モデル, 推定
概要
本サンプルは時系列モデルの推定の計算を行うC#によるサンプルプログラムです。 本サンプルは以下に示される時系列データを分析し、時系列モデルの推定値を出力します。
※本サンプルはnAG Library for .NETに含まれる関数 g13be() のExampleコードです。本サンプル及び関数の詳細情報は g13be のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はg13be のマニュアルページを参照)| このデータをダウンロード |
g13be Example Program Data
1 1 40 2 3 20 0
1 0 0 0 0 1 4
1 0
0 0
1 0
3 0
0.0 0.0 2.0 0.5 0.0
8.075 105.0
7.819 119.0
7.366 119.0
8.113 109.0
7.380 117.0
7.134 135.0
7.222 126.0
7.768 112.0
7.386 116.0
6.965 122.0
6.478 115.0
8.105 115.0
8.060 122.0
7.684 138.0
7.580 135.0
7.093 125.0
6.129 115.0
6.026 108.0
6.679 100.0
7.414 96.0
7.112 107.0
7.762 115.0
7.645 123.0
8.639 122.0
7.667 128.0
8.080 136.0
6.678 140.0
6.739 122.0
5.569 102.0
5.049 103.0
5.642 89.0
6.808 77.0
6.636 89.0
8.241 94.0
7.968 104.0
8.044 108.0
7.791 119.0
7.024 126.0
6.102 119.0
6.053 103.0
- 1行目はタイトル行で読み飛ばされます。
- 2行目の1番目のパラメータ(kzef)は入力時系列と出力ノイズの値で時系列データの配列xxyを上書きするかどうかを指定しています。"1"は上書きすることを意味します。2番目のパラメータ(kfc)は定数cを初期値で固定するか推定するかを指定しています。"1"は推定することを意味します。3番目のパラメータ(nxxy)は時系列データの数を指定しています。4番目のパラメータ(nser)は入力時系列と出力時系列の合計数を指定しています。5番目のパラメータ(kef)は尤度オプションを指定しています。"3"は周辺尤度を意味します。6番目のパラメータ(nit)は最大反復数を指定しています。7番目のパラメータ(kzsp)は最小化プロシージャでパラメータzspの入力値を使うかデフォルト値を使うかを指定しています。"0"はデフォルト値を使用することを意味します。
- 3行目はARIMAモデルの次数ベクトル(mr:自己回帰の数、非季節階差の次数、移動平均、季節自己回帰、季節階差の次数、季節移動平均と季節期間)を指定しています。
- 4〜7行目は伝達関数モデルの次数(mt)を指定しています。
- 8行目は多入力モデルの引数(para)を指定しています。
- 9〜48行目に入力時系列と出力時系列の値(xxy)を指定しています。
出力結果
(本関数の詳細はg13be のマニュアルページを参照)| この出力例をダウンロード |
g13be Example Program Results
The number of iterations carried out is 11
Final values of the parameters and their standard deviations
I PARA(I) SD
1 0.380924 0.166379
2 -0.257786 0.178178
3 8.956084 0.948061
4 0.659641 0.060239
5 -75.435521 33.505341
The correlation matrix is
1.0000 -0.1839 -0.1775 -0.0340 0.1394
-0.1839 1.0000 0.0518 0.2547 -0.2860
-0.1775 0.0518 1.0000 -0.3070 -0.2926
-0.0340 0.2547 -0.3070 1.0000 -0.8185
0.1394 -0.2860 -0.2926 -0.8185 1.0000
The residuals and the z and n values are
I RES(I) z(t) n(t)
1 0.397 180.567 -75.567
2 3.086 191.430 -72.430
3 -2.818 196.302 -77.302
4 -9.941 195.460 -86.460
5 -5.061 201.594 -84.594
6 14.053 199.076 -64.076
7 2.624 195.211 -69.211
8 -5.823 193.450 -81.450
9 -2.147 197.179 -81.179
10 -0.216 196.217 -74.217
11 -2.517 191.812 -76.812
12 7.916 184.544 -69.544
13 1.423 194.322 -72.322
14 11.936 200.369 -62.369
15 5.117 200.990 -65.990
16 -5.672 200.468 -75.468
17 -5.681 195.763 -80.763
18 -1.637 184.025 -76.025
19 -1.019 175.360 -75.360
20 -2.623 175.492 -79.492
21 3.283 182.162 -75.162
22 6.896 183.857 -68.857
23 5.395 190.797 -67.797
24 0.875 194.327 -72.327
25 -4.153 205.558 -77.558
26 6.206 204.261 -68.261
27 4.208 207.104 -67.104
28 -2.387 196.423 -74.423
29 -11.803 189.924 -87.924
30 6.435 175.158 -72.158
31 1.342 160.761 -71.761
32 -4.924 156.575 -79.575
33 4.799 164.256 -75.256
34 -0.074 167.783 -73.783
35 -6.023 184.483 -80.483
36 -6.427 193.055 -85.055
37 -2.527 199.390 -80.390
38 2.039 201.302 -75.302
39 0.243 195.695 -76.695
40 -3.166 183.738 -80.738
The state set consists of 6 values
6.0530 183.7384 -5.7855 -0.1645 0.1800 -3.0977
The number of degrees of freedom is 34
- 3行目には実行した反復数が出力されています。
- 9〜13行目にはパラメータの最終値と標準偏差が出力されています。
- 17から21行目には相関行列が出力されています。
- 28〜67行目には残差、Z値、ノイズが出力されています。
- 71行目には状態集合を構成する6つの値が出力されています。
- 73行目には自由度が出力されています。
ソースコード
(本関数の詳細はg13be のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g13be Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
namespace NagDotNetExamples
{
public class G13BEE
{
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/g13bee.d");
}
else
{
sr = new DataReader(datafile);
}
double d, s; int dp, i, inc, itc, j, kef, kfc, kpriv, kzef, kzsp,
mx, ncd, nce, ncf, ndf, ndv, nit, npara, ncg, nser, nsttf,
nxxy, qp, qx, smx ;
double[] zsp = new double[4];
int[] mr = new int[7];
int ifail;
Console.WriteLine("g13be Example Program Results");
// Skip heading in data file
sr.Reset();
sr.Reset();
kzef = int.Parse(sr.Next());
kfc = int.Parse(sr.Next());
nxxy = int.Parse(sr.Next());
nser = int.Parse(sr.Next());
kef = int.Parse(sr.Next());
nit = int.Parse(sr.Next());
kzsp = int.Parse(sr.Next());
int[,] mt = new int[4, nser];
if (nxxy > 0 && nser > 0)
{
if (kzsp != 0)
{
sr.Reset();
for (int zsplen = 0; zsplen < zsp.Length; zsplen++)
{
zsp[zsplen] = Double.Parse(sr.Next());
}
}
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;
ncg = 0;
qx = 0;
smx = 0;
ncf = nser;
inc = 1;
for ( i = 1; i<= nser-1; i++)
{
npara = npara + mt[2-1,i-1] + mt[3-1,i-1];
if (mt[4-1,i-1]>1)
{
ncg = ncg + mt[0,i-1]+ mt[1,i-1]+ mt[2,i-1];
if (mt[4-1,i-1]==3)
{
mx = Math.Max(mt[1-1,i-1]+mt[2-1,i-1],mt[3-1,i-1]);
qx = Math.Max(qx,mx);
smx = smx + mx;
}
}
else if (mt[4-1,i-1]==1 && kef==3)
{
if (mt[3-1,i-1]>0)
{
ncf = ncf + 1;
}
inc = inc + 1;
}
}
npara = npara + mr[0] + mr[2] + mr[3] + mr[5] + nser;
// Calculate size of arrays
int isttf = mr[4-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]) + ncg;
qp = mr[3-1] + mr[6-1]*mr[7-1];
dp = mr[2-1] + mr[5-1]*mr[7-1];
if (mr[3-1]>0 && kef>1)
{
inc = inc + 1;
}
if (kfc>0 && kef==3)
{
inc = inc + 1;
}
qx = qp;
ncd = npara + kfc + smx;
if (mr[1-1]>0)
{
ncf = ncf + inc;
}
if (mr[3-1]>0)
{
ncf = ncf + inc;
}
if (mr[4-1]>0)
{
ncf = ncf + inc;
}
if (mr[6-1]>0)
{
ncf = ncf + inc;
}
if (qx>0)
{
ncf = ncf + 1;
}
if (kfc>0)
{
ncf = ncf + 1;
}
ncd = ncd + qx;
nce = nxxy + dp + 6*qx;
double[,] cm = new double[npara, npara];
double[] para = new double[npara];
double[] res = new double[nxxy];
double[] sd = new double[npara];
double[] sttf = new double[isttf];
double[,] xxy = new double[nxxy, nser];
if (npara > 0)
{
sr.Reset();
for (i = 1; i <= npara; i++)
{
para[i - 1] = double.Parse(sr.Next());
}
for (i = 1; i <= nxxy; i++)
{
sr.Reset();
for (j = 1; j <= nser; j++)
{
xxy[i - 1, j - 1] = double.Parse(sr.Next());
}
}
// Set kpriv to 1 to obtain monitoring information *
kpriv = 0;
//
G13.g13be(mr, nser, mt, para, npara, kfc, nxxy, xxy, kef, nit, kzsp, zsp, out itc, sd, cm,
out s, out d, out ndf, kzef, res, sttf, out nsttf, kpriv, out ifail);
//
if (ifail != 0)
{
Console.WriteLine("");
Console.WriteLine(" {0}{1,4}", "g13be fails. ifail =", ifail);
}
if ((((ifail == 0) || (ifail == 8)) || (ifail == 9)) || (ifail == 11))
{
Console.WriteLine("");
Console.WriteLine(" {0}{1,4}", "The number of iterations carried out is", itc);
Console.WriteLine("");
Console.WriteLine(" {0}", "Final values of the parameters and their standard deviations");
Console.WriteLine("");
Console.WriteLine(" {0}", " I PARA(I) SD");
Console.WriteLine("");
for (i = 1; i <= npara; i++)
{
Console.WriteLine(" {0,4}{1,20:f6}{2,20:f6}", i, para[i - 1], sd[i - 1]);
}
Console.WriteLine("");
Console.WriteLine(" {0}", "The correlation matrix is");
Console.WriteLine("");
for (i = 1; i <= npara; i++)
{
for (j = 1; j <= npara; j++)
{
Console.Write(" {0, 10:f4}", cm[i - 1, j - 1]);
}
Console.WriteLine(""); ;
}
Console.WriteLine(""); ;
Console.WriteLine("");
Console.WriteLine(" {0}", "The residuals and the z and n values are");
Console.WriteLine("");
Console.WriteLine(" {0}", " I RES(I) z(t) n(t)");
Console.WriteLine("");
ndv = nxxy - mr[1] - mr[4] * mr[6];
for (i = 1; i <= nxxy; i++)
{
if (i <= ndv)
{
Console.Write(" {0,4}", i);
Console.Write(" {0, 15:f3}", res[i - 1]);
for (j = 1; j <= nser; j++)
{
Console.Write(" {0, 15:f3}", xxy[i - 1, j - 1]);
}
Console.WriteLine(""); ;
}
else
{
Console.WriteLine(" {0, 4}", i);
for (j = 1; j <= nser; j++)
{
Console.Write(" {0, 15:f3}", xxy[i - 1, j - 1]);
}
Console.WriteLine(""); ;
}
}
if ((mr[1] != 0) || (mr[4] != 0))
{
Console.WriteLine("");
Console.WriteLine(" {0}", "** Note that the residuals relate to differenced values **");
}
Console.WriteLine("");
Console.WriteLine(" {0}{1,4}{2}", "The state set consists of", nsttf, " values");
Console.WriteLine("");
for (i = 1; i <= nsttf; i++)
{
Console.Write(" {0, 10:f4}", sttf[i - 1] );
}
Console.WriteLine(""); ;
Console.WriteLine("");
Console.WriteLine(" {0}{1,4}", "The number of degrees of freedom is", ndf);
}
}
}
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Exception Raised");
}
}
}
}
