Exemplo n.º 1
0
        public List <double[]> Fit()
        {
            var            vec     = new List <double[]>();
            ARMAFoundation ar_math = new ARMAFoundation();
            var            armaCoe = ar_math.computeARMACoe(this.data, this.p, this.q);

            var arCoe = new double[this.p + 1];

            for (int i = 0; i < arCoe.Length; i++)
            {
                arCoe[i] = armaCoe[i];
            }

            var maCoe = new double[this.q + 1];

            for (int i = 0; i < maCoe.Length; i++)
            {
                maCoe[i] = armaCoe[i + this.p + 1];
            }

            //aggregate coefficients
            vec.Add(arCoe);
            vec.Add(maCoe);

            return(vec);
        }
Exemplo n.º 2
0
        public override double[] Fit()
        {
            //
            ARMAFoundation ar_math = new ARMAFoundation();
            var            maCoe   = ar_math.computeARCoe(this.data, this.param);

            return(maCoe);
        }
Exemplo n.º 3
0
        public int[] getARIMAModel(int period, List <int[]> notModel, bool needNot)
        {
            var data = this.preDealDiff(period);
            //
            double          minAIC    = 1.7976931348623157E308;
            var             bestModel = new int[3];
            int             type      = 0;
            List <double[]> coe       = new List <double[]>();

            // The model is generated, that is, the corresponding p, q parameters are generated
            int len = data.Length > 5 ? 5 : data.Length;

            int          size  = ((len + 2) * (len + 1)) / 2 - 1;
            List <int[]> model = new List <int[]>();

            //
            for (int i = 0; i < size; i++)
            {
                model.Add(new int[size]);
            }

            int cnt = 0;

            for (int i = 0; i <= len; ++i)
            {
                for (int j = 0; j <= len - i; ++j)
                {
                    if (i == 0 && j == 0)
                    {
                        continue;
                    }

                    model[cnt][0]   = i;
                    model[cnt++][1] = j;
                }
            }
            //
            for (int i = 0; i < cnt; ++i)
            {
                // Control selected parameters
                bool token = false;
                if (needNot)
                {
                    for (int k = 0; k < notModel.Count; ++k)
                    {
                        if (model[i][0] == notModel[k][0] && model[i][1] == notModel[k][1])
                        {
                            token = true;
                            break;
                        }
                    }
                }
                if (token)
                {
                    continue;
                }

                if (model[i][0] == 0)
                {
                    MAModel ma = new MAModel(data, model[i][1]);
                    //std::vector<std::vector<double>>
                    var maC = ma.Fit();
                    coe.Add(maC);
                    type = 1;
                }
                else if (model[i][1] == 0)
                {
                    ARModel ar = new ARModel(data, model[i][0]);
                    //
                    var maC = ar.Fit();
                    coe.Add(maC);
                    type = 2;
                }
                else
                {
                    //
                    ARMAModel arma = new ARMAModel(data, model[i][0], model[i][1]);
                    //
                    coe  = arma.Fit();
                    type = 3;
                }

                ARMAFoundation ar_math = new ARMAFoundation();
                double         aic     = ar_math.getModelAIC(coe, data, type);
                // If the order is too long during the solution process, NAN or infinity may occur
                if (aic <= 1.7976931348623157E308 && !double.IsNaN(aic) && aic < minAIC)
                {
                    minAIC = aic;
                    // std::cout<<aic<<std::endl;
                    bestModel[0] = model[i][0];
                    bestModel[1] = model[i][1];
                    bestModel[2] = (int)Math.Round(minAIC);
                    this.arima   = coe;
                }
            }
            return(bestModel);
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
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);
        }