z-score to probability and vice verse in ruby

泄露秘密 提交于 2019-12-11 04:56:54

问题


How can I convert z-score to probability using ruby?

Example:

z_score = 0
probability should be 0.5

z_score = 1.76
probability should be 0.039204

回答1:


According to this https://stackoverflow.com/a/16197404/1062711 post, here is the function that give you the p proba from the z score

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)


来源:https://stackoverflow.com/questions/31875909/z-score-to-probability-and-vice-verse-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!