MATH-1443: Depend on "Commons Statistics". Deleted all classes that have a counterpart in "Commons Statistics".
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/ef846813 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/ef846813 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/ef846813 Branch: refs/heads/master Commit: ef84681392520396745def163b692a002b269be7 Parents: 3e24c28 Author: Gilles <er...@apache.org> Authored: Thu Jan 25 18:36:19 2018 +0100 Committer: Gilles <er...@apache.org> Committed: Thu Jan 25 18:36:19 2018 +0100 ---------------------------------------------------------------------- .../math4/distribution/BetaDistribution.java | 245 ------------ .../distribution/BinomialDistribution.java | 178 --------- .../math4/distribution/CauchyDistribution.java | 206 ---------- .../distribution/ChiSquaredDistribution.java | 154 ------- .../distribution/ConstantRealDistribution.java | 123 ------ .../distribution/ExponentialDistribution.java | 235 ----------- .../math4/distribution/FDistribution.java | 267 ------------- .../math4/distribution/GammaDistribution.java | 397 ------------------- .../distribution/GeometricDistribution.java | 170 -------- .../math4/distribution/GumbelDistribution.java | 141 ------- .../HypergeometricDistribution.java | 325 --------------- .../math4/distribution/LaplaceDistribution.java | 135 ------- .../math4/distribution/LevyDistribution.java | 166 -------- .../distribution/LogNormalDistribution.java | 312 --------------- .../distribution/LogisticDistribution.java | 136 ------- .../distribution/NakagamiDistribution.java | 157 -------- .../math4/distribution/NormalDistribution.java | 261 ------------ .../math4/distribution/ParetoDistribution.java | 269 ------------- .../math4/distribution/PascalDistribution.java | 228 ----------- .../math4/distribution/PoissonDistribution.java | 259 ------------ .../math4/distribution/TDistribution.java | 218 ---------- .../distribution/TriangularDistribution.java | 253 ------------ .../UniformIntegerDistribution.java | 165 -------- .../distribution/UniformRealDistribution.java | 180 --------- .../math4/distribution/WeibullDistribution.java | 297 -------------- .../math4/distribution/ZipfDistribution.java | 278 ------------- .../distribution/BetaDistributionTest.java | 386 ------------------ .../distribution/BinomialDistributionTest.java | 175 -------- .../distribution/CauchyDistributionTest.java | 122 ------ .../ChiSquaredDistributionTest.java | 137 ------- .../ConstantRealDistributionTest.java | 94 ----- .../ExponentialDistributionTest.java | 135 ------- .../math4/distribution/FDistributionTest.java | 159 -------- .../distribution/GammaDistributionTest.java | 358 ----------------- .../distribution/GeometricDistributionTest.java | 172 -------- .../distribution/GumbelDistributionTest.java | 72 ---- .../HypergeometricDistributionTest.java | 354 ----------------- .../distribution/LaplaceDistributionTest.java | 73 ---- .../distribution/LevyDistributionTest.java | 82 ---- .../distribution/LogNormalDistributionTest.java | 254 ------------ .../distribution/LogisticsDistributionTest.java | 73 ---- .../distribution/NakagamiDistributionTest.java | 73 ---- .../distribution/NormalDistributionTest.java | 216 ---------- .../distribution/ParetoDistributionTest.java | 206 ---------- .../distribution/PascalDistributionTest.java | 135 ------- .../distribution/PoissonDistributionTest.java | 248 ------------ .../math4/distribution/TDistributionTest.java | 173 -------- .../TriangularDistributionTest.java | 194 --------- .../UniformIntegerDistributionTest.java | 142 ------- .../UniformRealDistributionTest.java | 125 ------ .../distribution/WeibullDistributionTest.java | 126 ------ .../distribution/ZipfDistributionTest.java | 197 --------- 52 files changed, 10236 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/BetaDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/BetaDistribution.java b/src/main/java/org/apache/commons/math4/distribution/BetaDistribution.java deleted file mode 100644 index 550e89d..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/BetaDistribution.java +++ /dev/null @@ -1,245 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.distribution; - -import org.apache.commons.math4.exception.NumberIsTooSmallException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.numbers.gamma.RegularizedBeta; -import org.apache.commons.numbers.gamma.LogGamma; -import org.apache.commons.math4.util.FastMath; -import org.apache.commons.rng.UniformRandomProvider; -import org.apache.commons.rng.sampling.distribution.ContinuousSampler; -import org.apache.commons.rng.sampling.distribution.ChengBetaSampler; - -/** - * Implements the Beta distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Beta_distribution">Beta distribution</a> - * @since 2.0 (changed to concrete class in 3.0) - */ -public class BetaDistribution extends AbstractRealDistribution { - /** - * Default inverse cumulative probability accuracy. - * @since 2.1 - */ - public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; - /** Serializable version identifier. */ - private static final long serialVersionUID = 20160311L; - /** First shape parameter. */ - private final double alpha; - /** Second shape parameter. */ - private final double beta; - /** Normalizing factor used in density computations.*/ - private final double z; - /** Inverse cumulative probability accuracy. */ - private final double solverAbsoluteAccuracy; - - /** - * Creates a new instance. - * - * @param alpha First shape parameter (must be positive). - * @param beta Second shape parameter (must be positive). - */ - public BetaDistribution(double alpha, double beta) { - this(alpha, beta, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a new instance. - * - * @param alpha First shape parameter (must be positive). - * @param beta Second shape parameter (must be positive). - * @param inverseCumAccuracy Maximum absolute error in inverse - * cumulative probability estimates (defaults to - * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * - * @since 3.1 - */ - public BetaDistribution(double alpha, - double beta, - double inverseCumAccuracy) { - this.alpha = alpha; - this.beta = beta; - z = LogGamma.value(alpha) + LogGamma.value(beta) - LogGamma.value(alpha + beta); - solverAbsoluteAccuracy = inverseCumAccuracy; - } - - /** - * Access the first shape parameter, {@code alpha}. - * - * @return the first shape parameter. - */ - public double getAlpha() { - return alpha; - } - - /** - * Access the second shape parameter, {@code beta}. - * - * @return the second shape parameter. - */ - public double getBeta() { - return beta; - } - - /** {@inheritDoc} */ - @Override - public double density(double x) { - final double logDensity = logDensity(x); - return logDensity == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logDensity); - } - - /** {@inheritDoc} **/ - @Override - public double logDensity(double x) { - if (x < 0 || x > 1) { - return Double.NEGATIVE_INFINITY; - } else if (x == 0) { - if (alpha < 1) { - throw new NumberIsTooSmallException(LocalizedFormats.CANNOT_COMPUTE_BETA_DENSITY_AT_0_FOR_SOME_ALPHA, - alpha, 1, false); - } - return Double.NEGATIVE_INFINITY; - } else if (x == 1) { - if (beta < 1) { - throw new NumberIsTooSmallException(LocalizedFormats.CANNOT_COMPUTE_BETA_DENSITY_AT_1_FOR_SOME_BETA, - beta, 1, false); - } - return Double.NEGATIVE_INFINITY; - } else { - double logX = FastMath.log(x); - double log1mX = FastMath.log1p(-x); - return (alpha - 1) * logX + (beta - 1) * log1mX - z; - } - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(double x) { - if (x <= 0) { - return 0; - } else if (x >= 1) { - return 1; - } else { - return RegularizedBeta.value(x, alpha, beta); - } - } - - /** - * Return the absolute accuracy setting of the solver used to estimate - * inverse cumulative probabilities. - * - * @return the solver absolute accuracy. - * @since 2.1 - */ - @Override - protected double getSolverAbsoluteAccuracy() { - return solverAbsoluteAccuracy; - } - - /** - * {@inheritDoc} - * - * For first shape parameter {@code alpha} and second shape parameter - * {@code beta}, the mean is {@code alpha / (alpha + beta)}. - */ - @Override - public double getNumericalMean() { - final double a = getAlpha(); - return a / (a + getBeta()); - } - - /** - * {@inheritDoc} - * - * For first shape parameter {@code alpha} and second shape parameter - * {@code beta}, the variance is - * {@code (alpha * beta) / [(alpha + beta)^2 * (alpha + beta + 1)]}. - */ - @Override - public double getNumericalVariance() { - final double a = getAlpha(); - final double b = getBeta(); - final double alphabetasum = a + b; - return (a * b) / ((alphabetasum * alphabetasum) * (alphabetasum + 1)); - } - - /** - * {@inheritDoc} - * - * The lower bound of the support is always 0 no matter the parameters. - * - * @return lower bound of the support (always 0) - */ - @Override - public double getSupportLowerBound() { - return 0; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is always 1 no matter the parameters. - * - * @return upper bound of the support (always 1) - */ - @Override - public double getSupportUpperBound() { - return 1; - } - - /** - * {@inheritDoc} - * - * The support of this distribution is connected. - * - * @return {@code true} - */ - @Override - public boolean isSupportConnected() { - return true; - } - - /** - * {@inheritDoc} - * - * Sampling is performed using Cheng's algorithm: - * <blockquote> - * <pre> - * R. C. H. Cheng, - * "Generating beta variates with nonintegral shape parameters", - * Communications of the ACM, 21, 317-322, 1978. - * </pre> - * </blockquote> - */ - @Override - public RealDistribution.Sampler createSampler(final UniformRandomProvider rng) { - return new RealDistribution.Sampler() { - /** - * Beta distribution sampler. - */ - private final ContinuousSampler sampler = - new ChengBetaSampler(rng, alpha, beta); - - /**{@inheritDoc} */ - @Override - public double sample() { - return sampler.sample(); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/BinomialDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/BinomialDistribution.java b/src/main/java/org/apache/commons/math4/distribution/BinomialDistribution.java deleted file mode 100644 index ef9c077..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/BinomialDistribution.java +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.distribution; - -import org.apache.commons.math4.exception.NotPositiveException; -import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.numbers.gamma.RegularizedBeta; -import org.apache.commons.math4.util.FastMath; - -/** - * Implementation of the binomial distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Binomial_distribution">Binomial distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/BinomialDistribution.html">Binomial Distribution (MathWorld)</a> - */ -public class BinomialDistribution extends AbstractIntegerDistribution { - /** Serializable version identifier. */ - private static final long serialVersionUID = 6751309484392813623L; - /** The number of trials. */ - private final int numberOfTrials; - /** The probability of success. */ - private final double probabilityOfSuccess; - - /** - * Creates a binomial distribution. - * - * @param trials Number of trials. - * @param p Probability of success. - * @throws NotPositiveException if {@code trials < 0}. - * @throws OutOfRangeException if {@code p < 0} or {@code p > 1}. - */ - public BinomialDistribution(int trials, - double p) { - if (trials < 0) { - throw new NotPositiveException(LocalizedFormats.NUMBER_OF_TRIALS, - trials); - } - if (p < 0 || p > 1) { - throw new OutOfRangeException(p, 0, 1); - } - - probabilityOfSuccess = p; - numberOfTrials = trials; - } - - /** - * Access the number of trials for this distribution. - * - * @return the number of trials. - */ - public int getNumberOfTrials() { - return numberOfTrials; - } - - /** - * Access the probability of success for this distribution. - * - * @return the probability of success. - */ - public double getProbabilityOfSuccess() { - return probabilityOfSuccess; - } - - /** {@inheritDoc} */ - @Override - public double probability(int x) { - final double logProbability = logProbability(x); - return logProbability == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logProbability); - } - - /** {@inheritDoc} **/ - @Override - public double logProbability(int x) { - if (numberOfTrials == 0) { - return (x == 0) ? 0. : Double.NEGATIVE_INFINITY; - } - double ret; - if (x < 0 || x > numberOfTrials) { - ret = Double.NEGATIVE_INFINITY; - } else { - ret = SaddlePointExpansion.logBinomialProbability(x, - numberOfTrials, probabilityOfSuccess, - 1.0 - probabilityOfSuccess); - } - return ret; - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(int x) { - double ret; - if (x < 0) { - ret = 0.0; - } else if (x >= numberOfTrials) { - ret = 1.0; - } else { - ret = 1.0 - RegularizedBeta.value(probabilityOfSuccess, - x + 1.0, numberOfTrials - x); - } - return ret; - } - - /** - * {@inheritDoc} - * - * For {@code n} trials and probability parameter {@code p}, the mean is - * {@code n * p}. - */ - @Override - public double getNumericalMean() { - return numberOfTrials * probabilityOfSuccess; - } - - /** - * {@inheritDoc} - * - * For {@code n} trials and probability parameter {@code p}, the variance is - * {@code n * p * (1 - p)}. - */ - @Override - public double getNumericalVariance() { - final double p = probabilityOfSuccess; - return numberOfTrials * p * (1 - p); - } - - /** - * {@inheritDoc} - * - * The lower bound of the support is always 0 except for the probability - * parameter {@code p = 1}. - * - * @return lower bound of the support (0 or the number of trials) - */ - @Override - public int getSupportLowerBound() { - return probabilityOfSuccess < 1.0 ? 0 : numberOfTrials; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is the number of trials except for the - * probability parameter {@code p = 0}. - * - * @return upper bound of the support (number of trials or 0) - */ - @Override - public int getSupportUpperBound() { - return probabilityOfSuccess > 0.0 ? numberOfTrials : 0; - } - - /** - * {@inheritDoc} - * - * The support of this distribution is connected. - * - * @return {@code true} - */ - @Override - public boolean isSupportConnected() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/CauchyDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/CauchyDistribution.java b/src/main/java/org/apache/commons/math4/distribution/CauchyDistribution.java deleted file mode 100644 index a16cf22..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/CauchyDistribution.java +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.distribution; - -import org.apache.commons.math4.exception.NotStrictlyPositiveException; -import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.FastMath; - -/** - * Implementation of the Cauchy distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Cauchy_distribution">Cauchy distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/CauchyDistribution.html">Cauchy Distribution (MathWorld)</a> - * @since 1.1 (changed to concrete class in 3.0) - */ -public class CauchyDistribution extends AbstractRealDistribution { - /** - * Default inverse cumulative probability accuracy. - * @since 2.1 - */ - public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; - /** Serializable version identifier */ - private static final long serialVersionUID = 20160311L; - /** The median of this distribution. */ - private final double median; - /** The scale of this distribution. */ - private final double scale; - /** Inverse cumulative probability accuracy */ - private final double solverAbsoluteAccuracy; - - /** - * Creates a Cauchy distribution with the median equal to zero and scale - * equal to one. - */ - public CauchyDistribution() { - this(0, 1); - } - - /** - * Creates a distribution. - * - * @param median Median for this distribution. - * @param scale Scale parameter for this distribution. - * @throws NotStrictlyPositiveException if {@code scale <= 0}. - */ - public CauchyDistribution(double median, - double scale) { - this(median, scale, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a distribution. - * - * @param median Median for this distribution. - * @param scale Scale parameter for this distribution. - * @param inverseCumAccuracy Maximum absolute error in inverse - * cumulative probability estimates - * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NotStrictlyPositiveException if {@code scale <= 0}. - */ - public CauchyDistribution(double median, - double scale, - double inverseCumAccuracy) { - if (scale <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, scale); - } - this.scale = scale; - this.median = median; - solverAbsoluteAccuracy = inverseCumAccuracy; - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(double x) { - return 0.5 + (FastMath.atan((x - median) / scale) / FastMath.PI); - } - - /** - * Access the median. - * - * @return the median for this distribution. - */ - public double getMedian() { - return median; - } - - /** - * Access the scale parameter. - * - * @return the scale parameter for this distribution. - */ - public double getScale() { - return scale; - } - - /** {@inheritDoc} */ - @Override - public double density(double x) { - final double dev = x - median; - return (1 / FastMath.PI) * (scale / (dev * dev + scale * scale)); - } - - /** - * {@inheritDoc} - * - * Returns {@code Double.NEGATIVE_INFINITY} when {@code p == 0} - * and {@code Double.POSITIVE_INFINITY} when {@code p == 1}. - */ - @Override - public double inverseCumulativeProbability(double p) throws OutOfRangeException { - double ret; - if (p < 0 || p > 1) { - throw new OutOfRangeException(p, 0, 1); - } else if (p == 0) { - ret = Double.NEGATIVE_INFINITY; - } else if (p == 1) { - ret = Double.POSITIVE_INFINITY; - } else { - ret = median + scale * FastMath.tan(FastMath.PI * (p - .5)); - } - return ret; - } - - /** {@inheritDoc} */ - @Override - protected double getSolverAbsoluteAccuracy() { - return solverAbsoluteAccuracy; - } - - /** - * {@inheritDoc} - * - * The mean is always undefined no matter the parameters. - * - * @return mean (always Double.NaN) - */ - @Override - public double getNumericalMean() { - return Double.NaN; - } - - /** - * {@inheritDoc} - * - * The variance is always undefined no matter the parameters. - * - * @return variance (always Double.NaN) - */ - @Override - public double getNumericalVariance() { - return Double.NaN; - } - - /** - * {@inheritDoc} - * - * The lower bound of the support is always negative infinity no matter - * the parameters. - * - * @return lower bound of the support (always Double.NEGATIVE_INFINITY) - */ - @Override - public double getSupportLowerBound() { - return Double.NEGATIVE_INFINITY; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is always positive infinity no matter - * the parameters. - * - * @return upper bound of the support (always Double.POSITIVE_INFINITY) - */ - @Override - public double getSupportUpperBound() { - return Double.POSITIVE_INFINITY; - } - - /** - * {@inheritDoc} - * - * The support of this distribution is connected. - * - * @return {@code true} - */ - @Override - public boolean isSupportConnected() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/ChiSquaredDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/ChiSquaredDistribution.java b/src/main/java/org/apache/commons/math4/distribution/ChiSquaredDistribution.java deleted file mode 100644 index afd35c0..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/ChiSquaredDistribution.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.distribution; - -/** - * Implementation of the chi-squared distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Chi-squared_distribution">Chi-squared distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/Chi-SquaredDistribution.html">Chi-squared Distribution (MathWorld)</a> - */ -public class ChiSquaredDistribution extends AbstractRealDistribution { - /** - * Default inverse cumulative probability accuracy - * @since 2.1 - */ - public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; - /** Serializable version identifier */ - private static final long serialVersionUID = 20160311L; - /** Internal Gamma distribution. */ - private final GammaDistribution gamma; - /** Inverse cumulative probability accuracy */ - private final double solverAbsoluteAccuracy; - - /** - * Creates distribution with the given degrees of freedom. - * - * @param degreesOfFreedom Degrees of freedom. - */ - public ChiSquaredDistribution(double degreesOfFreedom) { - this(degreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a distribution with the given degrees of freedom and - * inverse cumulative probability accuracy. - * - * @param degreesOfFreedom Degrees of freedom. - * @param inverseCumAccuracy the maximum absolute error in inverse - * cumulative probability estimates (defaults to - * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * - * @since 2.1 - */ - public ChiSquaredDistribution(double degreesOfFreedom, - double inverseCumAccuracy) { - gamma = new GammaDistribution(degreesOfFreedom / 2, 2); - solverAbsoluteAccuracy = inverseCumAccuracy; - } - - /** - * Access the number of degrees of freedom. - * - * @return the degrees of freedom. - */ - public double getDegreesOfFreedom() { - return gamma.getShape() * 2.0; - } - - /** {@inheritDoc} */ - @Override - public double density(double x) { - return gamma.density(x); - } - - /** {@inheritDoc} **/ - @Override - public double logDensity(double x) { - return gamma.logDensity(x); - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(double x) { - return gamma.cumulativeProbability(x); - } - - /** {@inheritDoc} */ - @Override - protected double getSolverAbsoluteAccuracy() { - return solverAbsoluteAccuracy; - } - - /** - * {@inheritDoc} - * - * For {@code k} degrees of freedom, the mean is {@code k}. - */ - @Override - public double getNumericalMean() { - return getDegreesOfFreedom(); - } - - /** - * {@inheritDoc} - * - * @return {@code 2 * k}, where {@code k} is the number of degrees of freedom. - */ - @Override - public double getNumericalVariance() { - return 2 * getDegreesOfFreedom(); - } - - /** - * {@inheritDoc} - * - * The lower bound of the support is always 0 no matter the - * degrees of freedom. - * - * @return zero. - */ - @Override - public double getSupportLowerBound() { - return 0; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is always positive infinity no matter the - * degrees of freedom. - * - * @return {@code Double.POSITIVE_INFINITY}. - */ - @Override - public double getSupportUpperBound() { - return Double.POSITIVE_INFINITY; - } - - /** - * {@inheritDoc} - * - * The support of this distribution is connected. - * - * @return {@code true} - */ - @Override - public boolean isSupportConnected() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/ConstantRealDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/ConstantRealDistribution.java b/src/main/java/org/apache/commons/math4/distribution/ConstantRealDistribution.java deleted file mode 100644 index b699e03..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/ConstantRealDistribution.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.math4.distribution; - -import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.rng.UniformRandomProvider; - -/** - * Implementation of the constant real distribution. - * - * @since 3.4 - */ -public class ConstantRealDistribution extends AbstractRealDistribution { - - /** Serialization ID */ - private static final long serialVersionUID = -4157745166772046273L; - - /** Constant value of the distribution */ - private final double value; - - /** - * Create a constant real distribution with the given value. - * - * @param value the constant value of this distribution - */ - public ConstantRealDistribution(double value) { - this.value = value; - } - - /** {@inheritDoc} */ - @Override - public double density(double x) { - return x == value ? 1 : 0; - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(double x) { - return x < value ? 0 : 1; - } - - /** {@inheritDoc} */ - @Override - public double inverseCumulativeProbability(final double p) - throws OutOfRangeException { - if (p < 0.0 || p > 1.0) { - throw new OutOfRangeException(p, 0, 1); - } - return value; - } - - /** - * {@inheritDoc} - */ - @Override - public double getNumericalMean() { - return value; - } - - /** - * {@inheritDoc} - */ - @Override - public double getNumericalVariance() { - return 0; - } - - /** - * {@inheritDoc} - */ - @Override - public double getSupportLowerBound() { - return value; - } - - /** - * {@inheritDoc} - */ - @Override - public double getSupportUpperBound() { - return value; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isSupportConnected() { - return true; - } - - /** - * {@inheritDoc} - * - * @param rng Not used: distribution contains a single value. - * @return the value of the distribution. - */ - @Override - public RealDistribution.Sampler createSampler(final UniformRandomProvider rng) { - return new RealDistribution.Sampler() { - /** {@inheritDoc} */ - @Override - public double sample() { - return value; - } - }; - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/ExponentialDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/ExponentialDistribution.java b/src/main/java/org/apache/commons/math4/distribution/ExponentialDistribution.java deleted file mode 100644 index 6ee541c..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/ExponentialDistribution.java +++ /dev/null @@ -1,235 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.distribution; - -import org.apache.commons.math4.exception.NotStrictlyPositiveException; -import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.FastMath; -import org.apache.commons.rng.UniformRandomProvider; -import org.apache.commons.rng.sampling.distribution.ContinuousSampler; -import org.apache.commons.rng.sampling.distribution.AhrensDieterExponentialSampler; - -/** - * Implementation of the exponential distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Exponential_distribution">Exponential distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/ExponentialDistribution.html">Exponential distribution (MathWorld)</a> - */ -public class ExponentialDistribution extends AbstractRealDistribution { - /** - * Default inverse cumulative probability accuracy. - * @since 2.1 - */ - public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; - /** Serializable version identifier */ - private static final long serialVersionUID = 20160311L; - /** The mean of this distribution. */ - private final double mean; - /** The logarithm of the mean, stored to reduce computing time. **/ - private final double logMean; - /** Inverse cumulative probability accuracy. */ - private final double solverAbsoluteAccuracy; - - /** - * Creates a distribution. - * - * @param mean mean of this distribution. - */ - public ExponentialDistribution(double mean) { - this(mean, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a distribution. - * - * @param mean Mean of this distribution. - * @param inverseCumAccuracy Maximum absolute error in inverse - * cumulative probability estimates (defaults to - * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NotStrictlyPositiveException if {@code mean <= 0}. - * - * @since 2.1 - */ - public ExponentialDistribution(double mean, - double inverseCumAccuracy) - throws NotStrictlyPositiveException { - if (mean <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean); - } - this.mean = mean; - logMean = FastMath.log(mean); - solverAbsoluteAccuracy = inverseCumAccuracy; - } - - /** - * Access the mean. - * - * @return the mean. - */ - public double getMean() { - return mean; - } - - /** {@inheritDoc} */ - @Override - public double density(double x) { - final double logDensity = logDensity(x); - return logDensity == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logDensity); - } - - /** {@inheritDoc} **/ - @Override - public double logDensity(double x) { - if (x < 0) { - return Double.NEGATIVE_INFINITY; - } - return -x / mean - logMean; - } - - /** - * {@inheritDoc} - * - * The implementation of this method is based on: - * <ul> - * <li> - * <a href="http://mathworld.wolfram.com/ExponentialDistribution.html"> - * Exponential Distribution</a>, equation (1).</li> - * </ul> - */ - @Override - public double cumulativeProbability(double x) { - double ret; - if (x <= 0.0) { - ret = 0.0; - } else { - ret = 1.0 - FastMath.exp(-x / mean); - } - return ret; - } - - /** - * {@inheritDoc} - * - * Returns {@code 0} when {@code p= = 0} and - * {@code Double.POSITIVE_INFINITY} when {@code p == 1}. - */ - @Override - public double inverseCumulativeProbability(double p) throws OutOfRangeException { - double ret; - - if (p < 0.0 || p > 1.0) { - throw new OutOfRangeException(p, 0.0, 1.0); - } else if (p == 1.0) { - ret = Double.POSITIVE_INFINITY; - } else { - ret = -mean * FastMath.log(1.0 - p); - } - - return ret; - } - - /** {@inheritDoc} */ - @Override - protected double getSolverAbsoluteAccuracy() { - return solverAbsoluteAccuracy; - } - - /** - * {@inheritDoc} - * - * For mean parameter {@code k}, the mean is {@code k}. - */ - @Override - public double getNumericalMean() { - return getMean(); - } - - /** - * {@inheritDoc} - * - * For mean parameter {@code k}, the variance is {@code k^2}. - */ - @Override - public double getNumericalVariance() { - final double m = getMean(); - return m * m; - } - - /** - * {@inheritDoc} - * - * The lower bound of the support is always 0 no matter the mean parameter. - * - * @return lower bound of the support (always 0) - */ - @Override - public double getSupportLowerBound() { - return 0; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is always positive infinity - * no matter the mean parameter. - * - * @return upper bound of the support (always Double.POSITIVE_INFINITY) - */ - @Override - public double getSupportUpperBound() { - return Double.POSITIVE_INFINITY; - } - - /** - * {@inheritDoc} - * - * The support of this distribution is connected. - * - * @return {@code true} - */ - @Override - public boolean isSupportConnected() { - return true; - } - - /** - * {@inheritDoc} - * - * <p>Sampling algorithm uses the - * <a href="http://www.jesus.ox.ac.uk/~clifford/a5/chap1/node5.html"> - * inversion method</a> to generate exponentially distributed - * random values from uniform deviates. - * </p> - */ - @Override - public RealDistribution.Sampler createSampler(final UniformRandomProvider rng) { - return new RealDistribution.Sampler() { - /** - * Exponential distribution sampler. - */ - private final ContinuousSampler sampler = - new AhrensDieterExponentialSampler(rng, mean); - - /**{@inheritDoc} */ - @Override - public double sample() { - return sampler.sample(); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/FDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/FDistribution.java b/src/main/java/org/apache/commons/math4/distribution/FDistribution.java deleted file mode 100644 index 26caf51..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/FDistribution.java +++ /dev/null @@ -1,267 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.math4.distribution; - -import org.apache.commons.math4.exception.NotStrictlyPositiveException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.numbers.gamma.LogBeta; -import org.apache.commons.numbers.gamma.RegularizedBeta; -import org.apache.commons.math4.util.FastMath; - -/** - * Implementation of the F-distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/F-distribution">F-distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/F-Distribution.html">F-distribution (MathWorld)</a> - */ -public class FDistribution extends AbstractRealDistribution { - /** - * Default inverse cumulative probability accuracy. - * @since 2.1 - */ - public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; - /** Serializable version identifier. */ - private static final long serialVersionUID = -8516354193418641566L; - /** The numerator degrees of freedom. */ - private final double numeratorDegreesOfFreedom; - /** The numerator degrees of freedom. */ - private final double denominatorDegreesOfFreedom; - /** Inverse cumulative probability accuracy. */ - private final double solverAbsoluteAccuracy; - /** Cached numerical variance */ - private double numericalVariance = Double.NaN; - /** Whether or not the numerical variance has been calculated */ - private boolean numericalVarianceIsCalculated = false; - - /** - * Creates a using the given degrees of freedom. - * - * @param numeratorDegreesOfFreedom Numerator degrees of freedom. - * @param denominatorDegreesOfFreedom Denominator degrees of freedom. - * @throws NotStrictlyPositiveException if - * {@code numeratorDegreesOfFreedom <= 0} or - * {@code denominatorDegreesOfFreedom <= 0}. - */ - public FDistribution(double numeratorDegreesOfFreedom, - double denominatorDegreesOfFreedom) - throws NotStrictlyPositiveException { - this(numeratorDegreesOfFreedom, denominatorDegreesOfFreedom, - DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a distribution. - * - * @param numeratorDegreesOfFreedom Numerator degrees of freedom. - * @param denominatorDegreesOfFreedom Denominator degrees of freedom. - * @param inverseCumAccuracy the maximum absolute error in inverse - * cumulative probability estimates. - * @throws NotStrictlyPositiveException if {@code numeratorDegreesOfFreedom <= 0} or - * {@code denominatorDegreesOfFreedom <= 0}. - */ - public FDistribution(double numeratorDegreesOfFreedom, - double denominatorDegreesOfFreedom, - double inverseCumAccuracy) - throws NotStrictlyPositiveException { - if (numeratorDegreesOfFreedom <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.DEGREES_OF_FREEDOM, - numeratorDegreesOfFreedom); - } - if (denominatorDegreesOfFreedom <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.DEGREES_OF_FREEDOM, - denominatorDegreesOfFreedom); - } - this.numeratorDegreesOfFreedom = numeratorDegreesOfFreedom; - this.denominatorDegreesOfFreedom = denominatorDegreesOfFreedom; - solverAbsoluteAccuracy = inverseCumAccuracy; - } - - /** - * {@inheritDoc} - * - * @since 2.1 - */ - @Override - public double density(double x) { - return FastMath.exp(logDensity(x)); - } - - /** {@inheritDoc} **/ - @Override - public double logDensity(double x) { - final double nhalf = numeratorDegreesOfFreedom / 2; - final double mhalf = denominatorDegreesOfFreedom / 2; - final double logx = FastMath.log(x); - final double logn = FastMath.log(numeratorDegreesOfFreedom); - final double logm = FastMath.log(denominatorDegreesOfFreedom); - final double lognxm = FastMath.log(numeratorDegreesOfFreedom * x + - denominatorDegreesOfFreedom); - return nhalf * logn + nhalf * logx - logx + - mhalf * logm - nhalf * lognxm - mhalf * lognxm - - LogBeta.value(nhalf, mhalf); - } - - /** - * {@inheritDoc} - * - * The implementation of this method is based on - * <ul> - * <li> - * <a href="http://mathworld.wolfram.com/F-Distribution.html"> - * F-Distribution</a>, equation (4). - * </li> - * </ul> - */ - @Override - public double cumulativeProbability(double x) { - double ret; - if (x <= 0) { - ret = 0; - } else { - double n = numeratorDegreesOfFreedom; - double m = denominatorDegreesOfFreedom; - - ret = RegularizedBeta.value((n * x) / (m + n * x), - 0.5 * n, - 0.5 * m); - } - return ret; - } - - /** - * Access the numerator degrees of freedom. - * - * @return the numerator degrees of freedom. - */ - public double getNumeratorDegreesOfFreedom() { - return numeratorDegreesOfFreedom; - } - - /** - * Access the denominator degrees of freedom. - * - * @return the denominator degrees of freedom. - */ - public double getDenominatorDegreesOfFreedom() { - return denominatorDegreesOfFreedom; - } - - /** {@inheritDoc} */ - @Override - protected double getSolverAbsoluteAccuracy() { - return solverAbsoluteAccuracy; - } - - /** - * {@inheritDoc} - * - * For denominator degrees of freedom parameter {@code b}, the mean is - * <ul> - * <li>if {@code b > 2} then {@code b / (b - 2)},</li> - * <li>else undefined ({@code Double.NaN}). - * </ul> - */ - @Override - public double getNumericalMean() { - final double denominatorDF = getDenominatorDegreesOfFreedom(); - - if (denominatorDF > 2) { - return denominatorDF / (denominatorDF - 2); - } - - return Double.NaN; - } - - /** - * {@inheritDoc} - * - * For numerator degrees of freedom parameter {@code a} and denominator - * degrees of freedom parameter {@code b}, the variance is - * <ul> - * <li> - * if {@code b > 4} then - * {@code [2 * b^2 * (a + b - 2)] / [a * (b - 2)^2 * (b - 4)]}, - * </li> - * <li>else undefined ({@code Double.NaN}). - * </ul> - */ - @Override - public double getNumericalVariance() { - if (!numericalVarianceIsCalculated) { - numericalVariance = calculateNumericalVariance(); - numericalVarianceIsCalculated = true; - } - return numericalVariance; - } - - /** - * used by {@link #getNumericalVariance()} - * - * @return the variance of this distribution - */ - protected double calculateNumericalVariance() { - final double denominatorDF = getDenominatorDegreesOfFreedom(); - - if (denominatorDF > 4) { - final double numeratorDF = getNumeratorDegreesOfFreedom(); - final double denomDFMinusTwo = denominatorDF - 2; - - return ( 2 * (denominatorDF * denominatorDF) * (numeratorDF + denominatorDF - 2) ) / - ( (numeratorDF * (denomDFMinusTwo * denomDFMinusTwo) * (denominatorDF - 4)) ); - } - - return Double.NaN; - } - - /** - * {@inheritDoc} - * - * The lower bound of the support is always 0 no matter the parameters. - * - * @return lower bound of the support (always 0) - */ - @Override - public double getSupportLowerBound() { - return 0; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is always positive infinity - * no matter the parameters. - * - * @return upper bound of the support (always Double.POSITIVE_INFINITY) - */ - @Override - public double getSupportUpperBound() { - return Double.POSITIVE_INFINITY; - } - - /** - * {@inheritDoc} - * - * The support of this distribution is connected. - * - * @return {@code true} - */ - @Override - public boolean isSupportConnected() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/GammaDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/GammaDistribution.java b/src/main/java/org/apache/commons/math4/distribution/GammaDistribution.java deleted file mode 100644 index 5aa27cc..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/GammaDistribution.java +++ /dev/null @@ -1,397 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.distribution; - -import org.apache.commons.math4.exception.NotStrictlyPositiveException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.numbers.gamma.LanczosApproximation; -import org.apache.commons.numbers.gamma.RegularizedGamma; -import org.apache.commons.math4.util.FastMath; -import org.apache.commons.rng.UniformRandomProvider; -import org.apache.commons.rng.sampling.distribution.ContinuousSampler; -import org.apache.commons.rng.sampling.distribution.AhrensDieterMarsagliaTsangGammaSampler; - -/** - * Implementation of the Gamma distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Gamma_distribution">Gamma distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/GammaDistribution.html">Gamma distribution (MathWorld)</a> - */ -public class GammaDistribution extends AbstractRealDistribution { - /** - * Default inverse cumulative probability accuracy. - * @since 2.1 - */ - public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; - /** Lanczos constant. */ - private static final double LANCZOS_G = LanczosApproximation.g(); - /** Serializable version identifier. */ - private static final long serialVersionUID = 20160311L; - /** The shape parameter. */ - private final double shape; - /** The scale parameter. */ - private final double scale; - /** - * The constant value of {@code shape + g + 0.5}, where {@code g} is the - * Lanczos constant {@link LanczosApproximation#g()}. - */ - private final double shiftedShape; - /** - * The constant value of - * {@code shape / scale * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)}, - * where {@code L(shape)} is the Lanczos approximation returned by - * {@link LanczosApproximation#value(double)}. This prefactor is used in - * {@link #density(double)}, when no overflow occurs with the natural - * calculation. - */ - private final double densityPrefactor1; - /** - * The constant value of - * {@code log(shape / scale * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape))}, - * where {@code L(shape)} is the Lanczos approximation returned by - * {@link LanczosApproximation#value(double)}. This prefactor is used in - * {@link #logDensity(double)}, when no overflow occurs with the natural - * calculation. - */ - private final double logDensityPrefactor1; - /** - * The constant value of - * {@code shape * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)}, - * where {@code L(shape)} is the Lanczos approximation returned by - * {@link LanczosApproximation#value(double)}. This prefactor is used in - * {@link #density(double)}, when overflow occurs with the natural - * calculation. - */ - private final double densityPrefactor2; - /** - * The constant value of - * {@code log(shape * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape))}, - * where {@code L(shape)} is the Lanczos approximation returned by - * {@link LanczosApproximation#value(double)}. This prefactor is used in - * {@link #logDensity(double)}, when overflow occurs with the natural - * calculation. - */ - private final double logDensityPrefactor2; - /** - * Lower bound on {@code y = x / scale} for the selection of the computation - * method in {@link #density(double)}. For {@code y <= minY}, the natural - * calculation overflows. - */ - private final double minY; - /** - * Upper bound on {@code log(y)} ({@code y = x / scale}) for the selection - * of the computation method in {@link #density(double)}. For - * {@code log(y) >= maxLogY}, the natural calculation overflows. - */ - private final double maxLogY; - /** Inverse cumulative probability accuracy. */ - private final double solverAbsoluteAccuracy; - - /** - * Creates a distribution. - * - * @param shape the shape parameter - * @param scale the scale parameter - * @throws NotStrictlyPositiveException if {@code shape <= 0} or - * {@code scale <= 0}. - */ - public GammaDistribution(double shape, double scale) - throws NotStrictlyPositiveException { - this(shape, scale, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a distribution. - * - * @param shape the shape parameter - * @param scale the scale parameter - * @param inverseCumAccuracy the maximum absolute error in inverse - * cumulative probability estimates (defaults to - * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NotStrictlyPositiveException if {@code shape <= 0} or - * {@code scale <= 0}. - */ - public GammaDistribution(double shape, - double scale, - double inverseCumAccuracy) - throws NotStrictlyPositiveException { - if (shape <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, shape); - } - if (scale <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, scale); - } - - this.shape = shape; - this.scale = scale; - this.solverAbsoluteAccuracy = inverseCumAccuracy; - this.shiftedShape = shape + LANCZOS_G + 0.5; - final double aux = FastMath.E / (2.0 * FastMath.PI * shiftedShape); - this.densityPrefactor2 = shape * FastMath.sqrt(aux) / LanczosApproximation.value(shape); - this.logDensityPrefactor2 = FastMath.log(shape) + 0.5 * FastMath.log(aux) - - FastMath.log(LanczosApproximation.value(shape)); - this.densityPrefactor1 = this.densityPrefactor2 / scale * - FastMath.pow(shiftedShape, -shape) * - FastMath.exp(shape + LANCZOS_G); - this.logDensityPrefactor1 = this.logDensityPrefactor2 - FastMath.log(scale) - - FastMath.log(shiftedShape) * shape + - shape + LANCZOS_G; - this.minY = shape + LANCZOS_G - FastMath.log(Double.MAX_VALUE); - this.maxLogY = FastMath.log(Double.MAX_VALUE) / (shape - 1.0); - } - - /** - * Returns the shape parameter of {@code this} distribution. - * - * @return the shape parameter - * @since 3.1 - */ - public double getShape() { - return shape; - } - - /** - * Returns the scale parameter of {@code this} distribution. - * - * @return the scale parameter - * @since 3.1 - */ - public double getScale() { - return scale; - } - - /** {@inheritDoc} */ - @Override - public double density(double x) { - /* The present method must return the value of - * - * 1 x a - x - * ---------- (-) exp(---) - * x Gamma(a) b b - * - * where a is the shape parameter, and b the scale parameter. - * Substituting the Lanczos approximation of Gamma(a) leads to the - * following expression of the density - * - * a e 1 y a - * - sqrt(------------------) ---- (-----------) exp(a - y + g), - * x 2 pi (a + g + 0.5) L(a) a + g + 0.5 - * - * where y = x / b. The above formula is the "natural" computation, which - * is implemented when no overflow is likely to occur. If overflow occurs - * with the natural computation, the following identity is used. It is - * based on the BOOST library - * http://www.boost.org/doc/libs/1_35_0/libs/math/doc/sf_and_dist/html/math_toolkit/special/sf_gamma/igamma.html - * Formula (15) needs adaptations, which are detailed below. - * - * y a - * (-----------) exp(a - y + g) - * a + g + 0.5 - * y - a - g - 0.5 y (g + 0.5) - * = exp(a log1pm(---------------) - ----------- + g), - * a + g + 0.5 a + g + 0.5 - * - * where log1pm(z) = log(1 + z) - z. Therefore, the value to be - * returned is - * - * a e 1 - * - sqrt(------------------) ---- - * x 2 pi (a + g + 0.5) L(a) - * y - a - g - 0.5 y (g + 0.5) - * * exp(a log1pm(---------------) - ----------- + g). - * a + g + 0.5 a + g + 0.5 - */ - if (x < 0) { - return 0; - } - final double y = x / scale; - if ((y <= minY) || (FastMath.log(y) >= maxLogY)) { - /* - * Overflow. - */ - final double aux1 = (y - shiftedShape) / shiftedShape; - final double aux2 = shape * (FastMath.log1p(aux1) - aux1); - final double aux3 = -y * (LANCZOS_G + 0.5) / shiftedShape + LANCZOS_G + aux2; - return densityPrefactor2 / x * FastMath.exp(aux3); - } - /* - * Natural calculation. - */ - return densityPrefactor1 * FastMath.exp(-y) * FastMath.pow(y, shape - 1); - } - - /** {@inheritDoc} **/ - @Override - public double logDensity(double x) { - /* - * see the comment in {@link #density(double)} for computation details - */ - if (x < 0) { - return Double.NEGATIVE_INFINITY; - } - final double y = x / scale; - if ((y <= minY) || (FastMath.log(y) >= maxLogY)) { - /* - * Overflow. - */ - final double aux1 = (y - shiftedShape) / shiftedShape; - final double aux2 = shape * (FastMath.log1p(aux1) - aux1); - final double aux3 = -y * (LANCZOS_G + 0.5) / shiftedShape + LANCZOS_G + aux2; - return logDensityPrefactor2 - FastMath.log(x) + aux3; - } - /* - * Natural calculation. - */ - return logDensityPrefactor1 - y + FastMath.log(y) * (shape - 1); - } - - /** - * {@inheritDoc} - * - * The implementation of this method is based on: - * <ul> - * <li> - * <a href="http://mathworld.wolfram.com/Chi-SquaredDistribution.html"> - * Chi-Squared Distribution</a>, equation (9). - * </li> - * <li>Casella, G., & Berger, R. (1990). <i>Statistical Inference</i>. - * Belmont, CA: Duxbury Press. - * </li> - * </ul> - */ - @Override - public double cumulativeProbability(double x) { - double ret; - - if (x <= 0) { - ret = 0; - } else { - ret = RegularizedGamma.P.value(shape, x / scale); - } - - return ret; - } - - /** {@inheritDoc} */ - @Override - protected double getSolverAbsoluteAccuracy() { - return solverAbsoluteAccuracy; - } - - /** - * {@inheritDoc} - * - * For shape parameter {@code alpha} and scale parameter {@code beta}, the - * mean is {@code alpha * beta}. - */ - @Override - public double getNumericalMean() { - return shape * scale; - } - - /** - * {@inheritDoc} - * - * For shape parameter {@code alpha} and scale parameter {@code beta}, the - * variance is {@code alpha * beta^2}. - * - * @return {@inheritDoc} - */ - @Override - public double getNumericalVariance() { - return shape * scale * scale; - } - - /** - * {@inheritDoc} - * - * The lower bound of the support is always 0 no matter the parameters. - * - * @return lower bound of the support (always 0) - */ - @Override - public double getSupportLowerBound() { - return 0; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is always positive infinity - * no matter the parameters. - * - * @return upper bound of the support (always Double.POSITIVE_INFINITY) - */ - @Override - public double getSupportUpperBound() { - return Double.POSITIVE_INFINITY; - } - - /** - * {@inheritDoc} - * - * The support of this distribution is connected. - * - * @return {@code true} - */ - @Override - public boolean isSupportConnected() { - return true; - } - - /** - * {@inheritDoc} - * - * <p> - * Sampling algorithms: - * <ul> - * <li> - * For {@code 0 < shape < 1}: - * <blockquote> - * Ahrens, J. H. and Dieter, U., - * <i>Computer methods for sampling from gamma, beta, Poisson and binomial distributions,</i> - * Computing, 12, 223-246, 1974. - * </blockquote> - * </li> - * <li> - * For {@code shape >= 1}: - * <blockquote> - * Marsaglia and Tsang, <i>A Simple Method for Generating - * Gamma Variables.</i> ACM Transactions on Mathematical Software, - * Volume 26 Issue 3, September, 2000. - * </blockquote> - * </li> - * </ul> - */ - @Override - public RealDistribution.Sampler createSampler(final UniformRandomProvider rng) { - return new RealDistribution.Sampler() { - /** - * Gamma distribution sampler. - */ - private final ContinuousSampler sampler = - new AhrensDieterMarsagliaTsangGammaSampler(rng, scale, shape); - - /**{@inheritDoc} */ - @Override - public double sample() { - return sampler.sample(); - } - }; - - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/GeometricDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/GeometricDistribution.java b/src/main/java/org/apache/commons/math4/distribution/GeometricDistribution.java deleted file mode 100644 index 4c37953..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/GeometricDistribution.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.distribution; - -import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.FastMath; - -/** - * Implementation of the geometric distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Geometric_distribution">Geometric distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/GeometricDistribution.html">Geometric Distribution (MathWorld)</a> - * @since 3.3 - */ -public class GeometricDistribution extends AbstractIntegerDistribution { - - /** Serializable version identifier. */ - private static final long serialVersionUID = 20160318L; - /** The probability of success. */ - private final double probabilityOfSuccess; - /** {@code log(p)} where p is the probability of success. */ - private final double logProbabilityOfSuccess; - /** {@code log(1 - p)} where p is the probability of success. */ - private final double log1mProbabilityOfSuccess; - - /** - * Creates a geometric distribution. - * - * @param p Probability of success. - * @throws OutOfRangeException if {@code p <= 0} or {@code p > 1}. - */ - public GeometricDistribution(double p) { - if (p <= 0 || p > 1) { - throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_LEFT, p, 0, 1); - } - - probabilityOfSuccess = p; - logProbabilityOfSuccess = FastMath.log(p); - log1mProbabilityOfSuccess = FastMath.log1p(-p); - } - - /** - * Access the probability of success for this distribution. - * - * @return the probability of success. - */ - public double getProbabilityOfSuccess() { - return probabilityOfSuccess; - } - - /** {@inheritDoc} */ - @Override - public double probability(int x) { - if (x < 0) { - return 0.0; - } else { - return FastMath.exp(log1mProbabilityOfSuccess * x) * probabilityOfSuccess; - } - } - - /** {@inheritDoc} */ - @Override - public double logProbability(int x) { - if (x < 0) { - return Double.NEGATIVE_INFINITY; - } else { - return x * log1mProbabilityOfSuccess + logProbabilityOfSuccess; - } - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(int x) { - if (x < 0) { - return 0.0; - } else { - return -FastMath.expm1(log1mProbabilityOfSuccess * (x + 1)); - } - } - - /** - * {@inheritDoc} - * - * For probability parameter {@code p}, the mean is {@code (1 - p) / p}. - */ - @Override - public double getNumericalMean() { - return (1 - probabilityOfSuccess) / probabilityOfSuccess; - } - - /** - * {@inheritDoc} - * - * For probability parameter {@code p}, the variance is - * {@code (1 - p) / (p * p)}. - */ - @Override - public double getNumericalVariance() { - return (1 - probabilityOfSuccess) / (probabilityOfSuccess * probabilityOfSuccess); - } - - /** - * {@inheritDoc} - * - * The lower bound of the support is always 0. - * - * @return lower bound of the support (always 0) - */ - @Override - public int getSupportLowerBound() { - return 0; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is infinite (which we approximate as - * {@code Integer.MAX_VALUE}). - * - * @return upper bound of the support (always Integer.MAX_VALUE) - */ - @Override - public int getSupportUpperBound() { - return Integer.MAX_VALUE; - } - - /** - * {@inheritDoc} - * - * The support of this distribution is connected. - * - * @return {@code true} - */ - @Override - public boolean isSupportConnected() { - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public int inverseCumulativeProbability(double p) throws OutOfRangeException { - if (p < 0 || p > 1) { - throw new OutOfRangeException(p, 0, 1); - } - if (p == 1) { - return Integer.MAX_VALUE; - } - if (p == 0) { - return 0; - } - return Math.max(0, (int) Math.ceil(FastMath.log1p(-p)/log1mProbabilityOfSuccess-1)); - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/GumbelDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/GumbelDistribution.java b/src/main/java/org/apache/commons/math4/distribution/GumbelDistribution.java deleted file mode 100644 index be06d09..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/GumbelDistribution.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.distribution; - -import org.apache.commons.math4.exception.NotStrictlyPositiveException; -import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.FastMath; -import org.apache.commons.math4.util.MathUtils; - -/** - * This class implements the Gumbel distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Gumbel_distribution">Gumbel Distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/GumbelDistribution.html">Gumbel Distribution (Mathworld)</a> - * - * @since 3.4 - */ -public class GumbelDistribution extends AbstractRealDistribution { - - /** Serializable version identifier. */ - private static final long serialVersionUID = 20160311L; - - /** - * Approximation of Euler's constant - * see http://mathworld.wolfram.com/Euler-MascheroniConstantApproximations.html - */ - private static final double EULER = FastMath.PI / (2 * FastMath.E); - - /** The location parameter. */ - private final double mu; - /** The scale parameter. */ - private final double beta; - - /** - * Creates a distribution. - * - * @param mu location parameter - * @param beta scale parameter (must be positive) - * @throws NotStrictlyPositiveException if {@code beta <= 0} - */ - public GumbelDistribution(double mu, double beta) { - if (beta <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, beta); - } - - this.beta = beta; - this.mu = mu; - } - - /** - * Access the location parameter, {@code mu}. - * - * @return the location parameter. - */ - public double getLocation() { - return mu; - } - - /** - * Access the scale parameter, {@code beta}. - * - * @return the scale parameter. - */ - public double getScale() { - return beta; - } - - /** {@inheritDoc} */ - @Override - public double density(double x) { - final double z = (x - mu) / beta; - final double t = FastMath.exp(-z); - return FastMath.exp(-z - t) / beta; - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(double x) { - final double z = (x - mu) / beta; - return FastMath.exp(-FastMath.exp(-z)); - } - - /** {@inheritDoc} */ - @Override - public double inverseCumulativeProbability(double p) throws OutOfRangeException { - if (p < 0.0 || p > 1.0) { - throw new OutOfRangeException(p, 0.0, 1.0); - } else if (p == 0) { - return Double.NEGATIVE_INFINITY; - } else if (p == 1) { - return Double.POSITIVE_INFINITY; - } - return mu - FastMath.log(-FastMath.log(p)) * beta; - } - - /** {@inheritDoc} */ - @Override - public double getNumericalMean() { - return mu + EULER * beta; - } - - /** {@inheritDoc} */ - @Override - public double getNumericalVariance() { - return (MathUtils.PI_SQUARED) / 6.0 * (beta * beta); - } - - /** {@inheritDoc} */ - @Override - public double getSupportLowerBound() { - return Double.NEGATIVE_INFINITY; - } - - /** {@inheritDoc} */ - @Override - public double getSupportUpperBound() { - return Double.POSITIVE_INFINITY; - } - - /** {@inheritDoc} */ - @Override - public boolean isSupportConnected() { - return true; - } - -}