Skip to content

Commit

Permalink
Merge pull request #847 from smoteval/sina-ttest-correction
Browse files Browse the repository at this point in the history
Fix t-test p-value calculation (cdf instead of pdf)
  • Loading branch information
dlwh authored Feb 12, 2024
2 parents 5c0cd8c + 0ace42b commit c2eba11
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions math/src/main/scala/breeze/stats/hypothesis/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ package object hypothesis {
val tScore = (mu1 - mu2) / sqrt((var1 / n1) + (var2 / n2)) // T statistic
val dof = pow((var1 / n1) + (var2 / n2), 2) / (pow(var1, 2) / (pow(n1, 2) * (n1 - 1)) +
pow(var2, 2) / (pow(n2, 2) * (n2 - 1))) //Welch–Satterthwaite equation
new StudentsT(dof)(RandBasis.mt0).unnormalizedPdf(tScore) //return p value
new StudentsT(dof)(RandBasis.mt0).cdf(tScore) //return p value
}

def tTest[T](it1: Traversable[T])(implicit numeric: Numeric[T]): Double =
Expand All @@ -40,7 +40,7 @@ package object hypothesis {
val MeanAndVariance(mu1, var1, n1) = meanAndVariance(it1)
val Z = mu1 / sqrt(var1 / n1)
val dof = n1 - 1
new StudentsT(dof)(RandBasis.mt0).unnormalizedPdf(Z) //return p value
new StudentsT(dof)(RandBasis.mt0).cdf(Z) //return p value
}

case class Chi2Result(chi2: Double, pVal: Double)
Expand Down
6 changes: 3 additions & 3 deletions math/src/test/scala/breeze/stats/hypothesis/TTestTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import matchers.should.Matchers._
class TTestTest extends AnyFunSuite {
val threshold = 0.01
test("T Test two sample") {
tTest(List(1.0, 1, 2, 3), List(9.0, 9, 8, 9)) should be(4.29E-5 +- threshold)
tTest(List(1.0, 1, 2, 3), List(9.0, 9, 8, 9)) should be(4.752E-5 +- threshold)
}
test("T Test one sample") {
tTest(Array(1.0, 1, 2, 3)) should be(0.0336 +- threshold)
tTest(Array(1.0, 1, 2, 3)) should be(0.982 +- threshold)
}
test("T Test one sample for Traversable") {
//This test is designed to detect this bug, just in case a refactoring re-introduces it: https://github.com/scalanlp/breeze/issues/486
tTest(List(1.0, 1, 2, 3)) should be(0.0336 +- threshold)
tTest(List(1.0, 1, 2, 3)) should be(0.982323 +- threshold)
}
test("T Test one sample should throw error when given vector of length 1") {
intercept[IllegalArgumentException] {
Expand Down

0 comments on commit c2eba11

Please sign in to comment.