HP趋势滤波:
其中,y是真实的时间序列,x是估计的时间序列,D矩阵表示如下:
L1趋势滤波:
区别在于第二项,从二范数改成了一范数。
代码实现,利用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)
HP趋势滤波
L1趋势滤波
对比可以发现,L1趋势滤波更能保持原有数据的极值点。
【参考资料】Introduction to Trend Filtering with Applications in Python
网友评论