Fix the prior spend feature in a fraud model
Your task
An AI wrote a fraud model feature step that averages each account's amounts over its whole history, so a transaction's feature includes spending that happened after it. The holdout AUC of 0.99 was too good to be true. The fix uses only each account's earlier transactions.
An assistant wrote prior_account_mean_amount(account_ids, timestamps, amounts). It is in the editor. It has a bug. Find it and fix it.
Input and output:
account_ids,timestampsandamounts: one entry per transaction, at the same index. Timestamps are ISO dates such as2026-01-31, so plain string comparison puts them in time order.- Return one number per transaction, in input order: the mean amount of that account's earlier transactions, or -1 if it has none.
Rules the tests check:
- If the lists differ in length, use only the first n transactions, where n is the shortest length.
- Transaction j is earlier than transaction i when both are for the same account and either j's timestamp is smaller, or the timestamps are equal and j comes before i in the input.
- A transaction never counts toward its own value.
- Answers within 0.000001 of the expected values pass.
Examples
prior_account_mean_amount(account_ids: list[str], timestamps: list[str], amounts: list[float]) -> list[float]Example 1
Input
prior_account_mean_amount(["a", "a", "a"], ["2026-01-01", "2026-01-02", "2026-01-03"], [10, 20, 60])Output
[-1, 10, 15]Example 2
Input
prior_account_mean_amount(["a", "b", "a", "b"], ["2026-01-03", "2026-01-01", "2026-01-01", "2026-01-02"], [30, 5, 10, 7])Output
[10, -1, -1, 5]Example 3
Input
prior_account_mean_amount(["a", "a"], ["2026-01-01", "2026-01-01"], [4, 8])Output
[-1, 4]