Skip to content

Instantly share code, notes, and snippets.

@sanity
Created June 9, 2009 22:00
Show Gist options
  • Save sanity/126828 to your computer and use it in GitHub Desktop.
Save sanity/126828 to your computer and use it in GitHub Desktop.
object MartelliEstimator {
def main(args : Array[String]) : Unit = {
for (bought <- 0 until 5) {
val result = estimate(List(0.3, 0.7), bought, 4-bought)
println("b="+bought+", p="+result)
}
}
def estimate(priorProbs : List[Double], successes : Int, failures : Int) : Double = {
val condProbs = priorProbs.map( p => Math.pow(p, successes) * Math.pow(1.0-p, failures))
val normalize = 1.0 / sum(condProbs)
val priorMeta = condProbs.map(cp => normalize * cp)
sum(priorMeta.zip(priorProbs).map({case (a, b) => a*b}))
}
private def sum (l : List[Double]) : Double = {
l.foldRight (0.0) (_ + _)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment