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.
Metadata
Release files for desolate 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| desolate-0.0.3.tar.gz | 8.7 kB | Without provenance | Details for desolate-0.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| desolate-0.0.3-py3-none-any.whl | Python 3 | none | any | Details for desolate-0.0.3-py3-none-any.whl |
Total release size: 18.5 kB
Release files / desolate-0.0.3.tar.gz
| Download URL | desolate-0.0.3.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
af2139c71fe032bdc7ab67d8f49bf1ffc1f6c375e5db8391f205bf78d57a4371
|
|
BLAKE2b-256 checksum How to use checksums |
a729dbedc8d708040964240b7f4ef848341c44f811735c9f0211d3c31e373c2f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.8
|
Release files / desolate-0.0.3-py3-none-any.whl
| Download URL | desolate-0.0.3-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
56813224b0a611dad8d810f36b794a396c1a39354e3236fa3b9d0fd13ae04b6e
|
|
BLAKE2b-256 checksum How to use checksums |
7f1f6a095e02afc9b8f78bf956becee85caacd24ec246551c65f066e4c7892cf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.8
|
Security for desolate 0.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.