Skip to content

Instantly share code, notes, and snippets.

@invkrh
Created August 30, 2017 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save invkrh/6a9be6831e76560ea75b14d8f57fd324 to your computer and use it in GitHub Desktop.
Save invkrh/6a9be6831e76560ea75b14d8f57fd324 to your computer and use it in GitHub Desktop.
import scala.util.Random
object Bootstrap {
def generate[T](seed: T, numBootStraps: Int): Iterator[Int] = {
val rng = new Random(seed.hashCode())
(0 until numBootStraps)
.toIterator
.map(_ => samplePoisson(rng, 1.0))
}
def samplePoisson(rng: Random, lambda: Double): Int = {
val expLambda = math.exp(-lambda)
var k = 0
var p = 1.0
do {
k += 1
p *= rng.nextDouble()
} while (p > expLambda)
k - 1
}
}
val a = (1 to 100).flatMap(i => Bootstrap.generate(i, 30).toList.zipWithIndex).groupBy(_._2).mapValues(v => v.map(_._1).sum).values
a.sum / a.size.toDouble
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment