anomaly detection isolation forest unsupervised outlier detection

# Anomaly Detection: Isolation Forest & Unsupervised Outlier Detection

## Introduction & Motivation

Isolation Forest isolates anomalies via random trees—anomalies isolated in few splits vs. normal points requiring many splits. Efficient; scalable to high dimensions; no distance metric required. Contrast with density/distance-based methods which struggle in high-D (curse of dimensionality).

Motivation: Most points are normal; anomalies rare. Random isolation exploits rarity: anomalies separated quickly. Probabilistic anomaly score; no labeling required.

Applications: Fraud detection, network intrusion, manufacturing defects, sensor faults, outlier detection in ML pipelines.

---

## Core Concepts & Theory

### Isolation Path Length

Anomaly score: path length in isolation tree. Short path → anomaly, long path → normal. Normalized by average path length.

### Ensemble Approach

Multiple random trees aggregate predictions; reduce variance.

---

## Mathematical Formulation

Anomaly score:
$$s(\mathbf{x}, T) = 2^{-\frac{E[h(\mathbf{x})]}{c(n)}}$$

where h(\mathbf{x}) is path length, c(n) is normalization constant, E[\cdot] averages over ensemble.

s close to 1 → anomaly, close to 0 → normal.

---

## Advanced Theory & Extensions

### Extended Isolation Forest

Non-axis-aligned splits; improved detection for complex patterns.

### Streaming Anomaly Detection

Online variant for real-time detection.

---

## Computational Considerations

Training: O(t imes n \log n) for t trees, n samples.

Inference: O(t imes \log n) per sample.

Memory: O(t imes n) worst-case; O(t imes s) average (s = avg tree size).

---

## Practical Implementation Strategies

### Anomaly Threshold

No ground truth; threshold selection heuristic (e.g., top 5% anomalies).

### Hyperparameters

n_estimators: More trees → more stable (typically 100-200).

max_samples: Subsample size; typically √n or 256.

### Evaluation Metrics

ROC-AUC (if labels available), precision-recall, or domain-specific metrics.

---

## Benchmark Datasets & Evaluation

Synthetic: Point clouds with known outliers.

Real: Credit card fraud, network intrusion datasets (KDD99).

---

## Key Challenges & Limitations

### Threshold Selection

No principled threshold without labels; requires domain knowledge or heuristics.

### High-Dimensional Data

Curse of dimensionality; uniform random splits become ineffective. Feature selection recommended.

---

## Hyperparameter Tuning

n_estimators \in {50, 100, 200\}, max_samples \in {auto, 256, 512\}, contamination \in {0.01, 0.05, 0.1\}.

---

## Real-World Applications & Case Studies

Finance: Fraud detection via anomalous transaction patterns.

Manufacturing: Defect detection via sensor data anomalies.

---

## Integration with Other Methods

Isolation Forest + Feature Selection → Improved detection in high-D.

Isolation Forest + Ensemble → Multiple anomaly detectors combined.

---

## Future Research Directions

Explainable anomaly scores; online learning; multimodal anomaly detection.

---

## Summary & Key Takeaways

Isolation Forest detects anomalies via random partitioning, efficiently isolating rare points without explicit distance computation or density modeling.

Principles:
1. Anomalies isolated in few splits.
2. Ensemble reduces variance.
3. No distance metric required; scalable to high-D.
4. Anomaly score based on path length.
5. Threshold selection critical.

---

---

## Appendix: Practical Labs

### Lab 1: Isolation Forest Basics

from sklearn.ensemble import IsolationForest
from sklearn.datasets import make_classification
import numpy as np

X, y = make_classification(n_samples=100, n_features=10, n_informative=5, random_state=42)

# Add outliers
outliers = np.random.uniform(-4, 4, (10, 10))
X = np.vstack([X, outliers])
y = np.hstack([y, [-1] * 10]) # -1 for outliers

iso_forest = IsolationForest(n_estimators=100, contamination=0.1, random_state=42)
predictions = iso_forest.fit_predict(X)

n_anomalies = np.sum(predictions == -1)
print(f"Anomalies detected: {n_anomalies}")

assert n_anomalies >= 5, "Should detect anomalies"
print("✓ Isolation Forest working")

if __name__ == "__main__":
 print("Lab 1: Isolation Forest - PASSED")

### Lab 2: Anomaly Score

from sklearn.ensemble import IsolationForest
from sklearn.datasets import make_blobs
import numpy as np

X, _ = make_blobs(n_samples=100, n_features=2, centers=1, random_state=42)

# Add outliers
outliers = np.array([[10, 10], [-10, -10], [15, -15]])
X = np.vstack([X, outliers])

iso_forest = IsolationForest(n_estimators=100, random_state=42)
iso_forest.fit(X)

scores = iso_forest.score_samples(X)

# Outliers should have lower (more negative) scores
outlier_scores = scores[-3:]
normal_scores = scores[:-3]

print(f"Outlier scores: {outlier_scores.mean():.4f}")
print(f"Normal scores: {normal_scores.mean():.4f}")

assert outlier_scores.mean() < normal_scores.mean(), "Outliers should score lower"
print("✓ Anomaly score working")

if __name__ == "__main__":
 print("Lab 2: Anomaly Score - PASSED")

### Lab 3: Contamination Parameter

from sklearn.ensemble import IsolationForest
from sklearn.datasets import make_blobs
import numpy as np

X, _ = make_blobs(n_samples=100, n_features=2, centers=1, random_state=42)
outliers = np.random.uniform(-3, 3, (5, 2))
X = np.vstack([X, outliers])

contaminations = [0.01, 0.05, 0.1, 0.2]
for cont in contaminations:
 iso_forest = IsolationForest(contamination=cont, random_state=42)
 predictions = iso_forest.fit_predict(X)
 n_anomalies = np.sum(predictions == -1)
 print(f"Contamination={cont}: {n_anomalies} anomalies")

print("✓ Contamination parameter working")

if __name__ == "__main__":
 print("Lab 3: Contamination - PASSED")

### Lab 4: High-Dimensional Detection

from sklearn.ensemble import IsolationForest
import numpy as np

# Normal data
np.random.seed(42)
X_normal = np.random.randn(100, 50)

# High-dimensional outliers
outliers = np.random.uniform(-5, 5, (10, 50))
X = np.vstack([X_normal, outliers])

iso_forest = IsolationForest(n_estimators=100, contamination=0.1, random_state=42)
predictions = iso_forest.fit_predict(X)

n_anomalies = np.sum(predictions == -1)
print(f"High-D: {n_anomalies} anomalies in {X.shape[1]} dimensions")

assert n_anomalies >= 5, "Should detect anomalies in high-D"
print("✓ High-dimensional detection working")

if __name__ == "__main__":
 print("Lab 4: High-Dimensional - PASSED")

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account