code: def binomial_confidence_interval(successes, trials, error_rate):
      """Computes a confidence interval on the true p of a binomial.
  
      Assumes:
      - The given `successes` count outcomes of an iid Bernoulli trial
        with unknown probability p, that was repeated `trials` times.
  
      Guarantees:
      - The probability (over the randomness of drawing the given sample)
        that the true p is outside the returned interval is no more than
        the given `error_rate`.
  
      Args:
        successes: Python or numpy `int` number of successes.
        trials: Python or numpy `int` number of trials.
        error_rate: Python `float` admissible rate of mistakes.
  
      Returns:
        low_p: Lower bound of confidence interval.
        high_p: Upper bound of confidence interval.
  
      Raises:
        ValueError: If scipy is not available.
  
      """
  
      def p_small_enough(p):
          # This is positive iff p is smaller than the desired upper bound.
          log_prob = stats.binom.logcdf(successes, trials, p)
          return log_prob - np.log(error_rate / 2.0)
  
      def p_big_enough(p):
          # This is positive iff p is larger than the desired lower bound.
          # Scipy's survival function for discrete random variables excludes
          # the argument, but I want it included for this purpose.
          log_prob = stats.binom.logsf(successes - 1, trials, p)
          return log_prob - np.log(error_rate / 2.0)
  
      if successes < trials:
          high_p = optimize.brentq(
              p_small_enough, successes / float(trials), 1.0, rtol=1e-9
          )
      else:
          high_p = 1.0
      if successes > 0:
          low_p = optimize.brentq(
              p_big_enough, 0.0, successes / float(trials), rtol=1e-9
          )
      else:
          low_p = 0.0
      return low_p, high_p
  
imports: import collections
  import numpy
  import scipy.optimize
  import scipy.stats
label_desc: Use image node_id 1 for calling a global function with example usage: binomial_confidence_interval(successes, trials, error_rate) and returns: low_p,  high_p, unknown, unknown