samthebest
samthebest

Reputation: 31513

Why does Breeze binomial distribution return NaN when it should return 1.0?

The following gives NaN, but should give 1.0, why?

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

similarily for Binomial(1, 0.0).probabilityOf(0)

Upvotes: 1

Views: 468

Answers (1)

mistahenry
mistahenry

Reputation: 8724

From the breeze source.

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)
  }

The log of 0 is undefined. In your first example, you have p as 1 so the final log(1-p) = log(0) = undefined and in your second example, log(p) = log(0) = undefined

Since these probabilities both should have been 1, I think this is a bug in breeze source code

Upvotes: 1

Related Questions