Results 1 to 4 of 4

Thread: About GARCH Method

  1. #1

    About GARCH Method

    Can anyone explain Garch method that is shown below with a sample dataset ?

    public GARCH(
    int p,
    int q,
    double[] y,
    double[] xguess
    );
    Parameters
    p
    A int scalar containing the number of autoregressive (AR) parameters.
    q
    A int scalar containing the number of moving average (MA) parameters.
    y
    A double array containing the observed time series data.
    xguess
    A double array of length p + q + 1 containing the initial values for the parameter array.

    Best Regards,

    Ersin.

  2. #2
    Senior Member ed's Avatar
    Join Date
    Aug 2005
    Posts
    312
    From the API you've posted, it looks like you're using the GARCH class in the JMSL Library. We have an Example included with the API docs. Of course this example uses a method to generate a random time series with some very specific characteristics. I searched around a bit for a more complete example that uses a time series, but it seems most people prefer to show how GARCH works with generated data. Do you have any specific questions not answered by the documentation?


    The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.

  3. #3
    Senior Member ed's Avatar
    Join Date
    Aug 2005
    Posts
    312
    One of our "stat guys" had a PV-WAVE example of using the IMSL GARCH function laying around. I translated this to Java and hope it might be of some use to you. This code tries to simulate and model a GARCH(1,1) process.

    Code:
    import com.imsl.chart.*;
    import com.imsl.stat.*;
    import javax.swing.*;
    import java.awt.Font;
    
    /**
     *  An example of GARCH in practice
     */
    public class GarchTest extends JFrameChart {
    
        private JTextArea displayText;
        private Chart chart;
        private AxisXY axis;
    
        public GarchTest() {
            setSize(750, 650);
            setTitle("GARCH Test");
    
            initComponents();
            runTest();
        }
    
        private void runTest() {
            int alpha = 1;
            int beta = 1;
            int length = 1000;
            double[] actualParams = {1.5, 0.15, 0.35}; // sqrt(var), alpha, beta
            double[] initialGuess = {1.0, 0.4, 0.5};
            double[] series = simulateGarch(alpha, beta, length, actualParams);
            GARCH garch = new GARCH(alpha, beta, series, initialGuess);
            try {
                garch.compute();
            } catch (com.imsl.IMSLException e) {
                System.out.println("GARCH Exception: " + e.getMessage());
            }
            double[] result = garch.getX();
            Summary summary = new Summary();
            summary.update(series);
            double longRunVar = summary.getStandardDeviation();
            double gamma = result[0] / longRunVar;
            double alpha_r = result[1];
            double beta_r = result[2];
    
            plotSeries(series);
            displayValues(longRunVar, gamma, alpha_r, beta_r);
    
            // For the sake of example, pretend we know the variance associated with today's return
            double todaysVar = 2 * longRunVar;
    
            // Now we can compute the expected variance for tomorrow's return
            double tomorrowsVar = gamma * longRunVar + alpha_r * Math.pow(series[length - 1], 2) + beta * todaysVar;
    
            // Or we can compute a forecast of the next 20 expected variances
            int nForecast = 20;
            double[] forecasts = new double[nForecast];
            for (int i = 0; i < forecasts.length; i++)
            {
                forecasts[i] = garchForecast(alpha_r, beta_r, longRunVar, todaysVar, i + 1);
            }
            displayForecast(forecasts);
    
        }
    
        private double garchForecast(double alpha_r, double beta_r, double longRunVar, double prevVar, int stepAhead)
        {
            double persistence = Math.pow(alpha_r + beta_r, stepAhead);
            return longRunVar + persistence * (prevVar - longRunVar);
        }
    
        private void displayForecast(double[] x)
        {
            java.text.DecimalFormat fmt = new java.text.DecimalFormat("#.00000000");
            displayText.append("\n\n20-day GARCH forecast of variance (should converge to Long-run Variance):\n");
            int w = 4;
            for (int i = 0; i < x.length; i += w)
            {
                displayText.append("\n\t" + fmt.format(x[i]) +
                    "\t\t" + fmt.format(x[i + 1]) +
                    "\t\t" + fmt.format(x[i + 2]) +
                    "\t\t" + fmt.format(x[i + 3]));
            }
        }
    
        private void displayValues(double longRunVar, double gamma, double alpha, double beta)
        {
            displayText.setText("Long-run Variance of series: " + longRunVar);
            displayText.append("\nGARCH(1,1) model parameters...");
            displayText.append("\n\tgamma: " + gamma);
            displayText.append("\n\talpha: " + alpha);
            displayText.append("\n\tbeta: " + beta);
            displayText.append("\n\tpersistence: " + (alpha + beta));
            displayText.append("\n\nSum of parameters (should be ~ 1): " +(gamma + alpha + beta));
        }
    
    
        private void plotSeries(double[] x)
        {
            chart = getChart();
            chart.getChartTitle().setTitle("Time Series of Returns");
            AxisXY a = new AxisXY(chart);
            a.getAxisX().setTextFormat("0");
            a.getAxisY().setTextFormat("0");
            a.setViewport(0.05, 0.95, 0.1, 0.9);
            Data d = new Data(a, x);
        }
    
        private double[] simulateGarch(int alpha, int beta, int length, double[] x)
        {
            int n = length + 1000;
            double[] z = new double[n];
            double[] y0 = new double[n];
            double[] sigma = new double[n];
            com.imsl.stat.Random ran = new com.imsl.stat.Random(182198625);
            for (int i = 0; i < z.length; i++)
            {
                z[i] = ran.nextNormal();
            }
            int loc = Math.max(Math.max(alpha, beta), 1);
            for (int i = 0; i < loc; i++)
            {
                y0[i] = z[i] * x[0];
            }
    
            // Compute the initial value of sigma
            double s3 = 0.0;
            if (Math.max(alpha, beta) >= 1)
            {
                int end = alpha + beta + 1;
                for (int i = 1; i < end; i++)
                {
                    s3 += x[i];
                }
            }
            for (int i = 0; i < loc; i++)
            {
                sigma[i] = x[0]/(1.0 - s3);
            }
            double s1, s2;
            for (int i = 1; i < n; i++)
            {
                s1 = 0.0;
                s2 = 0.0;
                if (beta >= 1)
                {
                    for (int j = 0; j < beta; j++)
                    {
                        s1 += x[j + 1] * y0[i - j - 1] * y0[i - j - 1];
                    }
                }
                if (alpha >= 1)
                {
                    for (int j = 0; j < alpha; j++)
                    {
                        s2 += x[beta + 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
            double[] y_ret = new double[length];
            for (int i = 0; i < y_ret.length; i++)
            {
                y_ret[i] = y0[i + 1000];
            }
            return y_ret;
        }
    
        private void initComponents() {
            // Set up the panel
            java.awt.Container cp = getContentPane();
            JPanel msgPanel = new JPanel();
            displayText = new JTextArea(" ", 16, 100);
            displayText.setEditable(false);
            msgPanel.add(displayText);
            cp.add(msgPanel, java.awt.BorderLayout.SOUTH);
            displayText.setFont(new Font("monospaced", Font.PLAIN, 11));
            displayText.setText("Click in the chart area for exact values.");
            repaint();
        }
    
        public static void main(String argv[]) {
            new GarchTest().setVisible(true);
        }
    }
    A thumbnail of a screen shot of the output is attached too.


    The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.

  4. #4
    Junior Member
    Join Date
    Jun 2009
    Posts
    1

    Garch forecasting

    I notice that the above Garch example only shows how to get the forecasted variance for future periods. However, if I have a time series Yt which has mean or trend, then how can I use the Garch class to forecast the future value for Yt+1 ?

    Thanks;

    Markham96

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •