code: def ascertainment_bias(mt, y, P):
      r"""Adds ascertainment bias to a binary phenotype to give it a sample
      prevalence of `P` = cases/(cases+controls).
  
      Parameters
      ----------
      mt : :class:`.MatrixTable`
          :class:`.MatrixTable` containing binary phenotype to be used.
      y : :class:`.Expression`
          Column field of binary phenotype.
      P : :obj:`int` or :obj:`float`
          Desired "sample prevalence" of phenotype.
  
      Returns
      -------
      :class:`.MatrixTable`
          :class:`.MatrixTable` containing binary phenotype with prevalence of approx. P
      """
      assert P >= 0 and P <= 1, "P must be in [0,1]"
      uid = Env.get_uid(base=100)
      mt = mt.annotate_cols(y_w_asc_bias=y)
      y_stats = mt.aggregate_cols(hl.agg.stats(mt.y_w_asc_bias))
      K = y_stats.mean
      n = y_stats.n
      assert (
          abs(P - K) < 1
      ), "Specified sample prevalence is incompatible with population prevalence."
      if P < K:
          p = (1 - K) * P / (K * (1 - P))
          con = mt.filter_cols(mt.y_w_asc_bias == 0)
          cas = mt.filter_cols(mt.y_w_asc_bias == 1).add_col_index(
              name="col_idx_" + uid
          )
          keep = round(p * n * K) * [1] + round((1 - p) * n * K) * [0]
          cas = cas.annotate_cols(
              **{
                  "keep_"
                  + uid: hl.literal(keep)[
                      hl.int32(cas["col_idx_" + uid])
                  ]
              }
          )
          cas = cas.filter_cols(cas["keep_" + uid] == 1)
          cas = _clean_fields(cas, uid)
          mt = cas.union_cols(con)
      elif P > K:
          p = K * (1 - P) / ((1 - K) * P)
          cas = mt.filter_cols(mt.y_w_asc_bias == 1)
          con = mt.filter_cols(mt.y_w_asc_bias == 0).add_col_index(
              name="col_idx_" + uid
          )
          keep = round(p * n * (1 - K)) * [1] + round(
              (1 - p) * n * (1 - K)
          ) * [0]
          con = con.annotate_cols(
              **{
                  "keep_"
                  + uid: hl.literal(keep)[
                      hl.int32(con["col_idx_" + uid])
                  ]
              }
          )
          con = con.filter_cols(con["keep_" + uid] == 1)
          con = _clean_fields(con, uid)
          mt = con.union_cols(cas)
      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 13 for calling a global function with example usage: ascertainment_bias(mt, y, P) and returns: mt