Keyword: Ridge, リッジ, パラメータ, 回帰
概要
本サンプルはRidgeパラメータを最適化するRidge回帰の計算を行うC#によるサンプルプログラムです。 本サンプルは以下に示されるデータについてRidge回帰の計算を行います。
※本サンプルはnAG Library for .NETに含まれる関数 g02ka() のExampleコードです。本サンプル及び関数の詳細情報は g02ka のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はg02ka のマニュアルページを参照)| このデータをダウンロード |
g02ka Example Program Data 20 3 0.5 1 1.0e-4 25 2 2 : N, M, H, OPT, TOL, NITER, ORIG, OPTLOO 19.5 43.1 29.1 11.9 24.7 49.8 28.2 22.8 30.7 51.9 37.0 18.7 29.8 54.3 31.1 20.1 19.1 42.2 30.9 12.9 25.6 53.9 23.7 21.7 31.4 58.5 27.6 27.1 27.9 52.1 30.6 25.4 22.1 49.9 23.2 21.3 25.5 53.5 24.8 19.3 31.1 56.6 30.0 25.4 30.4 56.7 28.3 27.2 18.7 46.5 23.0 11.7 19.7 44.2 28.6 17.8 14.6 42.7 21.3 12.8 29.5 54.4 30.1 23.9 27.7 55.3 25.7 22.6 30.2 58.6 24.6 25.4 22.7 48.2 27.1 14.8 25.2 51.0 27.5 21.1 : End of data 1 1 1 : ISX
- 1行目はタイトル行で読み飛ばされます。
- 2行目の1番目のパラメータ(n)は観測値の数を指定しています。2番目のパラメータ(m)はデータ行列で有効な独立変数の数を指定しています。3番目のパラメータ(h)はRidge回帰パラメータの初期値を指定しています。4番目のパラメータ(opt)はRidge回帰パラメータの最適化に使用される予測誤差の測定法を指定しています。"1"は "Generalised cross-validation"を意味します。5番目のパラメータ(tol)は回帰パラメータの反復が中止となる値を指定しています。6番目のパラメータ(niter)はRidge回帰パラメータの最適化が可能な反復の最大値を指定しています。7番目のパラメータ(orig)はパラメータ推定値が元データに対して計算されるか、標準化されたデータに対して計算されるかを指定しています。 "2"は標準化されたデータに対して計算されることを意味します。8番目のパラメータ(optloo)は予測誤差の交差検定(leave-one-out cross-validation)の推定値が計算されるかを指定しています。 "2"は交差検定の推定値が計算されることを意味します。
- 3〜22行目は独立変数の観測値(x)と従属変数の観測値(y)を指定しています。
- 23行目はどの独立変数がモデルに含まれるかを示すパラメータ(isx)を指定しています。
出力結果
(本関数の詳細はg02ka のマニュアルページを参照)| この出力例をダウンロード |
g02ka Example Program Results Value of ridge parameter: 0.0712 Sum of squares of residuals: 1.0917e+002 Degrees of freedom: 16 Number of effective parameters: 2.9059 Parameter estimates 1 1 20.1950 2 9.7934 3 9.9576 4 -2.0125 Number of iterations: 6 Ridge parameter minimises GCV Estimated prediction errors: GCV = 7.4718 UEV = 6.3862 FPE = 7.3141 BIC = 8.2380 LOO CV = 7.5495 Residuals 1 1 -1.9894 2 3.5469 3 -3.0392 4 -3.0309 5 -0.1899 6 -0.3146 7 0.9775 8 4.0157 9 2.5332 10 -2.3560 11 0.5446 12 2.3989 13 -4.0876 14 3.2778 15 0.2894 16 0.7330 17 -0.7116 18 -0.6092 19 -2.9995 20 1.0110 Variance inflation factors 1 1 0.2928 2 0.4162 3 0.8089
- 3行目にRidgeパラメータの値が出力されています。
- 5行目に残差平方和が出力されています。
- 6行目に自由度が出力されています。
- 7行目に効果パラメータの数が出力されています。
- 9〜14行目にパラメータ推定値が出力されています。
- 16行目に反復数が出力されています。
- 20〜25行目に推定予測誤差が出力されています。"GCV" は "Generalised cross-validation" を表します。"UEV" は "Unbiased estimate of variance" を表します。"FPE" は "Future prediction error" を表します。"BIC" は "Bayesian information criterion" を表します。"LOO CV" は "Leave-one-out cross-validation" を表します。
- 27〜48行目に残差が出力されています。
- 50〜54行目に分散拡大要因が出力されています。
ソースコード
(本関数の詳細はg02ka のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g02ka Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G02KAE
{
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/g02kae.d");
}
else
{
sr = new DataReader(datafile);
}
double h, nep, rss, tau, tol; int df, i, ip, j, m, n, niter, opt, optloo, orig;
int ifail;
Console.WriteLine("g02ka Example Program Results");
//
// Skip heading in data file
sr.Reset();
// Read in data and check array limits
sr.Reset();
n = int.Parse(sr.Next());
m = int.Parse(sr.Next());
h = double.Parse(sr.Next());
opt = int.Parse(sr.Next());
tol = double.Parse(sr.Next());
niter = int.Parse(sr.Next());
orig = int.Parse(sr.Next());
optloo = int.Parse(sr.Next());
double[] perr = new double[5];
double[] res = new double[n];
double[,] x = new double[n, m];
double[] y = new double[n];
int[] isx = new int[m];
if (n < 1 || m > n)
{
Console.WriteLine(" ** Problem size is too small.");
goto L40;
}
sr.Reset();
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
x[i - 1, j - 1] = double.Parse(sr.Next());
}
y[i - 1] = double.Parse(sr.Next());
}
sr.Reset();
for (j = 1; j <= m; j++)
{
isx[j - 1] = int.Parse(sr.Next());
}
//
// Total number of variables.
ip = 0;
for (j = 1; j <= m; j++)
{
if (isx[j - 1] == 1)
{
ip = ip + 1;
}
}
//
// Tolerance for setting singular values of H to zero.
tau = 0.00e0;
df = 0;
int blength = ip + 1;
double[] b = new double[blength];
double[] vif = new double[ip];
//
// Call function.
G02.g02ka(n, m, x, isx, ip, tau, y, ref h, opt, ref niter, tol, out nep, orig, b, vif, res,
out rss, out df, optloo, perr, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g02ka failed with ifail = {0,5}", ifail);
if (ifail != -1)
{
goto L40;
}
}
// Print results:
Console.WriteLine("");
Console.WriteLine("{0} {1,10:f4}", "Value of ridge parameter:", h);
Console.WriteLine("");
Console.WriteLine("{0} {1,10:e4}", "Sum of squares of residuals:", rss);
Console.WriteLine("{0} {1,5}", "Degrees of freedom: ", df);
Console.WriteLine("{0} {1,10:f4}", "Number of effective parameters:", nep);
Console.WriteLine("");
printArrays(blength,1,b,"Parameter estimates" );
Console.WriteLine("");
Console.WriteLine(" {0} {1}", "Number of iterations:", niter);
Console.WriteLine("");
if (opt == 1)
{
Console.WriteLine(" {0}", "Ridge parameter minimises GCV");
}
else if (opt == 2)
{
Console.WriteLine(" {0}", "Ridge parameter minimises UEV");
}
else if (opt == 3)
{
Console.WriteLine(" {0}", "Ridge parameter minimises FPE");
}
else if (opt == 4)
{
Console.WriteLine(" {0}", "Ridge parameter minimises BIC");
}
Console.WriteLine("");
Console.WriteLine(" {0}", "Estimated prediction errors:");
Console.WriteLine("{0} {1,10:f4}", "GCV =", perr[0]);
Console.WriteLine("{0} {1,10:f4}", "UEV =", perr[1]);
Console.WriteLine("{0} {1,10:f4}", "FPE =", perr[2]);
Console.WriteLine("{0} {1,10:f4}", "BIC =", perr[3]);
if (optloo == 2)
{
Console.WriteLine("{0} {1,10:f4}", "LOO CV =", perr[4]);
}
Console.WriteLine("");
printArrays(n, 1, res, "Residuals" );
Console.WriteLine("");
//
printArrays(ip, 1, vif, "Variance inflation factors" );
L40: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Write("Exception Raised");
}
}
internal static void printArrays(int m, int n, double[] array ,string title)
{
Console.WriteLine(title);
Console.WriteLine("{0}\t{1,8}", " ", 1);
for (int bindex = 0; bindex < m; bindex++)
{
Console.WriteLine("{0}\t{1,8:f4}", bindex + 1, array[bindex]);
}
}
}
}
