Analyze A/B Test Results

This project will assure you have mastered the subjects covered in the statistics lessons. The hope is to have this project be as comprehensive of these topics as possible. Good luck!

Table of Contents


A/B tests are very commonly performed by data analysts and data scientists. It is important that you get some practice working with the difficulties of these.

For this project, you will be working to understand the results of an A/B test run by an e-commerce website. Your goal is to work through this notebook to help the company understand if they should implement the new page, keep the old page, or perhaps run the experiment longer to make their decision.

Part I - Probability

To get started, let's import our libraries.

1. Now, read in the ab_data.csv data. Store it in df.

a. Read in the dataset and take a look at the top few rows here:

b. Use the cell below to find the number of rows in the dataset.

c. The number of unique users in the dataset.

d. The proportion of users converted.

e. The number of times the new_page and treatment don't match.

f. Do any of the rows have missing values?

As we can see above, none of our columns contain null values.

2. For the rows where treatment does not match with new_page or control does not match with old_page, we cannot be sure if this row truly received the new or old page.

Store your new dataframe in df2.

3. Use df2 and the cells below to answer questions.

a. How many unique user_ids are in df2?

b. There is one user_id repeated in df2. What is it?

c. What is the row information for the repeat user_id?

d. Remove one of the rows with a duplicate user_id, but keep your dataframe as df2.

4. Use df2 in the cells below to answer the following questions.

a. What is the probability of an individual converting regardless of the page they receive?

b. Given that an individual was in the control group, what is the probability they converted?

c. Given that an individual was in the treatment group, what is the probability they converted?

d. What is the probability that an individual received the new page?

We can see from the above that our experiment has run roughly 22 days.

e. Consider your results from parts (a) through (d) above, and explain below whether you think there is sufficient evidence to conclude that the new treatment page leads to more conversions.


The probability that a user converts on the old page is only 0.15% higher than the probability that a user converts on the new page, i.e. the difference is a very slim margin. Additionally, the test has only be running for 22 days. It is possible, for example, that the test hasn't been run long enough to account for change aversion. There does not appear to be sufficient evidence yet to make a conclusion.

Part II - A/B Test

Notice that because of the time stamp associated with each event, you could technically run a hypothesis test continuously as each observation was observed.

However, then the hard question is do you stop as soon as one page is considered significantly better than another or does it need to happen consistently for a certain amount of time? How long do you run to render a decision that neither page is better than another?

These questions are the difficult parts associated with A/B tests in general.

1. For now, consider you need to make the decision just based on all the data provided. If you want to assume that the old page is better unless the new page proves to be definitely better at a Type I error rate of 5%, what should your null and alternative hypotheses be? You can state your hypothesis in terms of words or in terms of $p_{old}$ and $p_{new}$, which are the converted rates for the old and new pages.


$H_0$: $p_{new}$ ≤ $p_{old}$

$H_1$: $p_{new}$ > $p_{old}$


$H_0$: $p_{new}$ - $p_{old}$ ≤ 0

$H_1$: $p_{new}$ - $p_{old}$ > 0

2. Assume under the null hypothesis, $p_{new}$ and $p_{old}$ both have "true" success rates equal to the converted success rate regardless of page - that is $p_{new}$ and $p_{old}$ are equal. Furthermore, assume they are equal to the converted rate in ab_data.csv regardless of the page.

Use a sample size for each page equal to the ones in ab_data.csv.

Perform the sampling distribution for the difference in converted between the two pages over 10,000 iterations of calculating an estimate from the null.

a. What is the conversion rate for $p_{new}$ under the null?

b. What is the conversion rate for $p_{old}$ under the null?

c. What is $n_{new}$, the number of individuals in the treatment group?

d. What is $n_{old}$, the number of individuals in the control group?

e. Simulate $n_{new}$ transactions with a conversion rate of $p_{new}$ under the null. Store these $n_{new}$ 1's and 0's in new_page_converted.

f. Simulate $n_{old}$ transactions with a conversion rate of $p_{old}$ under the null. Store these $n_{old}$ 1's and 0's in old_page_converted.

g. Find $p_{new}$ - $p_{old}$ for your simulated values from part (e) and (f).

h. Create 10,000 $p_{new}$ - $p_{old}$ values using the same simulation process you used in parts (a) through (g) above. Store all 10,000 values in a NumPy array called p_diffs.

i. Plot a histogram of the p_diffs. Does this plot look like what you expected? Use the matching problem in the classroom to assure you fully understand what was computed here.

The plot resembles a normal distribution, which is what I expected given the Central Limit Theorem: "With a large enough sample size, the sampling distribution of the mean will be normally distributed."

j. What proportion of the p_diffs are greater than the actual difference observed in ab_data.csv?

k. Please explain what you just computed in part j. What is this value called in scientific studies? What does this value mean in terms of whether or not there is a difference between the new and old pages?


