Keyword: Ridge, リッジ, パラメータ, 回帰
概要
本サンプルはユーザ提供のRidge回帰パラメータを用いたRidge回帰の計算を行うC#によるサンプルプログラムです。 本サンプルは以下に示されるデータについてRidge回帰の計算を行います。
※本サンプルはnAG Library for .NETに含まれる関数 g02kb() のExampleコードです。本サンプル及び関数の詳細情報は g02kb のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はg02kb のマニュアルページを参照)| このデータをダウンロード |
g02kb Example Program Data 20 3 16 5 1 : N, M, LH, LPEC, WANTB 'L' 'G' 'U' 'F' 'B' : PEC 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 observations 1 1 1 : ISX 0.0 0.002 0.004 0.006 0.008 0.010 0.012 0.014 0.016 0.018 0.020 0.022 0.024 0.026 0.028 0.030 : Ridge co-efficients
- 1行目はタイトル行で読み飛ばされます。
- 2行目の1番目のパラメータ(n)は観測値の数を指定しています。2番目のパラメータ(m)はデータ行列で有効な独立変数の数を指定しています。3番目のパラメータ(lh)は提供されたRidgeパラメータの数を指定しています。4番目のパラメータ(lpec)は予想誤差統計量の数を指定しています。5番目のパラメータ(wantb)はパラメータ推定のオプションを指定しています。"1"はパラメータ推定値が元データに対し計算されることを意味します。
- 3行目は予測誤差の手法(pec)を指定しています。"L" は Leave-one-out cross-validation、 "G" は Generalized cross-validation、 "U" は Unbiased estimate of variance、 "F" は Future prediction error、 "B" は Bayesian information criterion を意味します。
- 4〜23行目は独立変数の観測値(x)と従属変数の観測値(y)を指定しています。
- 24行目はどの独立変数がモデルに含まれるかを示すパラメータ(isx)を指定しています。
- 25〜28行目はRidge係数(h)を指定しています。
出力結果
(本関数の詳細はg02kb のマニュアルページを参照)| この出力例をダウンロード |
g02kb Example Program Results
Number of parameters used = 4
Effective number of parameters (NEP):
Ridge
Coeff. NEP
0.0000 4.0000
0.0020 3.2634
0.0040 3.1475
0.0060 3.0987
0.0080 3.0709
0.0100 3.0523
0.0120 3.0386
0.0140 3.0278
0.0160 3.0189
0.0180 3.0112
0.0200 3.0045
0.0220 2.9984
0.0240 2.9928
0.0260 2.9876
0.0280 2.9828
0.0300 2.9782
Parameter Estimates (Original scalings)
Ridge
Coeff. Intercept 1 2 3
0.0000 117.0847 4.3341 -2.8568 -2.1861
0.0020 22.2748 1.4644 -0.4012 -0.6738
0.0040 7.7209 1.0229 -0.0242 -0.4408
0.0060 1.8363 0.8437 0.1282 -0.3460
0.0080 -1.3396 0.7465 0.2105 -0.2944
0.0100 -3.3219 0.6853 0.2618 -0.2619
0.0120 -4.6734 0.6432 0.2968 -0.2393
0.0140 -5.6511 0.6125 0.3222 -0.2228
0.0160 -6.3891 0.5890 0.3413 -0.2100
0.0180 -6.9642 0.5704 0.3562 -0.1999
0.0200 -7.4236 0.5554 0.3681 -0.1916
0.0220 -7.7978 0.5429 0.3779 -0.1847
0.0240 -8.1075 0.5323 0.3859 -0.1788
0.0260 -8.3673 0.5233 0.3926 -0.1737
0.0280 -8.5874 0.5155 0.3984 -0.1693
0.0300 -8.7758 0.5086 0.4033 -0.1653
Variance Inflation Factors
Ridge
Coeff. 1 2 3
0.0000 708.8429 564.3434 104.6060
0.0020 50.5592 40.4483 8.2797
0.0040 16.9816 13.7247 3.3628
0.0060 8.5033 6.9764 2.1185
0.0080 5.1472 4.3046 1.6238
0.0100 3.4855 2.9813 1.3770
0.0120 2.5434 2.2306 1.2356
0.0140 1.9581 1.7640 1.1463
0.0160 1.5698 1.4541 1.0859
0.0180 1.2990 1.2377 1.0428
0.0200 1.1026 1.0805 1.0105
0.0220 0.9556 0.9627 0.9855
0.0240 0.8427 0.8721 0.9655
0.0260 0.7541 0.8007 0.9491
0.0280 0.6832 0.7435 0.9353
0.0300 0.6257 0.6969 0.9235
Prediction error criterion
Ridge
Coeff. 1 2 3 4 5
0.0000 8.0368 7.6879 6.1503 7.3804 8.6052
0.0020 7.5464 7.4238 6.2124 7.2261 8.2355
0.0040 7.5575 7.4520 6.2793 7.2675 8.2515
0.0060 7.5656 7.4668 6.3100 7.2876 8.2611
0.0080 7.5701 7.4749 6.3272 7.2987 8.2661
0.0100 7.5723 7.4796 6.3381 7.3053 8.2685
0.0120 7.5732 7.4823 6.3455 7.3095 8.2695
0.0140 7.5734 7.4838 6.3508 7.3122 8.2696
0.0160 7.5731 7.4845 6.3548 7.3140 8.2691
0.0180 7.5724 7.4848 6.3578 7.3151 8.2683
0.0200 7.5715 7.4847 6.3603 7.3158 8.2671
0.0220 7.5705 7.4843 6.3623 7.3161 8.2659
0.0240 7.5694 7.4838 6.3639 7.3162 8.2645
0.0260 7.5682 7.4832 6.3654 7.3162 8.2630
0.0280 7.5669 7.4825 6.3666 7.3161 8.2615
0.0300 7.5657 7.4818 6.3677 7.3159 8.2600
Key:
1 Leave one out cross-validation
2 Generalised cross-validation
3 Unbiased estimate of variance
4 Final prediction error
5 Bayesian information criterion
- 2行目に使用されたパラメータの数が出力されています。
- 3〜21行目に有効なパラメータの数が出力されています。Ridge係数と有効数が出力されています。
- 23〜42行目にパラメータ推定値が出力されています。Ridge係数と切片、3種類の予測誤差が出力されています。
- 44〜62行目に分散拡大要因が出力されています。Ridge係数と3種類の予測誤差が出力されています。
- 64〜82行目に予測誤差の基準が出力されています。Ridge係数と5種類の予測誤差が出力されています。
- 84〜89行目に各番号に対応する予測誤差の手法が出力されています。
ソースコード
(本関数の詳細はg02kb のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g02kb Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G02KBE
{
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/g02kbe.d");
}
else
{
sr = new DataReader(datafile);
}
int i, ip, ip1, j, lh, lpec, m, n, pl, wantb, wantvf;
int ldb, tdb, ldpe, tdpe, ldvf, tdvf;
//
int ifail;
Console.WriteLine("g02kb Example Program Results");
//
// Skip heading in data file
sr.Reset();
//
// Read in the problem size information
sr.Reset();
n = int.Parse(sr.Next());
m = int.Parse(sr.Next());
lh = int.Parse(sr.Next());
lpec = int.Parse(sr.Next());
wantb = int.Parse(sr.Next());
wantvf = 1;
ldpe = 5;
double[] h = new double[lh];
double[] nep = new double[lh];
double[,] x = new double[n, m];
double[] y = new double[n];
int[] isx = new int[m];
string[] pec = new string[lpec];
//
// Check array sizes
if (n < 1 || m > n || lh < 1 || ldpe < lpec)
{
Console.Write(" ** Problem size is too small or inconsistent");
goto L160;
}
//
// Read in the data
if (lpec > 0)
{
sr.Reset();
for (i = 1; i <= lpec; i++)
{
pec[i - 1] = sr.Next();
}
}
for (i = 1; i <= n; i++)
{
sr.Reset();
for (j = 1; j <= m; j++)
{
x[i - 1, j - 1] = double.Parse(sr.Next());
}
y[i - 1] = double.Parse(sr.Next());
}
//
// Read in the isx flags
sr.Reset();
for (i = 1; i <= m; i++)
{
isx[i - 1] = int.Parse(sr.Next());
}
//
// Read in the ridge coefficients
sr.Reset();
for (i = 1; i <= lh; i++)
{
h[i - 1] = double.Parse(sr.Next());
}
//
// Total number of variables
ip = 0;
for (j = 1; j <= m; j++)
{
if (isx[j - 1] == 1)
{
ip = ip + 1;
}
}
if (wantb != 0)
{
ldb = ip + 1;
tdb = lh;
}
else
{
ldb = 1;
tdb = 1;
}
if (wantvf != 0)
{
ldvf = ip;
tdvf = lh;
}
else
{
ldvf = 0;
tdvf = 0;
}
if (lpec>0)
{
ldpe = lpec;
tdpe = lh;
}
else
{
ldpe = 0;
tdpe = 0;
}
double[,] pe = new double[ldpe, tdpe];
double[,] vf = new double[ldvf, tdvf];
double[,] b = new double[ldb, tdb];
//
// Output the variance inflation factors and parameter estimates
// (original scalings)
//
// Run the analysis
G02.g02kb( n, m, x, isx, ip, y, lh ,h, nep, wantb, b, wantvf, vf, lpec,pec, pe, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g02kb failed with ifail = {0,5}", ifail);
goto L160;
}
// Output results
ip1 = ip - 1;
//
// Summaries
Console.WriteLine("{0}{1,10}", "Number of parameters used = ", ip + 1);
Console.WriteLine(" {0}", "Effective number of parameters (NEP):");
Console.WriteLine(" {0}", " Ridge ");
Console.WriteLine(" {0} {1}", " Coeff. ", "NEP");
for (i = 1; i <= lh; i++)
{
Console.WriteLine("{0,10:f4}{1,10:f4}", h[i - 1], nep[i - 1]);
}
//
// Parameter estimates
if (wantb != 0)
{
Console.WriteLine(" ");
if (wantb == 1)
{
Console.WriteLine(" {0}", "Parameter Estimates (Original scalings)");
}
else
{
Console.WriteLine(" {0}", "Parameter Estimates (Standarised)");
}
pl = Math.Min(ip, 4);
Console.WriteLine(" {0}", " Ridge ");
Console.WriteLine(" ");
Console.Write(" {0}", " Coeff. ");
Console.Write(" {0}", " Intercept ");
for (i = 1; i <= pl; i++)
{
Console.Write(" {0}", i);
}
Console.WriteLine(" ");
if (pl < ip1)
{
for (i = pl + 1; i <= ip1; i++)
{
Console.Write(" {0}", i);
}
Console.WriteLine(" ");
}
pl = Math.Min(ip + 1, 5);
for (i = 1; i <= lh; i++)
{
Console.Write(" {0, 10:f4}", h[i - 1]);
for (j = 1; j <= pl; j++)
{
Console.Write(" {0, 10:f4}", b[j - 1, i - 1]);
}
Console.WriteLine(" ");
if (pl < ip)
{
for (j = pl + 1; j <= ip; j++)
{
Console.Write(" {0, 10:f4}", b[j - 1, i - 1]);
}
Console.WriteLine(" ");
}
}
}
//
// Variance inflation factors
if (wantvf != 0)
{
Console.WriteLine(" ");
Console.WriteLine(" {0}", "Variance Inflation Factors");
pl = Math.Min(ip, 5);
Console.Write(" {0}", " Ridge ");
Console.WriteLine(" ");
Console.Write(" {0}", " Coeff. ");
for (i = 1; i <= pl; i++)
{
Console.Write(" {0, 10}", i);
}
Console.WriteLine(" ");
if (pl < ip)
{
for (i = pl + 1; i <= ip; i++)
{
Console.Write(" {0, 10}", i);
}
Console.WriteLine(" ");
}
for (i = 1; i <= lh; i++)
{
Console.Write(" {0, 10:f4}", h[i - 1]);
for (j = 1; j <= pl; j++)
{
Console.Write(" {0, 10:f4}", vf[j - 1, i - 1]);
}
Console.WriteLine(" ");
if (pl < ip)
{
for (j = pl + 1; j <= ip; j++)
{
Console.Write(" {0, 10:f4}", vf[j - 1, i - 1]);
}
Console.WriteLine(" ");
}
}
}
//
// Prediction error criterion
if (lpec > 0)
{
Console.WriteLine(" ");
Console.WriteLine(" {0}", "Prediction error criterion");
pl = Math.Min(lpec, 5);
Console.Write(" {0}", " Ridge ");
Console.WriteLine(" ");
Console.Write(" {0}", " Coeff. ");
for (i = 1; i <= pl; i++)
{
Console.Write(" {0, 10}", i);
}
Console.WriteLine(" ");
if (pl < lpec)
{
for (i = pl + 1; i <= lpec; i++)
{
Console.Write(" {0, 10}", i);
}
Console.WriteLine(" ");
}
for (i = 1; i <= lh; i++)
{
Console.Write(" {0, 10:f4}", h[i - 1]);
for (j = 1; j <= pl; j++)
{
Console.Write(" {0, 10:f4}", pe[j - 1, i - 1]);
}
Console.WriteLine(" ");
if (pl < ip)
{
for (j = pl + 1; j <= ip; j++)
{
Console.Write(" {0, 10:f4}", pe[j - 1, i - 1]);
}
Console.WriteLine(" ");
}
}
Console.WriteLine(" ");
Console.WriteLine(" {0}", "Key:");
for (i = 1; i <= lpec; i++)
{
if (pec[i - 1] == "L")
{
Console.WriteLine(" {0,5} {1}", i, "Leave one out cross-validation");
}
else if (pec[i - 1] == "G")
{
Console.WriteLine(" {0,5} {1}", i, "Generalised cross-validation");
}
else if (pec[i - 1] == "U")
{
Console.WriteLine(" {0,5} {1}", i, "Unbiased estimate of variance");
}
else if (pec[i - 1] == "F")
{
Console.WriteLine(" {0,5} {1}", i, "Final prediction error");
}
else if (pec[i - 1] == "B")
{
Console.WriteLine(" {0,5} {1}", i, "Bayesian information criterion");
}
}
}
//
L160: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Write( "Exception Raised");
}
}
}
}
