Last active
November 8, 2016 17:34
-
-
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%.
This file contains hidden or 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
| 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." | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Resources:
[1] https://en.wikipedia.org/wiki/Statistical_hypothesis_testing
[2] http://stats.stackexchange.com/questions/113602/test-if-two-binomial-distributions-are-statistically-different-from-each-other
[3] http://www.ats.ucla.edu/stat/mult_pkg/faq/general/tail_tests.htm