Beispiel #1
0
        /// <summary>
        ///  This function uses the Durbin-Levinson algorithm
        ///  to get one-step predictors and residuals.
        /// </summary>
        /// <param name="rs">double[] array of one-step predictive MSEs / sigma^2,
        //     based on sigma when this routine was called </param>
        /// <returns>double[] array of residuals = [ (x_i - \hat{x}_i)/\sqrt{rs_i} ]</returns>
        protected double[] ComputeSpecialResiduals(TimeSeries startData, out double[] rs, int forecastHorizon,
                                                   out double[] forecasts)
        {
            // 1.  residuals are returned: retval[] =  [ (x_i - \hat{x}_i)/\sqrt{rs_{i-1}} ]
            // 2.  rs[...] are one-step predictive MSEs / sigma^2,
            //     based on sigma when this routine was called

            int i, n, nobs = startData.Count + forecastHorizon;
            var xhat = Vector <double> .Build.Dense(Math.Max(nobs + 1, 1));

            var localResiduals = new double[nobs];

            rs = new double[nobs + 1];

            var dl = new DurbinLevinsonPredictor(Mu, Autocovariance, nobs + 1);

            xhat[0] = dl.CurrentPredictor;
            rs[0]   = dl.CurrentMSPE;
            for (i = 1; i <= nobs; ++i)
            {
                if (i <= startData.Count)
                {
                    dl.Register(startData[i - 1]);
                }
                else
                {
                    dl.Register(xhat[i - 1]);
                }
                xhat[i] = dl.CurrentPredictor;
                rs[i]   = dl.CurrentMSPE;
            }
            for (i = 0; i < rs.Length; ++i)
            {
                rs[i] /= (Sigma * Sigma);
            }

            // store the results
            for (n = 0; n < startData.Count; ++n)
            {
                localResiduals[n] = (startData[n] - xhat[n]) / Math.Sqrt(rs[n]);
            }

            if (forecastHorizon > 0)
            {
                forecasts = new double[forecastHorizon];
                for (n = startData.Count; n < nobs; ++n)
                {
                    forecasts[n - startData.Count] = xhat[n];
                }
            }
            else
            {
                forecasts = null;
            }

            return(localResiduals);
        }
Beispiel #2
0
 public virtual void ResetRealTimePrediction()
 {
     realTimePredictor = new DurbinLevinsonPredictor(Mu, Autocovariance, 20000);
 }