Task Documentation
Task Documentation
Smoothing (DES)
Shikhar Sharma
October 15, 2024
1 Introduction
This document provides an explanation and the LaTeX representation of the implementation of real-
time anomaly detection using Double Exponential Smoothing (DES). The system detects anomalies in a
simulated data stream by utilizing DES to reduce lag in trend detection and allows for faster identification
of anomalies compared to Simple Moving Averages (SMA).
2 Task Overview
The task is to implement an anomaly detection system for a data stream with the following features:
• A simulated data stream containing a sine wave with periodicity, random noise, and occasional
anomalies.
• Double Exponential Smoothing (DES) is used to smooth the data and reduce lag in trend detection.
• A real-time plotting mechanism to visualize the data stream, the smoothed values (using DES),
and anomalies detected based on a z-score threshold.
• Beta (β): The smoothing factor for the trend (direction of the data’s movement).
The DES method helps reduce the lag seen with other smoothing methods like SMA and is more respon-
sive to sudden changes.
4 Code Implementation
The following sections outline the Python implementation of the task.
1
7
8 def next value ( s e l f ) :
9 ”””
10 Produces t h e n e x t v a l u e i n t h e s i g n a l with p e r i o d i c b e h a v i o r , n o i s e ,
11 and i n f r e q u e n t a n o m a l i e s .
12 ”””
13 # G e n e r a t e a p e r i o d i c p a t t e r n with added n o i s e
14 p e r i o d i c v a l u e = 50 ∗ ( s e l f . s t e p % 3 6 5 ) / 3 6 5 . 0
15 r a n d o m n o i s e = random . g a u s s ( 0 , 1 )
16
17 # I n t r o d u c e o c c a s i o n a l a n o m a l i e s (2% c ha nc e )
18 anomaly = random . g a u s s ( 1 5 , 5 ) i f random . random ( ) < 0 . 0 2 e l s e 0
19 s e l f . s t e p += 1
20
21 r e t u r n 100 + p e r i o d i c v a l u e + r a n d o m n o i s e + anomaly
Listing 1: Signal Stream Generator
2
46 else :
47 z score = 0 # No anomaly d e t e c t i o n f o r a s i n g l e v a l u e
48
49 # D e t e c t a n o m a l i e s i f o u t s i d e t h e t h r e s h o l d and cooldown i s i n a c t i v e
50 i f s e l f . cooldown counter > 0:
51 s e l f . c o o l d o w n c o u n t e r −= 1
52 return smoothed value , False
53 else :
54 i f abs ( z s c o r e ) > s e l f . t h r e s h o l d :
55 s e l f . cooldown counter = s e l f . cooldown duration
56 r e t u r n s m o o t h e d v a l u e , True
57 return smoothed value , False
Listing 2: DES Anomaly Detector
3
4.4 AnomalyDetectionRunner Class
This class orchestrates the signal generation, anomaly detection, and real-time plotting by integrating
the classes defined above.
1 # Main c o n t r o l l e r c l a s s f o r r u n n i n g t h e s i g n a l g e n e r a t i o n , anomaly d e t e c t i o n , and
visualization
2 c l a s s AnomalyDetectionRunner :
3 def init ( s e l f , s i g n a l s t r e a m , a n o m a l y d e t e c t o r , v i s u a l i z e r , t o t a l s t e p s =1000) :
4 ”””
5 I n i t i a l i z e s t h e system with :
6 − s i g n a l s t r e a m : Instance of the SignalStream .
7 − a n o m a l y d e t e c t o r : I n s t a n c e o f t h e DESAnomalyDetector .
8 − v i s u a l i z e r : I n s t a n c e o f t h e RealTimeGraph .
9 − t o t a l s t e p s : Number o f p o i n t s t o v i s u a l i z e .
10 ”””
11 s e l f . signal stream = signal stream
12 s e l f . anomaly detector = anomaly detector
13 self . visualizer = visualizer
14 self . total steps = total steps
15
16 def start ( s e l f ) :
17 ”””
18 S t a r t s t h e r e a l −time anomaly d e t e c t i o n and v i s u a l i z a t i o n l o o p .
19 ”””
20 p r i n t ( ” S t a r t i n g r e a l −time d e t e c t i o n . . . ” )
21
22 f o r i in range ( s e l f . t o t a l s t e p s ) :
23 signal = s e l f . signal stream . next value ()
24 smooth , i s o u t l i e r = s e l f . a n o m a l y d e t e c t o r . d e t e c t a n o m a l y ( s i g n a l )
25 s e l f . v i s u a l i z e r . u p d a t e g r a p h ( i , s i g n a l , smooth , i s o u t l i e r )
26
27 p r i n t ( ” Detection complete . ” )
Listing 4: Anomaly Detection Runner
6 Conclusion
This document outlines a real-time anomaly detection system that uses Double Exponential Smoothing
to efficiently smooth a data stream while minimizing lag. The system can detect anomalies and plot the
results in real time.