There is no standard library function to do this. How can I do it efficiently?
Asked
Active
Viewed 1,102 times
2 Answers
3
Scala Breeze, http://www.scalanlp.org/ has a Poisson class in its stats.distributions package.
case class Poisson(mean: Double)(implicit rand: RandBasis = Rand)

Keith Pinson
- 1,725
- 1
- 14
- 17
0
I translated this answer to Scala:
def recursive_poisson_helper(m:Long, r:Long, p:Double, i:Long):Double = {
if (r == i) {
p
} else {
recursive_poisson_helper(m, r, (p * m) / (i + 1), i + 1)
}
}
def efficient_poisson(m:Long, r:Long): Double = {
val p = math.exp(-m)
recursive_poisson_helper(m, r, p, 0)
}

Community
- 1
- 1

Moishe Lettvin
- 8,462
- 1
- 26
- 40
-
`i` should probably be a `Long` as well otherwise you shouldn’t really test for equality. – Debilski Apr 22 '13 at 21:24
-
And the function names don't follow Scalas style guide, there are all these unnecessary curly braces and `recursive_poisson_helper` should be a inner function. – kiritsuku Apr 22 '13 at 21:39