⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ttestimpl.java

📁 Apache的common math数学软件包
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.math.stat.inference;import org.apache.commons.math.MathException;import org.apache.commons.math.distribution.DistributionFactory;import org.apache.commons.math.distribution.TDistribution;import org.apache.commons.math.distribution.TDistributionImpl;import org.apache.commons.math.stat.StatUtils;import org.apache.commons.math.stat.descriptive.StatisticalSummary;/** * Implements t-test statistics defined in the {@link TTest} interface. * <p> * Uses commons-math {@link org.apache.commons.math.distribution.TDistribution} * implementation to estimate exact p-values.</p> * * @version $Revision: 617953 $ $Date: 2008-02-02 22:54:00 -0700 (Sat, 02 Feb 2008) $ */public class TTestImpl implements TTest  {    /** Distribution used to compute inference statistics. */    private TDistribution distribution;        /**     * Default constructor.     */    public TTestImpl() {        this(new TDistributionImpl(1.0));    }        /**     * Create a test instance using the given distribution for computing     * inference statistics.     * @param t distribution used to compute inference statistics.     * @since 1.2     */    public TTestImpl(TDistribution t) {        super();        setDistribution(t);    }        /**     * Computes a paired, 2-sample t-statistic based on the data in the input      * arrays.  The t-statistic returned is equivalent to what would be returned by     * computing the one-sample t-statistic {@link #t(double, double[])}, with     * <code>mu = 0</code> and the sample array consisting of the (signed)      * differences between corresponding entries in <code>sample1</code> and      * <code>sample2.</code>     * <p>     * <strong>Preconditions</strong>: <ul>     * <li>The input arrays must have the same length and their common length     * must 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     * @throws MathException if the statistic can not be computed do to a     *         convergence or other numerical error.     */    public double pairedT(double[] sample1, double[] sample2)        throws IllegalArgumentException, MathException {        if ((sample1 == null) || (sample2 == null ||                Math.min(sample1.length, sample2.length) < 2)) {            throw new IllegalArgumentException("insufficient data for t statistic");        }        double meanDifference = StatUtils.meanDifference(sample1, sample2);        return t(meanDifference, 0,                  StatUtils.varianceDifference(sample1, sample2, meanDifference),                (double) sample1.length);    }     /**     * Returns the <i>observed significance level</i>, or      * <i> p-value</i>, associated with a paired, two-sample, two-tailed t-test      * based on the data in the input arrays.     * <p>     * The number returned is the smallest significance level     * at which one can reject the null hypothesis that the mean of the paired     * differences is 0 in favor of the two-sided alternative that the mean paired      * difference is not equal to 0. For a one-sided test, divide the returned      * value by 2.</p>     * <p>     * This test is equivalent to a one-sample t-test computed using     * {@link #tTest(double, double[])} with <code>mu = 0</code> and the sample     * array consisting of the signed differences between corresponding elements of      * <code>sample1</code> and <code>sample2.</code></p>     * <p>     * <strong>Usage Note:</strong><br>     * The validity of the p-value 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 input array lengths must be the same and their common length must     * be at least 2.     * </li></ul></p>     *     * @param sample1 array of sample data values     * @param sample2 array of sample data values     * @return p-value for t-test     * @throws IllegalArgumentException if the precondition is not met     * @throws MathException if an error occurs computing the p-value     */    public double pairedTTest(double[] sample1, double[] sample2)        throws IllegalArgumentException, MathException {        double meanDifference = StatUtils.meanDifference(sample1, sample2);        return tTest(meanDifference, 0,                 StatUtils.varianceDifference(sample1, sample2, meanDifference),                 (double) sample1.length);    }     /**     * Performs a paired t-test evaluating the null hypothesis that the      * mean of the paired differences between <code>sample1</code> and     * <code>sample2</code> is 0 in favor of the two-sided alternative that the      * mean paired difference is not equal to 0, with significance level      * <code>alpha</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>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 input array lengths must be the same and their common length      * must be at least 2.     * </li>     * <li> <code> 0 < alpha < 0.5 </code>     * </li></ul></p>     *     * @param sample1 array of sample data values     * @param sample2 array of sample data values     * @param alpha significance level of the test     * @return true if the null hypothesis can be rejected with      * confidence 1 - alpha     * @throws IllegalArgumentException if the preconditions are not met     * @throws MathException if an error occurs performing the test     */    public boolean pairedTTest(double[] sample1, double[] sample2, double alpha)        throws IllegalArgumentException, MathException {        if ((alpha <= 0) || (alpha > 0.5)) {            throw new IllegalArgumentException("bad significance level: " + alpha);        }        return (pairedTTest(sample1, sample2) < alpha);    }    /**     * Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc22.htm#formula">      * t statistic </a> given observed values and a comparison constant.     * <p>     * This statistic can be used to perform a one sample t-test for the mean.     * </p><p>     * <strong>Preconditions</strong>: <ul>     * <li>The observed array length must be at least 2.     * </li></ul></p>     *     * @param mu comparison constant     * @param observed array of values     * @return t statistic     * @throws IllegalArgumentException if input array length is less than 2     */    public double t(double mu, double[] observed)    throws IllegalArgumentException {        if ((observed == null) || (observed.length < 2)) {            throw new IllegalArgumentException("insufficient data for t statistic");        }        return t(StatUtils.mean(observed), mu, StatUtils.variance(observed),                observed.length);    }    /**     * Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc22.htm#formula">     * t statistic </a> to use in comparing the mean of the dataset described by      * <code>sampleStats</code> to <code>mu</code>.     * <p>     * This statistic can be used to perform a one sample t-test for the mean.     * </p><p>     * <strong>Preconditions</strong>: <ul>     * <li><code>observed.getN() > = 2</code>.     * </li></ul></p>     *     * @param mu comparison constant     * @param sampleStats DescriptiveStatistics holding sample summary statitstics     * @return t statistic     * @throws IllegalArgumentException if the precondition is not met     */    public double t(double mu, StatisticalSummary sampleStats)    throws IllegalArgumentException {        if ((sampleStats == null) || (sampleStats.getN() < 2)) {            throw new IllegalArgumentException("insufficient data for t statistic");        }        return t(sampleStats.getMean(), mu, sampleStats.getVariance(),                sampleStats.getN());    }    /**     * Computes a 2-sample t statistic,  under the hypothesis of equal      * subpopulation variances.  To compute a t-statistic without the     * equal variances hypothesis, use {@link #t(double[], double[])}.     * <p>     * This statistic can be used to perform a (homoscedastic) two-sample     * t-test to compare sample means.</p>     * <p>     * The t-statisitc is</p>     * <p>     * &nbsp;&nbsp;<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</li>     * </ul>     * 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><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 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 homoscedasticT(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 homoscedasticT(StatUtils.mean(sample1), StatUtils.mean(sample2),                StatUtils.variance(sample1), StatUtils.variance(sample2),                (double) sample1.length, (double) sample2.length);    }        /**     * Computes a 2-sample t statistic, without the hypothesis of equal     * subpopulation variances.  To compute a t-statistic assuming equal     * variances, use {@link #homoscedasticT(double[], double[])}.     * <p>     * This statistic can be used to perform a two-sample t-test to compare

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -