def calcRank(stat,nbin,distr_hit,distr_miss,pos,selpos):
    """Bins by rank instead of value: of the causal SNPs, how many
    are in the top 5%, 10%, 15% of the values of this statistic?  same for
    non-causal SNPs.

    Input params:

       stat - for each SNP, the value of a statistic
         (e.g. the composite likelihood score)
       nbin - number of bins

       pos - for each SNP, its position on the chromosome ( in bases );
          only needed so we can tell which SNP is the causal one.
       selpos - position of causal SNP on the chromosome, in bases.

    Output params:
       
      distr_hit/distr_miss - for each bin of ranks, how many causal/non-causal SNPs fall in that bin?
        NOTE: The counts are _added_ to the existing counts in these arrays.

     Returns:

       the bin number of the bin into which the causal SNP fell. 

    """
    bin_scale = 1. / nbin
    causalBin = -1

    sortInd = stat.argsort()

    for i in range(len(stat)):
        bin = int(float(i)/len(stat)/bin_scale)
        
        if bin == nbin:
            bin -= 1
        assert bin < nbin
        if pos[sortInd[i]] == selpos:
            distr_hit[bin] += 1
            causalBin = bin
        else:
            distr_miss[bin] += 1

    return causalBin