/* ---- This file is part of SECONDO. Copyright (C) 2020, Faculty of Mathematics and Computer Science, Database Systems for New Applications. SECONDO is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. SECONDO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with SECONDO; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ---- //[$][\$] //[_][\_] */ #ifndef ARIMA_H #define ARIMA_H #include #include #include #include #include #include #include using std::vector; using std::cout; using std::string; using std::min; class Arima { public: Arima(int, int, int, int, vector); vector ar(); vector ma(); vector arma(); vector arima(); vector forecast(); void printPACF(int lags); void printACF(int lags); private: int order_ma; int order_ar; int order_differencing; int steps; vector timeseries_data; vector fitted_data; vector differenced_data; double mean; //matrix of parameters for the AR-process vector> phi; //Matrix of parameters for the MA-process vector> theta; //matrix of the partial autocorrelation coefficients of the process vector> pacf; //matrix of the autocorrelation coefficients of the process vector> acf; //innovations of the MA-process vector ny; vector acvf; void differencing(); void innovations_algorithm(); void durbin_levinson_algorithm(); double gamma_hat(int index); Eigen::VectorXd compute_ar_coeffs(); }; template struct Functor { typedef _Scalar Scalar; enum { InputsAtCompileTime = NX, ValuesAtCompileTime = NY, }; typedef Eigen::Matrix InputType; typedef Eigen::Matrix ValueType; typedef Eigen::Matrix JacobianType; int m_inputs, m_values; Functor() : m_inputs(ValuesAtCompileTime), m_values(InputsAtCompileTime){} Functor(int inputs, int values) : m_inputs(inputs), m_values(values) {} //Returns the number of values int values() const {return m_values;} int inputs() const {return m_inputs;} }; struct LMFunctor : Functor { int operator()(const Eigen::VectorXd &x, Eigen::VectorXd &fvec) const { Eigen::VectorXd predictions(values()); for(int i = 0; i < inputs(); ++i) predictions(i) = timeseries_data[i]; for(int k = inputs(); k < values()-1; ++k) { double predicted_value = 0; for(int i = 0 ; min(i, inputs()) < inputs(); ++i) { double coeff = x(i); predicted_value += coeff * (timeseries_data[k + 1 - i] - predictions[k + 1 - i]) ; } predictions(k) = predicted_value; fvec(k) = predicted_value - timeseries_data[k]; } return 0; } int order_ma; int order() const {return order_ma;} vector timeseries_data; }; #endif // ARIMA_H