Updated to use factory constructors for Statistics distributions

This commit is contained in:
aherbert 2021-10-13 14:01:08 +01:00
parent 3a5cf279bb
commit 84677cd0db
16 changed files with 28 additions and 28 deletions

View File

@ -551,8 +551,8 @@ public final class EmpiricalDistribution extends AbstractRealDistribution
stats.getVariance() == 0) { stats.getVariance() == 0) {
return new ConstantContinuousDistribution(stats.getMean()); return new ConstantContinuousDistribution(stats.getMean());
} else { } else {
return new NormalDistribution(stats.getMean(), return NormalDistribution.of(stats.getMean(),
stats.getStandardDeviation()); stats.getStandardDeviation());
} }
}; };
} }

View File

@ -181,7 +181,7 @@ public class MultivariateNormalDistribution
public MultivariateRealDistribution.Sampler createSampler(final UniformRandomProvider rng) { public MultivariateRealDistribution.Sampler createSampler(final UniformRandomProvider rng) {
return new MultivariateRealDistribution.Sampler() { return new MultivariateRealDistribution.Sampler() {
/** Normal distribution. */ /** Normal distribution. */
private final ContinuousDistribution.Sampler gauss = new NormalDistribution(0, 1).createSampler(rng); private final ContinuousDistribution.Sampler gauss = NormalDistribution.of(0, 1).createSampler(rng);
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override

View File

@ -246,7 +246,7 @@ public class CMAESOptimizer
this.isActiveCMA = isActiveCMA; this.isActiveCMA = isActiveCMA;
this.diagonalOnly = diagonalOnly; this.diagonalOnly = diagonalOnly;
this.checkFeasableCount = Math.max(0, checkFeasableCount); this.checkFeasableCount = Math.max(0, checkFeasableCount);
this.random = new NormalDistribution(0, 1).createSampler(rng); this.random = NormalDistribution.of(0, 1).createSampler(rng);
this.generateStatistics = generateStatistics; this.generateStatistics = generateStatistics;
} }

View File

@ -192,7 +192,7 @@ public class PearsonsCorrelation {
* @throws NullPointerException if this instance was created with no data. * @throws NullPointerException if this instance was created with no data.
*/ */
public RealMatrix getCorrelationPValues() { public RealMatrix getCorrelationPValues() {
TDistribution tDistribution = new TDistribution(nObs - 2); TDistribution tDistribution = TDistribution.of(nObs - 2);
int nVars = correlationMatrix.getColumnDimension(); int nVars = correlationMatrix.getColumnDimension();
double[][] out = new double[nVars][nVars]; double[][] out = new double[nVars][nVars];
for (int i = 0; i < nVars; i++) { for (int i = 0; i < nVars; i++) {

View File

@ -119,7 +119,7 @@ public class BinomialTest {
throw new NullArgumentException(); throw new NullArgumentException();
} }
final BinomialDistribution distribution = new BinomialDistribution(numberOfTrials, probability); final BinomialDistribution distribution = BinomialDistribution.of(numberOfTrials, probability);
switch (alternativeHypothesis) { switch (alternativeHypothesis) {
case GREATER_THAN: case GREATER_THAN:
return 1 - distribution.cumulativeProbability(numberOfSuccesses - 1); return 1 - distribution.cumulativeProbability(numberOfSuccesses - 1);

View File

@ -157,7 +157,7 @@ public class ChiSquareTest {
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final ChiSquaredDistribution distribution = final ChiSquaredDistribution distribution =
new ChiSquaredDistribution(expected.length - 1.0); ChiSquaredDistribution.of(expected.length - 1.0);
return 1.0 - distribution.cumulativeProbability(chiSquare(expected, observed)); return 1.0 - distribution.cumulativeProbability(chiSquare(expected, observed));
} }
@ -332,7 +332,7 @@ public class 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);
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final ChiSquaredDistribution distribution = new ChiSquaredDistribution(df); final ChiSquaredDistribution distribution = ChiSquaredDistribution.of(df);
return 1 - distribution.cumulativeProbability(chiSquare(counts)); return 1 - distribution.cumulativeProbability(chiSquare(counts));
} }
@ -536,7 +536,7 @@ public class ChiSquareTest {
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final ChiSquaredDistribution distribution = final ChiSquaredDistribution distribution =
new ChiSquaredDistribution((double) observed1.length - 1); ChiSquaredDistribution.of((double) observed1.length - 1);
return 1 - distribution.cumulativeProbability( return 1 - distribution.cumulativeProbability(
chiSquareDataSetsComparison(observed1, observed2)); chiSquareDataSetsComparison(observed1, observed2));

View File

@ -154,7 +154,7 @@ public class GTest {
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final ChiSquaredDistribution distribution = final ChiSquaredDistribution distribution =
new ChiSquaredDistribution(expected.length - 1.0); ChiSquaredDistribution.of(expected.length - 1.0);
return 1.0 - distribution.cumulativeProbability(g(expected, observed)); return 1.0 - distribution.cumulativeProbability(g(expected, observed));
} }
@ -185,7 +185,7 @@ public class GTest {
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final ChiSquaredDistribution distribution = final ChiSquaredDistribution distribution =
new ChiSquaredDistribution(expected.length - 2.0); ChiSquaredDistribution.of(expected.length - 2.0);
return 1.0 - distribution.cumulativeProbability(g(expected, observed)); return 1.0 - distribution.cumulativeProbability(g(expected, observed));
} }
@ -475,7 +475,7 @@ public class GTest {
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final ChiSquaredDistribution distribution = final ChiSquaredDistribution distribution =
new ChiSquaredDistribution((double) observed1.length - 1); ChiSquaredDistribution.of((double) observed1.length - 1);
return 1 - distribution.cumulativeProbability( return 1 - distribution.cumulativeProbability(
gDataSetsComparison(observed1, observed2)); gDataSetsComparison(observed1, observed2));
} }

View File

@ -181,7 +181,7 @@ public class MannWhitneyUTest {
// No try-catch or advertised exception because args are valid // No try-catch or advertised exception because args are valid
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final NormalDistribution standardNormal = new NormalDistribution(0, 1); final NormalDistribution standardNormal = NormalDistribution.of(0, 1);
return 2 * standardNormal.cumulativeProbability(z); return 2 * standardNormal.cumulativeProbability(z);
} }

View File

@ -126,7 +126,7 @@ public class OneWayAnova {
final AnovaStats a = anovaStats(categoryData); final AnovaStats a = anovaStats(categoryData);
// No try-catch or advertised exception because args are valid // No try-catch or advertised exception because args are valid
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final FDistribution fdist = new FDistribution(a.dfbg, a.dfwg); final FDistribution fdist = FDistribution.of(a.dfbg, a.dfwg);
return 1.0 - fdist.cumulativeProbability(a.f); return 1.0 - fdist.cumulativeProbability(a.f);
} }
@ -168,7 +168,7 @@ public class OneWayAnova {
final AnovaStats a = anovaStats(categoryData, allowOneElementData); final AnovaStats a = anovaStats(categoryData, allowOneElementData);
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final FDistribution fdist = new FDistribution(a.dfbg, a.dfwg); final FDistribution fdist = FDistribution.of(a.dfbg, a.dfwg);
return 1.0 - fdist.cumulativeProbability(a.f); return 1.0 - fdist.cumulativeProbability(a.f);
} }

