The ARIMA algorithm is widely used in econometrics, statistics, and time series analysis. It models a series as a combination of autoregressive (AR), integrated (I), and moving average (MA) components. The ARIMA model can be extended to include seasonality, resulting in a seasonal ARIMA (SARIMA) model. Additionally, the SARIMA model can incorporate external covariates, resulting in an SARIMAX model. The SARIMAX model can be estimated using a procedure that involves differencing the series, preliminary estimation, transformation for stability, numeric optimization, and inverse transformation. The SARIMAX model can be used for forecasting, either by applying the SARIMAX formula or using the innovations algorithm. The ARIMA model output table provides various key-value pairs that describe the model parameters and statistics. The forecast algorithm is compatible with previous versions of the ARIMA model generated by SAP HANA PAL.
------

SET SCHEMA DM_PAL;

DROP TABLE PAL_ARIMA_DATA_TBL;
CREATE COLUMN TABLE PAL_ARIMA_DATA_TBL ("TIMESTAMP" INTEGER, "Y" DOUBLE);
INSERT INTO PAL_ARIMA_DATA_TBL VALUES (1, -0.636126431 );
INSERT INTO PAL_ARIMA_DATA_TBL VALUES (2, 3.092508651  );
INSERT INTO PAL_ARIMA_DATA_TBL VALUES (3, -0.73733556  );
INSERT INTO PAL_ARIMA_DATA_TBL VALUES (4, -3.142190983 );
INSERT INTO PAL_ARIMA_DATA_TBL VALUES (5, 2.088819813  );

DROP TABLE PAL_PARAMETER_TBL;
CREATE COLUMN TABLE PAL_PARAMETER_TBL ( "NAME" VARCHAR (50),"INT_VALUE" INTEGER,"DOUBLE_VALUE" DOUBLE,"STRING_VALUE" VARCHAR (100));
INSERT INTO PAL_PARAMETER_TBL VALUES ('D', 0, NULL, NULL);
INSERT INTO PAL_PARAMETER_TBL VALUES ('Q', 1, NULL, NULL);
INSERT INTO PAL_PARAMETER_TBL VALUES ('SEASONAL_PERIOD', 4, NULL, NULL);
INSERT INTO PAL_PARAMETER_TBL VALUES ('SEASONAL_P', 1, NULL, NULL);
INSERT INTO PAL_PARAMETER_TBL VALUES ('METHOD', 1, NULL, NULL);
INSERT INTO PAL_PARAMETER_TBL VALUES ('THREAD_RATIO', NULL, 1.0, NULL);

DROP TABLE PAL_ARIMA_MODEL_TBL;  -- for the following forecast
CREATE COLUMN TABLE PAL_ARIMA_MODEL_TBL ("KEY" NVARCHAR(100), "VALUE" NVARCHAR(5000));

do begin 
	lt_data = select * from PAL_ARIMA_DATA_TBL;
	lt_control = select * from PAL_PARAMETER_TBL;
	_SYS_AFL.PAL_ARIMA(:lt_data, :lt_control, lt_model, lt_fit);
	INSERT INTO PAL_ARIMA_MODEL_TBL SELECT * FROM :lt_model;
	SELECT * FROM PAL_ARIMA_MODEL_TBL;
	SELECT* FROM :lt_fit;
end;
