Advanced Use

<- Previous: Forcasts
Next ->: Session File

The FANPAC keyword functions are also useful for more complicated tasks. For instance, we might want to solve a time series problem recursively in order to confirm the models success. In the following example, a series of GARCH(3,3) models is applied to a 126 period moving window of the Wilshire 5000 index time series beginning with a series from May 20, 1992 to October 15, 1992 and ending with a series from June 30, 1992 to December 28, 1992.

In this example, the same session name is used in each iteration, and thus the session file will be over-written at each iteration and at the end only the last session file will be available. For this reason, the information we desire, in this case, the predicted conditional variance and the actual squared residual, are stored in arrays. The estimates are also saved and stored in a matrix that is saved to the disk for start values in case the run is to be repeated.

The selection of the observations is accomplished by constructing a string to be used in the argument in the call to the keyword function setSeries.

Three lagged values of the dependent variable are also included as independent variables in the model.  ;   

    library fanpac,pgraph;
    #include pgraph.ext;

    numPeriods = 50;

    actual = error(0)*ones(numPeriods,1);
    predicted = error(0)*ones(numPeriods+1,1);

    starts = {};

    t1 = 3126; /* 19920520 */
    t2 = 3251; /* 19921015 */

    i = 1; do until  i > numPeriods;

        t0 = "price "$+ftos(t1+i-1,"*.*lf",1,0) 
                    $+ " " $+ ftos(t2+i-1,"*.*lf",1,0);

        session wilshire;

        setDataSet w5daily;

        setSeries ^t0;

        setIndVars LPRICE1, LPRICE2, LPRICE3;

        estimate run1 garch(3,3);

        forecast 1;    /* generate forecast */

        loadm wilshire;
        retcode = vread(vread(w5,"run1"),"rcode");  

        if retcode == 0;  /* estimation successful */

            starts = starts ~ _fan_Estimates;

            getResiduals;  /* put residuals into global */

            actual[i,1] = ( _fan_Residuals[rows(_fan_Resid)]^2;
            predicted[i+1,1] = _fan_CVforecast;

        else;  /* estimation failed */

            starts = starts ~ miss(zeros(rows(_fan_estimates),1),0);

        endif;

        if i > 1; /* print iteration results      */
                  /* so progress can be monitored */

            format /rd 2,0;
            print i "   " rcode;;

            format /ld 7,3;
            print actual[i,1];;
            print predicted[i,1];;
            print "  " _fan_estimates';

       endif;


    i = i + 1; endo;


    save starts;


    graphset;

    z = packr(actual[2:numPeriods]~predicted[2:numPeriods]);


    /* 
    ** fit a line to scattergram to show 
    ** how well it's doing
    */

    m0 = minc(z[.,1]);
    m1 = maxc(z[.,1]);

    b = z[.,2]/(ones(rows(z),1)~z[.,1]);

    n0 = b[1] + b[2] * m0;
    n1 = b[1] + b[2] * m1;

    _pline = { 1 6 0 0 0 0 1 6 1 };
    _pline[1,3] = m0;
    _pline[1,4] = n0;
    _pline[1,5] = m1;
    _pline[1,6] = n1;


    _plctrl = -1;

    xy(z[.,1],z[.,2]);

The resulting plot shows a moderate confirmation of the forecast.


 
Figure 1: Plot of forecaset conditional variances against observed squared residuals

\includegraphics[angle=270,scale=.5]{plot1.eps}




R. Schoenberg
1999-03-29