View File

@ -1057,7 +1057,7 @@ public class TTest {
final double t = AccurateMath.abs(t(m, mu, v, n)); final double t = AccurateMath.abs(t(m, mu, v, n));
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final TDistribution distribution = new TDistribution(n - 1); final TDistribution distribution = TDistribution.of(n - 1);
return 2.0 * distribution.cumulativeProbability(-t); return 2.0 * distribution.cumulativeProbability(-t);
} }
@ -1087,7 +1087,7 @@ public class TTest {
final double t = AccurateMath.abs(t(m1, m2, v1, v2, n1, n2)); final double t = AccurateMath.abs(t(m1, m2, v1, v2, n1, n2));
final double degreesOfFreedom = df(v1, v2, n1, n2); final double degreesOfFreedom = df(v1, v2, n1, n2);
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final TDistribution distribution = new TDistribution(degreesOfFreedom); final TDistribution distribution = TDistribution.of(degreesOfFreedom);
return 2.0 * distribution.cumulativeProbability(-t); return 2.0 * distribution.cumulativeProbability(-t);
} }
@ -1117,7 +1117,7 @@ public class TTest {
final double t = AccurateMath.abs(homoscedasticT(m1, m2, v1, v2, n1, n2)); final double t = AccurateMath.abs(homoscedasticT(m1, m2, v1, v2, n1, n2));
final double degreesOfFreedom = n1 + n2 - 2; final double degreesOfFreedom = n1 + n2 - 2;
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final TDistribution distribution = new TDistribution(degreesOfFreedom); final TDistribution distribution = TDistribution.of(degreesOfFreedom);
return 2.0 * distribution.cumulativeProbability(-t); return 2.0 * distribution.cumulativeProbability(-t);
} }

