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.

import java.text.*;
import com.imsl.stat.*;
import com.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;
        Random rand = new Random(182198625L);
        
        rand.setMultiplier(16807);
        for (i = 0; i < m+1000; i++) z[i] = rand.nextNormal();
        
        l = Math.max(p, q);
        l = 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 (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[]) throws Exception {
        int n, p, q, m;
        double[] x = {1.3, 0.2, 0.3, 0.4};
        double[] xguess = {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];
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(3);
        
        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();
        
        System.out.println("Sigma estimate is " + nf.format(garch.getSigma()));
        System.out.println();
        new PrintMatrix("AR estimate is ").print(garch.getAR());
        new PrintMatrix("MR estimate is ").print(garch.getMA());
        System.out.println("Log-likelihood function value is " +
        nf.format(garch.getLogLikelihood()));
        System.out.println("Akaike Information Criterion value is " +
        nf.format(garch.getAkaike()));
    }
}

Output

Sigma estimate is 1.692

AR estimate is 
     0    
0  0.245  
1  0.337  

MR estimate is 
    0    
0  0.31  

Log-likelihood function value is -2,707.072
Akaike Information Criterion value is 5,422.144
Link to Java source.