From 4f12877f683ffef9a0ac64ae1746539bd5b0f33e Mon Sep 17 00:00:00 2001 From: Sina Motevalli Bashi Date: Thu, 18 Aug 2022 10:35:55 -0400 Subject: [PATCH 1/2] fix ttest p-value --- math/src/main/scala/breeze/stats/hypothesis/package.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/math/src/main/scala/breeze/stats/hypothesis/package.scala b/math/src/main/scala/breeze/stats/hypothesis/package.scala index 6a7843d3d..508df9c9c 100644 --- a/math/src/main/scala/breeze/stats/hypothesis/package.scala +++ b/math/src/main/scala/breeze/stats/hypothesis/package.scala @@ -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 = @@ -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) From 0ace42bb3503b54946fb65588987be0b089fac4f Mon Sep 17 00:00:00 2001 From: Sina Motevalli Bashi Date: Thu, 18 Aug 2022 10:46:09 -0400 Subject: [PATCH 2/2] fix tests --- math/src/test/scala/breeze/stats/hypothesis/TTestTest.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/math/src/test/scala/breeze/stats/hypothesis/TTestTest.scala b/math/src/test/scala/breeze/stats/hypothesis/TTestTest.scala index 28355f965..dd32b04e6 100644 --- a/math/src/test/scala/breeze/stats/hypothesis/TTestTest.scala +++ b/math/src/test/scala/breeze/stats/hypothesis/TTestTest.scala @@ -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] {