Example usage for org.apache.commons.math3.stat StatUtils meanDifference

List of usage examples for org.apache.commons.math3.stat StatUtils meanDifference

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat StatUtils meanDifference.

Prototype

public static double meanDifference(final double[] sample1, final double[] sample2)
        throws DimensionMismatchException, NoDataException 

Source Link

Document

Returns the mean of the (signed) differences between corresponding elements of the input arrays -- i.e., sum(sample1[i] - sample2[i]) / sample1.length.

Usage

From source file:embedded2.ESecure.TTest.java

/**
 * 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>/*from w w  w  .j  a  v a2s .c  o m*/
 * <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 NullArgumentException if the arrays are <code>null</code>
 * @throws NoDataException if the arrays are empty
 * @throws DimensionMismatchException if the length of the arrays is not equal
 * @throws NumberIsTooSmallException if the length of the arrays is &lt; 2
 */
public static double pairedT(final double[] sample1, final double[] sample2)
        throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooSmallException {

    checkSampleData(sample1);
    checkSampleData(sample2);
    double meanDifference = StatUtils.meanDifference(sample1, sample2);
    return t(meanDifference, 0, StatUtils.varianceDifference(sample1, sample2, meanDifference), sample1.length);

}

From source file:embedded2.ESecure.TTest.java

/**
 * 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>/*from w ww . j ava  2s .c o  m*/
 * 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 NullArgumentException if the arrays are <code>null</code>
 * @throws NoDataException if the arrays are empty
 * @throws DimensionMismatchException if the length of the arrays is not equal
 * @throws NumberIsTooSmallException if the length of the arrays is &lt; 2
 * @throws MaxCountExceededException if an error occurs computing the p-value
 */
public static double pairedTTest(final double[] sample1, final double[] sample2) throws NullArgumentException,
        NoDataException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException {

    double meanDifference = StatUtils.meanDifference(sample1, sample2);
    return tTest(meanDifference, 0, StatUtils.varianceDifference(sample1, sample2, meanDifference),
            sample1.length);

}

From source file:org.apache.solr.client.solrj.io.eval.MeanDifferenceEvaluator.java

@Override
public Object doWork(Object first, Object second) throws IOException {
    if (null == first) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }//from w  w w  .  j  a v a 2 s.  c om
    if (null == second) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));
    }
    if (!(first instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the first value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }
    if (!(second instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the second value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }

    return StatUtils.meanDifference(
            ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),
            ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray());
}