Ridgeパラメータを最適化するRidge回帰

C言語によるサンプルソースコード : 使用関数名:nag_regsn_ridge_opt (g02kac)

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > Ridgeパラメータを最適化するRidge回帰 (C言語/C++)

Keyword: Ridge, リッジ, パラメータ, 回帰

概要

本サンプルはRidgeパラメータを最適化するRidge回帰の計算を行うC言語によるサンプルプログラムです。 本サンプルは以下に示されるデータについてRidge回帰の計算を行います。

Ridge回帰のデータ 

※本サンプルはnAG Cライブラリに含まれる関数 nag_regsn_ridge_opt() のExampleコードです。本サンプル及び関数の詳細情報は nag_regsn_ridge_opt のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで

入力データ

(本関数の詳細はnag_regsn_ridge_opt のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

このデータをダウンロード
nag_regsn_ridge_opt (g02kac) Example Program Data
20 3 0.5 Nag_GCV 1.0e-4 25
Nag_EstimatesStand
Nag_WantLOO                : 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行目は観測値の数(n)、データ行列で有効な独立変数の数(m)、Ridge回帰パラメータの初期値(h)、Ridge回帰パラメータの最適化に使用される予測誤差の測定法(sopt)、回帰パラメータの反復が中止となる値(tol)、Ridge回帰パラメータの最適化が可能な反復の最大値(niter)を指定しています。"Nag_GCV"は "Generalised cross-validation"を意味します。
  • 3行目はパラメータ推定値が元データに対して計算されるか、標準化されたデータに対して計算されるか(sorig)を指定しています。 "Nag_EstimatesStand"は標準化されたデータに対して計算されることを意味します。
  • 4行目は予測誤差の交差検定(leave-one-out cross-validation)の推定値が計算されるか(soptloo)を指定しています。 "Nag_WantLOO"は交差検定の推定値が計算されることを意味します。
  • 5~24行目は独立変数の観測値(x)と従属変数の観測値(y)を指定しています。
  • 25行目はどの独立変数がモデルに含まれるか(isx)を指定しています。

出力結果

(本関数の詳細はnag_regsn_ridge_opt のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

この出力例をダウンロード
nag_regsn_ridge_opt (g02kac) Example Program Results

Value of ridge parameter:       0.0712

Sum of squares of residuals:      1.0917e+02
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~15行目にパラメータ推定値が出力されています。
  • 17行目に反復数が出力されています。
  • 21~26行目に推定予測誤差が出力されています。"GCV" は "Generalised cross-validation" を表します。"UEV" は "Unbiased estimate of variance" を表します。"FPE" は "Future prediction error" を表します。"BIC" は "Bayesian information criterion" を表します。"LOO CV" は "Leave-one-out cross-validation" を表します。
  • 28~50行目に残差が出力されています。
  • 52~57行目に分散拡大要因が出力されています。

ソースコード

(本関数の詳細はnag_regsn_ridge_opt のマニュアルページを参照)

※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

このソースコードをダウンロード
/* nag_regsn_ridge_opt (g02kac) Example Program.
 *
 * CLL6I261D/CLL6I261DL Version.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
/* Pre-processor includes */
#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg02.h>
#include <nagx04.h>

int main(void)
{
  /*Integer scalar and array declarations */
  Integer exit_status = 0;
  Integer df, i, ip, ip1, j, m, n, niter, one = 1;
  Integer pdb, pdres, pdvif, pdx;
  Integer *isx = 0;
  /*Double scalar and array declarations */
  double h, nep, rss, tau, tol;
  double *b = 0, *perr = 0, *res = 0, *vif = 0, *x = 0, *y = 0;
  /*Character scalar and array declarations */
  char sopt[40], sorig[40], soptloo[40];
  /*nAG Types */
  Nag_OrderType order;
  Nag_PredictError opt;
  Nag_EstimatesOption orig;
  Nag_OptionLOO optloo;
  NagError fail;

  INIT_FAIL(fail);

  printf("%s\n", "nag_regsn_ridge_opt (g02kac) Example Program Results");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read in data and check array limits */
  scanf("%ld%ld%lf%39s %lf%ld%39s %39s%*[^\n] ",
        &n, &m, &h, sopt, &tol, &niter, sorig, soptloo);
  opt = (Nag_PredictError) nag_enum_name_to_value(sopt);
  orig = (Nag_EstimatesOption) nag_enum_name_to_value(sorig);
  optloo = (Nag_OptionLOO) nag_enum_name_to_value(soptloo);

#ifdef nAG_COLUMN_MAJOR
  pdx = n;
#define X(I, J) x[(J-1)*pdx + I-1]
  order = Nag_ColMajor;
#else
  pdx = m;
#define X(I, J) x[(I-1)*pdx + J-1]
  order = Nag_RowMajor;
#endif
  if (!(b = nAG_ALLOC(m + 1, double)) ||
      !(perr = nAG_ALLOC(5, double)) ||
      !(res = nAG_ALLOC(n, double)) ||
      !(vif = nAG_ALLOC(m, double)) ||
      !(x = nAG_ALLOC(pdx * (order == Nag_RowMajor ? n : m), double)) ||
      !(y = nAG_ALLOC(n, double)) || !(isx = nAG_ALLOC(m, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++)
      scanf("%lf ", &X(i, j));
    scanf("%lf ", &y[i - 1]);
  }
  scanf("%*[^\n] ");
  for (j = 0; j < m; j++)
    scanf("%ld ", &isx[j]);
  scanf("%*[^\n] ");

  /* Total number of variables. */
  ip = 0;
  for (j = 0; j < m; j++) {
    if (isx[j] == 1)
      ip = ip + 1;
  }
#ifdef nAG_COLUMN_MAJOR
  pdb = n;
  pdres = n;
  pdvif = ip;
#else
  pdb = one;
  pdres = one;
  pdvif = one;
#endif
  /* Tolerance for setting singular values of H to zero. */
  tau = 0.00e0;
  df = 0;
  /* Call function. */
  /*
   * nag_regsn_ridge_opt (g02kac)
   * Ridge regression
   */
  nag_regsn_ridge_opt(order, n, m, x, pdx, isx, ip, tau, y, &h, opt, &niter,
                      tol, &nep, orig, b, vif, res, &rss, &df, optloo, perr,
                      &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_regsn_ridge_opt (g02kac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Print results: */
  printf("\n");
  printf("%s   %10.4f\n", "Value of ridge parameter:", h);
  printf("\n");
  printf("%s   %13.4e\n", "Sum of squares of residuals:", rss);
  printf("%s  %5ld\n", "Degrees of freedom: ", df);
  printf("%s   %10.4f\n", "Number of effective parameters:", nep);
  printf("\n");
  ip1 = ip + 1;
  /*
   * nag_gen_real_mat_print (x04cac)
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, ip1, one,
                         b, pdb, "Parameter estimates", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  printf("%s%ld\n", "Number of iterations:            ", niter);
  printf("\n");
  if (opt == Nag_GCV) {
    printf("%s\n", "Ridge parameter minimises GCV");
  }
  else if (opt == Nag_UEV) {
    printf("%s\n", "Ridge parameter minimises UEV");
  }
  else if (opt == Nag_FPE) {
    printf("%s\n", "Ridge parameter minimises FPE");
  }
  else if (opt == Nag_BIC) {
    printf("%s\n", "Ridge parameter minimises BIC");
  }
  printf("\n");
  printf("%s\n", "Estimated prediction errors:");
  printf("%s   %10.4f\n", "GCV    =", perr[0]);
  printf("%s   %10.4f\n", "UEV    =", perr[1]);
  printf("%s   %10.4f\n", "FPE    =", perr[2]);
  printf("%s   %10.4f\n", "BIC    =", perr[3]);
  if (optloo == Nag_WantLOO) {
    printf("%s   %10.4f\n", "LOO CV =", perr[4]);
  }
  printf("\n");

  /*
   * nag_gen_real_mat_print (x04cac)
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, one,
                         res, pdres, "Residuals", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\n");
  /*
   * nag_gen_real_mat_print (x04cac)
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, ip, one,
                         vif, pdvif, "Variance inflation factors", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

END:
  nAG_FREE(b);
  nAG_FREE(perr);
  nAG_FREE(res);
  nAG_FREE(vif);
  nAG_FREE(x);
  nAG_FREE(y);
  nAG_FREE(isx);

  return exit_status;
}


関連情報
© 日本ニューメリカルアルゴリズムズグループ株式会社 2025
Privacy Policy  /  Trademarks