The Bayesian Structural Time Series (BSTS) model is a time series analysis method that can be used for forecasting, decomposition, and feature selection. It is considered superior to other methods like ARIMA and Additive Model Time Series Analysis because it can handle larger scale exogenous data and provide more precise forecasting. 

The BSTS model includes a structural time series model for the target series, with a regression component that captures the contributions of several contemporaneous time series data. The model consists of equations for the target series, trend, slope of trend, seasonal component, and regression component. The model also includes independent Gaussian random noise terms with variances.

BSTS uses Bayesian techniques, specifically a combination of three methods: Kalman filter, spike-and-slab regression, and Bayesian model averaging. The Kalman filter is used to extract the inner trend and seasonal term from the target series. Spike-and-slab regression is a Bayesian variable selection technique that determines the importance of each exogenous contemporaneous series to the target series. Bayesian model averaging is used to convert samples from repeated Markov Chain Monte Carlo draws into the final model outputs.

The BSTS algorithm runs through three steps in a single draw: sampling the trend, slope of trend, and seasonal terms from the Kalman filter; sampling the variances of the noise terms from inverse gamma distributions; and sampling the regression coefficients and noise variance using the stochastic search variable selection algorithm applied to spike-and-slab regression.
------

SET SCHEMA DM_PAL;

DROP TABLE PAL_BSTS_DATA_TBL;
CREATE COLUMN TABLE PAL_BSTS_DATA_TBL (
	"TIME_STAMP" INTEGER,
        "TARGET_SERIES" DOUBLE,
        "FEATURE_01" DOUBLE,
	"FEATURE_02" DOUBLE,
	"FEATURE_03" DOUBLE,
	"FEATURE_04" DOUBLE,
	"FEATURE_05" DOUBLE,
	"FEATURE_06" DOUBLE,
	"FEATURE_07" DOUBLE,
	"FEATURE_08" DOUBLE,
	"FEATURE_09" DOUBLE,
	"FEATURE_10" DOUBLE

);

