Sub topic 4
Segmenting customers so thresholds make sense
This chapter assumes you know what a scenario and a threshold are, because the previous chapters covered them. It also assumes you can read basic Python. By the end you should be able to explain how a customer is placed in a segment, how the segment selects the parameters a rule runs with, and which mistakes make segmentation quietly break detection.
Why one threshold for everyone fails
Take a rule that alerts when a customer deposits $15,000 or more in cash in a month. For a salaried employee, that is unusual. For a convenience store that banks its daily takings, it is a normal week.
If you apply the same threshold to both, you get two kinds of error:
- Too many alerts on high-activity customers. The store alerts every month. Analysts review the same harmless pattern repeatedly and learn to close it quickly, which makes them less likely to notice a real problem.
- Missed activity on low-activity customers. If you raise the threshold to stop the store alerting, a retail customer can move large cash amounts without ever reaching it.
Segmentation addresses this. It groups customers whose normal behavior is similar, then lets each group have its own thresholds. The scenario logic stays the same. Only the parameters change.
What a segment is
A segment is a group of customers that a monitoring program expects to behave in broadly the same way. Each customer belongs to exactly one segment at a time for a given scenario. The segment name is a key. The system uses that key to look up the parameter set a rule should run with.
Common attributes used to define segments:
| Attribute | Example values | What it separates |
|---|---|---|
| Customer type | Individual, sole trader, company, trust | Personal activity from business activity |
| Business activity | Restaurant, money services business, law firm | Cash-heavy trades from others |
| Size | Annual turnover bands, salary bands | Small from large expected volumes |
| Product | Current account, credit card, trade finance | Which transaction types occur at all |
| Geography | Domestic, cross-border operations | Expected payment corridors |
Programs usually combine two or three attributes, such as "business, cash-intensive, turnover under $1 million". More attributes give more precise thresholds but create many small segments. A small segment has too few customers to set thresholds with confidence, and each one must be maintained.
Segments, risk ratings and peer groups
These three ideas are related but not the same.
- A segment answers "what is normal for customers like this?" It controls thresholds.
- A risk rating answers "how exposed are we to this customer?" It is based on factors such as country, occupation and product, and it often changes how closely the customer is watched. A program may apply lower thresholds to high-risk customers inside the same segment.
- A peer group is the set of customers a given customer is compared against. Often the peer group is the segment, but it can be narrower.
Peer groups matter for rules that are relative rather than absolute. Instead of "alert above $15,000", a relative rule says "alert when this customer's cash total is at least 10 times the peer median." The threshold then adapts as the segment's behavior changes.
From profile to parameters
Segmentation sits between the data inputs and the rule. The sequence at evaluation time is:
- Read the customer's profile attributes.
- Apply the segmentation logic to get a segment key.
- Look up the scenario parameters for that key.
- Run the rule with those parameters.
The fallback branch matters. Profile data is never complete, and the system needs a defined behavior when it is missing.
A worked example
The code below assigns segments and applies segment-specific thresholds to a monthly cash volume scenario. It uses a conservative threshold for customers who could not be segmented.
SEGMENT_THRESHOLDS = {
"retail": 15000,
"small_business": 60000,
"corporate": 250000,
"unassigned": 15000,
}
DEFAULT_SEGMENT = "unassigned"
def assign_segment(customer):
"""Return a segment key from profile attributes."""
if customer["kind"] == "individual":
return "retail"
if customer["kind"] == "business":
turnover = customer.get("annual_turnover")
if turnover is None:
return DEFAULT_SEGMENT
if turnover < 1_000_000:
return "small_business"
return "corporate"
return DEFAULT_SEGMENT
def monthly_cash_alerts(customers, cash_totals):
"""Alert when monthly cash deposits reach the segment's threshold."""
alerts = []
for customer in customers:
segment = assign_segment(customer)
threshold = SEGMENT_THRESHOLDS[segment]
total = cash_totals.get(customer["customer_id"], 0)
if total >= threshold:
alerts.append({
"customer_id": customer["customer_id"],
"segment": segment,
"total_amount": total,
"threshold": threshold,
})
return alerts
customers = [
{"customer_id": "C1", "kind": "individual"},
{"customer_id": "C2", "kind": "business", "annual_turnover": 400000},
{"customer_id": "C3", "kind": "business", "annual_turnover": None},
]
cash_totals = {"C1": 22000, "C2": 22000, "C3": 22000}
for a in monthly_cash_alerts(customers, cash_totals):
print(a["customer_id"], a["segment"], a["total_amount"], a["threshold"])
# C1 retail 22000 15000
# C3 unassigned 22000 15000All three customers deposited $22,000. C1 and C3 alert, and C2 does not, because a small business has a threshold of $60,000. C3 is a business whose turnover is missing, so it falls to the unassigned segment and gets the strict threshold.
Note that the alert record stores both the segment and the threshold. An analyst reading the alert can see why it fired, and a later review can see which parameters were in force at that time.
Relative thresholds with peer groups
An absolute threshold needs someone to choose a number and keep it current. A peer-relative rule derives the number from the segment's own data.
from statistics import median
peer_totals = {"C1": 22000, "C4": 1200, "C5": 900, "C6": 1500, "C7": 1000}
RATIO_LIMIT = 10
peer_median = median(peer_totals.values())
for customer_id, total in peer_totals.items():
ratio = total / peer_median
if ratio >= RATIO_LIMIT:
print(customer_id, f"{ratio:.1f}")
# C1 18.3The peer median is 1,200, so C1 is 18.3 times the median and exceeds the limit of 10. The median is used rather than the mean because one large customer would pull a mean upward and hide itself. The cost of this approach is that it depends on peer group quality. If a store and a student sit in one peer group, the median describes neither.
Keeping segment assignment stable
Segment assignment is not a one-time label. Customers change, and the logic has to account for that.
- Re-segmentation schedule. Assignment is recalculated periodically, for example monthly or when the profile is updated. Doing it on every transaction adds cost and can cause thresholds to shift mid-window.
- Migration. When a business grows past $1 million turnover, it moves to the corporate segment. Its thresholds rise sharply. Programs often review such moves, because a customer could be moved into a more permissive segment by an inaccurate profile.
- History. Keep the segment and parameter version that applied when each alert was generated. Without this, you cannot explain old alerts after the thresholds change.
- New customers. A customer with no history has no behavior to compare against. Programs often use the declared expected activity from onboarding, or a stricter default, until enough history exists.
Common errors
Segments that are too coarse. A single "business" segment places a corner shop and a trading company together. The shop's threshold is set high enough to suit the trading company, and the shop's own suspicious activity falls below it.
Segments that are too fine. Dozens of segments with a few customers each give unstable statistics and a large parameter table to maintain. Merge segments until each has enough customers to support the thresholds applied to it.
Letting missing data choose a permissive segment. If the code defaults a missing attribute to the segment with the highest thresholds, data gaps reduce detection. Missing data should route to a strict fallback, as in the example, and the number of customers in the fallback should be monitored.
Setting thresholds from the alerts you want. If a segment is built around the customers who generated false positives, thresholds will be raised until they stop. That removes noise, but it also removes real signal. Thresholds should be justified by the segment's observed behavior and tested, which later chapters on tuning cover.
Treating the segment as the risk assessment. A customer in a low-threshold segment is not necessarily high risk, and a customer in a high-threshold segment is not necessarily safe. Segment answers a statistical question about normal volume. Risk rating answers a different question.
Summary of the working model
Segmentation turns one scenario into a family of parameter sets. The scenario logic is written once. The segment key, derived from the customer profile, selects the parameters. A fallback covers missing data, peer-relative rules can replace fixed numbers, and the alert should record which segment and parameters produced it.
Exercise
A scenario alerts when monthly cash deposits reach the threshold for the customer's segment. A bakery's turnover field is blank in the customer profile, and the segmentation code assigns customers with missing turnover to the "corporate" segment (threshold $250,000). Describe the problem and give a better design.
Show answer
The bakery is a small cash-intensive business, but the blank field puts it in the segment with the highest threshold. Its deposits would need to reach $250,000 a month to alert, so the data gap reduces detection and can hide real activity. A better design routes customers with missing required attributes to a strict fallback segment (such as "unassigned" with a low threshold), reports how many customers sit in that fallback, and sends the missing data to be corrected so the customer can be assigned to the right segment.
Exercise
A peer group has monthly cash totals of 900, 1000, 1200, 1500 and 22000. Why does a peer-relative rule use the median instead of the mean, and what ratio would the customer with 22000 have against the median if the limit is 10?
Show answer
The mean is (900 + 1000 + 1200 + 1500 + 22000) / 5 = 5,320, which the single large value pulls upward. Against that mean, 22,000 is only about 4.1 times the peer value, so it would not exceed a limit of 10 and would hide itself. The median is 1,200, which is not distorted by the outlier. The ratio is 22,000 / 1,200 = 18.3, which exceeds 10, so the customer alerts.