Esempio n. 1
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);
        }