INSERT INTO PAL_BSTS_DATA_TBL VALUES (0,2.536,1.488,-0.561,1.773,0.909,2.021,1.64,0.3,1.75,0.498,0.073);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (1,0.882,1.1,-0.992,0.9,0.148,1.28,1.014,0.18,-0.011,0.264,0.584);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (2,-0.077,1.155,-1.212,1.477,0.21,1.08,1.009,0.119,-0.028,0.031,0.448);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (3,0.135,0.53,-1.034,1.244,-0.308,1.067,0.734,0.727,-0.23,-0.143,-0.269);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (4,0.373,0.698,-1.195,0.643,0.57,1.125,0.502,0.598,0.625,-0.219,-1.006);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (5,-0.437,0.441,-1.386,0.049,-0.182,0.615,-0.226,-0.199,-0.401,-0.526,-1.124);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (6,-0.556,0.405,-0.844,0.198,-0.553,0.784,-0.518,-0.245,-0.976,-0.699,-0.504);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (7,-0.432,-0.016,-1.001,-0.401,-0.339,-0.163,-0.55,-0.871,-1.236,-0.884,-1.254);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (8,-0.46,0.271,-1.234,0.252,-0.666,0.321,-0.558,-0.359,-0.555,-0.778,-2.114);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (9,-0.698,-0.357,-1.269,-1.189,-1.911,-0.645,-1.468,-1.116,0.156,-1.182,-2.958);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (10,-0.765,-0.006,-1.326,0.242,0.175,-0.185,-0.912,-0.276,0.158,-0.917,-0.939);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (11,-0.833,-0.647,-2.124,-0.71,-0.14,-1.235,-1.924,-0.978,-0.572,-1.158,-1.758);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (12,-0.767,-0.282,-1.615,-0.489,-0.556,0.879,-0.593,-0.444,-1.992,-0.898,-0.831);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (13,-0.356,-0.503,-1.035,-0.201,-0.367,-0.34,-0.331,-0.397,-0.897,-0.844,-0.425);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (14,-0.496,-0.998,-1.356,-0.717,-0.514,-0.806,-1.27,-0.669,-0.338,-1.145,-1.21);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (15,-0.684,-0.618,-1.06,-0.012,-0.93,-0.093,-1.162,-0.805,-0.373,-1.04,-0.868);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (16,-0.953,-0.547,-1.437,0.007,-0.302,0.221,-1.277,-0.504,-0.512,-0.898,-1.441);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (17,-0.869,-0.403,-1.36,-0.386,-0.129,0.001,-1.214,-0.636,0.065,-1.069,-0.929);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (18,-0.831,-0.691,-1.553,-0.002,-0.348,-0.775,-0.428,-0.626,-0.489,-0.858,-1.033);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (19,-0.858,-0.793,-1.302,-0.401,-0.565,0.159,-0.657,-0.855,-0.629,-0.983,-0.942);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (20,-0.769,-0.654,-1.332,-0.052,-0.432,-0.134,-0.674,-0.562,0.569,-0.953,-1.054);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (21,-0.732,-0.78,-1.362,-0.518,-0.748,0.309,-0.366,-0.668,-0.649,-0.55,-1.276);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (22,-0.682,-0.575,-1.459,-0.39,0.024,-0.416,0.034,-0.693,-0.846,-0.701,-0.941);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (23,-0.606,-0.575,-1.522,0.589,-0.189,-0.206,-0.129,-0.681,0.457,-0.443,-0.218);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (24,-0.639,-0.792,-1.255,0.036,-0.19,1.17,-0.284,-0.483,0.024,-0.672,-1.335);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (25,-0.363,-0.189,-1.096,-0.005,-0.299,0.858,-0.292,0.131,-0.958,-0.453,0.058);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (26,0.473,0.56,-1.211,-0.024,-0.496,0.281,-0.508,-0.297,-0.025,-0.449,-0.401);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (27,0.03,0.045,-1.113,-0.113,0.441,0.599,0.283,-0.385,-0.316,-0.472,-0.597);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (28,-0.688,-0.233,-1.157,-0.408,-0.539,-0.102,-0.729,-0.575,-0.742,-0.585,-0.609);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (29,-0.963,-0.209,-1.211,-0.113,0.134,-0.288,-0.388,-0.535,-0.166,-0.524,-0.88);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (30,-0.879,-0.23,-1.22,-0.617,-0.03,-0.503,-0.464,-0.607,0.652,-0.326,-0.605);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (31,-1.132,-0.757,-1.427,-0.291,0.29,-0.05,-0.213,-0.851,-1.309,-0.798,-0.841);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (32,-1.031,-0.429,-1.555,-0.586,-0.283,0.264,0.132,-0.702,-0.767,-0.718,-0.369);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (33,-1.014,-0.342,-1.204,0.107,-0.195,-0.059,-0.043,-0.743,-0.671,-0.577,-0.632);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (34,-1.026,-0.819,-1.208,-0.056,-0.133,0.521,-0.828,-0.767,0.112,-0.828,0.007);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (35,-1.242,-0.724,-1.386,-0.443,-0.259,-0.437,-0.803,-0.989,-0.535,-0.737,-1.559);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (36,-1.018,-0.484,-1.163,-0.257,-0.645,-0.068,-0.975,-0.767,-0.896,-1.042,-1.585);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (37,-0.957,-0.74,-1.053,-0.52,-0.712,-0.691,-1.082,-0.695,-0.778,-0.71,-0.734);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (38,-0.985,-0.98,-0.788,-0.549,-0.602,-0.249,-0.777,-0.886,-1.033,-1.022,-1.522);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (39,-0.463,-0.43,-0.495,-0.205,-0.779,-0.413,-0.583,-0.718,-0.42,-0.628,-0.998);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (40,-0.983,-0.954,-0.712,-0.778,-1.309,-0.534,-1.448,-0.725,-0.978,-1.014,-0.917);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (41,-0.649,-0.58,-0.924,-0.412,-0.872,-0.5,-0.414,-0.609,-0.581,-0.946,-1.054);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (42,-0.756,-0.683,-1.117,-0.932,-0.854,-0.543,-0.748,-0.624,-0.5,-1.093,-0.283);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (43,-0.35,-0.556,-0.637,-0.584,-0.602,-0.275,-0.009,-0.576,-0.161,-0.848,-0.137);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (44,-0.7,-0.725,-1.322,-0.458,-0.563,-0.604,-1.213,-1.164,-0.955,-1.357,-0.888);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (45,-0.31,-0.12,-0.726,-0.343,0.199,-0.749,-0.74,-0.825,-0.944,-0.967,-0.006);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (46,-0.622,-0.666,-0.806,-0.756,-1.138,-0.947,-0.938,-1.363,-1.181,-1.506,-1.339);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (47,0.73,-0.282,-1.019,-0.001,0.057,-0.366,-0.774,-0.511,-1.127,-0.792,-0.368);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (48,-0.181,-0.145,-0.585,-0.052,-0.521,0.253,-1.157,-0.939,-0.388,-1.062,-0.547);
INSERT INTO PAL_BSTS_DATA_TBL VALUES (49,-0.144,-0.12,-0.496,-0.414,-1.088,-0.251,-0.27,-0.856,-1.313,-1.161,0.15);

