Unit 6, lesson 49

Comparing Two Proportions

How to estimate a difference in proportions in percentage points, test equality with the two-proportion z test and the pooled proportion, build the unpooled confidence interval and read a randomized A/B test.

  • Video 49
  • Tests for Means and Proportions

An experiment randomly assigns 100 users to interface A and 100 other users to interface B and records whether each user completes a task. Sixty users complete it with A and 45 with B. Is a gap between 60 and 45 percent larger than random assignment alone would typically produce, how large could the true difference be, and should it be reported as 15 points or as 33 percent? This lesson estimates a difference in proportions, tests it with the two-proportion z test and its pooled proportion, builds the unpooled confidence interval, and sets out what a randomized A/B test can and cannot support.

Key terms

Difference in proportions

Definition Suppose group A has nAn_A units and group B has nBn_B units, each unit gives one binary outcome, success or failure, defined before the data are collected, and xAx_A and xBx_B are the numbers of successes. The sample proportions are p^A=xA/nA\hat{p}_A = x_A/n_A and p^B=xB/nB\hat{p}_B = x_B/n_B, and the difference in proportions pA−pBp_A - p_B is estimated by p^A−p^B\hat{p}_A - \hat{p}_B. On the probability scale this absolute difference is also called the risk difference, and it is measured in percentage points. If the outcomes are independent Bernoulli trials within each group, with success probabilities pAp_A and pBp_B, and the two groups are independent, then

Var⁡(p^A−p^B)=pA(1−pA)nA+pB(1−pB)nB.\begin{aligned} &\operatorname{Var}(\hat{p}_A - \hat{p}_B) \\[0.4em] &\quad = \frac{p_A(1 - p_A)}{n_A} \\[0.4em] &\qquad + \frac{p_B(1 - p_B)}{n_B} . \end{aligned}

When pB>0p_B > 0, the relative difference (pA−pB)/pB(p_A - p_B)/p_B and the risk ratio pA/pBp_A/p_B describe the same comparison on a relative scale.

In plain words A difference in proportions, written in percentage points, says how many more successes per 100 units to expect in one group than in the other. A change from 45 percent to 60 percent is 15 percentage points. The same change is a relative increase of about 33 percent, because 15 is one third of 45. Both numbers are correct, but they answer different questions, so every reported effect needs a label. Each unit counts once, so repeated clicks by one user are not extra independent outcomes.

Example With 60 successes of 100 in group A and 45 of 100 in group B,

p^A−p^B=0.60−0.45=0.15,\begin{aligned} \hat{p}_A - \hat{p}_B &= 0.60 - 0.45 \\[0.4em] &= 0.15, \end{aligned}

a difference of 15 percentage points. Relative to group B the increase is 0.15/0.45=1/30.15/0.45 = 1/3, about 33.3 percent, and the estimated risk ratio is 0.60/0.45=4/3≈1.330.60/0.45 = 4/3 \approx 1.33. Calling the result a 15 percent improvement would mix the two scales. If the population difference were 0.150.15, then among 100 comparable users given each version, version A would produce 15 more completions than version B on average, not exactly 15 every time.

Pooled proportion

Definition Under the null hypothesis H0:pA=pBH_0: p_A = p_B both groups share one success probability pp. Its estimate, the pooled proportion, is

p^pool=xA+xBnA+nB,\hat{p}_{\text{pool}} = \frac{x_A + x_B}{n_A + n_B},

the proportion of successes in the two groups combined. It is the weighted average of p^A\hat{p}_A and p^B\hat{p}_B with weights nAn_A and nBn_B. Putting it in place of both probabilities in the variance formula gives the estimated null standard error

SE⁡0=p^pool(1−p^pool)×1nA+1nB.\begin{aligned} \operatorname{SE}_0 &= \sqrt{\hat{p}_{\text{pool}}(1 - \hat{p}_{\text{pool}})} \\[0.4em] &\quad \times \sqrt{\frac{1}{n_A} + \frac{1}{n_B}} . \end{aligned}

Pooling belongs to tests of equality. A null hypothesis of a nonzero difference, pA−pB=δ0p_A - p_B = \delta_0 with δ0≠0\delta_0 \ne 0, does not give the groups a common probability, so the pooled proportion does not fit it.

In plain words A test computes its p-value as if the null hypothesis were true. If the two probabilities are equal, every unit carries information about the same number, so the natural estimate uses all the outcomes together. Pooling follows from the null hypothesis. It is not a claim that the two sample proportions are equal.

Example The A/B data have 105 successes among 200 users, so

p^pool=105200=0.525.\hat{p}_{\text{pool}} = \frac{105}{200} = 0.525 .

