KatInterview

Flagged Account Precision and Expected Alert Counts

Arrays & HashingAuto-graded · 19 test cases
EasyAuto-gradedArrays & Hashingbayes-theoremconfusion-matrixprobability

Last 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 malicious
  • recall: the probability that a malicious account is flagged
  • false_positive_rate: the probability that a legitimate account is flagged
  • total_users: the number of accounts in the population

return a dictionary with exactly these five keys:

KeyMeaning
"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.0 for "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 when total_users is 0. In that case both expected counts are 0.
  • Return the values as numbers (floats or ints are both fine for the counts).

Constraints

  • 0 <= prevalence <= 1
  • 0 <= recall <= 1
  • 0 <= false_positive_rate <= 1
  • 0 <= 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.