checkSignificanceLevel(alpha); return tTest(mu, sample) < alpha;
/** * Computes p-value for 2-sided, 2-sample t-test. * <p> * Does not assume subpopulation variances are equal. Degrees of freedom * are estimated from the data.</p> * * @param m1 first sample mean * @param m2 second sample mean * @param v1 first sample variance * @param v2 second sample variance * @param n1 first sample n * @param n2 second sample n * @return p-value * @throws MathException if an error occurs computing the p-value */ protected double tTest(double m1, double m2, double v1, double v2, double n1, double n2) throws MathException { double t = Math.abs(t(m1, m2, v1, v2, n1, n2)); double degreesOfFreedom = 0; degreesOfFreedom = df(v1, v2, n1, n2); distribution.setDegreesOfFreedom(degreesOfFreedom); return 1.0 - distribution.cumulativeProbability(-t, t); }
double alpha) throws IllegalArgumentException, MathException { checkSignificanceLevel(alpha); return homoscedasticTTest(sample1, sample2) < alpha;
checkSignificanceLevel(alpha); return pairedTTest(sample1, sample2) < alpha;
checkSampleData(sample); return tTest( StatUtils.mean(sample), mu, StatUtils.variance(sample), sample.length);
/** * 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 { checkSampleData(sample1); checkSampleData(sample2); double meanDifference = StatUtils.meanDifference(sample1, sample2); return t(meanDifference, 0, StatUtils.varianceDifference(sample1, sample2, meanDifference), sample1.length); }
throw new IllegalArgumentException("bad significance level: " + alpha); return (tTest(mu, sample) < alpha);
public IValue tTest(IList sample1, IList sample2){ double s1[] = makeT(sample1); double s2[] = makeT(sample2); //for(int i = 0; i < s1.length; i++) System.err.println("s1[" + i + "] = " + s1[i]); //for(int i = 0; i < s2.length; i++) System.err.println("s2[" + i + "] = " + s2[i]); try { double r = new TTestImpl().tTest(s1, s2); //System.err.println("r = " + r); return values.real(r); } catch (IllegalArgumentException e) { throw RuntimeExceptionFactory.illegalArgument(sample1, null, null, e.getMessage()); } catch (MathException e) { throw RuntimeExceptionFactory.illegalArgument(sample1, null, null, e.getMessage()); } }
checkSampleData(sample1); checkSampleData(sample2); return homoscedasticTTest(StatUtils.mean(sample1), StatUtils.mean(sample2), StatUtils.variance(sample1), StatUtils.variance(sample2), sample1.length,
checkSampleData(sample1); checkSampleData(sample2); return homoscedasticT(StatUtils.mean(sample1), StatUtils.mean(sample2), StatUtils.variance(sample1), StatUtils.variance(sample2), sample1.length, sample2.length);
/** * Computes p-value for 2-sided, 1-sample t-test. * * @param m sample mean * @param mu constant to test against * @param v sample variance * @param n sample n * @return p-value * @throws MathException if an error occurs computing the p-value */ protected double tTest(double m, double mu, double v, double n) throws MathException { double t = Math.abs(t(m, mu, v, n)); distribution.setDegreesOfFreedom(n - 1); return 1.0 - distribution.cumulativeProbability(-t, t); }
/** * Computes p-value for 2-sided, 2-sample t-test, under the assumption * of equal subpopulation variances. * <p> * The sum of the sample sizes minus 2 is used as degrees of freedom.</p> * * @param m1 first sample mean * @param m2 second sample mean * @param v1 first sample variance * @param v2 second sample variance * @param n1 first sample n * @param n2 second sample n * @return p-value * @throws MathException if an error occurs computing the p-value */ protected double homoscedasticTTest(double m1, double m2, double v1, double v2, double n1, double n2) throws MathException { double t = Math.abs(homoscedasticT(m1, m2, v1, v2, n1, n2)); double degreesOfFreedom = n1 + n2 - 2; distribution.setDegreesOfFreedom(degreesOfFreedom); return 2.0 * distribution.cumulativeProbability(-t); }
/** * 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); }
throw new IllegalArgumentException("bad significance level: " + alpha); return (homoscedasticTTest(sample1, sample2) < alpha);
throw new IllegalArgumentException("bad significance level: " + alpha); return (pairedTTest(sample1, sample2) < alpha);
/** * Create a TTest instance. * * @return a new TTest instance */ public TTest createTTest() { return new TTestImpl(); }
checkSampleData(sample); return tTest( StatUtils.mean(sample), mu, StatUtils.variance(sample), sample.length);
/** * 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 { checkSampleData(observed); return t(StatUtils.mean(observed), mu, StatUtils.variance(observed), observed.length); }
throw new IllegalArgumentException("bad significance level: " + alpha); return (tTest(mu, sampleStats) < alpha);
public IValue tTest(IList sample1, IList sample2, INumber alpha){ double s1[] = makeT(sample1); double s2[] = makeT(sample2); try { return values.bool(new TTestImpl().tTest(s1, s2, alpha.toReal(values.getPrecision()).doubleValue())); } catch (IllegalArgumentException e) { throw RuntimeExceptionFactory.illegalArgument(sample1, null, null, e.getMessage()); } catch (MathException e) { throw RuntimeExceptionFactory.illegalArgument(sample1, null, null, e.getMessage()); } }