The null standard error is 0.525×0.475×0.02≈0.07062\sqrt{0.525 \times 0.475 \times 0.02} \approx 0.07062, where 0.02=1/100+1/1000.02 = 1/100 + 1/100. Under H0H_0 the estimated expected counts in each group of 100 are 52.552.5 successes and 47.547.5 failures.

Two-proportion z test

Definition Suppose the outcomes are independent Bernoulli trials with probability pAp_A in group A and pBp_B in group B, the two groups are independent, and the expected counts under the null hypothesis, nAp^pooln_A\hat{p}_{\text{pool}}, nA(1−p^pool)n_A(1 - \hat{p}_{\text{pool}}), nBp^pooln_B\hat{p}_{\text{pool}} and nB(1−p^pool)n_B(1 - \hat{p}_{\text{pool}}), are all reasonably large, with at least 5 or at least 10 each as common guidelines. The two-proportion z test of H0:pA=pBH_0: p_A = p_B uses

z=p^A−p^BSE⁡0,z = \frac{\hat{p}_A - \hat{p}_B}{\operatorname{SE}_0},

which has approximately the standard normal distribution when H0H_0 is true. For H1:pA≠pBH_1: p_A \ne p_B the p-value is P(∣Z∣≥∣z∣)P(\lvert Z \rvert \ge \lvert z \rvert) with Z∼N(0,1)Z \sim N(0, 1), and a one-sided alternative uses the single tail in its direction. The guidelines are rules of thumb, not guarantees of accuracy, and for small or sparse counts an exact procedure such as Fisher’s exact test is often preferred.

In plain words The statistic counts how many null standard errors the observed difference lies from zero. The normal reference rests on the central limit theorem, so it needs enough successes and enough failures in both groups.

Example For the A/B data the statistic is z=0.15/0.07062≈2.124z = 0.15/0.07062 \approx 2.124, and the two-sided p-value is about 0.0340.034, so the test rejects H0:pA=pBH_0: p_A = p_B at the level 0.050.05. Now take a smaller experiment with 17 successes of 20 in group A and 12 of 20 in group B. Its estimate is larger, 0.85−0.60=0.250.85 - 0.60 = 0.25, but the pooled proportion is 29/40=0.72529/40 = 0.725, the null standard error is about 0.1410.141, and z≈1.77z \approx 1.77 gives a two-sided p-value of about 0.0770.077. A larger observed difference does not guarantee a smaller p-value when the sample sizes change. The expected failures are only 5.55.5 per group, so the normal approximation is less trustworthy here, and Fisher’s exact test gives a two-sided p-value of about 0.1550.155.

Unpooled confidence interval

Definition Without assuming pA=pBp_A = p_B, each group’s variance is estimated from its own proportion,

vA=p^A(1−p^A)nA,vB=p^B(1−p^B)nB,\begin{gathered} v_A = \frac{\hat{p}_A(1 - \hat{p}_A)}{n_A}, \\[0.4em] v_B = \frac{\hat{p}_B(1 - \hat{p}_B)}{n_B}, \end{gathered}

and the approximate 95 percent confidence interval for pA−pBp_A - p_B is

(p^A−p^B)±z∗vA+vB,(\hat{p}_A - \hat{p}_B) \pm z^{*}\sqrt{v_A + v_B},

where z∗≈1.960z^{*} \approx 1.960 is the 0.9750.975 quantile of the standard normal distribution. It needs independent Bernoulli outcomes within each group, independent groups and enough observed successes and failures in each group, with at least 10 of each as a common guideline. Its actual coverage can fall well below 95 percent with small counts or with proportions near 0 or 1, and score-based intervals, such as Newcombe’s interval built from a Wilson interval for each group, usually perform better there.

In plain words A test of equality pools because equality is its starting assumption. An interval estimates the difference without assuming that it is zero, so each group keeps its own variance. These are deliberate choices, not inconsistent arithmetic. Because the two standard errors differ, the test and the interval can occasionally disagree about zero for results near the 0.050.05 boundary.

Example For the A/B data vA=0.60×0.40/100=0.0024v_A = 0.60 \times 0.40/100 = 0.0024 and vB=0.45×0.55/100=0.002475v_B = 0.45 \times 0.55/100 = 0.002475, so the standard error is 0.004875≈0.0698\sqrt{0.004875} \approx 0.0698. The margin is 1.960×0.0698≈0.1371.960 \times 0.0698 \approx 0.137, and the interval runs from about 0.0130.013 to 0.2870.287, that is from about 1.3 to 28.7 percentage points. It excludes zero, in agreement with the test, but it is wide, and a gain near 1 point and a gain near 29 points could lead to different decisions. In the smaller experiment with 17 of 20 and 12 of 20, group A has only 3 failures, below the guideline. The unpooled interval, from about −0.016-0.016 to 0.5160.516, is then unreliable, and Newcombe’s interval, from about −0.028-0.028 to 0.4850.485, is a more dependable choice.

