Skip to content

Instantly share code, notes, and snippets.

@avibryant
Created September 28, 2012 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avibryant/3802616 to your computer and use it in GitHub Desktop.
Save avibryant/3802616 to your computer and use it in GitHub Desktop.
Likelihood ratio test for binomials
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