code: def heterozygosity_expected(af, ploidy, fill=np.nan):
      
  
      # check inputs
      af = asarray_ndim(af, 2)
  
      # calculate expected heterozygosity
      out = 1 - np.sum(np.power(af, ploidy), axis=1)
  
      # fill values where allele frequencies could not be calculated
      af_sum = np.sum(af, axis=1)
      with ignore_invalid():
          out[(af_sum < 1) | np.isnan(af_sum)] = fill
  
      return out
docs: Calculate the expected rate of heterozygosity for each variant
      under Hardy-Weinberg equilibrium.
  
      Parameters
      ----------
  
      af : array_like, float, shape (n_variants, n_alleles)
          Allele frequencies array.
      ploidy : int
          Sample ploidy.
      fill : float, optional
          Use this value for variants where allele frequencies do not sum to 1.
  
      Returns
      -------
  
      he : ndarray, float, shape (n_variants,)
          Expected heterozygosity
  
      Examples
      --------
  
      >>> import allel
      >>> g = allel.GenotypeArray([[[0, 0], [0, 0], [0, 0]],
      ...                          [[0, 0], [0, 1], [1, 1]],
      ...                          [[0, 0], [1, 1], [2, 2]],
      ...                          [[1, 1], [1, 2], [-1, -1]]])
      >>> af = g.count_alleles().to_frequencies()
      >>> allel.heterozygosity_expected(af, ploidy=2)
      array([0.        , 0.5       , 0.66666667, 0.375     ])