我想为这个线程贡献一个我自己开发的算法:
它基于分散原理:如果一个新的数据点与某个移动均值相差给定 x 个标准差,则算法发出信号(也称为z-score)。该算法非常稳健,因为它构造了单独的移动均值和偏差,因此信号不会破坏阈值。因此,无论先前信号的数量如何,都以大致相同的精度识别未来信号。该算法需要 3 个输入lag = the lag of the moving window
:threshold = the z-score at which the algorithm signals
和influence = the influence (between 0 and 1) of new signals on the mean and standard deviation
。例如,a lag
of 5 将使用最后 5 个观测值来平滑数据。threshold
如果数据点与移动平均值相差 3.5 个标准差,则A值为 3.5。而influence
0.5 的 an 给出了一半的信号正常数据点的影响。同样,influence
为 0 的 an 完全忽略重新计算新阈值的信号:因此 0 的影响是最稳健的选择。
它的工作原理如下:
伪代码
# Let y be a vector of timeseries data of at least length lag+2
# Let mean() be a function that calculates the mean
# Let std() be a function that calculates the standard deviaton
# Let absolute() be the absolute value function
# Settings (the ones below are examples: choose what is best for your data)
set lag to 5; # lag 5 for the smoothing functions
set threshold to 3.5; # 3.5 standard deviations for signal
set influence to 0.5; # between 0 and 1, where 1 is normal influence, 0.5 is half
# Initialise variables
set signals to vector 0,...,0 of length of y; # Initialise signal results
set filteredY to y(1,...,lag) # Initialise filtered series
set avgFilter to null; # Initialise average filter
set stdFilter to null; # Initialise std. filter
set avgFilter(lag) to mean(y(1,...,lag)); # Initialise first value
set stdFilter(lag) to std(y(1,...,lag)); # Initialise first value
for i=lag+1,...,t do
if absolute(y(i) - avgFilter(i-1)) > threshold*stdFilter(i-1) then
if y(i) > avgFilter(i-1)
set signals(i) to +1; # Positive signal
else
set signals(i) to -1; # Negative signal
end
# Adjust the filters
set filteredY(i) to influence*y(i) + (1-influence)*filteredY(i-1);
set avgFilter(i) to mean(filteredY(i-lag,i),lag);
set stdFilter(i) to std(filteredY(i-lag,i),lag);
else
set signals(i) to 0; # No signal
# Adjust the filters
set filteredY(i) to y(i);
set avgFilter(i) to mean(filteredY(i-lag,i),lag);
set stdFilter(i) to std(filteredY(i-lag,i),lag);
end
end
演示