Cached DistributionFactory instance.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141284 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d8a43bf62a
commit
444c87274c
|
@ -15,8 +15,6 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.math.stat.inference;
|
package org.apache.commons.math.stat.inference;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import org.apache.commons.math.MathException;
|
import org.apache.commons.math.MathException;
|
||||||
import org.apache.commons.math.distribution.DistributionFactory;
|
import org.apache.commons.math.distribution.DistributionFactory;
|
||||||
import org.apache.commons.math.distribution.ChiSquaredDistribution;
|
import org.apache.commons.math.distribution.ChiSquaredDistribution;
|
||||||
|
@ -24,9 +22,12 @@ import org.apache.commons.math.distribution.ChiSquaredDistribution;
|
||||||
/**
|
/**
|
||||||
* Implements Chi-Square test statistics defined in the {@link ChiSquareTest} interface.
|
* Implements Chi-Square test statistics defined in the {@link ChiSquareTest} interface.
|
||||||
*
|
*
|
||||||
* @version $Revision: 1.4 $ $Date: 2004/06/05 20:11:06 $
|
* @version $Revision: 1.5 $ $Date: 2004/06/06 23:14:09 $
|
||||||
*/
|
*/
|
||||||
public class ChiSquareTestImpl implements ChiSquareTest {
|
public class ChiSquareTestImpl implements ChiSquareTest {
|
||||||
|
|
||||||
|
/** Cached DistributionFactory used to create ChiSquaredDistribution instances */
|
||||||
|
private DistributionFactory distributionFactory = null;
|
||||||
|
|
||||||
public ChiSquareTestImpl() {
|
public ChiSquareTestImpl() {
|
||||||
super();
|
super();
|
||||||
|
@ -67,7 +68,7 @@ public class ChiSquareTestImpl implements ChiSquareTest {
|
||||||
public double chiSquareTest(double[] expected, long[] observed)
|
public double chiSquareTest(double[] expected, long[] observed)
|
||||||
throws IllegalArgumentException, MathException {
|
throws IllegalArgumentException, MathException {
|
||||||
ChiSquaredDistribution chiSquaredDistribution =
|
ChiSquaredDistribution chiSquaredDistribution =
|
||||||
DistributionFactory.newInstance().createChiSquareDistribution((double) expected.length - 1);
|
getDistributionFactory().createChiSquareDistribution((double) expected.length - 1);
|
||||||
return 1 - chiSquaredDistribution.cumulativeProbability(chiSquare(expected, observed));
|
return 1 - chiSquaredDistribution.cumulativeProbability(chiSquare(expected, observed));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +136,7 @@ public class ChiSquareTestImpl implements ChiSquareTest {
|
||||||
checkArray(counts);
|
checkArray(counts);
|
||||||
double df = ((double) counts.length -1) * ((double) counts[0].length - 1);
|
double df = ((double) counts.length -1) * ((double) counts[0].length - 1);
|
||||||
ChiSquaredDistribution chiSquaredDistribution =
|
ChiSquaredDistribution chiSquaredDistribution =
|
||||||
DistributionFactory.newInstance().createChiSquareDistribution(df);
|
getDistributionFactory().createChiSquareDistribution(df);
|
||||||
return 1 - chiSquaredDistribution.cumulativeProbability(chiSquare(counts));
|
return 1 - chiSquaredDistribution.cumulativeProbability(chiSquare(counts));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,6 +184,17 @@ public class ChiSquareTestImpl implements ChiSquareTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------- Protected methods ---------------------------------
|
||||||
|
/**
|
||||||
|
* Gets a DistributionFactory to use in creating ChiSquaredDistribution instances.
|
||||||
|
*/
|
||||||
|
protected DistributionFactory getDistributionFactory() {
|
||||||
|
if (distributionFactory == null) {
|
||||||
|
distributionFactory = DistributionFactory.newInstance();
|
||||||
|
}
|
||||||
|
return distributionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------- Private array methods -- should find a utility home for these
|
//--------------------- Private array methods -- should find a utility home for these
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue