参考资料:时间序列中的异常

    image.png

    例子:

    1. import numpy as np
    2. import pandas as pd
    3. import matplotlib.pyplot as plt
    4. from scipy.signal import lfilter, unit_impulse
    5. def arma_process(ar, ma, size=200, scale=1., e=None):
    6. if e is None:
    7. e = np.random.normal(size=size)
    8. return lfilter(ma, ar, scale*e)
    9. # 考虑一个 ARMA(1, 1) 模型
    10. ar = [1, 0.85]
    11. ma = [1, 0.65]
    12. sample = arma_process(ar, ma)
    13. io = arma_process(ar, ma, e=10*unit_impulse(200, 100))
    14. ao = 10*unit_impulse(200, 100)
    15. tc = arma_process([1, -0.9], [1], e=10*unit_impulse(200, 100))
    16. ls = arma_process([1, -1], [1], e=10*unit_impulse(200, 100))
    17. x = range(200)
    18. plt.figure(figsize=(12, 12))
    19. plt.subplot(511)
    20. plt.plot(x, sample)
    21. plt.title('original signal')
    22. plt.subplot(523)
    23. plt.plot(x, io)
    24. plt.title('innovational outlier (IO)')
    25. plt.subplot(524)
    26. plt.plot(x, sample+io, label='with IO')
    27. plt.plot(x, sample, label='without IO')
    28. plt.title('signal with IO')
    29. plt.legend()
    30. plt.subplot(525)
    31. plt.plot(x, ao)
    32. plt.title('additive outlier (AO)')
    33. plt.subplot(526)
    34. plt.plot(x, sample+ao, label='with AO')
    35. plt.plot(x, sample, label='without AO')
    36. plt.title('signal with AO')
    37. plt.legend()
    38. plt.subplot(527)
    39. plt.plot(x, tc)
    40. plt.title('temporary change (TC)')
    41. plt.subplot(528)
    42. plt.plot(x, sample+tc, label='with TC')
    43. plt.plot(x, sample, label='without TC')
    44. plt.title('signal with TC')
    45. plt.legend()
    46. plt.subplot(529)
    47. plt.plot(x, ls)
    48. plt.title('level shift (LS)')
    49. plt.subplot(5,2,10)
    50. plt.plot(x, sample+ls, label='with LS')
    51. plt.plot(x, sample, label='without LS')
    52. plt.title('signal with LS')
    53. plt.legend()
    54. plt.tight_layout()
    55. plt.show()

    image.png
    image.png