예제 #1
0
        public double getModelAIC(List <double[]> vec, double[] data, int type)
        {
            int    n      = data.Length;
            int    p      = 0;
            int    q      = 0;
            double tmpAR  = 0.0;
            double tmpMA  = 0.0;
            double sumErr = 0.0;

            if (type == 1)
            {
                double[] maCoe = vec[0];
                q = maCoe.Length;

                double[] errData = new double[q];

                for (int i = q - 1; i < n; i++)
                {
                    tmpMA = 0.0;
                    for (int j = 1; j < q; j++)
                    {
                        tmpMA += maCoe[j] * errData[j];
                    }
                    for (int j = q - 1; j > 0; j--)
                    {
                        errData[j] = errData[j - 1];
                    }

                    errData[0] = ARMAFoundation.gaussrand0() * Math.Sqrt(maCoe[0]);
                    sumErr    += (data[i] - tmpMA) * (data[i] - tmpMA);
                }
                return((n - (q - 1)) * Math.Log(sumErr / (n - (q - 1))) + (q + 1) * 2);
            }
            else if (type == 2)
            {
                double[] arCoe = vec[0];
                p = arCoe.Length;

                for (int i = p - 1; i < n; ++i)
                {
                    tmpAR = 0.0;
                    for (int j = 0; j < p - 1; ++j)
                    {
                        tmpAR += arCoe[j] * data[i - j - 1];
                    }
                    sumErr += (data[i] - tmpAR) * (data[i] - tmpAR);
                }
                //return Math.log(sumErr) + (p + 1) * 2 / n;
                return((n - (p - 1)) * Math.Log(sumErr / (n - (p - 1))) + (p + 1) * 2);
            }
            else
            {
                double[] arCoe = vec[0];
                double[] maCoe = vec[1];
                p = arCoe.Length;
                q = maCoe.Length;
                var errData = new double[q];

                for (int i = p - 1; i < n; ++i)
                {
                    tmpAR = 0.0;
                    for (int j = 0; j < p - 1; ++j)
                    {
                        tmpAR += arCoe[j] * data[i - j - 1];
                    }
                    tmpMA = 0.0;
                    for (int j = 1; j < q; ++j)
                    {
                        tmpMA += maCoe[j] * errData[j];
                    }

                    for (int j = q - 1; j > 0; --j)
                    {
                        errData[j] = errData[j - 1];
                    }
                    errData[0] = ARMAFoundation.gaussrand0() * Math.Sqrt(maCoe[0]);

                    sumErr += (data[i] - tmpAR - tmpMA) * (data[i] - tmpAR - tmpMA);
                }
                //return Math.log(sumErr) + (q + p + 1) * 2 / n;
                return((n - (q + p - 1)) * Math.Log(sumErr / (n - (q + p - 1))) + (p + q) * 2);
            }
        }
예제 #2
0
        public int predictValue(int p, int q, int period)
        {
            var    data    = this.preDealDiff(period);
            int    n       = data.Length;
            int    predict = 0;
            double tmpAR   = 0.0;
            double tmpMA   = 0.0;

            double[] errData = new double[q + 1];

            if (p == 0)
            {
                List <double> maCoe = new List <double>(this.arima[0]);
                for (int k = q; k < n; ++k)
                {
                    tmpMA = 0;
                    for (int i = 1; i <= q; ++i)
                    {
                        tmpMA += maCoe[i] * errData[i];
                    }
                    //
                    for (int j = q; j > 0; --j)
                    {
                        errData[j] = errData[j - 1];
                    }
                    errData[0] = ARMAFoundation.gaussrand0() * Math.Sqrt(maCoe[0]);
                }

                predict = (int)(tmpMA);                 //
            }
            else if (q == 0)
            {
                List <double> arCoe = new List <double>(this.arima[0]);

                for (int k = p; k < n; ++k)
                {
                    tmpAR = 0;
                    for (int i = 0; i < p; ++i)
                    {
                        tmpAR += arCoe[i] * data[k - i - 1];
                    }
                }
                predict = (int)(tmpAR);
            }
            else
            {
                List <double> arCoe = new List <double>(this.arima[0]);
                List <double> maCoe = new List <double>(this.arima[1]);

                for (int k = p; k < n; ++k)
                {
                    tmpAR = 0;
                    tmpMA = 0;
                    for (int i = 0; i < p; ++i)
                    {
                        tmpAR += arCoe[i] * data[k - i - 1];
                    }
                    for (int i = 1; i <= q; ++i)
                    {
                        tmpMA += maCoe[i] * errData[i];
                    }

                    //
                    for (int j = q; j > 0; --j)
                    {
                        errData[j] = errData[j - 1];
                    }

                    errData[0] = ARMAFoundation.gaussrand0() * Math.Sqrt(maCoe[0]);
                }

                predict = (int)(tmpAR + tmpMA);
            }

            return(predict);
        }