Sub topic 5
Scenarios: turning a typology into a rule
This chapter assumes you know what structuring and other typologies are, you can read basic Python, and you have seen the data inputs from the earlier chapters: transactions, customer profiles and risk ratings.
From red flag to scenario
A typology describes a pattern of laundering behavior in words. A scenario is the executable version of that description. It names the transactions to look at, the condition that counts as suspicious, and the parameters that control how sensitive the condition is.
Take structuring, which means splitting cash deposits into amounts just under a reporting limit to avoid attention. The red flag reads: "several cash deposits just below the limit in a short period." That sentence has vague words in it. Turning it into a scenario means replacing each vague word with something a program can check.
| Vague wording | Concrete scenario element |
|---|---|
| "cash deposits" | Filter: transaction type equals `cash_deposit` |
| "just below the limit" | Amount range: `floor` up to, but not including, `ceiling` |
| "a short period" | Rolling window of `lookback_days` |
| "several" | Count of at least `min_count` |
The last three rows become parameters. The first row is fixed logic. Keeping parameters out of the code lets analysts adjust sensitivity per customer segment without rewriting the rule.
The anatomy of a rule
Most scenarios share four parts.
- Population filter. Which transactions and customers are in scope.
- Grouping key. What the rule aggregates by, usually the customer or the account.
- Condition. The test applied to each group, such as a count, a sum or a ratio.
- Output. The alert record created when the condition is true.
A structuring scenario in code
The function below implements all four parts. The filter keeps only cash deposits inside the amount band. The grouping key is customer_id. The condition slides a window across each customer's sorted deposits and checks the count.
from collections import defaultdict
from datetime import timedelta
def structuring_alerts(transactions, params):
"""Alert when a customer makes several cash deposits just under a limit
inside a rolling window."""
window = timedelta(days=params["lookback_days"])
by_customer = defaultdict(list)
for t in transactions:
if t["type"] == "cash_deposit" and params["floor"] <= t["amount"] < params["ceiling"]:
by_customer[t["customer_id"]].append(t)
alerts = []
for customer_id, txns in by_customer.items():
txns.sort(key=lambda t: t["timestamp"])
start = 0
for end, current in enumerate(txns):
while current["timestamp"] - txns[start]["timestamp"] > window:
start += 1
hits = txns[start:end + 1]
if len(hits) >= params["min_count"]:
alerts.append({
"customer_id": customer_id,
"scenario": "structuring_cash",
"window_start": hits[0]["timestamp"],
"window_end": hits[-1]["timestamp"],
"txn_count": len(hits),
"total_amount": sum(t["amount"] for t in hits),
})
break # one alert per customer per run
return alertsThe start index moves forward whenever the oldest deposit falls outside the window, so each check looks only at deposits within lookback_days of the current one. The break limits output to one alert per customer per run.
Running it
Pass in the retail segment's parameters and a few transactions.
from datetime import datetime
PARAMS = {
"retail": {"lookback_days": 7, "min_count": 3, "floor": 8000, "ceiling": 10000},
}
transactions = [
{"customer_id": "C1", "type": "cash_deposit", "amount": 9500, "timestamp": datetime(2026, 3, 2, 10, 0)},
{"customer_id": "C1", "type": "cash_deposit", "amount": 9200, "timestamp": datetime(2026, 3, 3, 15, 30)},
{"customer_id": "C1", "type": "cash_deposit", "amount": 9800, "timestamp": datetime(2026, 3, 5, 9, 15)},
{"customer_id": "C2", "type": "cash_deposit", "amount": 9500, "timestamp": datetime(2026, 3, 2, 11, 0)},
]
alerts = structuring_alerts(transactions, PARAMS["retail"])
for a in alerts:
print(a["customer_id"], a["txn_count"], a["total_amount"])
# C1 3 28500C1 has three qualifying deposits in three days, so the count reaches min_count and an alert is created. C2 has one, so nothing fires.
Common errors
- Boundary mistakes. The code uses
floor <= amount < ceiling. A deposit of exactly 10,000 is excluded, and one of exactly 8,000 is included. Decide these edges on purpose and document them, because they change which customers alert. - Hard-coded values. Putting 3 or 9500 inside the function forces a code change for every tuning decision.
- Wrong grouping key. Grouping by account misses a customer who spreads deposits across several accounts. Grouping by customer catches it.
- Unsorted data. The sliding window only works on time-ordered transactions. The sort step is required.
- Gaps in the typology. A rule that checks only cash deposits will not see the same behavior carried out through several branches by different people or by another transaction type. A scenario covers one expression of a typology, so institutions run several scenarios per typology.
Exercise
A scenario has the parameters lookbackdays=7, mincount=3, floor=8000 and ceiling=10000. A customer makes cash deposits of 9,900 on day 1, 9,900 on day 5 and 9,900 on day 9. Does the structuring_alerts function create an alert? Explain.
Show answer
No. Day 1 and day 9 are 8 days apart, which is more than the 7-day window, so when the day 9 deposit is checked the day 1 deposit is dropped from the window. At most two deposits (day 5 and day 9) fall in any window, which is below min_count of 3. Day 1 and day 5 are also only two. No alert is created.