Keyword: 一変量, GJR, GARCH, 非対称, パラメータ推定
概要
本サンプルは一変量の非対称なGJR GARCHプロセスのパラメータ推定を行うC#によるサンプルプログラムです。 本サンプルでは g05pf により生成される時系列を分析対象としています。
※本サンプルはnAG Library for .NETに含まれる関数 g13fe() のExampleコードです。本サンプル及び関数の詳細情報は g13fe のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はg13fe のマニュアルページを参照)| この出力例をダウンロード |
g13fe Example Program Results
Normal distribution
Parameter Standard Correct
estimates errors values
0.4175 0.0832 0.4000
0.1204 0.0269 0.1000
0.6702 0.0462 0.7000
0.1161 0.0395 0.1000
3.9274 0.0925 4.0000
1.6036 0.0576 1.5000
2.5280 0.0671 2.5000
Volatility forecast = 2.3541
Students T distribution
Parameter Standard Correct
estimates errors values
0.0589 0.0122 0.0500
0.1315 0.0323 0.1000
0.7480 0.0315 0.8000
0.1258 0.0424 0.0900
5.8938 0.7073 5.0000
2.9315 0.0444 3.0000
1.5446 0.0278 1.5000
2.5466 0.0329 2.5000
Volatility forecast = 0.7046
- 4〜14行目に正規分布のパラメータ推定値、標準誤差、正しい値が出力されています。
- 12行目にはボラティリティの予測値が出力されています。
- 19〜30行目にスチューデントt分布のパラメータ推定値、標準誤差、正しい値が出力されています。
- 32行目にはボラティリティの予測値が出力されています。
ソースコード
(本関数の詳細はg13fe のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g13fe Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
namespace NagDotNetExamples
{
public class G13FEE
{
const int ipmax = 10;
const int iqmax = 10;
const int num = 2000;
const int mseed = 1;
const int nregmx = 10;
const int mnmx = 1;
const double zero = 0.00e0;
static void Main(String[] args)
{
StartExample();
}
public static void StartExample()
{
try
{
double fac1, gamma, hp, lgf, mean, tol, xterm; int d, df, genid, i, ip, iq, j, k, ldx, lseed, maxit, mn, npar, npar2, nreg, nt, subid;
bool fcall = false;
string dist = "";
double[] bx = new double[nregmx];
double[] etm = new double[num];
double[] ht = new double[num];
double[] htm = new double[num];
double[] param = new double[ipmax+iqmax+1];
double[,] x = new double[num, Math.Max(1, nregmx+mnmx)];
double[] yt = new double[num];
double[] rvec = new double[2*(ipmax+ipmax+2)];
int[] seed = new int[mseed];
bool[] copts = new bool[2];
int ifail;
Console.WriteLine("g13fe Example Program Results");
Console.WriteLine("");
df = 0;
hp = 0.0;
df = 0;
hp = 0.0;
// Initialise the seed
seed[0] = 111;
// genid and subid identify the base generator
genid = 1;
subid = 1;
// Initialise the generator to a repeatable sequence
G05.G05State g05State = new G05.G05State(genid, subid, seed, out ifail);
//
if (ifail != 0)
{
goto L200;
}
//
// Initialise the time dependent, exogenous, variables
gamma = 0.10e0;
bx[0] = 1.50e0;
bx[1] = 2.50e0;
bx[2] = 3.00e0;
for (i = 1; i <= num; i++)
{
fac1 = (double)i * 0.010e0;
x[i - 1, 1] = 0.010e0 + 0.70e0 * Math.Sin(fac1);
x[i - 1, 0] = 0.50e0 + fac1 * 0.10e0;
x[i - 1, 2] = 1.00e0;
}
//
for (d = 1; d <= 2; d++)
{
if (d == 1)
{
// Errors distributed as a normal distribution
Console.WriteLine("");
Console.WriteLine(" {0}", "Normal distribution");
Console.WriteLine("");
dist = "N";
// Initialise the series parameters required
// for generation of some test data
mean = 4.00e0;
mn = 1;
nreg = 2;
gamma = 0.10e0;
ip = 1;
iq = 1;
param[0] = 0.40e0;
param[1] = 0.10e0;
param[2] = 0.70e0;
}
else
{
// Errors distributed as a students t distribution
Console.WriteLine("");
Console.WriteLine(" {0}", "Students T distribution");
Console.WriteLine("");
dist = "T";
// Initialise the series parameters required
// for generation of some test data
mean = 3.00e0;
df = 5;
mn = 1;
nreg = 2;
gamma = 0.090e0;
ip = 1;
iq = 1;
param[0] = 0.050e0;
param[1] = 0.10e0;
param[2] = 0.80e0;
}
//
// Start of data generation
// Generate the errors
fcall = true;
G05.g05pf(dist, num, ip, iq, param, gamma, df, ht, yt, fcall, rvec, g05State, out ifail);
//
// Calculate the observed data
for (i = 1; i <= num; i++)
{
xterm = zero;
for (k = 1; k <= nreg; k++)
{
xterm = xterm + x[i - 1, k - 1] * bx[k - 1];
}
if (mn == 1)
{
yt[i - 1] = mean + xterm + yt[i - 1];
}
else
{
yt[i - 1] = xterm + yt[i - 1];
}
}
// End of data generation
//
// Calculate the number of parameters
npar = iq + ip + 1;
npar2 = npar + mn + nreg + 1;
if (dist == "T")
{
npar2 = npar2 + 1;
}
double[] theta = new double[npar2];
double[,] covr = new double[npar2, npar2];
double[] sc = new double[npar2];
double[] se = new double[npar2];
//
// Set the model fitting options
copts[0] = true;
copts[1] = true;
maxit = 55;
tol = 1.00e-5;
//
// Generate initial values for the alpha_i and beta_j parameters
for (i = 1; i <= npar; i++)
{
theta[i - 1] = param[i - 1] * 0.50e0;
}
// Generate initial values for the asymmetry parameter, gamma
theta[npar + 1 - 1] = gamma * 0.50e0;
// Generate initial values for df estimate
j = npar + 2;
if (dist == "T")
{
theta[j - 1] = df * 0.650e0;
j = j + 1;
}
// Generate the initial values for b_0
if (mn == 1)
{
theta[j - 1] = mean * 0.50e0;
j = j + 1;
}
// Generate the initial values for the linear regression
// coefficients b_i, if required
if (!(copts[1]))
{
for (i = 1; i <= nreg; i++)
{
theta[i + j - 1 - 1] = bx[i - 1] * 0.50e0;
}
}
//
// Call the analysis method
G13.g13fe(dist, yt, x, num, ip, iq, nreg, mn, npar2, theta, se, sc, covr, ref hp, etm, htm,
out lgf, copts, maxit, tol, out ifail);
//
// Output the results
Console.WriteLine(" {0} {1}", " Parameter Standard", " Correct");
Console.WriteLine(" {0} {1}", " estimates errors ", " values");
// Output the coefficient alpha_0
Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", theta[0], se[0], param[0]);
// Output the coefficients alpha_i
for (i = 2; i <= iq; i++)
{
Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", theta[i - 1], se[i - 1], param[i - 1]);
}
// Output the coefficients beta_j
for (i = iq + 1; i <= npar; i++)
{
Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", theta[i - 1], se[i - 1], param[i - 1]);
}
// Output the estimated asymmetry parameter, gamma
Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", theta[npar + 1 - 1], se[npar + 1 - 1], gamma);
j = npar + 2;
// Output the estimated degrees of freedom, df
if (dist == "T")
{
Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", theta[j - 1], se[j - 1], (double)df);
j = j + 1;
}
// Output the estimated mean term, b_0
if (mn == 1)
{
Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", theta[j - 1], se[j - 1], mean);
j = j + 1;
}
// Output the estimated linear regression coefficients, b_i
for (i = 1; i <= nreg; i++)
{
Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", theta[i + j - 1 - 1], se[i + j - 1 - 1], bx[i - 1]);
}
// Calculate the volatility forecast
nt = 4;
double[] cvar = new double[nt];
G13.g13ff(num, nt, ip, iq, theta, gamma, cvar, htm, etm, out ifail);
// Display the volatility forecast
Console.WriteLine("");
Console.WriteLine(" {0}{1,12:f4}", "Volatility forecast = ", cvar[nt - 1]);
Console.WriteLine("");
}
//
L200: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Exception Raised");
}
}
}
}
