Anomaly detection algorithm