A/B test

Definition An A/B test is a randomized experiment that compares two versions of something, such as a web page, a message or an interface, by randomly assigning each unit, usually a user, to version A or version B and recording a primary outcome chosen in advance, often a binary one such as a purchase. Random assignment supports a causal comparison of the two versions for the units in the experiment, under its conditions, provided each unit’s outcome depends only on its own version, units receive the version they are assigned and outcomes are recorded for all assigned units. A sound plan fixes the outcome, the measurement window, the sample size and the analysis before the data arrive.

In plain words Randomization is what lets the comparison speak to cause, for the users who took part and during the test. It does not decide how far the result reaches, since users in another month, region or device type may respond differently. Checking repeatedly and stopping as soon as one version looks significantly better, or testing many outcomes and reporting the best one, raises the chance of a false positive above the stated significance level, and dropping users after assignment can bias the comparison.

Example In the experiment with 100 users per version, each user contributes one outcome, a completed task or not, within a window fixed in advance. The denominator is the number of assigned users, not the number of page views, since several page views from one user are not independent outcomes. Random assignment supports the conclusion that version A raised the completion rate for these users, by an amount the interval puts between about 1.3 and 28.7 percentage points. If the users were all current mobile users, the result does not automatically apply to desktop users or to next year’s customers.

Check yourself

  1. In an A/B test of a newsletter sign-up form, 48 of 400 visitors shown version A sign up, and 36 of 400 visitors shown version B. Find the difference in proportions in percentage points and the relative increase of A over B. A report says that version A raises sign-ups by 33 percentage points. Is that correct?
Show the answer

The sample proportions are p^A=48/400=0.12\hat{p}_A = 48/400 = 0.12 and p^B=36/400=0.09\hat{p}_B = 36/400 = 0.09, so the difference is 0.030.03, or 3 percentage points. The relative increase is 0.03/0.09=1/30.03/0.09 = 1/3, about 33.3 percent. The report mixes the two scales. Version A’s sign-up rate is 3 percentage points higher, which is about 33 percent higher relative to version B. A rise of 33 percentage points would mean going from 9 percent to 42 percent.

  1. For the same data, carry out the two-sided two-proportion z test at the level 0.050.05, check the count conditions and compute the unpooled 95 percent interval. Does the result show that the two versions perform equally well?
Show the answer

The pooled proportion is 84/800=0.10584/800 = 0.105, so under the null hypothesis the estimated expected counts in each group are 4242 successes and 358358 failures, and the observed counts 4848, 352352, 3636 and 364364 are also all above 10. The null standard error is 0.105×0.895×2/400≈0.02168\sqrt{0.105 \times 0.895 \times 2/400} \approx 0.02168, so

z=0.030.02168≈1.38,z = \frac{0.03}{0.02168} \approx 1.38,

with a two-sided p-value of about 0.1660.166. The test does not reject H0:pA=pBH_0: p_A = p_B. For the interval, vA=0.12×0.88/400=0.000264v_A = 0.12 \times 0.88/400 = 0.000264 and vB=0.09×0.91/400=0.00020475v_B = 0.09 \times 0.91/400 = 0.00020475, so the standard error is 0.00046875≈0.02165\sqrt{0.00046875} \approx 0.02165, the margin is about 0.04240.0424, and the interval runs from about −0.012-0.012 to 0.0720.072.

No, the result does not show equal performance. The interval contains differences from about 1.2 percentage points in favor of B to 7.2 points in favor of A, and a gain of 7 points might matter a great deal. Not rejecting the null hypothesis means only that the data did not rule out zero. Supporting a claim of practical equivalence would need an interval that lies entirely inside a margin of unimportant differences fixed in advance.

  1. For each study below, decide whether the design supports a causal conclusion about the versions, a generalization to a larger population, both or neither.

    • A store draws a random sample of 2000 of its registered customers and randomly assigns each sampled customer to receive email A or email B, then compares the purchase rates of the two groups.
    • A website randomly assigns every visitor during one week to checkout page A or checkout page B.
    • A random sample of customers reports which app version each one chose to install and whether they bought anything last month.
    • Volunteers choose which of two versions to try, and their purchase rates are compared.
Show the answer

The first study supports both. Random sampling lets the result extend to the registered customers at that time, and random assignment lets the difference in purchase rates be read as an effect of the emails.

The second study supports a causal conclusion for the visitors of that week. They are all of that week’s visitors rather than a random sample of future ones, so applying the result to later visitors needs an argument that the week was typical.

The third study supports a generalization of the association to the customer population, but not a causal conclusion, since customers chose their versions and those who chose differently may differ in other ways.

The fourth study supports neither. It describes an association among these volunteers only.

How this lesson connects