#Python code shown below
#This code is issued under the Creative Commons CC0 "License"
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
#Set ___domain of the graph
MaxTrials = 10000
MinTrials = 1
UCB = np.zeros(MaxTrials)
#Get points that are evenly spaced in log-space
trials = np.exp(np.linspace(np.log(MinTrials),np.log(MaxTrials),100))
#One-sided exact binomial upper confidence bound, equivalent to Clopper-Pearson
#The choice of confidence interval does matter, but this is reasonable and
#is typically the one from which Rule of 3 is derived
#See Agresti and Coull 1998 for alternative binomial confidence intervals
UCB = [1-0.05**(1/i) for i in trials]
Rule_of_3 = [3/i for i in trials]
plt.figure(figsize=(4,3.2))
plt.axes([0.17,0.13,0.79,0.8])
plt.hold(True)
A = []
a, = plt.plot(trials, UCB,'-', color='orange',lw =2.5)
b, = plt.plot(trials, Rule_of_3,'-', color='purple',lw =2.5)
#Formatting
A.append(a)
A.append(b)
ax = plt.gca()
ax.set_yscale('log')
ax.set_xscale('log')
ax.grid()
plt.xlabel("Sample Size")
plt.ylabel(r"95% Upper Confidence Bound")
leg_str = []
leg_str.append(" Exact Binomial")
leg_str.append(" Rule of Three")
bx = plt.legend(A, leg_str,numpoints=1, handletextpad=0, loc="upper right")
plt.savefig("rule_of_three.svg")