Keyword: 相関行列, ロバスト推定
概要
本サンプルは相関行列のロバスト推定値の計算を行うC#によるサンプルプログラムです。 本サンプルは以下に示されるデータについてロバスト推定値の計算を行います。
※本サンプルはnAG Library for .NETに含まれる関数 g02hm() のExampleコードです。本サンプル及び関数の詳細情報は g02hm のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はg02hm のマニュアルページを参照)| このデータをダウンロード |
g02hm Example Program Data
10 3 : N M
3.4 6.9 12.2
6.4 2.5 15.1
4.9 5.5 14.2
7.3 1.9 18.2
8.8 3.6 11.7
8.4 1.3 17.9
5.3 3.1 15.0
2.7 8.1 7.7
6.1 3.0 21.9
5.3 2.2 13.9 : End of X1 X2 and X3 values
1.0 0.0 1.0 0.0 0.0 1.0 : A
0.0 0.0 0.0 : THETA
4.0 2.0 : CU CW
- 1行目はタイトル行で読み飛ばされます。
- 2行目は観測値の数(n)と独立変数の数(m)を指定しています。
- 3〜12行目に独立変数の観測値(x)を指定しています。
- 13行目は下三角行列Aの初期推定値を指定しています。
- 14行目は位置パラメータθの初期推定値(theta)を指定しています。
- 15行目はU関数の値(cu)とW関数の値(cw)を指定しています。
出力結果
(本関数の詳細はg02hm のマニュアルページを参照)| この出力例をダウンロード |
g02hm Example Program Results
g02hm required 34 iterations to converge
Robust covariance matrix
3.278
-3.692 5.284
4.739 -6.409 11.837
Robust estimates of THETA
5.700
3.864
14.704
- 3行目には収束するのに34回の反復が必要だったことが示されています。
- 4〜7行目にロバスト共分散行列が出力されています。
- 9〜12行目に位置パラメータθのロバスト推定値が出力されています。
ソースコード
(本関数の詳細はg02hm のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g02hm Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G02HME
{
static bool defaultdata = true;
static string datafile = "";
static double[] ruser = new double[2];
static void Main(String[] args)
{
if (args.Length == 1)
{
defaultdata = false;
datafile = args[0];
}
StartExample();
}
public static void StartExample()
{
try
{
G02.G02HM_UCV ucvG02HM = new G02.G02HM_UCV(ucv);
DataReader sr = null;
if (defaultdata)
{
sr = new DataReader("exampledata/g02hme.d");
}
else
{
sr = new DataReader(datafile);
}
double bd, bl, tol; int i, indm, j, k, l1, l2, m, maxit, mm, n, nit, nitmon;
int ifail;
Console.WriteLine("g02hm Example Program Results");
// Skip heading in data file
sr.Reset();
// Read in the dimensions of X
sr.Reset();
n = int.Parse(sr.Next());
m = int.Parse(sr.Next());
double[] a = new double[m*(m+1)/2];
double[] cov = new double[m*(m+1)/2];
double[] theta = new double[m];
double[] wt = new double[n];
double[,] x = new double[n, m];
if (n > 0 && m > 0 && m <= n)
{
// Read in the x matrix
for (i = 1; i <= n; i++)
{
sr.Reset();
for (j = 1; j <= m; j++)
{
x[i - 1, j - 1] = double.Parse(sr.Next());
}
}
// Read in the initial value of A
mm = ((m + 1) * m) / 2;
sr.Reset();
for (j = 1; j <= mm; j++)
{
a[j - 1] = double.Parse(sr.Next());
}
// Read in the initial value of theta
sr.Reset();
for (j = 1; j <= m; j++)
{
theta[j - 1] = double.Parse(sr.Next());
}
// Read in the values of the parameters of the ucv functions
sr.Reset();
ruser[0] = double.Parse(sr.Next());
ruser[1] = double.Parse(sr.Next());
// Set the values remaining parameters
indm = 1;
bl = 0.90e0;
bd = 0.90e0;
maxit = 50;
tol = 0.50e-4;
// Change nitmon to a positive value if monitoring information
// is required
nitmon = 0;
//
G02.g02hm(ucvG02HM, indm, n, m, x, cov, a, wt, theta, bl, bd, maxit, nitmon, tol, out nit,
out ifail);
//
if (ifail == 0)
{
Console.WriteLine(" ");
Console.Write(" {0}{1,4}{2}", "g02hm required ", nit, " iterations to converge");
Console.WriteLine(" ");
Console.Write(" {0}", "Robust covariance matrix");
Console.WriteLine(" ");
l2 = 0;
for (j = 1; j <= m; j++)
{
l1 = l2 + 1;
l2 = l2 + j;
for (k = l1; k <= l2; k++)
{
Console.Write(" {0, 10:f3}", cov[k - 1]);
}
Console.WriteLine(" ");
}
Console.WriteLine(" ");
Console.WriteLine(" {0}", "Robust estimates of THETA");
for (j = 1; j <= m; j++)
{
Console.WriteLine(" {0,10:f3}", theta[j - 1]);
}
}
else
{
Console.WriteLine(" ");
Console.WriteLine("** g02hm failed with ifail = {0,5}", ifail);
}
}
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Write( "Exception Raised");
}
}
public static void ucv(double t, out double u, out double w)
{
// u function
double cu, cw, t2; cu = ruser[0];
u = 1.00e0;
if (t != 0)
{
t2 = t * t;
if (t2 > cu)
{
u = cu / t2;
}
}
// w function
cw = ruser[1];
if (t > cw)
{
w = cw / t;
}
else
{
w = 1.00e0;
}
}
}
}
