Skip to content

Instantly share code, notes, and snippets.

@nkconnor
Last active July 27, 2016 17:54
Show Gist options
  • Save nkconnor/96c7c517aaa514490bc1c8d6c8eaaf97 to your computer and use it in GitHub Desktop.
Save nkconnor/96c7c517aaa514490bc1c8d6c8eaaf97 to your computer and use it in GitHub Desktop.
import breeze.linalg._
import breeze.math._
import breeze.numerics._
case class BernoulliTrial(outcome: Boolean)
object Experiment {
def apply(successes: Int, failures: Int) = new Experiment(successes, failures)
}
case class Experiment(trials: Seq[BernoulliTrial]) {
def this(successes: Int, failures: Int) = {
this(
Seq.fill(successes)(BernoulliTrial(true)) ++
Seq.fill(failures)(BernoulliTrial(false))
)
}
def alpha = 1 + trials.count(_.outcome == true).toDouble
def beta = 1 + trials.count(_.outcome == false).toDouble
def probabilityOfBeating(otherExperiment: Experiment) = {
(0 until alpha.toInt).map { i =>
math.exp(
lbeta(Array(otherExperiment.alpha + i, otherExperiment.beta + beta))
- log(beta + i)
- lbeta(Array(1 + i, beta))
- lbeta(Array(otherExperiment.alpha, otherExperiment.beta))
)
}.sum
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment