Keyword: 指数平滑化, ホルトウィンタース, 線形ホルト, 2重指数平滑化, Holt Winters
概要
本サンプルはC言語により指数平滑化による予測を行うプログラムです。 入力データとして11個の観察データと指数平滑パラメータ(α、γ、φ)を与え、5ステップ分の予測を行います。
以下のプログラム例では線形ホルト指数平滑化を利用して予測を行いますが、その他に単純指数平滑化、ブラウン2重指数平滑化、ホルトウィンタース乗法、ホルトウィンタース加法の各平滑化法にも対応しています。
※本サンプルはnAG Cライブラリに含まれる関数nag_tsa_exp_smooth()のExampleコードです。本サンプル及び関数の詳細情報はnag_tsa_exp_smoothのマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_tsa_exp_smoothのマニュアルページを参照)1 2 3 4 5 6 7
このデータをダウンロード |
nag_tsa_exp_smooth (g13amc) Example Program Data Nag_EstimateInitialValues Nag_LinearHolt 11 5 : mode,itype,n,nf 180 135 213 181 148 204 228 225 198 200 187 : y dependent arguments for itype = Nag_LinearHolt 0.01 1.0 1.0 : param[0],param[1],param[2] dependent arguments for mode = Nag_ContinueAndUpdate 11 : k
- 1行目はタイトル行で読み飛ばされます。
- 2行目では初期値の与え方 (mode=Nag_EstimateInitialValues)、平滑化の手法 (itype=Nag_LinearHolt)、観察データ数 (n=11)、予測を行いステップ数 (nf=5)を与えます。コロン(:)以降は無視されます。
- 3行目で観察データの値(y)を与えます。コロン(:)以降は無視されます。
- 4行目は読み飛ばされます。
- 5行目では指数平滑化パラメータ(param)としてα(=0.01)、γ(=1.0)、φ(=1.0)を与えています。コロン(:)以降は無視されます。
- 6行目は読み飛ばされます。
- 7行目は初期値の推定に使用する観察データ数 (k=11)を与えています。コロン(:)以降は無視されます。
出力結果
(本関数の詳細はnag_tsa_exp_smoothのマニュアルページを参照)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
この出力例をダウンロード |
nag_tsa_exp_smooth (g13amc) Example Program Results Initial values used: 1 168.018 2 3.800 Mean Deviation = 2.5473e+01 Absolute Deviation = 2.1233e+01 Observed 1-Step Period Values Forecast Residual 1 180.000 171.818 8.182 2 135.000 175.782 -40.782 3 213.000 178.848 34.152 4 181.000 183.005 -2.005 5 148.000 186.780 -38.780 6 204.000 189.800 14.200 7 228.000 193.492 34.508 8 225.000 197.732 27.268 9 198.000 202.172 -4.172 10 200.000 206.256 -6.256 11 187.000 210.256 -23.256 Forecast Standard Period Values Errors 12 213.854 25.473 13 217.685 25.478 14 221.516 25.490 15 225.346 25.510 16 229.177 25.542
- 1行目はタイトルです
- 3~4行目は計算に使われた初期値です。
- 6行目は平均偏差、7行目は絶対偏差で双方ともモデルの当てはまり具合を示しています。小さい値であればあるほど当てはまりが良いことを示します。
- 11行目~21行目は観察値、1ステップ予測、及び残差を示します。
- 25行目~29行目は5ステップ分の予測値とその標準誤差を示します。
ソースコード
(本関数の詳細はnag_tsa_exp_smoothのマニュアルページを参照)
※本サンプルソースコードは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 194 195 196 197 198 199 200 201
このソースコードをダウンロード |
/* nag_tsa_exp_smooth (g13amc) 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 <nagg13.h> int main(void) { /* Integer scalar and array declarations */ Integer exit_status = 0; Integer i, ival, k, n, nf, p; /* Double scalar and array declarations */ double ad, dv; double *fse = 0, *fv = 0, *init = 0, *param = 0, *r = 0, *res = 0; double *y = 0, *yhat = 0; /* Character scalar and array declarations */ char smode[40], sitype[40]; /* nAG structures */ Nag_InitialValues mode; Nag_ExpSmoothType itype; NagError fail; /* Initialize the error structure */ INIT_FAIL(fail); printf("nag_tsa_exp_smooth (g13amc) Example Program Results\n"); /* Skip headings in data file */ scanf("%*[^\n] "); /* Read in the initial arguments */ scanf("%39s%39s%ld%ld%*[^\n] ", smode, sitype, &n, &nf); /* * nag_enum_name_to_value (x04nac). * Converts nAG enum member name to value */ mode = (Nag_InitialValues) nag_enum_name_to_value(smode); itype = (Nag_ExpSmoothType) nag_enum_name_to_value(sitype); if (!(fse = nAG_ALLOC(nf, double)) || !(fv = nAG_ALLOC(nf, double)) || !(res = nAG_ALLOC(n, double)) || !(y = nAG_ALLOC(n, double)) || !(yhat = nAG_ALLOC(n, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Read in the observed data */ for (i = 0; i < n; i++) scanf("%lf ", &y[i]); scanf("%*[^\n] "); /* Read in the itype dependent arguments (skipping headings) */ scanf("%*[^\n] "); if (itype == Nag_SingleExponential) { /* Single exponential smoothing required */ if (!(param = nAG_ALLOC(1, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } scanf("%lf%*[^\n] ", ¶m[0]); p = 0; ival = 1; } else if (itype == Nag_BrownsExponential) { /* Browns exponential smoothing required */ if (!(param = nAG_ALLOC(2, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } scanf("%lf %lf%*[^\n] ", ¶m[0], ¶m[1]); p = 0; ival = 2; } else if (itype == Nag_LinearHolt) { /* Linear Holt smoothing required */ if (!(param = nAG_ALLOC(3, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } scanf("%lf %lf %lf%*[^\n] ", ¶m[0], ¶m[1], ¶m[2]); p = 0; ival = 2; } else if (itype == Nag_AdditiveHoltWinters) { /* Additive Holt Winters smoothing required */ if (!(param = nAG_ALLOC(4, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } scanf("%lf %lf %lf %lf %ld%*[^\n] ", ¶m[0], ¶m[1], ¶m[2], ¶m[3], &p); ival = p + 2; } else if (itype == Nag_MultiplicativeHoltWinters) { /* Multiplicative Holt Winters smoothing required */ if (!(param = nAG_ALLOC(4, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } scanf("%lf %lf %lf %lf %ld%*[^\n] ", ¶m[0], ¶m[1], ¶m[2], ¶m[3], &p); ival = p + 2; } else { printf("%s is an unknown type\n", sitype); exit_status = -1; goto END; } /* Allocate some more memory */ if (!(init = nAG_ALLOC(p + 2, double)) || !(r = nAG_ALLOC(p + 13, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Read in the mode dependent arguments (skipping headings) */ scanf("%*[^\n] "); if (mode == Nag_InitialValuesSupplied) { /* User supplied initial values */ for (i = 0; i < ival; i++) scanf("%lf ", &init[i]); scanf("%*[^\n] "); } else if (mode == Nag_ContinueAndUpdate) { /* Continuing from a previously saved R */ for (i = 0; i < p + 13; i++) scanf("%lf ", &r[i]); scanf("%*[^\n] "); } else if (mode == Nag_EstimateInitialValues) { /* Initial values calculated from first k observations */ scanf("%ld%*[^\n] ", &k); } else { printf("%s is an unknown mode\n", smode); exit_status = -1; goto END; } /* Call the library routine to smooth the series */ nag_tsa_exp_smooth(mode, itype, p, param, n, y, k, init, nf, fv, fse, yhat, res, &dv, &ad, r, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_tsa_exp_smooth (g13amc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Display the output */ printf("Initial values used:\n"); for (i = 0; i < ival; i++) printf("%4ld %12.3f \n", i + 1, init[i]); printf("\n"); printf("Mean Deviation = %13.4e\n", dv); printf("Absolute Deviation = %13.4e\n", ad); printf("\n"); printf(" Observed 1-Step\n"); printf(" Period Values Forecast Residual\n"); for (i = 0; i < n; i++) printf("%4ld %12.3f %12.3f %12.3f\n", i + 1, y[i], yhat[i], res[i]); printf("\n"); printf(" Forecast Standard\n"); printf(" Period Values Errors\n"); for (i = 0; i < nf; i++) printf("%4ld %12.3f %12.3f \n", n + i + 1, fv[i], fse[i]); END: nAG_FREE(fse); nAG_FREE(fv); nAG_FREE(init); nAG_FREE(param); nAG_FREE(r); nAG_FREE(res); nAG_FREE(y); nAG_FREE(yhat); return exit_status; }