z得分为概率,反之亦然为红宝石

时间:2015-08-07 10:50:01

标签: ruby math probability standard-deviation

如何使用ruby将z-score转换为概率?

示例:

mats

1 个答案:

答案 0 :(得分:4)

根据这篇https://stackoverflow.com/a/16197404/1062711帖子,这里的函数可以从z分数中找出p proba

def getPercent(z)
  return 0 if z < -6.5
  return 1 if z > 6.5

  factk = 1
  sum = 0
  term = 1
  k = 0

  loopStop = Math.exp(-23)
  while term.abs > loopStop do
      term = 0.3989422804 * ((-1)**k) * (z**k) / (2*k+1) / (2**k) * (z**(k+1)) /factk
      sum += term
      k += 1
      factk *= k
  end

  sum += 0.5
  1-sum
end

puts getPercent(1.76)