Flagged Account Precision and Expected Alert Counts
Arrays & HashingAuto-graded · 19 test casesLast updated
Apply Bayes' rule and expected-count arithmetic to a fraud classifier, returning the posterior precision of a flag plus the expected hits, false alarms and error rates.
Flagged Account Precision and Expected Alert Counts
A trust-and-safety team runs an automated detector over a population of accounts. A fraction of those accounts are malicious. The detector marks, or "flags", some accounts as suspicious. Its behavior is described by two known rates:
- Recall (true-positive rate): the probability that a malicious account is flagged.
- False-positive rate: the probability that a legitimate account is flagged.
Write a function that summarizes how the detector performs on a population of a given size.
Task
Given the following inputs:
prevalence: the probability that a randomly chosen account is maliciousrecall: the probability that a malicious account is flaggedfalse_positive_rate: the probability that a legitimate account is flaggedtotal_users: the number of accounts in the population
return a dictionary with exactly these five keys:
| Key | Meaning |
|---|---|
"posterior_probability" | The probability that a flagged account is actually malicious, computed with Bayes' theorem from prevalence, recall and false_positive_rate. |
"bad_actors_caught" | The expected number of malicious accounts that get flagged: total_users * prevalence * recall. |
"good_users_incorrectly_flagged" | The expected number of legitimate accounts that get flagged: total_users * (1 - prevalence) * false_positive_rate. |
"type_i_error_rate" | The chance of wrongly flagging a legitimate account. This equals false_positive_rate. |
"type_ii_error_rate" | The chance of missing a malicious account. This equals 1 - recall. |
Rules
- The posterior is the share of flagged accounts that are malicious:
prevalence * recall / (prevalence * recall + (1 - prevalence) * false_positive_rate). - If the overall probability of being flagged (the denominator above) is exactly zero, return
0.0for"posterior_probability". - The expected counts are plain products of rates and counts. Do not round them to integers; return them as computed.
- The posterior must not depend on
total_users, so it must still be computed whentotal_usersis0. In that case both expected counts are0. - Return the values as numbers (floats or ints are both fine for the counts).
Constraints
0 <= prevalence <= 10 <= recall <= 10 <= false_positive_rate <= 10 <= total_users <= 10^9
Examples
Example 1
Input: flagged_account_report(0.01, 0.95, 0.02, 10000000)
Output: {'posterior_probability': 0.3242320819112628, 'bad_actors_caught': 95000, 'good_users_incorrectly_flagged': 198000, 'type_i_error_rate': 0.02, 'type_ii_error_rate': 0.050000000000000044}
Example 2
Input: flagged_account_report(0.1, 0.8, 0.05, 1000)
Output: {'posterior_probability': 0.64, 'bad_actors_caught': 80, 'good_users_incorrectly_flagged': 45, 'type_i_error_rate': 0.05, 'type_ii_error_rate': 0.19999999999999996}
Example 3
Input: flagged_account_report(0.5, 0.9, 0.01, 20000)
Output: {'posterior_probability': 0.989010989010989, 'bad_actors_caught': 9000, 'good_users_incorrectly_flagged': 100, 'type_i_error_rate': 0.01, 'type_ii_error_rate': 0.09999999999999998}
Hints
Hint 1
Define the events: bad account, good account and flagged. Recall is P(flag | bad) and the false-positive rate is P(flag | good). The posterior you need is the reverse conditional, P(bad | flag).
Hint 2
Use the law of total probability to get P(flag) = prevalence * recall + (1 - prevalence) * false_positive_rate. The posterior is the bad-and-flagged share of that total. Guard against a zero denominator.
Hint 3
The expected counts need no Bayes step: multiply the population by the size of each group (bad or good), then by the relevant rate. The two error rates are read straight off the inputs.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The function has two independent parts.
1. Expected counts. Split the population into malicious and legitimate accounts using the prevalence:
- malicious accounts =
total_users * prevalence - legitimate accounts =
total_users * (1 - prevalence)
Each malicious account is flagged with probability recall, so the expected number caught is malicious * recall. Each legitimate account is flagged with probability false_positive_rate, so the expected number of false alarms is legitimate * false_positive_rate.
2. Posterior probability. We want P(malicious | flagged), but the inputs give the reverse conditional, P(flagged | malicious). Bayes' theorem bridges the two:
- P(flagged) =
prevalence * recall + (1 - prevalence) * false_positive_rate(law of total probability) - P(malicious | flagged) =
prevalence * recall / P(flagged)
If P(flagged) is exactly zero, nobody is ever flagged and the posterior is defined to be 0.0.
3. Error rates. The Type I error rate (flagging a legitimate account) is the false-positive rate. The Type II error rate (missing a malicious account) is 1 - recall.
Why it is correct
The expected count of flagged accounts in each group is the group size times the flag probability within that group, by linearity of expectation. The posterior is the ratio of the malicious-and-flagged probability to the overall flagged probability. Multiplying numerator and denominator by total_users gives the ratio of expected counts, so the posterior does not depend on total_users and is well defined even when the population is empty.
The base-rate effect is visible in the numbers. With 1% prevalence, 95% recall and a 2% false-positive rate, the false alarms (198,000) outnumber the true catches (95,000). The posterior is therefore only about 32%, even though recall is high.
Complexity
Time O(1) and space O(1): the function only does a fixed number of arithmetic operations.
Reference solution
from typing import Dict
def flagged_account_report(prevalence: float, recall: float, false_positive_rate: float, total_users: int) -> Dict[str, float]:
bad_users = total_users * prevalence
good_users = total_users * (1.0 - prevalence)
bad_caught = bad_users * recall
good_flagged = good_users * false_positive_rate
# P(flag) by total probability; total_users cancels out of the posterior.
flagged_probability = prevalence * recall + (1.0 - prevalence) * false_positive_rate
posterior = (prevalence * recall / flagged_probability
if flagged_probability else 0.0)
return {
"posterior_probability": posterior,
"bad_actors_caught": bad_caught,
"good_users_incorrectly_flagged": good_flagged,
"type_i_error_rate": false_positive_rate,
"type_ii_error_rate": 1.0 - recall,
}