📄 ttestimpl.java
字号:
* sample means.</p> * <p> * The t-statisitc is</p> * <p> * <code> t = (m1 - m2) / sqrt(var1/n1 + var2/n2)</code> * </p><p> * where <strong><code>n1</code></strong> is the size of the first sample * <strong><code> n2</code></strong> is the size of the second sample; * <strong><code> m1</code></strong> is the mean of the first sample; * <strong><code> m2</code></strong> is the mean of the second sample; * <strong><code> var1</code></strong> is the variance of the first sample; * <strong><code> var2</code></strong> is the variance of the second sample; * </p><p> * <strong>Preconditions</strong>: <ul> * <li>The observed array lengths must both be at least 2. * </li></ul></p> * * @param sample1 array of sample data values * @param sample2 array of sample data values * @return t statistic * @throws IllegalArgumentException if the precondition is not met */ public double t(double[] sample1, double[] sample2) throws IllegalArgumentException { if ((sample1 == null) || (sample2 == null || Math.min(sample1.length, sample2.length) < 2)) { throw new IllegalArgumentException("insufficient data for t statistic"); } return t(StatUtils.mean(sample1), StatUtils.mean(sample2), StatUtils.variance(sample1), StatUtils.variance(sample2), (double) sample1.length, (double) sample2.length); } /** * Computes a 2-sample t statistic </a>, comparing the means of the datasets * described by two {@link StatisticalSummary} instances, without the * assumption of equal subpopulation variances. Use * {@link #homoscedasticT(StatisticalSummary, StatisticalSummary)} to * compute a t-statistic under the equal variances assumption. * <p> * This statistic can be used to perform a two-sample t-test to compare * sample means.</p> * <p> * The returned t-statisitc is</p> * <p> * <code> t = (m1 - m2) / sqrt(var1/n1 + var2/n2)</code> * </p><p> * where <strong><code>n1</code></strong> is the size of the first sample; * <strong><code> n2</code></strong> is the size of the second sample; * <strong><code> m1</code></strong> is the mean of the first sample; * <strong><code> m2</code></strong> is the mean of the second sample * <strong><code> var1</code></strong> is the variance of the first sample; * <strong><code> var2</code></strong> is the variance of the second sample * </p><p> * <strong>Preconditions</strong>: <ul> * <li>The datasets described by the two Univariates must each contain * at least 2 observations. * </li></ul></p> * * @param sampleStats1 StatisticalSummary describing data from the first sample * @param sampleStats2 StatisticalSummary describing data from the second sample * @return t statistic * @throws IllegalArgumentException if the precondition is not met */ public double t(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws IllegalArgumentException { if ((sampleStats1 == null) || (sampleStats2 == null || Math.min(sampleStats1.getN(), sampleStats2.getN()) < 2)) { throw new IllegalArgumentException("insufficient data for t statistic"); } return t(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(), sampleStats2.getVariance(), (double) sampleStats1.getN(), (double) sampleStats2.getN()); } /** * Computes a 2-sample t statistic, comparing the means of the datasets * described by two {@link StatisticalSummary} instances, under the * assumption of equal subpopulation variances. To compute a t-statistic * without the equal variances assumption, use * {@link #t(StatisticalSummary, StatisticalSummary)}. * <p> * This statistic can be used to perform a (homoscedastic) two-sample * t-test to compare sample means.</p> * <p> * The t-statisitc returned is</p> * <p> * <code> t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var))</code> * </p><p> * where <strong><code>n1</code></strong> is the size of first sample; * <strong><code> n2</code></strong> is the size of second sample; * <strong><code> m1</code></strong> is the mean of first sample; * <strong><code> m2</code></strong> is the mean of second sample * and <strong><code>var</code></strong> is the pooled variance estimate: * </p><p> * <code>var = sqrt(((n1 - 1)var1 + (n2 - 1)var2) / ((n1-1) + (n2-1)))</code> * <p> * with <strong><code>var1<code></strong> the variance of the first sample and * <strong><code>var2</code></strong> the variance of the second sample. * </p><p> * <strong>Preconditions</strong>: <ul> * <li>The datasets described by the two Univariates must each contain * at least 2 observations. * </li></ul></p> * * @param sampleStats1 StatisticalSummary describing data from the first sample * @param sampleStats2 StatisticalSummary describing data from the second sample * @return t statistic * @throws IllegalArgumentException if the precondition is not met */ public double homoscedasticT(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) throws IllegalArgumentException { if ((sampleStats1 == null) || (sampleStats2 == null || Math.min(sampleStats1.getN(), sampleStats2.getN()) < 2)) { throw new IllegalArgumentException("insufficient data for t statistic"); } return homoscedasticT(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(), sampleStats2.getVariance(), (double) sampleStats1.getN(), (double) sampleStats2.getN()); } /** * Returns the <i>observed significance level</i>, or * <i>p-value</i>, associated with a one-sample, two-tailed t-test * comparing the mean of the input array with the constant <code>mu</code>. * <p> * The number returned is the smallest significance level * at which one can reject the null hypothesis that the mean equals * <code>mu</code> in favor of the two-sided alternative that the mean * is different from <code>mu</code>. For a one-sided test, divide the * returned value by 2.</p> * <p> * <strong>Usage Note:</strong><br> * The validity of the test depends on the assumptions of the parametric * t-test procedure, as discussed * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">here</a> * </p><p> * <strong>Preconditions</strong>: <ul> * <li>The observed array length must be at least 2. * </li></ul></p> * * @param mu constant value to compare sample mean against * @param sample array of sample data values * @return p-value * @throws IllegalArgumentException if the precondition is not met * @throws MathException if an error occurs computing the p-value */ public double tTest(double mu, double[] sample) throws IllegalArgumentException, MathException { if ((sample == null) || (sample.length < 2)) { throw new IllegalArgumentException("insufficient data for t statistic"); } return tTest( StatUtils.mean(sample), mu, StatUtils.variance(sample), sample.length); } /** * Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm"> * two-sided t-test</a> evaluating the null hypothesis that the mean of the population from * which <code>sample</code> is drawn equals <code>mu</code>. * <p> * Returns <code>true</code> iff the null hypothesis can be * rejected with confidence <code>1 - alpha</code>. To * perform a 1-sided test, use <code>alpha * 2</code> * </p><p> * <strong>Examples:</strong><br><ol> * <li>To test the (2-sided) hypothesis <code>sample mean = mu </code> at * the 95% level, use <br><code>tTest(mu, sample, 0.05) </code> * </li> * <li>To test the (one-sided) hypothesis <code> sample mean < mu </code> * at the 99% level, first verify that the measured sample mean is less * than <code>mu</code> and then use * <br><code>tTest(mu, sample, 0.02) </code> * </li></ol></p> * <p> * <strong>Usage Note:</strong><br> * The validity of the test depends on the assumptions of the one-sample * parametric t-test procedure, as discussed * <a href="http://www.basic.nwu.edu/statguidefiles/sg_glos.html#one-sample">here</a> * </p><p> * <strong>Preconditions</strong>: <ul> * <li>The observed array length must be at least 2. * </li></ul></p> * * @param mu constant value to compare sample mean against * @param sample array of sample data values * @param alpha significance level of the test * @return p-value * @throws IllegalArgumentException if the precondition is not met * @throws MathException if an error computing the p-value */ public boolean tTest(double mu, double[] sample, double alpha) throws IllegalArgumentException, MathException { if ((alpha <= 0) || (alpha > 0.5)) { throw new IllegalArgumentException("bad significance level: " + alpha); } return (tTest(mu, sample) < alpha); } /** * Returns the <i>observed significance level</i>, or * <i>p-value</i>, associated with a one-sample, two-tailed t-test * comparing the mean of the dataset described by <code>sampleStats</code> * with the constant <code>mu</code>. * <p> * The number returned is the smallest significance level * at which one can reject the null hypothesis that the mean equals * <code>mu</code> in favor of the two-sided alternative that the mean * is different from <code>mu</code>. For a one-sided test, divide the * returned value by 2.</p> * <p> * <strong>Usage Note:</strong><br> * The validity of the test depends on the assumptions of the parametric * t-test procedure, as discussed * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html"> * here</a></p> * <p> * <strong>Preconditions</strong>: <ul> * <li>The sample must contain at least 2 observations. * </li></ul></p> * * @param mu constant value to compare sample mean against * @param sampleStats StatisticalSummary describing sample data * @return p-value * @throws IllegalArgumentException if the precondition is not met * @throws MathException if an error occurs computing the p-value */ public double tTest(double mu, StatisticalSummary sampleStats) throws IllegalArgumentException, MathException { if ((sampleStats == null) || (sampleStats.getN() < 2)) { throw new IllegalArgumentException("insufficient data for t statistic"); } return tTest(sampleStats.getMean(), mu, sampleStats.getVariance(), sampleStats.getN()); } /** * Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm"> * two-sided t-test</a> evaluating the null hypothesis that the mean of the * population from which the dataset described by <code>stats</code> is * drawn equals <code>mu</code>. * <p> * Returns <code>true</code> iff the null hypothesis can be rejected with * confidence <code>1 - alpha</code>. To perform a 1-sided test, use * <code>alpha * 2.</code></p> * <p> * <strong>Examples:</strong><br><ol> * <li>To test the (2-sided) hypothesis <code>sample mean = mu </code> at * the 95% level, use <br><code>tTest(mu, sampleStats, 0.05) </code> * </li> * <li>To test the (one-sided) hypothesis <code> sample mean < mu </code> * at the 99% level, first verify that the measured sample mean is less * than <code>mu</code> and then use * <br><code>tTest(mu, sampleStats, 0.02) </code> * </li></ol></p> * <p> * <strong>Usage Note:</strong><br> * The validity of the test depends on the assumptions of the one-sample * parametric t-test procedure, as discussed * <a href="http://www.basic.nwu.edu/statguidefiles/sg_glos.html#one-sample">here</a> * </p><p> * <strong>Preconditions</strong>: <ul> * <li>The sample must include at least 2 observations. * </li></ul></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -