Agresti-Coull Interval for a Low-Volume Rate

Quick Overview

Write a SQL query that turns a table of per-user conversion flags into a 95% Agresti-Coull (adjusted-Wald) confidence interval for the conversion rate. It tests aggregation in a CTE, applying an interval formula to small samples, and numeric casting and rounding in PostgreSQL.

Agresti-Coull Interval for a Low-Volume Rate

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `trials` table has one row per user with a `converted` flag (1 or 0). Return one row with the 95% adjusted-Wald (Agresti-Coull) interval for the conversion rate: `p_tilde`, `ci_lower` and `ci_upper`, each rounded to 4 decimals. Use these definitions, where $n$ is the number of users, $x$ is the number of conversions and $z = 1.96$ (so $z^2 = 3.8416$): - $\tilde{n} = n + 3.8416$ - $\tilde{p} = (x + 1.9208) / \tilde{n}$, returned as `p_tilde` - $\tilde{p} - 1.96\sqrt{\tilde{p}(1 - \tilde{p}) / \tilde{n}}$, returned as `ci_lower` - $\tilde{p} + 1.96\sqrt{\tilde{p}(1 - \tilde{p}) / \tilde{n}}$, returned as `ci_upper` ```hint Order of operations Reduce the table to n and x in one aggregate, apply the formulas to those two numbers, and round only in the final SELECT. ``` ### Input Table(s) `trials` | Column | Type | Meaning | |---|---|---| | converted | SMALLINT | 1 if the user converted, 0 if not | There is no key column. Each row is one user, so the row count is the number of users. ### Output Contract - Exactly one row with the columns `p_tilde`, `ci_lower`, `ci_upper`, in that order. - Each value is rounded to 4 decimal places. Round only the final values, never the intermediate ones. - The result has a single row, so no ordering is needed. ### Example Input `trials` (12 users, 1 conversion): | converted | |---| | 0 | | 0 | | 1 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | Expected output: | p_tilde | ci_lower | ci_upper | |---|---|---| | 0.1844 | -0.0066 | 0.3753 | Here $n = 12$ and $x = 1$, so $\tilde{n} = 15.8416$ and $\tilde{p} = 2.9208 / 15.8416 \approx 0.18438$. The half-width is ${1.96\sqrt{0.18438 \cdot 0.81562 / 15.8416} \approx 0.19096}$, which puts the bounds at -0.0066 and 0.3753 after rounding. ### Constraints and Clarifications - `converted` is never NULL and holds only 0 or 1. - The table has at least one row. - Report the bounds exactly as the formula gives them. Do not clip `ci_lower` at 0 or `ci_upper` at 1; the negative lower bound in the example is the expected value. - Use the constants 1.96, 3.8416 and 1.9208 as given. - There are no ties or duplicates to resolve: the answer is a single aggregate row, and each row of `trials` is a distinct user.

Overview: Write a SQL query that turns a table of per-user conversion flags into a 95% Agresti-Coull (adjusted-Wald) confidence interval for the conversion rate. It tests aggregation in a CTE, applying an interval formula to small samples, and numeric casting and rounding in PostgreSQL.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The trials table has one row per user with a converted flag (1 or 0). Return one row with the 95% adjusted-Wald (Agresti-Coull) interval for the conversion rate: p_tilde, ci_lower and ci_upper, each rounded to 4 decimals.

Use these definitions, where nn is the number of users, xx is the number of conversions and z=1.96z = 1.96 (so z2=3.8416z^2 = 3.8416):

  • n~=n+3.8416\tilde{n} = n + 3.8416
  • p~=(x+1.9208)/n~\tilde{p} = (x + 1.9208) / \tilde{n} , returned as p_tilde
  • p~−1.96p~(1−p~)/n~\tilde{p} - 1.96\sqrt{\tilde{p}(1 - \tilde{p}) / \tilde{n}} , returned as ci_lower
  • p~+1.96p~(1−p~)/n~\tilde{p} + 1.96\sqrt{\tilde{p}(1 - \tilde{p}) / \tilde{n}} , returned as ci_upper

Input Table(s)

trials

ColumnTypeMeaning
convertedSMALLINT1 if the user converted, 0 if not

There is no key column. Each row is one user, so the row count is the number of users.

Output Contract

  • Exactly one row with the columns p_tilde , ci_lower , ci_upper , in that order.
  • Each value is rounded to 4 decimal places. Round only the final values, never the intermediate ones.
  • The result has a single row, so no ordering is needed.

Example

Input trials (12 users, 1 conversion):

converted
0
0
1
0
0
0
0
0
0
0
0
0

Expected output:

p_tildeci_lowerci_upper
0.1844-0.00660.3753

Here n=12n = 12 and x=1x = 1, so n~=15.8416\tilde{n} = 15.8416 and p~=2.9208/15.8416≈0.18438\tilde{p} = 2.9208 / 15.8416 \approx 0.18438. The half-width is 1.960.18438⋅0.81562/15.8416≈0.19096{1.96\sqrt{0.18438 \cdot 0.81562 / 15.8416} \approx 0.19096}, which puts the bounds at -0.0066 and 0.3753 after rounding.

Constraints and Clarifications

  • converted is never NULL and holds only 0 or 1.
  • The table has at least one row.
  • Report the bounds exactly as the formula gives them. Do not clip ci_lower at 0 or ci_upper at 1; the negative lower bound in the example is the expected value.
  • Use the constants 1.96, 3.8416 and 1.9208 as given.
  • There are no ties or duplicates to resolve: the answer is a single aggregate row, and each row of trials is a distinct user.