Created
August 30, 2017 10:17
-
-
Save invkrh/6a9be6831e76560ea75b14d8f57fd324 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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