signaflo / java-timeseries

Time series analysis in Java
MIT License
195 stars 49 forks source link

how to get the best p d q #17

Open liaozjabc opened 6 years ago

liaozjabc commented 6 years ago

Hi, @signaflo ! Sorry to bother you. i want to ask if there any way to get the best p d q or list of acf、pacf 。 I will appreciate your reply if you can spare some time to help me. Good Day!

signaflo commented 6 years ago

@liaozjabc, by best p, d, q, are you thinking of an auto arima type class? It's definitely on the to do list. Most implementations use AIC, AICc, or BIC to determine what the best order to use is. But I'm not sure if you had something else in mind.

Marina-Andric commented 6 years ago

@liaozjabc I faced a similar task, below is my attempt for finding optimal p, d, q using AIC criterion. I've put some limits for the orders of AR and MA models, as well as for d.

        for (int p = 0; p < 5; p++)
            for (int d = 0; d < 2; d++)
                for (int q = 0; q < 5; q++) {
                    models.add(ArimaOrder.order(p, d, q));
                    models.add(ArimaOrder.order(p, d, q, Arima.Drift.INCLUDE));
                    models.add(ArimaOrder.order(p, d, q, Arima.Constant.INCLUDE));
                }

        ArrayList<Arima> fmodels = new ArrayList<Arima>();

        Arima tmp;
        for (int i = 0; i < models.size(); i++) {
            try {
                tmp = Arima.model(timeSeries, models.get(i));
            } catch (Exception e) {
                continue;   
            }
            if (!Double.isNaN(tmp.aic()))
                fmodels.add(tmp);
        }
        Arima optimalModel = fmodels.get(0);
        double aic = fmodels.get(0).aic();
        double aic_curr;
        for (int i = 1; i < fmodels.size(); i++) {
            if ((aic_curr = fmodels.get(i).aic()) < aic) {
                aic = aic_curr;
                optimalModel = fmodels.get(i);
            }
        }
pjebs commented 6 years ago

R's forecast library has a function called auto Arima. The source is open so perhaps use that as a basis.