Keyword: 一変量時系列, GARCH, 非対称, パラメータ推定
概要
本サンプルは一変量のAGARCH type2プロセスのパラメータ推定を行うC#によるサンプルプログラムです。 本サンプルでは g05pe により生成される時系列を分析対象としています。
※本サンプルはnAG Library for .NETに含まれる関数 g13fc() のExampleコードです。本サンプル及び関数の詳細情報は g13fc のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はg13fc のマニュアルページを参照)| この出力例をダウンロード |
g13fc Example Program Results
Normal distribution
Parameter Standard Correct
estimates errors values
0.0921 0.0147 0.0800
0.2283 0.0324 0.2000
0.6576 0.0326 0.7000
-0.4175 0.0682 -0.4000
2.9803 0.0568 3.0000
1.5345 0.0362 1.5000
2.5316 0.0426 2.5000
Volatility forecast = 0.4676
Students T distribution
Parameter Standard Correct
estimates errors values
3.6207 1.2246 0.1000
0.9222 1.9652 0.1000
0.0768 0.5243 0.8000
-0.3422 0.3033 -0.4000
2.1663 0.1191 4.0000
2.9252 0.0777 3.0000
1.5034 0.0568 1.5000
2.5319 0.0554 2.5000
Volatility forecast = 14.8297
- 4〜14行目に正規分布のパラメータ推定値、標準誤差、正しい値が出力されています。
- 16行目にはボラティリティの予測値が出力されています。
- 19〜30行目にスチューデントt分布のパラメータ推定値、標準誤差、正しい値が出力されています。
- 32行目にはボラティリティの予測値が出力されています。
ソースコード
(本関数の詳細はg13fc のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g13fc Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
namespace NagDotNetExamples
{
public class G13FCE
{
const int ipmax = 3;
const int iqmax = 3;
const int nregmx = 10;
const int mnmx = 1;
const int num = 1500;
const int mseed = 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, lseed, lstate, maxit, mn, npar, npar2, nreg, nt, subid;
bool fcall = false;
string dist = "";
double[] bx = new double[nregmx];
double[] param = new double[ipmax+iqmax+1];
double[,] x = new double[num, num];
double[] yt = new double[num];
int[] seed = new int[mseed];
bool[] copts = new bool[2];
double[] etm = new double[num];
double[] ht = new double[num];
double[] htm = new double[num];
double[] rvec = new double[2*(ipmax+iqmax+1)];
int ifail;
Console.WriteLine("g13fc Example Program Results");
Console.WriteLine("");
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);
//
// Initialise the time dependent, exogenous, variables
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, 0] = 0.010e0 + 0.70e0 * Math.Sin(fac1);
x[i - 1, 1] = 0.50e0 + fac1 * 0.10e0;
x[i - 1, 2] = 1.00e0;
}
//
// Show two examples, normally distributed errors,
// and student t errors
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 = 3.00e0;
mn = 1;
nreg = 2;
gamma = -0.40e0;
ip = 1;
iq = 1;
param[0] = 0.080e0;
param[1] = 0.20e0;
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
df = 4;
mean = 3.00e0;
mn = 1;
nreg = 2;
gamma = -0.40e0;
ip = 1;
iq = 1;
param[0] = 0.10e0;
param[1] = 0.10e0;
param[2] = 0.80e0;
}
//
// Start of data generation
// Generate the errors
fcall = true;
G05.g05pe(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 = 50;
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.g13fc(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.g13fd(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("");
}
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Exception Raised");
}
}
}
}
