Change-point detection methods are used to identify abrupt changes in time-series data, such as changes in mean, variance, or distribution. One common approach is to minimize a cost function to determine the number and location of changepoints. However, directly minimizing the log-likelihood function can lead to over-fitting. To address this, penalty terms or constraints can be introduced to control the number of changepoints. 

The initial approach, Optimal Partitioning (Opt), uses dynamic programming to recursively solve sub-problems and minimize the cost function. A linear penalty term is used to calculate the penalized cost. A pruning method is applied to speed up the computation by discarding potential changepoints based on a certain condition. 

Choosing a suitable penalty value is a challenge in practice. The Adapted Pelt (AdpPelt) method is introduced to automatically optimize the penalty function based on the relationship between the total loss and the number of changepoints. 

The PrunedDP algorithm minimizes the cost function by considering a parameter that is affected by the changepoints. The algorithm works on a set of splitting points to determine the best partition in different regions. Points that do not contribute to the optimal solution are removed. The computational complexity of PrunedDP is O(KNlog(N)), where K is the maximum number of regions and N is the length of the time series.
------

SET SCHEMA DM_PAL;

DROP TABLE PAL_CPDETECTION_DATA_TBL;
CREATE COLUMN TABLE PAL_CPDETECTION_DATA_TBL (
	"TIME_STAMP" VARCHAR(10),
	"SERIES" DOUBLE
);


INSERT INTO PAL_CPDETECTION_DATA_TBL VALUES ('1-1', -5.36);
INSERT INTO PAL_CPDETECTION_DATA_TBL VALUES ('1-2', -5.14);
INSERT INTO PAL_CPDETECTION_DATA_TBL VALUES ('1-3', -4.94);
INSERT INTO PAL_CPDETECTION_DATA_TBL VALUES ('2-1', -5.15);
INSERT INTO PAL_CPDETECTION_DATA_TBL VALUES ('2-2', -4.95);
INSERT INTO PAL_CPDETECTION_DATA_TBL VALUES ('2-3', 0.55);
INSERT INTO PAL_CPDETECTION_DATA_TBL VALUES ('2-4', 0.88);
INSERT INTO PAL_CPDETECTION_DATA_TBL VALUES ('3-1', 0.95);
INSERT INTO PAL_CPDETECTION_DATA_TBL VALUES ('3-2', 0.68);
INSERT INTO PAL_CPDETECTION_DATA_TBL VALUES ('3-3', 0.86);


DROP TABLE #PAL_PARAMETER_TBL;
CREATE LOCAL TEMPORARY 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 ('SOLVER', NULL, NULL, 'Pelt');
INSERT INTO #PAL_PARAMETER_TBL VALUES ('COSTFUNCTION', NULL, NULL, 'Normal_MSE');
INSERT INTO #PAL_PARAMETER_TBL VALUES ('PENALTY', NULL, NULL, 'AIC');
INSERT INTO #PAL_PARAMETER_TBL VALUES ('PENALISATION_FACTOR', NULL, 0.02, NULL);

CALL _SYS_AFL.PAL_CPDETECTION(PAL_CPDETECTION_DATA_TBL,"#PAL_PARAMETER_TBL", ?, ?);

