Anomaly detection methods
2.5.13· AI-ML › Unsupervised Learning
Ek Point Anomalous Kyu Hota Hai?
YEH KYUN MATTER KARTA HAI: Real systems mein, anomalies rare hoti hain (<1% data) lekin high-impact hoti hain. Ek fraudulent transaction paise barbad karta hai; ek network breach security compromise karta hai. Humein automated methods chahiye kyunki human inspection scale nahi hoti.
Method 1: Statistical (Gaussian) Anomaly Detection
First Principles Se Derivation
KYA: Normal data ko multivariate Gaussian distribution follow karta hua model karo. Jo points bahut low probability density par hain, woh anomalies hain.
KYUN: Agar normal behavior ek mean ke around kuch spread ke saath cluster karta hai, toh is cluster se door ke points suspicious hain. Gaussian "typical" ko mean aur variance ke terms mein capture karta hai.
KAISE:
Step 1 - Distribution Fit Karo: Training data diya hua hai jahan :
Yeh step kyun? Mean squared loss ke under "normal behavior ke center" ka hamara best estimate hai.
Yeh step kyun? Covariance capture karta hai ki features ek saath kaise vary karte hain. Yeh hume normal data ki "shape" batata hai — kaunsi directions mein high/low variance hai.
Step 2 - Probability Density Compute Karo: Ek naye point ke liye, compute karo:
Yeh formula kyun?
- Term Mahalanobis distance hai: kitna door hai se, ke units mein measure kiya hua.
- Exponential distance badhne ke saath decay karta hai: door wale points → low probability.
- Normalization constant ensure karta hai ki .
Step 3 - Anomalies Flag Karo: Agar , toh ko anomaly flag karo.
Yeh step kyun? Hum threshold set karte hain desired false-positive rate ke basis par. Lower = stricter detection = kam false alarms lekin kuch anomalies miss ho sakti hain.
Equivalently, flag karo agar Mahalanobis distance jahan .
Naya observation:
Step 1: Mahalanobis distance compute karo:
Yeh step kyun? Hum covariance ko invert karte hain taaki distance ko data ke natural coordinate system mein measure kar sakein.
Step 2: compute karo:
Yeh step kyun? Probability astronomically small hai — yeh server bilkul normal jaisa behave nahi kar raha.
Step 3: Agar , toh yeh anomaly hai (possible CPU/memory leak).
Method 2: Isolation Forest
First Principles Se Derivation
KYA: Random decision trees banao jo points ko isolate karein. Anomalies ko isolate karna normal points se aasaan hota hai (kam splits chahiye).
KYUN: Normal points "crowd ke andar gehre" hote hain — unhe separate karne ke liye bahut saare questions chahiye. Anomalies "akele bahar" hote hain — ek ya do questions unhe isolate kar dete hain.
KAISE:
Step 1 - Isolation Trees Banao: Har tree ke liye:
- Randomly feature aur split value select karo, ke min/max ke beech mein
- Data partition karo: vs
- Recurse karo jab tak points isolated na ho jayein ya max depth na aa jaye
Random splits kyun? Hum classify nahi kar rahe — bas space partition kar rahe hain. Random splits "normal" structure mein overfitting se bachate hain aur naturally outliers ko jaldi isolate karte hain.
Step 2 - Path Length Compute Karo: Ek point ke liye, uska path length ek tree mein leaf tak pahunchne ke liye splits ki sankhya hai.
Yeh step kyun? Short path = isolate karna aasaan = likely anomaly. Normal points ko neighbors se separate karne ke liye zyada splits chahiye.
Step 3 - Trees Par Aggregate Karo: trees par average path length:
Anomaly score:
jahan points ke liye average path length hai (normalization ke liye).
Yeh formula kyun?
- ek Binary Search Tree mein unsuccessful search ki expected depth hai — yeh "normal" ke liye hamara baseline hai
- Hum se normalize karte hain taaki scores alag dataset sizes mein comparable hon
- Exponential [0,1] par map karta hai: 1 ke near scores = anomaly, 0 ke near = normal
Agar , typically anomaly flag karo.
Normal transaction: $45 dopahar 2 baje user ke sheher mein → average path length Kyun? Bahut saari similar transactions exist karti hain; neighbors se isolate karne ke liye ~7 splits lagte hain.
Anomalous transaction: $9,999 raat 3 baje foreign country mein → Kyun? Aise bahut kam transactions hain; ~2 splits mein isolate ho jata hai (shayad ek amount par, ek location par).
Scoring:
Decision: 0.92 > 0.6 threshold → fraud review ke liye flag karo.
Method 3: One-Class SVM
First Principles Se Derivation
KYA: Feature space mein ek hypersphere (ya hyperplane) dhundho jo normal data ko tightly enclose kare. Bahar ke points anomalies hain.
KYUN: Hum ek aisi decision boundary chahte hain jo normal data ko baki space se separate kare, margin maximize karke "normal" ko compactly capture kare.
KAISE:
Step 1 - Kernel Mapping: Data ko high-dimensional space mein map karo kernel ke zariye. Common choice: RBF kernel .
Yeh step kyun? High dimensions mein, woh data jo input space mein linearly separable nahi hai, woh aksar separable ho jaata hai. Kernel trick explicit computation se bachata hai.
Step 2 - Optimization Problem: Hyperplane dhundho jo data ko origin se maximum margin ke saath separate kare:
subject to:
Yeh objective kyun?
- : minimize karo (margin maximize karo)
- : hyperplane ko origin se door push karo (bada normal region)
- : training data mein kuch outliers ke liye slack allow karo
- : trade-off control karta hai (outliers ke fraction ka upper bound)
Step 3 - Decision Function: Naye point ke liye:
jahan dual problem solve karne se aaye Lagrange multipliers hain.
Yeh step kyun? matlab "origin side" par hai — normal region ke bahar, isliye anomaly.
jahan support vectors () boundary define karte hain.
Normal packet: size=512, duration=0.5s, protocol=HTTP → ✓ Kyun? Typical HTTP traffic ki seekhi hui boundary ke andar hai.
Suspicious packet: size=6535, duration=0.01s, protocol=ICMP → ✗ Kyun? Tiny duration ke saath max-size ICMP normal se bilkul alag hai; sabhi support vectors se kernel distance bada hai, ko negative push karta hai.
Method 4: Autoencoder-Based Detection
First Principles Se Derivation
KYA: Ek neural network train karo jo normal data ko compress karke phir reconstruct kare. Anomalies mein high reconstruction error hoti hai kyunki network ne unhe kabhi encode karna seekha hi nahi.
KYUN: Agar ek network normal data ka manifold seekh le, toh woh normal points ko faithfully reproduce kar sakta hai lekin anomalies ke saath struggle karta hai (woh off-manifold hote hain).
KAISE:
Step 1 - Architecture: Encoder: ko code mein map karta hai jahan Decoder: code se reconstruct karta hai
Bottleneck kyun? Low tak compression force karna ensure karta hai ki network essential structure seekhe, har point memorize na kare.
Step 2 - Training Objective: Normal data par reconstruction loss minimize karo:
Squared error kyun? Yeh Gaussian noise assumption ke under maximum likelihood estimate hai. Dusre losses (absolute, perceptual) bhi kaam karte hain.
Step 3 - Anomaly Detection: Test point ke liye, compute karo:
Agar (threshold jo validation set par set ki gayi ho), toh anomaly flag karo.
Yeh kyun kaam karta hai? Autoencoder ke weights encode karte hain. Jab anomaly dikhaya jata hai, decoder "nearest normal point hallucinate" karne ki koshish karta hai, jo large error deta hai.
Normal board: Input pixel patterns training se match karte hain → (low error, faithfully reconstruct hota hai) Kyun? Network ne solder joints, traces, components ke features seekhe. Woh unhe accurately regenerate kar sakta hai.
Defective board: Missing component, short circuit → (high error) Kyun? Defect out-of-distribution hai. Decoder wahan component "imagine" karne ki koshish karta hai (seekhe hue prior se), lekin actual pixels drastically different hain.
Decision: Agar , defect flag hoga (3400 > 500).
Methods Ki Comparison
| Method | Strengths | Weaknesses | Best For |
|---|---|---|---|
| Gaussian | Fast, interpretable, acha kaam karta hai agar data ~Gaussian ho | Gaussian assume karta hai, multi-modal ya skewed data ke saath break hota hai | Low-dimensional numerical data, jab explain-ability chahiye |
| Isolation Forest | Distribution assumption nahi, high-dim handle karta hai, fast | Interpret karna mushkil, tree count tuning chahiye | High-dimensional data, mixed feature types |