docstr: Implements the KL-LUCB algorithm (Kaufmann and Kalyanakrishnan, 2013).
  
  Parameters
  ----------
  anchors:
      A list of anchors from which two critical anchors are selected (see Kaufmann and Kalyanakrishnan, 2013).
  init_stats
      Dictionary with lists containing nb of samples used and where sample predictions equal the desired label.
  epsilon
      Precision bound tolerance for convergence.
  delta
      Used to compute `beta`.
  batch_size
      Number of samples.
  top_n
      Min of beam width size or number of candidate anchors.
  verbose
      Whether to print intermediate output.
  verbose_every
      Whether to print intermediate output every `verbose_every` steps.
  
  Returns
  -------
  Indices of best result options. Number of indices equals min of beam width or nb of candidate anchors.
docstr_tok: ["Implements","the","KL-LUCB","algorithm","(","Kaufmann","and","Kalyanakrishnan",",","2013",")",".","Parameters","--","--","--","--","--","anchors",":","A","list","of","anchors","from","which","two","critical","anchors","are","selected","(","see","Kaufmann","and","Kalyanakrishnan",",","2013",")",".","init_stats","Dictionary","with","lists","containing","nb","of","samples","used","and","where","sample","predictions","equal","the","desired","label",".","epsilon","Precision","bound","tolerance","for","convergence",".","delta","Used","to","compute","`","beta","`",".","batch_size","Number","of","samples",".","top_n","Min","of","beam","width","size","or","number","of","candidate","anchors",".","verbose","Whether","to","print","intermediate","output",".","verbose_every","Whether","to","print","intermediate","output","every","`","verbose_every","`","steps",".","Returns","--","--","--","-","Indices","of","best","result","options",".","Number","of","indices","equals","min","of","beam","width","or","nb","of","candidate","anchors","."]
code: def kllucb(
      self,
      anchors: list,
      init_stats: dict,
      epsilon: float,
      delta: float,
      batch_size: int,
      top_n: int,
      verbose: bool = False,
      verbose_every: int = 1,
  ) -> np.ndarray:
      """
      Implements the KL-LUCB algorithm (Kaufmann and Kalyanakrishnan, 2013).
  
      Parameters
      ----------
      anchors:
          A list of anchors from which two critical anchors are selected (see Kaufmann and Kalyanakrishnan, 2013).
      init_stats
          Dictionary with lists containing nb of samples used and where sample predictions equal the desired label.
      epsilon
          Precision bound tolerance for convergence.
      delta
          Used to compute `beta`.
      batch_size
          Number of samples.
      top_n
          Min of beam width size or number of candidate anchors.
      verbose
          Whether to print intermediate output.
      verbose_every
          Whether to print intermediate output every `verbose_every` steps.
  
      Returns
      -------
      Indices of best result options. Number of indices equals min of beam width or nb of candidate anchors.
      """
  
      # n_features equals to the nb of candidate anchors
      n_features = len(anchors)
  
      # arrays for total number of samples & positives (# samples where prediction equals desired label)
      n_samples, positives = (
          init_stats["n_samples"],
          init_stats["positives"],
      )
      anchors_to_sample, anchors_idx = [], []
      for f in np.where(n_samples == 0)[0]:
          anchors_to_sample.append(anchors[f])
          anchors_idx.append(f)
  
      if anchors_idx:
          pos, total = self.draw_samples(anchors_to_sample, 1)
          positives[anchors_idx] += pos
          n_samples[anchors_idx] += total
  
      if (
          n_features == top_n
      ):  # return all options b/c of beam search width
          return np.arange(n_features)
  
      # update the upper and lower precision bounds until the difference between the best upper ...
      # ... precision bound of the low precision anchors and the worst lower precision bound of the high ...
      # ... precision anchors is smaller than eps
      means = (
          positives / n_samples
      )  # fraction sample predictions equal to desired label
      ub, lb = np.zeros(n_samples.shape), np.zeros(n_samples.shape)
      t = 1
      crit_a_idx = self.select_critical_arms(
          means, ub, lb, n_samples, delta, top_n, t
      )
      B = ub[crit_a_idx.ut] - lb[crit_a_idx.lt]
      verbose_count = 0
  
      while B > epsilon:
          verbose_count += 1
          if verbose and verbose_count % verbose_every == 0:
              ut, lt = crit_a_idx
              print(
                  "Best: %d (mean:%.10f, n: %d, lb:%.4f)"
                  % (lt, means[lt], n_samples[lt], lb[lt]),
                  end=" ",
              )
              print(
                  "Worst: %d (mean:%.4f, n: %d, ub:%.4f)"
                  % (ut, means[ut], n_samples[ut], ub[ut]),
                  end=" ",
              )
              print("B = %.2f" % B)
  
          # draw samples for each critical result, update anchors' mean, upper and lower
          # bound precision estimate
          selected_anchors = [anchors[idx] for idx in crit_a_idx]
          pos, total = self.draw_samples(selected_anchors, batch_size)
          idx = list(crit_a_idx)
          positives[idx] += pos
          n_samples[idx] += total
          means = positives / n_samples
          t += 1
          crit_a_idx = self.select_critical_arms(
              means, ub, lb, n_samples, delta, top_n, t
          )
          B = ub[crit_a_idx.ut] - lb[crit_a_idx.lt]
      sorted_means = np.argsort(means)
  
      return sorted_means[-top_n:]
  
imports: import copy
  import logging
  from collections import defaultdict, namedtuple
  from typing import Callable, Dict, List, Optional, Set, Tuple
  import numpy
  from alibi.utils.distributions import kl_bernoulli
label_desc: Use image node_id 9 for calling the AnchorBaseBeam obj's underlying member method code with example usage: obj.kllucb(anchors, init_stats, epsilon, delta, batch_size, top_n, verbose, verbose_every) and returns: sorted_means, np