From 47b437f685269d766e65efab84b723794859ad62 Mon Sep 17 00:00:00 2001 From: chapuisk Date: Fri, 10 Dec 2021 16:03:03 +0100 Subject: [PATCH] [Stats] add tTest statistics to have p-value from 2 vectors --- .../META-INF/MANIFEST.MF | 1 + .../ummisco/gaml/extensions/stats/Stats.java | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/ummisco.gaml.extensions.maths/META-INF/MANIFEST.MF b/ummisco.gaml.extensions.maths/META-INF/MANIFEST.MF index 55451022b2..7e27d43231 100644 --- a/ummisco.gaml.extensions.maths/META-INF/MANIFEST.MF +++ b/ummisco.gaml.extensions.maths/META-INF/MANIFEST.MF @@ -11,6 +11,7 @@ Export-Package: org.apache.commons.math3.ml.clustering, org.apache.commons.math3.random, org.apache.commons.math3.stat.descriptive, org.apache.commons.math3.stat.descriptive.moment, + org.apache.commons.math3.stat.inference, org.apache.commons.math3.stat.regression, org.apache.commons.math3.util, ummisco.gaml.extensions.maths.ode.statements diff --git a/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats.java b/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats.java index cfcbce31aa..b6c3abbd9e 100644 --- a/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats.java +++ b/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats.java @@ -25,6 +25,7 @@ import org.apache.commons.math3.random.MersenneTwister; import org.apache.commons.math3.stat.descriptive.moment.Kurtosis; import org.apache.commons.math3.stat.descriptive.moment.Skewness; +import org.apache.commons.math3.stat.inference.TTest; import com.google.common.collect.Ordering; @@ -1080,6 +1081,30 @@ public static IList KMeansPlusplusApache(final IScope scope, final IList return results.items(); } } + + @operator ( + value = "tTest", + can_be_const = false, + category = { IOperatorCategory.STATISTICAL }, + concept = { IConcept.STATISTIC }) + @doc ( + value = "Returns the observed significance level, or p-value, associated with a two-sample, " + + "two-tailed t-test comparing the means of the two input lists." + + "The number returned is the smallest significance level at which one can reject the null hypothesis", + examples = { @example ( + value = "tTest([10.0,5.0,1.0, 3.0],[1.0,10.0,5.0,1.0])", + equals = "0.01") }) + public static Double tTestPValue(final IScope scope, final IList seq1, final IList seq2) { + TTest t = new TTest(); + + double[] s1 = new double[seq1.length(scope)]; + for (int i = 0; i < seq1.length(scope); i++) { s1[i] = Cast.asFloat(scope, seq1.get(i)); } + + double[] s2 = new double[seq2.length(scope)]; + for (int i = 0; i < seq2.length(scope); i++) { s2[i] = Cast.asFloat(scope, seq2.get(i)); } + + return t.tTest(s1,s2); + } @operator ( value = "dtw",