Last active
July 27, 2016 17:54
-
-
Save nkconnor/96c7c517aaa514490bc1c8d6c8eaaf97 to your computer and use it in GitHub Desktop.
Scala implementation of http://www.evanmiller.org/bayesian-ab-testing.html#binary_ab_implementation
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 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