Keyword: 一変量時系列, GARCH, 非対称, パラメータ推定
概要
本サンプルは一変量の対称なGARCHプロセスもしくはAGARCH type1プロセスのパラメータ推定を行うC#によるサンプルプログラムです。本サンプルでは g05pdc により生成される時系列を分析対象としています。
※本サンプルはnAG Library for .NETに含まれる関数 g13fa() のExampleコードです。本サンプル及び関数の詳細情報は g13fa のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はg13fa のマニュアルページを参照)| この出力例をダウンロード |
g13fa Example Program Results
Normal distribution
Parameter Standard Correct
estimates errors values
0.7266 0.0723 0.8000
0.6645 0.0572 0.6000
0.2201 0.0363 0.2000
0.0761 0.0238 0.1000
-0.3976 0.0621 -0.4000
3.0533 0.0313 3.0000
Volatility forecast = 2.1977
Students T distribution
Parameter Standard Correct
estimates errors values
0.1041 0.0171 0.1000
0.3565 0.0668 0.2000
0.1481 0.0808 0.3000
-0.1952 0.0483 -0.2000
4.0353 0.4427 4.0000
2.9607 0.0258 3.0000
1.4988 0.0179 1.5000
2.5221 0.0198 2.5000
Volatility forecast = 0.2003
- 4〜13行目に正規分布のパラメータ推定値、標準誤差、正しい値が出力されています。
- 15行目にはボラティリティの予測値が出力されています。
- 18〜29行目にスチューデントt分布のパラメータ推定値、標準誤差、正しい値が出力されています。
- 31行目にはボラティリティの予測値が出力されています。
ソースコード
(本関数の詳細はg13fa のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g13fa Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
namespace NagDotNetExamples
{
public class G13FAE
{
static void Main(String[] args)
{
StartExample();
}
public static void StartExample()
{
try
{
const int num = 1500;
const int mseed = 1;
const int nregmx = 10;
const int mnmx = 1;
const double zero = 0.00e0;
const int ipmax = 3;
const int iqmax = 3;
double fac1, gamma, hp, lgf, mean, tol, xterm; int d, df, genid, i, ip, iq, isym, j, k, maxit, mn, npar, npar2, nreg, nt, subid;
bool fcall = false;
string dist = "";
int[] seed = new int[mseed];
bool[] copts = new bool[2];
int ifail;
double[] param = new double[ipmax+iqmax+1];
double[,] x = new double[num, Math.Max(nregmx+mnmx,1)];
double[] yt = new double[num];
double[] etm = new double[num];
double[] htm = new double[num];
double[] ht = new double[num];
double[] rvec = new double[2*(ipmax+iqmax+1)];
double[] bx = new double[nregmx];
Console.WriteLine("g13fa 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 = 0;
isym = 1;
gamma = -0.40e0;
ip = 0;
iq = 3;
param[0] = 0.80e0;
param[1] = 0.60e0;
param[2] = 0.20e0;
param[3] = 0.10e0;
}
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;
ip = 1;
iq = 1;
isym = 1;
gamma = -0.20e0;
param[0] = 0.10e0;
param[1] = 0.20e0;
param[2] = 0.30e0;
}
//
// Start of data generation
// Generate the errors
fcall = true;
G05.g05pd(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 = 1 + iq + ip;
npar2 = npar + isym + mn + nreg;
if (dist == "T")
{
npar2 = npar2 + 1;
}
double[] theta = new double[npar2];
double[] sc = new double[npar2];
double[] se = new double[npar2];
double[,] covr = new double[npar2, npar2];
//
// Set the model fitting options
copts[0] = true;
copts[1] = true;
maxit = 200;
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
j = npar + 1;
if (isym == 1)
{
theta[j - 1] = gamma * 0.50e0;
j = j + 1;
}
// Generate initial values for df estimate
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.g13fa(dist, yt, x, num, ip, iq, nreg, mn, isym, 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
j = npar + 1;
if (isym == 1)
{
Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", theta[j - 1], se[j - 1], gamma);
j = j + 1;
}
// 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.g13fb(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");
}
}
}
}