View File

@ -253,7 +253,7 @@ public class WilcoxonSignedRankTest {
// No try-catch or advertised exception because args are valid // No try-catch or advertised exception because args are valid
// pass a null rng to avoid unneeded overhead as we will not sample from this distribution // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
final NormalDistribution standardNormal = new NormalDistribution(0, 1); final NormalDistribution standardNormal = NormalDistribution.of(0, 1);
return 2*standardNormal.cumulativeProbability(z); return 2*standardNormal.cumulativeProbability(z);
} }

View File

@ -34,7 +34,7 @@ public class AgrestiCoullInterval implements BinomialConfidenceInterval {
public ConfidenceInterval createInterval(int numberOfTrials, int numberOfSuccesses, double confidenceLevel) { public ConfidenceInterval createInterval(int numberOfTrials, int numberOfSuccesses, double confidenceLevel) {
IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel); IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
final double alpha = (1.0 - confidenceLevel) / 2; final double alpha = (1.0 - confidenceLevel) / 2;
final NormalDistribution normalDistribution = new NormalDistribution(0, 1); final NormalDistribution normalDistribution = NormalDistribution.of(0, 1);
final double z = normalDistribution.inverseCumulativeProbability(1 - alpha); final double z = normalDistribution.inverseCumulativeProbability(1 - alpha);
final double zSquared = AccurateMath.pow(z, 2); final double zSquared = AccurateMath.pow(z, 2);
final double modifiedNumberOfTrials = numberOfTrials + zSquared; final double modifiedNumberOfTrials = numberOfTrials + zSquared;

View File

@ -40,16 +40,16 @@ public class ClopperPearsonInterval implements BinomialConfidenceInterval {
final double alpha = 0.5 * (1 - confidenceLevel); final double alpha = 0.5 * (1 - confidenceLevel);
if (numberOfSuccesses > 0) { if (numberOfSuccesses > 0) {
final FDistribution distributionLowerBound = new FDistribution(2.0 * (numberOfTrials - numberOfSuccesses + 1), final FDistribution distributionLowerBound = FDistribution.of(2.0 * (numberOfTrials - numberOfSuccesses + 1),
2.0 * numberOfSuccesses); 2.0 * numberOfSuccesses);
final double fValueLowerBound = distributionLowerBound.inverseCumulativeProbability(1 - alpha); final double fValueLowerBound = distributionLowerBound.inverseCumulativeProbability(1 - alpha);
lowerBound = numberOfSuccesses / lowerBound = numberOfSuccesses /
(numberOfSuccesses + (numberOfTrials - numberOfSuccesses + 1) * fValueLowerBound); (numberOfSuccesses + (numberOfTrials - numberOfSuccesses + 1) * fValueLowerBound);
} }
if (numberOfSuccesses < numberOfTrials) { if (numberOfSuccesses < numberOfTrials) {
final FDistribution distributionUpperBound = new FDistribution(2.0 * (numberOfSuccesses + 1), final FDistribution distributionUpperBound = FDistribution.of(2.0 * (numberOfSuccesses + 1),
2.0 * (numberOfTrials - numberOfSuccesses)); 2.0 * (numberOfTrials - numberOfSuccesses));
final double fValueUpperBound = distributionUpperBound.inverseCumulativeProbability(1 - alpha); final double fValueUpperBound = distributionUpperBound.inverseCumulativeProbability(1 - alpha);
upperBound = (numberOfSuccesses + 1) * fValueUpperBound / upperBound = (numberOfSuccesses + 1) * fValueUpperBound /
(numberOfTrials - numberOfSuccesses + (numberOfSuccesses + 1) * fValueUpperBound); (numberOfTrials - numberOfSuccesses + (numberOfSuccesses + 1) * fValueUpperBound);

View File

@ -36,7 +36,7 @@ public class NormalApproximationInterval implements BinomialConfidenceInterval {
IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel); IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
final double mean = (double) numberOfSuccesses / (double) numberOfTrials; final double mean = (double) numberOfSuccesses / (double) numberOfTrials;
final double alpha = (1.0 - confidenceLevel) / 2; final double alpha = (1.0 - confidenceLevel) / 2;
final NormalDistribution normalDistribution = new NormalDistribution(0, 1); final NormalDistribution normalDistribution = NormalDistribution.of(0, 1);
final double difference = normalDistribution.inverseCumulativeProbability(1 - alpha) * final double difference = normalDistribution.inverseCumulativeProbability(1 - alpha) *
AccurateMath.sqrt(1.0 / numberOfTrials * mean * (1 - mean)); AccurateMath.sqrt(1.0 / numberOfTrials * mean * (1 - mean));
return new ConfidenceInterval(mean - difference, mean + difference, confidenceLevel); return new ConfidenceInterval(mean - difference, mean + difference, confidenceLevel);

View File

@ -32,7 +32,7 @@ public class WilsonScoreInterval implements BinomialConfidenceInterval {
public ConfidenceInterval createInterval(int numberOfTrials, int numberOfSuccesses, double confidenceLevel) { public ConfidenceInterval createInterval(int numberOfTrials, int numberOfSuccesses, double confidenceLevel) {
IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel); IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
final double alpha = (1 - confidenceLevel) / 2; final double alpha = (1 - confidenceLevel) / 2;
final NormalDistribution normalDistribution = new NormalDistribution(0, 1); final NormalDistribution normalDistribution = NormalDistribution.of(0, 1);
final double z = normalDistribution.inverseCumulativeProbability(1 - alpha); final double z = normalDistribution.inverseCumulativeProbability(1 - alpha);
final double zSquared = z * z; final double zSquared = z * z;
final double oneOverNumTrials = 1d / numberOfTrials; final double oneOverNumTrials = 1d / numberOfTrials;

View File

@ -698,7 +698,7 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
alpha, 0, 1); alpha, 0, 1);
} }
// No advertised NotStrictlyPositiveException here - will return NaN above // No advertised NotStrictlyPositiveException here - will return NaN above
TDistribution distribution = new TDistribution(n - 2d); TDistribution distribution = TDistribution.of(n - 2d);
return getSlopeStdErr() * return getSlopeStdErr() *
distribution.inverseCumulativeProbability(1d - alpha / 2d); distribution.inverseCumulativeProbability(1d - alpha / 2d);
} }
@ -730,7 +730,7 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
return Double.NaN; return Double.NaN;
} }
// No advertised NotStrictlyPositiveException here - will return NaN above // No advertised NotStrictlyPositiveException here - will return NaN above
TDistribution distribution = new TDistribution(n - 2d); TDistribution distribution = TDistribution.of(n - 2d);
return 2d * (1.0 - distribution.cumulativeProbability( return 2d * (1.0 - distribution.cumulativeProbability(
AccurateMath.abs(getSlope()) / getSlopeStdErr())); AccurateMath.abs(getSlope()) / getSlopeStdErr()));
} }