KatInterview

CTR Metrics with Deduplication Windowing and PR Model Evaluation

SQL & DataModel answer
MediumModel answerSQL & DataSQLWindow FunctionsPandas

Last updated

Evaluate A/B experiment unique CTR using windowed SQL deduplication and analyze model precision-recall performance with weighted sample thresholds in Python.

You are analyzing user engagement for an email campaign series.

Part A: SQL Analysis

Write an ANSI SQL query using the schema below to compute the 48-hour unique Click-Through Rate (CTR) for campaign_id = 100, aggregated by experiment variant.

Requirements & Rules:

  1. Filtering & Exclusions:
    • Only consider send records on or after 2025-08-30 00:00:00.
    • Exclude internal test user accounts defined as user_id <= 2.
  2. Deduplication:
    • Keep only the earliest send per (user_id, campaign_id) combination among eligible records. Break ties deterministically using the smallest send_id.
  3. Outcome Windowing:
    • A user is counted as a 48-hour clicker if they recorded at least one 'click' event inside the inclusive interval [send_time, send_time + 48 hours].
  4. Calculations:
    • sends: Total unique deduplicated users sent an email.
    • unique_clickers_48h: Count of deduplicated users who clicked within the 48-hour window.
    • ctr_48h: unique_clickers_48h / sends.
  5. Lift Output:
    • In addition to variant level metrics, provide a summary SQL query structure that returns the absolute lift (test_ctr - control_ctr) alongside raw counts (n_test, x_test, n_control, x_control) needed for a downstream 95% Wald confidence interval.

Schema

-- users: user demographics and account metadata
users (
  user_id INT,
  signup_dt DATE,
  locale STRING
);

-- email_sends: record of outbound emails sent
email_sends (
  send_id INT,
  user_id INT,
  campaign_id INT,
  send_time TIMESTAMP,
  variant STRING -- 'control' or 'test'
);

-- email_events: downstream user engagement logs
email_events (
  event_id INT,
  send_id INT,
  event_type STRING, -- 'open', 'click', 'unsubscribe'
  event_time TIMESTAMP
);

Sample Data

users

user_idsignup_dtlocale
12025-08-20US
22025-08-22US
32025-08-25CA
42025-08-27US
52025-08-28GB

email_sends

send_iduser_idcampaign_idsend_timevariant
1011002025-08-30 09:00:00control
1121002025-08-30 09:00:00control
1231002025-08-30 09:00:00test
1311002025-08-31 09:00:00test
1441012025-08-31 10:00:00control
1551002025-08-30 09:00:00test

email_events

event_idsend_idevent_typeevent_time
100010open2025-08-30 09:05:00
100110click2025-08-30 09:06:00
100211open2025-08-30 09:10:00
100312open2025-08-30 09:07:00
100412unsubscribe2025-08-30 10:00:00
100513click2025-08-31 09:12:00
100615click2025-08-31 09:00:00
100714click2025-08-31 10:05:00

Part B: Python Model Evaluation

Given a pandas DataFrame df containing columns:

  • y_true: Binary ground truth target (0 or 1)
  • y_prob: Model predicted probabilities in [0, 1]
  • weight: Non-negative sample weights

Write a function evaluate_ctr_model(df, top_percentile=0.01) that:

  1. Plots the sample-weighted Precision-Recall (PR) curve alongside the baseline prevalence.
  2. Identifies the classification decision threshold that maximizes the sample-weighted F1 score.
  3. Computes the deterministic Precision@Top 1% of the population by predicted score (breaking probability ties using original index order).

Finally, address the conceptual question: Why can a ranking model display an ROC-AUC of 0.86 while achieving a PR-AUC (Average Precision) of only 0.18 in a dataset with 1% positive class prevalence? Which metric should you optimize when tuning target selection for a marketing campaign?

Hints

Hint 1

Filter for send dates >= 2025-08-30 and internal users before computing ROW_NUMBER() window partitioning.

Hint 2

Left join email_events onto the deduplicated send set using send_id matching, event_type = 'click', and bounded timestamp ranges.

Hint 3

In Python, sample weights shift the denominator in precision and recall; scikit-learn metrics accept sample_weight natively.