Created
September 28, 2012 23:36
-
-
Save avibryant/3802616 to your computer and use it in GitHub Desktop.
Likelihood ratio test for binomials
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
def likelihoodRatio(k1 : Int, n1 : Int, k2 : Int, n2 : Int) = { | |
def kLogP(k : Int, p : Double) = if(k == 0) 0 else k * math.log(p) | |
def logL(p : Double, k : Int, n : Int) = kLogP(k, p) + kLogP(n - k, 1 - p) | |
val p1 = k1.toDouble / n1.toDouble | |
val p2 = k2.toDouble / n2.toDouble | |
val p = (k1 + k2).toDouble / (n1 + n2).toDouble | |
logL(p1, k1, n1) + logL(p2, k2, n2) - logL(p, k1, n1) - logL(p, k2, n2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment