How would you implement a function to detect anomalies in a stream of incoming payment data?
Implement detect_anomalies(payments, window_size, z_threshold) for an ordered stream. Each payment is a dictionary with user_id, timestamp, and numeric amount; compare each payment only with that user's previous payments in the rolling window. Return zero-based indices where the amount differs from the previous mean by more than z_threshold * (maximum_amount - minimum_amount); with a zero range, any different amount is anomalous. Do not classify a payment until at least two prior payments for that user are available.
def detect_anomalies(payments, window_size, z_threshold):