You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by br...@apache.org on 2007/06/07 15:30:20 UTC
svn commit: r545174 -
/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/inference/ChiSquareTestImpl.java
Author: brentworden
Date: Thu Jun 7 06:30:19 2007
New Revision: 545174
URL: http://svn.apache.org/viewvc?view=rev&rev=545174
Log:
Removed dependency on DistributionFactory. Added settable chi-squared distribution field.
Modified:
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/inference/ChiSquareTestImpl.java
Modified: jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/inference/ChiSquareTestImpl.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/inference/ChiSquareTestImpl.java?view=diff&rev=545174&r1=545173&r2=545174
==============================================================================
--- jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/inference/ChiSquareTestImpl.java (original)
+++ jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/inference/ChiSquareTestImpl.java Thu Jun 7 06:30:19 2007
@@ -17,8 +17,9 @@
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.ChiSquaredDistribution;
+import org.apache.commons.math.distribution.ChiSquaredDistributionImpl;
+import org.apache.commons.math.distribution.DistributionFactory;
/**
* Implements Chi-Square test statistics defined in the {@link ChiSquareTest} interface.
@@ -26,17 +27,27 @@
* @version $Revision$ $Date$
*/
public class ChiSquareTestImpl implements ChiSquareTest {
-
- /** Cached DistributionFactory used to create ChiSquaredDistribution instances */
- private DistributionFactory distributionFactory = null;
+
+ /** Distribution used to compute inference statistics. */
+ private ChiSquaredDistribution distribution;
/**
* Construct a ChiSquareTestImpl
*/
public ChiSquareTestImpl() {
- super();
+ this(new ChiSquaredDistributionImpl(1.0));
}
+ /**
+ * Create a test instance using the given distribution for computing
+ * inference statistics.
+ * @param x distribution used to compute inference statistics.
+ * @since 1.2
+ */
+ public ChiSquareTestImpl(ChiSquaredDistribution x) {
+ super();
+ setDistribution(x);
+ }
/**
* @param observed array of observed frequency counts
* @param expected array of expected frequency counts
@@ -72,11 +83,9 @@
*/
public double chiSquareTest(double[] expected, long[] observed)
throws IllegalArgumentException, MathException {
- ChiSquaredDistribution chiSquaredDistribution =
- getDistributionFactory().createChiSquareDistribution(
- (double) expected.length - 1);
- return 1 - chiSquaredDistribution.cumulativeProbability(
- chiSquare(expected, observed));
+ distribution.setDegreesOfFreedom(expected.length - 1.0);
+ return 1.0 - distribution.cumulativeProbability(
+ chiSquare(expected, observed));
}
/**
@@ -143,9 +152,8 @@
throws IllegalArgumentException, MathException {
checkArray(counts);
double df = ((double) counts.length -1) * ((double) counts[0].length - 1);
- ChiSquaredDistribution chiSquaredDistribution =
- getDistributionFactory().createChiSquareDistribution(df);
- return 1 - chiSquaredDistribution.cumulativeProbability(chiSquare(counts));
+ distribution.setDegreesOfFreedom(df);
+ return 1 - distribution.cumulativeProbability(chiSquare(counts));
}
/**
@@ -195,14 +203,11 @@
//--------------------- Protected methods ---------------------------------
/**
* Gets a DistributionFactory to use in creating ChiSquaredDistribution instances.
- *
- * @return a DistributionFactory
+ * @deprecated inject ChiSquaredDistribution instances directly instead of
+ * using a factory.
*/
protected DistributionFactory getDistributionFactory() {
- if (distributionFactory == null) {
- distributionFactory = DistributionFactory.newInstance();
- }
- return distributionFactory;
+ return DistributionFactory.newInstance();
}
//--------------------- Private array methods -- should find a utility home for these
@@ -277,4 +282,12 @@
return true;
}
+ /**
+ * Modify the distribution used to compute inference statistics.
+ * @param value the new distribution
+ * @since 1.2
+ */
+ public void setDistribution(ChiSquaredDistribution value) {
+ distribution = value;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org