为什么Breeze二项分布在返回1.0时会返回NaN?

时间:2014-07-24 15:05:48

标签: scala probability nan scala-breeze

以下给出NaN,但应该给1.0,为什么?

breeze.stats.distributions.Binomial(10, 1.0).probabilityOf(10)

类似于Binomial(1, 0.0).probabilityOf(0)

1 个答案:

答案 0 :(得分:1)

来自微风源。

case class Binomial(n: Int, p: Double)(implicit rand: RandBasis=Rand) extends DiscreteDistr[Int] with Moments[Double, Double] {
  require(n > 0, "n must be positive!");
  require(p >= 0.0, "p must be non-negative!");
  def probabilityOf(k: Int) = exp(logProbabilityOf(k));

  override def toString() = "Binomial(" + n + ", " + p + ")";

  override def logProbabilityOf(k: Int) = {
    require(n >= k);
    require(k >= 0);
    lgamma(n+1) - lgamma(k+1) - lgamma(n-k+1) + k * log(p) + (n-k) * log(1-p)
  }

0的日志未定义。在第一个示例中,您的p为1,因此最终为log(1-p) = log(0) = undefined,在第二个示例中为log(p) = log(0) = undefined

由于这些概率都应为1,我认为这是微风源代码中的错误