Sub topic 10
Detecting structuring with aggregation windows
This chapter assumes you can read short Python snippets and know how cash deposits and reporting thresholds work. Structuring means splitting cash into amounts below a reporting threshold. Each deposit looks harmless on its own, so a rule has to add deposits up over time.
Why a single-transaction rule fails
A rule that alerts on deposits of 10,000 or more catches nothing here. Someone who deposits 9,500, then 9,200, then 9,800 never trips it. Detection therefore needs an aggregation window: a span of time over which you group an account's transactions and then test the group.
The working model
A structuring scenario combines three tests:
- Near-threshold amounts. Count deposits in a band just under the threshold, such as 8,000 up to but not including 10,000.
- A short window. Look at a few days, because structuring is usually done in quick succession.
- A combined total. Require the window's total to exceed the threshold. Without this test, three small deposits would alert even though the reporting duty would never have applied.
Running the rule
from collections import defaultdict
from datetime import date, timedelta
THRESHOLD = 10_000
FLOOR = 8_000
def flag_structuring(deposits, window_days=3, min_count=3):
"""deposits: iterable of (account, date, amount) cash deposits."""
by_account = defaultdict(list)
for account, day, amount in deposits:
by_account[account].append((day, amount))
alerts = []
for account, items in by_account.items():
items.sort()
for i, (start, _) in enumerate(items):
end = start + timedelta(days=window_days)
window = [amt for day, amt in items[i:] if day < end]
near = [amt for amt in window if FLOOR <= amt < THRESHOLD]
if len(near) >= min_count and sum(window) > THRESHOLD:
alerts.append((account, start, sum(window)))
break
return alerts
deposits = [
("A1", date(2026, 3, 2), 9_500),
("A1", date(2026, 3, 3), 9_200),
("A1", date(2026, 3, 4), 9_800),
("A2", date(2026, 3, 2), 9_900),
("A2", date(2026, 3, 10), 2_000),
("A3", date(2026, 3, 1), 8_500),
("A3", date(2026, 3, 8), 8_500),
("A3", date(2026, 3, 15), 8_500),
]
print(flag_structuring(deposits))The output is [('A1', datetime.date(2026, 3, 2), 28500)].
Account A1 alerts because three near-threshold deposits fall inside one three-day window and total 28,500. A2 has only one near-threshold deposit. A3 deposits the same amount each time but spreads the deposits over two weeks, so no three-day window holds more than one.
The code starts a new window at each deposit, which gives a sliding window. A fixed calendar window, such as one that resets every Monday, would miss a pattern that straddles the boundary. The break stops after the first alert for an account, so the alert shows when the pattern began.
Common errors
- Using
<=at the threshold. A deposit of exactly 10,000 is reportable on its own, so the near-threshold band must stop below it. - Checking one account at a time. A customer may split deposits across accounts or branches. Aggregate by customer where your data allows it.
- Choosing parameters without testing. A wider window, a lower floor, or a smaller
min_countproduces more alerts. Run the rule against historical cases and tune it. - Ignoring the customer. A cash-intensive shop may legitimately deposit 9,000 every day. Compare alerts with the customer's expected activity, a topic a later chapter covers.
Exercise
Account B7 deposits 9,000 on Monday, 9,000 on Thursday, and 9,000 the next Monday. With windowdays=3 and mincount=3, does flag_structuring alert? What single parameter change would make it alert, and what is the cost?
Show answer
It does not alert. Every window of three days holds only one deposit, so the near-threshold count never reaches 3. Raising window_days to 8 or more puts all three deposits in one window, with a total of 27,000. The cost is more alerts on ordinary customers, because a longer window catches more coincidental deposits, which means more analyst work and more false positives.