~ubuntu-branches/ubuntu/quantal/commons-math/quantal

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/math/stat/inference/TTestImpl.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan, Torsten Werner, Damien Raude-Morvan
  • Date: 2011-03-07 21:14:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110307211446-4zea7og4eeyzhpai
Tags: 2.2-1
[ Torsten Werner ]
* Change maintainers into Maintainers.

[ Damien Raude-Morvan ]
* New upstream release (Closes: #617209).
* d/control: Bump Standards-Version to 3.9.1 (no changes needed).
* d/copyright: Refresh years, upgrade to DEP5 r166 and relicence my work
  under Apache-2.0.
* d/ant.properties: Set junit.jar to /usr/share/java/junit4.jar
  to ensure unit tests are launched.
* d/docs: Install upstream RELEASE-NOTES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import org.apache.commons.math.MathRuntimeException;
21
21
import org.apache.commons.math.distribution.TDistribution;
22
22
import org.apache.commons.math.distribution.TDistributionImpl;
 
23
import org.apache.commons.math.exception.util.LocalizedFormats;
23
24
import org.apache.commons.math.stat.StatUtils;
24
25
import org.apache.commons.math.stat.descriptive.StatisticalSummary;
 
26
import org.apache.commons.math.util.FastMath;
25
27
 
26
28
/**
27
29
 * Implements t-test statistics defined in the {@link TTest} interface.
28
30
 * <p>
29
 
 * Uses commons-math {@link org.apache.commons.math.distribution.TDistribution}
 
31
 * Uses commons-math {@link org.apache.commons.math.distribution.TDistributionImpl}
30
32
 * implementation to estimate exact p-values.</p>
31
33
 *
32
 
 * @version $Revision: 885278 $ $Date: 2009-11-29 16:47:51 -0500 (Sun, 29 Nov 2009) $
 
34
 * @version $Revision: 1042336 $ $Date: 2010-12-05 13:40:48 +0100 (dim. 05 déc. 2010) $
33
35
 */
34
36
public class TTestImpl implements TTest  {
35
37
 
36
 
    /** Message for insufficient data. */
37
 
    private static final String INSUFFICIENT_DATA_MESSAGE =
38
 
        "insufficient data for t statistic, needs at least 2, got {0}";
39
 
 
40
 
    /** Distribution used to compute inference statistics. */
 
38
    /** Distribution used to compute inference statistics.
 
39
     * @deprecated in 2.2 (to be removed in 3.0).
 
40
     */
 
41
    @Deprecated
41
42
    private TDistribution distribution;
42
43
 
43
44
    /**
52
53
     * inference statistics.
53
54
     * @param t distribution used to compute inference statistics.
54
55
     * @since 1.2
 
56
     * @deprecated in 2.2 (to be removed in 3.0).
55
57
     */
 
58
    @Deprecated
56
59
    public TTestImpl(TDistribution t) {
57
60
        super();
58
61
        setDistribution(t);
908
911
     * @return t test statistic
909
912
     */
910
913
    protected double t(double m, double mu, double v, double n) {
911
 
        return (m - mu) / Math.sqrt(v / n);
 
914
        return (m - mu) / FastMath.sqrt(v / n);
912
915
    }
913
916
 
914
917
    /**
926
929
     */
927
930
    protected double t(double m1, double m2,  double v1, double v2, double n1,
928
931
            double n2)  {
929
 
            return (m1 - m2) / Math.sqrt((v1 / n1) + (v2 / n2));
 
932
            return (m1 - m2) / FastMath.sqrt((v1 / n1) + (v2 / n2));
930
933
    }
931
934
 
932
935
    /**
944
947
    protected double homoscedasticT(double m1, double m2,  double v1,
945
948
            double v2, double n1, double n2)  {
946
949
            double pooledVariance = ((n1  - 1) * v1 + (n2 -1) * v2 ) / (n1 + n2 - 2);
947
 
            return (m1 - m2) / Math.sqrt(pooledVariance * (1d / n1 + 1d / n2));
 
950
            return (m1 - m2) / FastMath.sqrt(pooledVariance * (1d / n1 + 1d / n2));
948
951
    }
949
952
 
950
953
    /**
959
962
     */
960
963
    protected double tTest(double m, double mu, double v, double n)
961
964
    throws MathException {
962
 
        double t = Math.abs(t(m, mu, v, n));
 
965
        double t = FastMath.abs(t(m, mu, v, n));
963
966
        distribution.setDegreesOfFreedom(n - 1);
964
967
        return 2.0 * distribution.cumulativeProbability(-t);
965
968
    }
982
985
    protected double tTest(double m1, double m2, double v1, double v2,
983
986
            double n1, double n2)
984
987
    throws MathException {
985
 
        double t = Math.abs(t(m1, m2, v1, v2, n1, n2));
 
988
        double t = FastMath.abs(t(m1, m2, v1, v2, n1, n2));
986
989
        double degreesOfFreedom = 0;
987
990
        degreesOfFreedom = df(v1, v2, n1, n2);
988
991
        distribution.setDegreesOfFreedom(degreesOfFreedom);
1007
1010
    protected double homoscedasticTTest(double m1, double m2, double v1,
1008
1011
            double v2, double n1, double n2)
1009
1012
    throws MathException {
1010
 
        double t = Math.abs(homoscedasticT(m1, m2, v1, v2, n1, n2));
 
1013
        double t = FastMath.abs(homoscedasticT(m1, m2, v1, v2, n1, n2));
1011
1014
        double degreesOfFreedom = n1 + n2 - 2;
1012
1015
        distribution.setDegreesOfFreedom(degreesOfFreedom);
1013
1016
        return 2.0 * distribution.cumulativeProbability(-t);
1017
1020
     * Modify the distribution used to compute inference statistics.
1018
1021
     * @param value the new distribution
1019
1022
     * @since 1.2
 
1023
     * @deprecated in 2.2 (to be removed in 3.0).
1020
1024
     */
 
1025
    @Deprecated
1021
1026
    public void setDistribution(TDistribution value) {
1022
1027
        distribution = value;
1023
1028
    }
1030
1035
        throws IllegalArgumentException {
1031
1036
        if ((alpha <= 0) || (alpha > 0.5)) {
1032
1037
            throw MathRuntimeException.createIllegalArgumentException(
1033
 
                  "out of bounds significance level {0}, must be between {1} and {2}",
 
1038
                  LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL,
1034
1039
                  alpha, 0.0, 0.5);
1035
1040
        }
1036
1041
    }
1043
1048
        throws IllegalArgumentException {
1044
1049
        if ((data == null) || (data.length < 2)) {
1045
1050
            throw MathRuntimeException.createIllegalArgumentException(
1046
 
                  INSUFFICIENT_DATA_MESSAGE,
 
1051
                  LocalizedFormats.INSUFFICIENT_DATA_FOR_T_STATISTIC,
1047
1052
                  (data == null) ? 0 : data.length);
1048
1053
        }
1049
1054
    }
1056
1061
        throws IllegalArgumentException {
1057
1062
        if ((stat == null) || (stat.getN() < 2)) {
1058
1063
            throw MathRuntimeException.createIllegalArgumentException(
1059
 
                  INSUFFICIENT_DATA_MESSAGE,
 
1064
                  LocalizedFormats.INSUFFICIENT_DATA_FOR_T_STATISTIC,
1060
1065
                  (stat == null) ? 0 : stat.getN());
1061
1066
        }
1062
1067
    }