# MIT License # # Copyright (c) 2020 Nguyen Ngo # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # # https://github.com/mnguyenngo/ab-framework # import numpy as np import matplotlib.pyplot as plt # import pandas as pd import scipy.stats as scs from .stats import pooled_SE, confidence_interval, ab_dist, p_val, z_val plt.style.use('ggplot') def plot_norm_dist(ax, mu, std, with_CI=False, sig_level=0.05, label=None): """Adds a normal distribution to the axes provided Example: plot_norm_dist(ax, 0, 1) # plots a standard normal distribution Parameters: ax (matplotlib axes) mu (float): mean of the normal distribution std (float): standard deviation of the normal distribution Returns: None: the function adds a plot to the axes object provided """ x = np.linspace(mu - 12 * std, mu + 12 * std, 1000) y = scs.norm(mu, std).pdf(x) ax.plot(x, y, label=label) if with_CI: plot_CI(ax, mu, std, sig_level=sig_level) def plot_binom_dist(ax, n, p, label=None): """Adds a binomial distribution to the axes provided Example: plot_binom_dist(ax, 0, 1) # plots a standard normal distribution Parameters: ax (matplotlib axes) mu (float): mean of the normal distribution sig (float): standard deviation of the normal distribution Returns: None: the function adds a plot to the axes object provided """ x = np.linspace(0, n, n+1) y = scs.binom(n, p).pmf(x) ax.plot(x, y, label=label) def plot_CI(ax, mu, s, sig_level=0.05, color='grey'): """Calculates the two-tailed confidence interval and adds the plot to an axes object. Example: plot_CI(ax, mu=0, s=stderr, sig_level=0.05) Parameters: ax (matplotlib axes) mu (float): mean s (float): standard deviation Returns: None: the function adds a plot to the axes object provided """ left, right = confidence_interval(sample_mean=mu, sample_std=s, sig_level=sig_level) ax.axvline(left, c=color, linestyle='--', alpha=0.5) ax.axvline(right, c=color, linestyle='--', alpha=0.5) def plot_null(ax, stderr): """Plots the null hypothesis distribution where if there is no real change, the distribution of the differences between the test and the control groups will be normally distributed. The confidence band is also plotted. Example: plot_null(ax, stderr) Parameters: ax (matplotlib axes) stderr (float): the pooled standard error of the control and test group Returns: None: the function adds a plot to the axes object provided """ plot_norm_dist(ax, 0, stderr, label="Null") plot_CI(ax, mu=0, s=stderr, sig_level=0.05) def plot_alt(ax, stderr, d_hat): """Plots the alternative hypothesis distribution where if there is a real change, the distribution of the differences between the test and the control groups will be normally distributed and centered around d_hat The confidence band is also plotted. Example: plot_alt(ax, stderr, d_hat=0.025) Parameters: ax (matplotlib axes) stderr (float): the pooled standard error of the control and test group Returns: None: the function adds a plot to the axes object provided """ plot_norm_dist(ax, d_hat, stderr, label="Alternative") def abplot(N_A, N_B, bcr, d_hat, sig_level=0.05, show_power=False, show_alpha=False, show_beta=False, show_p_value=False, show_legend=True): """Example plot of AB test Example: abplot(n=4000, bcr=0.11, d_hat=0.03) Parameters: n (int): total sample size for both control and test groups (N_A + N_B) bcr (float): base conversion rate; conversion rate of control d_hat: difference in conversion rate between the control and test groups, sometimes referred to as **minimal detectable effect** when calculating minimum sample size or **lift** when discussing positive improvement desired from launching a change. Returns: None: the function plots an AB test as two distributions for visualization purposes """ # create a plot object fig, ax = plt.subplots(figsize=(12, 6)) # define parameters to find pooled standard error X_A = bcr * N_A X_B = (bcr + d_hat) * N_B stderr = pooled_SE(N_A, N_B, X_A, X_B) # plot the distribution of the null and alternative hypothesis plot_null(ax, stderr) plot_alt(ax, stderr, d_hat) # set extent of plot area ax.set_xlim(-8 * stderr, 8 * stderr) # shade areas according to user input if show_power: show_area(ax, d_hat, stderr, sig_level, area_type='power') if show_alpha: show_area(ax, d_hat, stderr, sig_level, area_type='alpha') if show_beta: show_area(ax, d_hat, stderr, sig_level, area_type='beta') # show p_value based on the binomial distributions for the two groups if show_p_value: null = ab_dist(stderr, 'control') p_value = p_val(N_A, N_B, bcr, bcr+d_hat) ax.text(3 * stderr, null.pdf(0), 'p-value = {0:.3f}'.format(p_value), fontsize=12, ha='left') # option to show legend if show_legend: plt.legend() plt.xlabel('d') plt.ylabel('PDF') plt.show() def show_area(ax, d_hat, stderr, sig_level, area_type='power'): """Fill between upper significance boundary and distribution for alternative hypothesis """ left, right = confidence_interval(sample_mean=0, sample_std=stderr, sig_level=sig_level) x = np.linspace(-12 * stderr, 12 * stderr, 1000) null = ab_dist(stderr, 'control') alternative = ab_dist(stderr, d_hat, 'test') # if area_type is power # Fill between upper significance boundary and distribution for alternative # hypothesis if area_type == 'power': ax.fill_between(x, 0, alternative.pdf(x), color='green', alpha=0.25, where=(x > right)) ax.text(-3 * stderr, null.pdf(0), 'power = {0:.3f}'.format(1 - alternative.cdf(right)), fontsize=12, ha='right', color='k') # if area_type is alpha # Fill between upper significance boundary and distribution for null # hypothesis if area_type == 'alpha': ax.fill_between(x, 0, null.pdf(x), color='green', alpha=0.25, where=(x > right)) ax.text(-3 * stderr, null.pdf(0), 'alpha = {0:.3f}'.format(1 - null.cdf(right)), fontsize=12, ha='right', color='k') # if area_type is beta # Fill between distribution for alternative hypothesis and upper # significance boundary if area_type == 'beta': ax.fill_between(x, 0, alternative.pdf(x), color='green', alpha=0.25, where=(x < right)) ax.text(-3 * stderr, null.pdf(0), 'beta = {0:.3f}'.format(alternative.cdf(right)), fontsize=12, ha='right', color='k') def zplot(area=0.95, two_tailed=True, align_right=False): """Plots a z distribution with common annotations Example: zplot(area=0.95) zplot(area=0.80, two_tailed=False, align_right=True) Parameters: area (float): The area under the standard normal distribution curve. align (str): The area under the curve can be aligned to the center (default) or to the left. Returns: None: A plot of the normal distribution with annotations showing the area under the curve and the boundaries of the area. """ # create plot object fig = plt.figure(figsize=(12, 6)) ax = fig.subplots() # create normal distribution norm = scs.norm() # create data points to plot x = np.linspace(-5, 5, 1000) y = norm.pdf(x) ax.plot(x, y) # code to fill areas # for two-tailed tests if two_tailed: left = norm.ppf(0.5 - area / 2) right = norm.ppf(0.5 + area / 2) ax.vlines(right, 0, norm.pdf(right), color='grey', linestyle='--') ax.vlines(left, 0, norm.pdf(left), color='grey', linestyle='--') ax.fill_between(x, 0, y, color='grey', alpha=0.25, where=(x > left) & (x < right)) plt.xlabel('z') plt.ylabel('PDF') plt.text(left, norm.pdf(left), "z = {0:.3f}".format(left), fontsize=12, rotation=90, va="bottom", ha="right") plt.text(right, norm.pdf(right), "z = {0:.3f}".format(right), fontsize=12, rotation=90, va="bottom", ha="left") # for one-tailed tests else: # align the area to the right if align_right: left = norm.ppf(1-area) ax.vlines(left, 0, norm.pdf(left), color='grey', linestyle='--') ax.fill_between(x, 0, y, color='grey', alpha=0.25, where=x > left) plt.text(left, norm.pdf(left), "z = {0:.3f}".format(left), fontsize=12, rotation=90, va="bottom", ha="right") # align the area to the left else: right = norm.ppf(area) ax.vlines(right, 0, norm.pdf(right), color='grey', linestyle='--') ax.fill_between(x, 0, y, color='grey', alpha=0.25, where=x < right) plt.text(right, norm.pdf(right), "z = {0:.3f}".format(right), fontsize=12, rotation=90, va="bottom", ha="left") # annotate the shaded area plt.text(0, 0.1, "shaded area = {0:.3f}".format(area), fontsize=12, ha='center') # axis labels plt.xlabel('z') plt.ylabel('PDF') plt.show() def abplot_CI_bars(N, X, sig_level=0.05, dmin=None): """Returns a confidence interval bar plot for multivariate tests Parameters: N (list or tuple): sample size for all groups X (list or tuple): number of conversions for each variant sig_level (float): significance level dmin (float): minimum desired lift; a red and green dashed lines are shown on the plot if dmin is provided. Returns: None: A plot of the confidence interval bars is returned inline. """ # initiate plot object fig, ax = plt.subplots(figsize=(12, 3)) # get control group values N_A = N[0] X_A = X[0] # initiate containers for standard error and differences SE = [] d = [] # iterate through X and N and calculate d and SE for idx in range(1, len(N)): X_B = X[idx] N_B = N[idx] d.append(X_B / N_B - X_A / N_A) SE.append(pooled_SE(N_A, N_B, X_A, X_B)) # convert to numpy arrays SE = np.array(SE) d = np.array(d) y = np.arange(len(N)-1) # get z value z = z_val(sig_level) # confidence interval values ci = SE * z # bar to represent the confidence interval ax.hlines(y, d-ci, d+ci, color='blue', alpha=0.35, lw=10, zorder=1) # marker for the mean ax.scatter(d, y, s=300, marker='|', lw=10, color='magenta', zorder=2) # vertical line to represent 0 ax.axvline(0, c='grey', linestyle='-') # plot veritcal dashed lines if dmin is provided if dmin is not None: ax.axvline(-dmin, c='red', linestyle='--', alpha=0.75) ax.axvline(dmin, c='green', linestyle='--', alpha=0.75) # invert y axis to show variant 1 at the top ax.invert_yaxis() # label variants on y axis labels = ['variant{}'.format(idx+1) for idx in range(len(N)-1)] plt.yticks(np.arange(len(N)-1), labels) def funnel_CI_plot(A, B, sig_level=0.05): """Returns a confidence interval bar plot for multivariate tests Parameters: A (list of tuples): (sample size, conversions) for control group funnel B (list of tuples): (sample size, conversions) for test group funnel sig_level (float): significance level Returns: None: A plot of the confidence interval bars is returned inline. """ # initiate plot object fig, ax = plt.subplots(figsize=(12, 3)) # initiate containers for standard error and differences SE = [] d = [] # iterate through X and N and calculate d and SE for idx in range(len(A)): X_A = A[idx][1] N_A = A[idx][0] X_B = B[idx][1] N_B = B[idx][0] d.append(X_B / N_B - X_A / N_A) SE.append(pooled_SE(N_A, N_B, X_A, X_B)) # convert to numpy arrays SE = np.array(SE) d = np.array(d) print(d) y = np.arange(len(A)) # get z value z = z_val(sig_level) # confidence interval values ci = SE * z # bar to represent the confidence interval ax.hlines(y, d-ci, d+ci, color='blue', alpha=0.35, lw=10, zorder=1) # marker for the mean ax.scatter(d, y, s=300, marker='|', lw=10, color='magenta', zorder=2) # vertical line to represent 0 ax.axvline(0, c='grey', linestyle='-') # invert y axis to show variant 1 at the top ax.invert_yaxis() # label variants on y axis labels = ['metric{}'.format(idx+1) for idx in range(len(A))] plt.yticks(np.arange(len(A)), labels)