Skip to content

Instantly share code, notes, and snippets.

@malcolmgreaves
Last active November 8, 2016 17:34
Show Gist options
  • Select an option

  • Save malcolmgreaves/67699cfcfdda26ea5545d25bb0ad7bda to your computer and use it in GitHub Desktop.

Select an option

Save malcolmgreaves/67699cfcfdda26ea5545d25bb0ad7bda to your computer and use it in GitHub Desktop.
Self-contained binomial hypothesis testing. Specifically 2-tailed test at a confidence interval of 95%.
object BinomialHypothesisTest {
def tTestBinomial(p1:Double, n1:Int, p2:Double, n2:Int): TtestRes = {
val k = (n1*p1 + n2*p2) / (n1 + n2).toDouble
val z = (p1 - p2) / math.sqrt( k*(1 - k) * (1 / n1.toDouble + 1 / n2.toDouble) )
if(z > scoreForTwoTailed95CiPval)
RejectNull
else
FailToReject
}
private[this] val scoreForTwoTailed95CiPval = 1.96
sealed abstract class TtestRes
case object RejectNull extends TtestRes {
override val toString = "Reject the null hypothesis."
}
case object FailToReject extends TtestRes {
override val toString = "Fail to reject the null hypothesis."
}
}