HP趨勢(shì)濾波:
其中蝌衔,y是真實(shí)的時(shí)間序列榛泛,x是估計(jì)的時(shí)間序列,D矩陣表示如下:
L1趨勢(shì)濾波:
區(qū)別在于第二項(xiàng)胚委,從二范數(shù)改成了一范數(shù)挟鸠。
代碼實(shí)現(xiàn),利用cvxpy包:
solver = cvxpy.CVXOPT
reg_norm = 2
x = cvxpy.Variable(shape=n)
# x is the filtered trend that we initialize
objective = cvxpy.Minimize(0.5 * cvxpy.sum_squares(y-x)
+ lambda_value * cvxpy.norm(D@x, reg_norm))
# Note: D@x is syntax for matrix multiplication
problem = cvxpy.Problem(objective)
problem.solve(solver=solver, verbose=False)
對(duì)比可以發(fā)現(xiàn)亩冬,L1趨勢(shì)濾波更能保持原有數(shù)據(jù)的極值點(diǎn)艘希。
【參考資料】Introduction to Trend Filtering with Applications in Python