We have just computed the p-value in part j above. If the p-value is less than the type I error threshold, then we have evidence to reject the null hypothesis and choose the alternative. In this case, however, our p-value is substantially higher than the threshold, therefore we have failed to reject the null hypothesis. In practical terms, this means that there is not enough evidence that the new page outperforms the old page.

l. We could also use a built-in to achieve similar results. Though using the built-in might be easier to code, the above portions are a walkthrough of the ideas that are critical to correctly thinking about statistical significance. Fill in the below to calculate the number of conversions for each page, as well as the number of individuals who received each page. Let n_old and n_new refer the the number of rows associated with the old page and new pages, respectively.

m. Now use stats.proportions_ztest to compute your test statistic and p-value. Here is a helpful link on using the built in.

n. What do the z-score and p-value you computed in the previous question mean for the conversion rates of the old and new pages? Do they agree with the findings in parts j. and k.?


The p-value is very close (almost the same) to that which we calculated in part j above, therefore we come to the same conculsion about not rejecting the null hypothesis.

Similar to the p-value, we can also use the z-score to determine whether or not to reject the null hypothesis. According to Wikipedia, the z-score is "the number of standard deviations by which the value of a raw score (i.e., an observed value or data point) is above or below the mean value of what is being observed or measured." Given our type I error threshold of 5%, we assume a confidence interval of 95%. If we consult a z-score table, we see that z-score values that fall within this interval are between -1.96 (the lower 2.5% threshold) and 1.96 (the highest 2.5% threshold). Our z-score of -1.31 falls within our confidence interval, therefore we do not reject the null hypothesis. This corresponds to our result in part k above.

Part III - A regression approach

1. In this final part, you will see that the result you achieved in the A/B test in Part II above can also be achieved by performing regression.

a. Since each row is either a conversion or no conversion, what type of regression should you be performing in this case?


Logistic Regression

b. The goal is to use statsmodels to fit the regression model you specified in part a. to see if there is a significant difference in conversion based on which page a customer receives. However, you first need to create in df2 a column for the intercept, and create a dummy variable column for which page each user received. Add an intercept column, as well as an ab_page column, which is 1 when an individual receives the treatment and 0 if control.

c. Use statsmodels to instantiate your regression model on the two columns you created in part b., then fit the model using the two columns you created in part b. to predict whether or not an individual converts.

d. Provide the summary of your model below, and use it as necessary to answer the following questions.

e. What is the p-value associated with ab_page? Why does it differ from the value you found in Part II?


The p-value associated with ab_page is 0.1899.

This differs from the value found in Part II because in this case our null and alternative hypotheses are slightly different:

$H_0$: $p_{new}$ - $p_{old}$ = 0

$H_1$: $p_{new}$ - $p_{old}$ ≠ 0

Note that our test in Part II was one-sided, whereas here in Part III we are using a two-sided test. As a result, we would expect different p-values.

However, given that our p-value is still greater than the type I error threshold (or alpha), we still fail to reject the hypothesis.

f. Now, you are considering other things that might influence whether or not an individual converts. Discuss why it is a good idea to consider other factors to add into your regression model. Are there any disadvantages to adding additional terms into your regression model?


It is a good idea to consider adding other factors into our model, especially if we want to improve our predictions or determine how other factors might influence our results. For example, in our case we might want to take into account whether or not we have a returning customer, the geographic location of a customer, age and/or other demographic characteristics.

A disadvantage of adding additional terms is that it complicates your model. Also, if one or more of the terms are correlated, then this could cause errors in our model (for example, coefficents being flipped from what we would expect.)

g. Now along with testing if the conversion rate changes for different pages, also add an effect based on which country a user lives in. You will need to read in the countries.csv dataset and merge together your datasets on the appropriate rows. Here are the docs for joining tables.

Does it appear that country had an impact on conversion? Don't forget to create dummy variables for these country columns.

We can see from the results above that the p-values are above the 0.05 threshold, therefore we fail to reject the null hypothesis. We can conclude that country does not affect conversions.

h. Though you have now looked at the individual factors of country and page on conversion, we would now like to look at an interaction between page and country to see if there significant effects on conversion. Create the necessary additional columns, and fit the new model.

Provide the summary results, and your conclusions based on the results.

Once again, our p-values are still above the 0.05 threshold and we fail to reject the null hypothesis. The interactions do not appear to impact conversions.


We used three different methods to analyze whether or not to launch a new landing page:

Using probability we did not find that our new landing page resulted in more conversions. During A/B testing and logically regression, we used the p-value to assess whether are results were statistically significant enough to conclude that the new landing page performed better than the old page. In each case, we were unable to reject our null hypothesis: i.e. that the old page was as effective or moreso in resulting in a conversion.

Given these results, we cannot recommend changing the landing page to the new page.