Detecting Anomalies with Censors
Project description
desolate
Desolation: (De)tect anomalies with I(solation) forests and survival analysis
A lightweight, dependency-free library combining isolation forests with survival analysis for anomaly detection in time-to-event data.
Installation
pip install desolate
Quick Start
from desolate import DesolateForest
import numpy as np
# Create synthetic data
n_samples = 1000
features = np.random.normal(size=(n_samples, 5))
times = np.random.exponential(50, size=n_samples)
censoring = np.random.exponential(30, size=n_samples)
observed = np.minimum(times, censoring)
events = (times <= censoring).astype(int)
# Fit model
model = DesolateForest(contamination=0.1)
model.fit(observed, events, features)
# Get predictions
predictions = model.predict(observed, events, features)
scores = model.score_samples(observed, events, features)
Mathematical Foundation
Core Concept
Desolate combines isolation forests with Kaplan-Meier survival analysis by augmenting the feature space with survival information:
$\mathbf{X}_{aug} = [\mathbf{X} | \hat{S}(t) | t | \delta]$
where:
- $\mathbf{X}$ is the original feature matrix
- $\hat{S}(t)$ is the Kaplan-Meier survival probability
- $t$ is the observed time
- $\delta$ is the event indicator
Kaplan-Meier Estimator
The survival function estimate is:
$\hat{S}(t) = \prod_{t_i \leq t} \left[1 - \frac{d_i}{n_i}\right]$
where:
- $t_i$ are the distinct event times
- $d_i$ is the number of events at time $t_i$
- $n_i$ is the number of subjects at risk at time $t_i$
Isolation Forest
The anomaly score for a point $x$ is:
$s(x) = 2^{-\frac{E[h(x)]}{c(n)}}$
where:
- $h(x)$ is the path length for point $x$
- $E[h(x)]$ is the average path length across trees
- $c(n) = 2H(n-1) - \frac{2(n-1)}{n}$, where $H(i)$ is the harmonic number
- $n$ is the number of samples
Properties
-
Censoring-Aware Detection:
$P(\text{anomaly}|x, t, \delta) = P(\text{anomaly}|x, t, \delta, \hat{S}(t))$
-
Temporal Consistency:
If $t_1 < t_2$ and $\hat{S}(t_1) > \hat{S}(t_2)$:
$s([x|\hat{S}(t_1)|t_1|\delta]) \leq s([x|\hat{S}(t_2)|t_2|\delta])$
-
Feature-Survival Interaction:
$s([x|\hat{S}(t)|t|\delta]) \neq s(x) + s([\hat{S}(t)|t|\delta])$
Key Features
- Minimal Dependencies: Only requires numpy
- Efficient: O(log n) average case complexity
- Flexible: Works with any feature type
- Interpretable: Decomposable anomaly scores
Benchmark Datasets
from desolate.datasets import DatasetLoader
# Load built-in benchmark dataset
loader = DatasetLoader()
features, durations, events = loader.load_dataset("turbofan")
# Available datasets:
# - turbofan: NASA Turbofan Engine Degradation
# - gbsg2: German Breast Cancer Study
# - bearing: IMS Bearing Dataset
# - valve: Industrial Control Valve
# - support: Study to Understand Prognoses
# - pbc: Primary Biliary Cirrhosis
# - semiconductor: SECOM Manufacturing
# - software: Software Project Survival
Advanced Usage
Custom Preprocessing
from desolate.preprocessing import Preprocessor
# Apply dataset-specific preprocessing
preprocessor = Preprocessor()
features_proc, durations_proc, events_proc = preprocessor.preprocess_turbofan(
features, durations, events
)
Anomaly Injection
from desolate.anomalies import LocalOutlierInjector
# Inject synthetic anomalies
injector = LocalOutlierInjector()
features_anom, durations_anom, events_anom = injector.inject(
features, durations, events,
contamination=0.1
)
Theoretical Details
Path Length Analysis
The expected path length in augmented space:
$E[h(x_{aug})] = E[h(x)] + E[h(\hat{S}(t))] + \text{interaction_term}$
This decomposition shows that the model captures:
- Standard feature anomalies
- Survival pattern anomalies
- Joint anomalies in both spaces
Asymptotic Properties
Under regularity conditions:
-
Consistency of Anomaly Detection:
As $n \to \infty$: $P(|s(x_{aug}) - s^*(x_{aug})| > \epsilon) \to 0$
-
Consistency of Survival Estimation:
As $n \to \infty$: $\sup|\hat{S}(t) - S(t)| \to 0$ in probability
Contributing
Contributions welcome! Please read our Contributing Guide.
License
MIT License - see LICENSE for details.
Citation
If you use Desolate in your research, please cite:
@software{desolate2024,
title = {Desolate: Anomaly Detection with Isolation Forests and Survival Analysis},
author = {Your Name},
year = {2024},
url = {https://github.com/yourusername/desolate}
}
References
- Liu, F. T., Ting, K. M., & Zhou, Z. H. (2008). Isolation forest. In 2008 Eighth IEEE International Conference on Data Mining
- Kaplan, E. L., & Meier, P. (1958). Nonparametric estimation from incomplete observations. Journal of the American Statistical Association
TODO: Recommendations for Enhancement:
Incorporate Isolation Forest Scores into Survival Analysis (Approach B):
Current Status: You've added survival probabilities to the Isolation Forest's feature space.
Enhancement: Consider the reverse: use the anomaly scores from the Isolation Forest as features in a survival analysis model. This bidirectional integration can provide deeper insights into how anomalies influence survival outcomes.
TODO: Implement Tree-Based Isolation in Survival Data (Approach C):
Concept: Develop a tree-based model that isolates subpopulations within your survival data, focusing on identifying groups with distinct survival characteristics.
Implementation: Adapt the Isolation Forest algorithm to account for censored data, creating splits that maximize differences in survival distributions between branches.
TODO: Outlier Filtering in Survival Analysis (Approach A):
Current Status: It's unclear if outlier filtering is applied before survival analysis in your workflow.
Enhancement: Use the Isolation Forest to detect and remove outliers prior to conducting survival analysis. This preprocessing step can lead to more accurate survival models by mitigating the influence of anomalous data points.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file desolate-0.0.3.tar.gz.
File metadata
- Download URL: desolate-0.0.3.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af2139c71fe032bdc7ab67d8f49bf1ffc1f6c375e5db8391f205bf78d57a4371
|
|
| MD5 |
a0452b796a71c2999fd202035c72aeb6
|
|
| BLAKE2b-256 |
a729dbedc8d708040964240b7f4ef848341c44f811735c9f0211d3c31e373c2f
|
File details
Details for the file desolate-0.0.3-py3-none-any.whl.
File metadata
- Download URL: desolate-0.0.3-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56813224b0a611dad8d810f36b794a396c1a39354e3236fa3b9d0fd13ae04b6e
|
|
| MD5 |
28f2940d4c948511b9bda491f5c522e3
|
|
| BLAKE2b-256 |
7f1f6a095e02afc9b8f78bf956becee85caacd24ec246551c65f066e4c7892cf
|