DROP TABLE PAL_PARAMETER_TBL;
CREATE COLUMN TABLE PAL_PARAMETER_TBL (
	PARAM_NAME VARCHAR(100),
	INT_VALUE INTEGER,
	DOUBLE_VALUE DOUBLE,
	STRING_VALUE VARCHAR(100)
);

INSERT INTO PAL_PARAMETER_TBL VALUES ('BURN_IN', NULL, 0.6, NULL);
INSERT INTO PAL_PARAMETER_TBL VALUES ('EXPECTED_MODEL_SIZE', 2, NULL, NULL);
INSERT INTO PAL_PARAMETER_TBL VALUES ('SEASONAL_PERIOD', 12, NULL, NULL);
INSERT INTO PAL_PARAMETER_TBL VALUES ('MAX_ITER', 2000, NULL, NULL);
INSERT INTO PAL_PARAMETER_TBL VALUES ('RANDOM_SEED', 1, NULL, NULL);

DROP TABLE PAL_BSTS_STATS_TBL;
CREATE COLUMN TABLE PAL_BSTS_STATS_TBL ( 
	DATA_NAME 	   NVARCHAR(5000),
	INCLUSION_PROB DOUBLE,
	AVG_COEFF      DOUBLE
);

DROP TABLE PAL_BSTS_DECOMP_TBL;
CREATE COLUMN TABLE PAL_BSTS_DECOMP_TBL ( 
	ROW_INDEX  INTEGER,
	TREND      DOUBLE,
	SEASONAL   DOUBLE,
	REGRESSION DOUBLE,
	RESIDUAL   DOUBLE
);

DROP TABLE PAL_BSTS_MODEL_TBL; -- for predict followed
CREATE COLUMN TABLE PAL_BSTS_MODEL_TBL ( 
	ROW_INDEX INTEGER,
	MODEL_CONTENT NVARCHAR(5000)
);

DO BEGIN
	lt_data = SELECT * FROM PAL_BSTS_DATA_TBL;
	lt_param = SELECT * FROM PAL_PARAMETER_TBL;
	CALL _SYS_AFL.PAL_BSTS_TRAIN (:lt_data, :lt_param, lt_stats, lt_decomp, lt_model);
	INSERT INTO PAL_BSTS_STATS_TBL  SELECT * FROM :lt_stats;
	INSERT INTO PAL_BSTS_DECOMP_TBL SELECT * FROM :lt_decomp;
	INSERT INTO PAL_BSTS_MODEL_TBL  SELECT * FROM :lt_model;
END;

select * from PAL_BSTS_STATS_TBL;
select * from PAL_BSTS_DECOMP_TBL;
select * from PAL_BSTS_MODEL_TBL;
