docstr: Configure the test statistic thresholds via bootstrapping.
docstr_tok: ["Configure","the","test","statistic","thresholds","via","bootstrapping","."]
code: def _configure_thresholds(self):
      """
      Configure the test statistic thresholds via bootstrapping.
      """
      # Each bootstrap sample splits the reference samples into a sub-reference sample (x)
      # and an extended test window (y). The extended test window will be treated as W overlapping
      # test windows of size W (so 2W-1 test samples in total)
  
      w_size = self.window_size
      etw_size = 2 * w_size - 1  # etw = extended test window
      rw_size = self.n - etw_size  # rw = ref window
  
      perms = [
          tf.random.shuffle(tf.range(self.n))
          for _ in range(self.n_bootstraps)
      ]
      x_inds_all = [perm[:-etw_size] for perm in perms]
      y_inds_all = [perm[-etw_size:] for perm in perms]
  
      if self.verbose:
          print("Generating permutations of kernel matrix..")
      # Need to compute mmd for each bs for each of W overlapping windows
      # Most of the computation can be done once however
      # We avoid summing the rw_size^2 submatrix for each bootstrap sample by instead computing the full
      # sum once and then subtracting the relavent parts (k_xx_sum = k_full_sum - 2*k_xy_sum - k_yy_sum).
      # We also reduce computation of k_xy_sum from O(nW) to O(W) by caching column sums
  
      k_full_sum = tf.reduce_sum(zero_diag(self.k_xx))
      k_xy_col_sums_all = [
          tf.reduce_sum(
              subset_matrix(self.k_xx, x_inds, y_inds), axis=0
          )
          for x_inds, y_inds in (
              tqdm(zip(x_inds_all, y_inds_all), total=self.n_bootstraps)
              if self.verbose
              else zip(x_inds_all, y_inds_all)
          )
      ]
      k_xx_sums_all = [
          (
              k_full_sum
              - tf.reduce_sum(
                  zero_diag(subset_matrix(self.k_xx, y_inds, y_inds))
              )
              - 2 * tf.reduce_sum(k_xy_col_sums)
          )
          / (rw_size * (rw_size - 1))
          for y_inds, k_xy_col_sums in zip(
              y_inds_all, k_xy_col_sums_all
          )
      ]
      k_xy_col_sums_all = [
          k_xy_col_sums / (rw_size * w_size)
          for k_xy_col_sums in k_xy_col_sums_all
      ]
  
      # Now to iterate through the W overlapping windows
      thresholds = []
      p_bar = (
          tqdm(range(w_size), "Computing thresholds")
          if self.verbose
          else range(w_size)
      )
      for w in p_bar:
          y_inds_all_w = [
              y_inds[w : w + w_size] for y_inds in y_inds_all
          ]  # test windows of size W
          mmds = [
              (
                  k_xx_sum
                  + tf.reduce_sum(
                      zero_diag(
                          subset_matrix(self.k_xx, y_inds_w, y_inds_w)
                      )
                  )
                  / (w_size * (w_size - 1))
                  - 2 * tf.reduce_sum(k_xy_col_sums[w : w + w_size])
              )
              for k_xx_sum, y_inds_w, k_xy_col_sums in zip(
                  k_xx_sums_all, y_inds_all_w, k_xy_col_sums_all
              )
          ]
          mmds = tf.stack(
              mmds, axis=0
          )  # an mmd for each bootstrap sample
  
          # Now we discard all bootstrap samples for which mmd is in top (1/ert)% and record the thresholds
          thresholds.append(quantile(mmds, 1 - self.fpr))
          y_inds_all = [
              y_inds_all[i]
              for i in range(len(y_inds_all))
              if mmds[i] < thresholds[-1]
          ]
          k_xx_sums_all = [
              k_xx_sums_all[i]
              for i in range(len(k_xx_sums_all))
              if mmds[i] < thresholds[-1]
          ]
          k_xy_col_sums_all = [
              k_xy_col_sums_all[i]
              for i in range(len(k_xy_col_sums_all))
              if mmds[i] < thresholds[-1]
          ]
  
      self.thresholds = thresholds
  
file_path: alibi-detect/alibi_detect/cd/tensorflow/mmd_online.py
imports: from tqdm import tqdm
  import numpy
  import tensorflow
  from typing import Any, Callable, Optional, Union
  from alibi_detect.cd.base_online import BaseMultiDriftOnline
  from alibi_detect.utils.tensorflow.kernels import GaussianRBF
  from alibi_detect.utils.tensorflow import zero_diag, quantile, subset_matrix
  from alibi_detect.utils.frameworks import Framework
label_desc: Use image node_id 4 for calling the MMDDriftOnlineTF obj's underlying member method code with example usage: obj._configure_thresholds() without return types