Poisson Distribution

Example

import slash.stats.probability.distributions.Poisson
import narr.*

val dist1 = Poisson(1)
// dist1: Poisson = Poisson(λ = 1.0)
for (i <- 0 to 5) {
  println( s"Poisson(1).pdf($i) = ${dist1.p(i)}" )
}
// Poisson(1).pdf(0) = 0.36787944117144233
// Poisson(1).pdf(1) = 0.36787944117144233
// Poisson(1).pdf(2) = 0.18393972058572114
// Poisson(1).pdf(3) = 0.06131324019524039
// Poisson(1).pdf(4) = 0.015328310048810101
// Poisson(1).pdf(5) = 0.00306566200976202

dist1// res1: Double = 1.0
dist1.`σ²`
// res2: Double = 1.0

val rand = slash.Random.defaultRandom
// rand: Random = scala.util.Random@54dc2ae9
type N = 10000
val v : NArray[Long] = dist1.sample(10000, rand)
// v: Array[Long] = Array(
//   2L,
//   1L,
//   0L,
//   1L,
//   2L,
//   1L,
//   2L,
//   2L,
//   1L,
//   1L,
//   0L,
//   0L,
//   3L,
//   3L,
//   0L,
//   1L,
//   2L,
//   0L,
//   1L,
//   2L,
//   0L,
//   2L,
//   1L,
//   1L,
//   2L,
//   1L,
//   1L,
//   2L,
//   1L,
//   0L,
//   1L,
//   1L,
//   1L,
//   2L,
//   0L,
//   1L,
//   0L,
//   0L,
//   0L,
//   0L,
//   1L,
//   1L,
//   0L,
//   2L,
//   0L,
//   0L,
//   2L,
//   0L,
// ...

Funsies

Our poisson distribution cross compiles to scalaJS... it turns out to be pretty simple from here, to build up a tiny app to investigate the Poisson distribution.

As we're using native (read - pretty quick) JS arrays behind the scenes - we can rag the ui thread pretty hard (100k samples) before we start to see any lag in our plots and stats.

For funsies - we compare theoretical and empirical distributions. This little app lets you pick a λ and a number of samples. Hover over a bar to see it's theoretical and sampled probability.