Example: GARCH

The data for this example are generated to follow a GARCH(p,q) process by using a random number generation function sgarch . The data set is analyzed and estimates of sigma, the AR parameters, and the MA parameters are returned. The values of the Log-likelihood function and the Akaike Information Criterion are returned.

using System;
using Imsl.Stat;
using PrintMatrix = Imsl.Math.PrintMatrix;

public class GARCHEx1
{
	static private void  sgarch(int p, int q, int m, double[] x, 
		double[] y, double[] z, double[] y0, double[] sigma)
	{
		int i, j, l;
		double s1, s2, s3;
		Imsl.Stat.Random rand = new Imsl.Stat.Random(182198625);
		rand.Multiplier = 16807;

		for (i = 0; i < m + 1000; i++)
			z[i] = rand.NextNormal();
		
		l = System.Math.Max(p, q);
		l = System.Math.Max(l, 1);
		for (i = 0; i < l; i++)
			y0[i] = z[i] * x[0];
		
		/* COMPUTE THE INITIAL VALUE OF SIGMA */
		s3 = 0.0;
		if (System.Math.Max(p, q) >= 1)
		{
			for (i = 1; i < (p + q + 1); i++)
				s3 += x[i];
		}
		for (i = 0; i < l; i++)
			sigma[i] = x[0] / (1.0 - s3);
		for (i = l; i < (m + 1000); i++)
		{
			s1 = 0.0;
			s2 = 0.0;
			if (q >= 1)
			{
				for (j = 0; j < q; j++)
					s1 += x[j + 1] * y0[i - j - 1] * y0[i - j - 1];
			}
			if (p >= 1)
			{
				for (j = 0; j < p; j++)
					s2 += x[q + 1 + j] * sigma[i - j - 1];
			}
			sigma[i] = x[0] + s1 + s2;
			y0[i] = z[i] * Math.Sqrt(sigma[i]);
		}
		/*
		 * DISCARD THE FIRST 1000 SIMULATED OBSERVATIONS
		 */
		for (i = 0; i < m; i++)
			y[i] = y0[1000 + i];
		return ;
	}
	

	public static void  Main(String[] args)
	{
		int n, p, q, m;
		double[] x = new double[]{1.3, 0.2, 0.3, 0.4};
		double[] xguess = new double[]{1.0, 0.1, 0.2, 0.3};
		double[] y = new double[1000];
		double[] wk1 = new double[2000];
		double[] wk2 = new double[2000];
		double[] wk3 = new double[2000];

		m = 1000;
		p = 2;
		q = 1;
		n = p + q + 1;
		sgarch(p, q, m, x, y, wk1, wk2, wk3);
		
		GARCH garch = new GARCH(p, q, y, xguess);
		garch.Compute();
		
		Console.Out.WriteLine
			("Sigma estimate is " + garch.Sigma.ToString("0.000"));
		Console.Out.WriteLine();
		new PrintMatrix("AR estimate is ").Print(garch.GetAR());
		new PrintMatrix("MR estimate is ").Print(garch.GetMA());
		Console.Out.WriteLine("Log-likelihood function value is " + 
			garch.LogLikelihood.ToString("0.000"));
		Console.Out.WriteLine("Akaike Information Criterion value is "
			 + garch.Akaike.ToString("0.000"));
	}
}

Output

Sigma estimate is 1.692

   AR estimate is 
           0          
0  0.244996117092089  
1  0.337235176981931  

   MR estimate is 
           0          
0  0.309586601528879  

Log-likelihood function value is -2707.073
Akaike Information Criterion value is 5422.146

Link to C# source.