[ML] Custom function in anomaly detection job

You should do this with a set of query aggregations in the datafeed of the ML job so that the sums and the ratio are calculated first, then fed to ML.

An older, but relevant example is here: Analyzing a ratio of documents over time with Anomaly Detection

In your case, you'd do two different sum aggregations, then do the ratio with a bucket_script aggregation