KatInterview

Session-Based Account Risk Scoring with Bayesian Updates

Arrays & HashingAuto-graded · 2 test cases
MediumAuto-gradedArrays & Hashingprobabilitybayes-theorembinomial-distribution

Last updated

Tests probabilistic modeling with the Binomial distribution, Bayes' theorem, two-stage classification pipelines, and F1-optimal threshold selection in a risk-scoring context.

Session-Based Account Risk Scoring with Bayesian Updates

A trust and safety team monitors user accounts by observing behavior across independent sessions. Your task is to build a complete probabilistic model of their detection pipeline and answer a series of questions about its performance.

Setup

Each account generates n = 5 independent sessions in a given observation window. In each session, a suspicious event occurs with probability:

  • p_R = 0.5 if the account is a risk account (bad actor)
  • p_L = 0.05 if the account is legitimate

The prior probability that any given account is a risk account is π = 0.03 (3%).

Let X be the count of suspicious sessions for a given account. Then:

  • X | risk ~ Binomial(5, 0.5)
  • X | legitimate ~ Binomial(5, 0.05)

An account is flagged whenever X ≥ k for some threshold k.

Part A — Detection rates at k = 2

Using the Binomial distribution, compute:

  • TPR = P(flag | risk account) = P(X ≥ 2 | p = 0.5)
  • FPR = P(flag | legitimate account) = P(X ≥ 2 | p = 0.05)

Part B — Posterior probabilities via Bayes' theorem

Still using k = 2, apply Bayes' theorem to compute:

  • PPV = P(risk | flagged)
  • NPV = P(legitimate | not flagged)

Part C — Adding a human review stage

Flagged accounts are sent to a human reviewer who operates independently of the automated rule (conditional on the true account type). The reviewer has:

  • Sensitivity = 0.90 (correctly identifies a risk account 90% of the time)
  • Specificity = 0.98 (correctly clears a legitimate account 98% of the time)

An account is actioned only when both the rule flags it and the reviewer calls it a risk account. Compute the combined pipeline's TPR, FPR, and revised PPV.

Part D — Absolute counts for a large population

For a population of 1,000,000 accounts processed through the full two-stage pipeline from Part C, compute the expected number of:

  • True positives (risk accounts that are actioned)
  • False positives (legitimate accounts that are actioned)
  • True negatives (legitimate accounts that are not actioned)
  • False negatives (risk accounts that are not actioned)

Part E — Threshold selection by F1 score (rule only, no reviewer)

Without the human review stage, evaluate k ∈ {1, 2, 3, 4, 5} and compute the F1 score at the prior π = 0.03 for each. Then repeat for π = 0.10. Report the optimal k under each prior.

The F1 score should be computed from population-level expected counts:

F1 = 2·TP / (2·TP + FP + FN)

where TP = π · TPR, FP = (1 − π) · FPR, FN = π · (1 − TPR).

Part F — Error taxonomy

Identify which errors in Parts A–E correspond to Type I errors and which correspond to Type II errors, using H₀ = "the account is legitimate."


Output format

Return a single dictionary with exactly these keys:

{
  "binomial_formulas": {
    "tail": <string describing the tail-sum formula>,
    "tpr_k2": <string describing how TPR at k=2 is computed>,
    "fpr_k2": <string describing how FPR at k=2 is computed>
  },
  "part_a": {"tpr": <float>, "fpr": <float>},
  "part_b": {"ppv": <float>, "npv": <float>},
  "part_c": {"overall_tpr": <float>, "overall_fpr": <float>, "revised_ppv": <float>},
  "part_d": {
    "true_positives": <float>,
    "false_positives": <float>,
    "true_negatives": <float>,
    "false_negatives": <float>
  },
  "part_e": {
    "f1_by_k_at_3_percent": {1: <float>, 2: <float>, 3: <float>, 4: <float>, 5: <float>},
    "optimal_k_at_3_percent": <int>,
    "f1_by_k_at_10_percent": {1: <float>, 2: <float>, 3: <float>, 4: <float>, 5: <float>},
    "optimal_k_at_10_percent": <int>
  },
  "part_f": {
    "type_i": <string>,
    "type_ii": <string>
  }
}

The f1_by_k_* dictionaries use integer keys (not strings).


Constraints

  • n = 5 sessions per account (fixed)
  • p_R = 0.5, p_L = 0.05 (fixed session-level probabilities)
  • Prior π = 0.03 for all parts except the π = 0.10 scenario in Part E
  • Reviewer sensitivity = 0.90, specificity = 0.98 (Parts C and D only)
  • Population size = 1,000,000 (Part D only)
  • Threshold k ranges over {1, 2, 3, 4, 5} in Part E
  • The reviewer's errors are conditionally independent of the rule's errors given the true account type
  • All floating-point results should be computed without manual rounding; return full-precision Python floats
  • The binomial_formulas strings must match the exact wording shown in the sample output

Examples

Example 1

Input: account_risk_scoring_metrics()

Output: {'binomial_formulas': {'tail': 'P(X >= k) = sum_{j=k}^{5} C(5,j) p^j (1-p)^(5-j)', 'tpr_k2': 'P(X >= 2 | fake), with p=0.5', 'fpr_k2': 'P(X >= 2 | authentic), with p=0.05'}, 'part_a': {'tpr': 0.8125, 'fpr': 0.0225925}, 'part_b': {'ppv': 0.5265747420188821, 'npv': 0.994101982386632}, 'part_c': {'overall_tpr': 0.7312500000000001, 'overall_fpr': 0.00045185000000000044, 'revised_ppv': 0.9804121145284919}, 'part_d': {'true_positives': 21937.500000000004, 'false_positives': 438.2945000000004, 'true_negatives': 969561.7054999999, 'false_negatives': 8062.499999999998}, 'part_e': {'f1_by_k_at_3_percent': {'1': 0.20871111494521163, '2': 0.6390113478584436, '3': 0.6504293307854571, '4': 0.3155317340782687, '5': 0.060605466794921306}, 'optimal_k_at_3_percent': 3, 'f1_by_k_at_10_percent': {'1': 0.48380392238567776, '2': 0.8061185639183812, '3': 0.6620661346137693, '4': 0.31571768945166157, '5': 0.060605895317255194}, 'optimal_k_at_10_percent': 2}, 'part_f': {'type_i': 'False positive: an authentic account is flagged/actioned as fake.', 'type_ii': 'False negative: a fake account is not flagged/actioned.'}}

Hints

Hint 1

Start by computing the full Binomial PMF for both p = 0.5 and p = 0.05 once. Every TPR and FPR value is a tail sum of those PMFs, and you will need them in both Part A and Part E.

Hint 2

For the two-stage pipeline in Part C, the key insight is that the combined rates multiply: overall_TPR = rule_TPR × reviewer_sensitivity, and overall_FPR = rule_FPR × (1 − reviewer_specificity). This follows from the conditional independence assumption stated in the problem.

Hint 3

When comparing optimal k values between π = 0.03 and π = 0.10 in Part E, notice that F1 depends on both precision and recall. A higher base rate means false positives become relatively less costly compared to false negatives, which shifts the optimal threshold toward a looser (smaller k) cutoff.