code: def binarize(mt, y, K, exact=False):
      r"""Binarize phenotype `y` such that it has prevalence `K` = cases/(cases+controls)
      Uses inverse CDF of Gaussian to set binarization threshold when `exact` = False,
      otherwise uses ranking to determine threshold.
  
      Parameters
      ----------
      mt : :class:`.MatrixTable`
          :class:`.MatrixTable` containing phenotype to be binarized.
      y : :class:`.Expression`
          Column field of phenotype.
      K : :obj:`int` or :obj:`float`
          Desired "population prevalence" of phenotype.
      exact : :obj:`bool`
          Whether to get prevalence as close as possible to `K` (does not use inverse CDF)
  
      Returns
      -------
      :class:`.MatrixTable`
          :class:`.MatrixTable` containing binary phenotype with prevalence of approx. `K`
      """
      if exact:
          key = list(mt.col_key)
          uid = Env.get_uid(base=100)
          mt = mt.annotate_cols(**{"y_" + uid: y})
          tb = mt.cols().order_by("y_" + uid)
          tb = tb.add_index("idx_" + uid)
          n = tb.count()
          # "+ 1" because of zero indexing
          tb = tb.annotate(
              y_binarized=tb["idx_" + uid] + 1 <= round(n * K)
          )
          tb, mt = tb.key_by("y_" + uid), mt.key_cols_by("y_" + uid)
          mt = mt.annotate_cols(
              y_binarized=tb[mt["y_" + uid]].y_binarized
          )
          mt = mt.key_cols_by(*map(lambda x: mt[x], key))
      else:  # use inverse CDF
          y_stats = mt.aggregate_cols(hl.agg.stats(y))
          threshold = stats.norm.ppf(
              1 - K, loc=y_stats.mean, scale=y_stats.stdev
          )
          mt = mt.annotate_cols(y_binarized=y > threshold)
      return mt
  
imports: import hail
  from hail.typecheck import typecheck, oneof, nullable
  from hail.expr.expressions import expr_float64, expr_int32, expr_array, expr_call
  from hail.matrixtable import MatrixTable
  from hail.table import Table
  from hail.utils.java import Env
  import numpy
  import pandas
  import scipy.stats
label_desc: Use image node_id 14 for calling a global function with example usage: binarize(mt, y, K, exact) and returns: mt