http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/HypergeometricDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/HypergeometricDistribution.java b/src/main/java/org/apache/commons/math4/distribution/HypergeometricDistribution.java deleted file mode 100644 index 137738d..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/HypergeometricDistribution.java +++ /dev/null @@ -1,325 +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.NotStrictlyPositiveException; -import org.apache.commons.math4.exception.NumberIsTooLargeException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.FastMath; - -/** - * Implementation of the hypergeometric distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Hypergeometric_distribution">Hypergeometric distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/HypergeometricDistribution.html">Hypergeometric distribution (MathWorld)</a> - */ -public class HypergeometricDistribution extends AbstractIntegerDistribution { - /** Serializable version identifier. */ - private static final long serialVersionUID = 20160318L; - /** The number of successes in the population. */ - private final int numberOfSuccesses; - /** The population size. */ - private final int populationSize; - /** The sample size. */ - private final int sampleSize; - /** Cached numerical variance */ - private double numericalVariance = Double.NaN; - /** Whether or not the numerical variance has been calculated */ - private boolean numericalVarianceIsCalculated = false; - - /** - * Creates a new hypergeometric distribution. - * - * @param populationSize Population size. - * @param numberOfSuccesses Number of successes in the population. - * @param sampleSize Sample size. - * @throws NotPositiveException if {@code numberOfSuccesses < 0}. - * @throws NotStrictlyPositiveException if {@code populationSize <= 0}. - * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize}, - * or {@code sampleSize > populationSize}. - */ - public HypergeometricDistribution(int populationSize, - int numberOfSuccesses, - int sampleSize) - throws NotPositiveException, - NotStrictlyPositiveException, - NumberIsTooLargeException { - if (populationSize <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.POPULATION_SIZE, - populationSize); - } - if (numberOfSuccesses < 0) { - throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES, - numberOfSuccesses); - } - if (sampleSize < 0) { - throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, - sampleSize); - } - - if (numberOfSuccesses > populationSize) { - throw new NumberIsTooLargeException(LocalizedFormats.NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE, - numberOfSuccesses, populationSize, true); - } - if (sampleSize > populationSize) { - throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_LARGER_THAN_POPULATION_SIZE, - sampleSize, populationSize, true); - } - - this.numberOfSuccesses = numberOfSuccesses; - this.populationSize = populationSize; - this.sampleSize = sampleSize; - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(int x) { - double ret; - - int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize); - if (x < domain[0]) { - ret = 0.0; - } else if (x >= domain[1]) { - ret = 1.0; - } else { - ret = innerCumulativeProbability(domain[0], x, 1); - } - - return ret; - } - - /** - * Return the domain for the given hypergeometric distribution parameters. - * - * @param n Population size. - * @param m Number of successes in the population. - * @param k Sample size. - * @return a two element array containing the lower and upper bounds of the - * hypergeometric distribution. - */ - private int[] getDomain(int n, int m, int k) { - return new int[] { getLowerDomain(n, m, k), getUpperDomain(m, k) }; - } - - /** - * Return the lowest domain value for the given hypergeometric distribution - * parameters. - * - * @param n Population size. - * @param m Number of successes in the population. - * @param k Sample size. - * @return the lowest domain value of the hypergeometric distribution. - */ - private int getLowerDomain(int n, int m, int k) { - return FastMath.max(0, m - (n - k)); - } - - /** - * Access the number of successes. - * - * @return the number of successes. - */ - public int getNumberOfSuccesses() { - return numberOfSuccesses; - } - - /** - * Access the population size. - * - * @return the population size. - */ - public int getPopulationSize() { - return populationSize; - } - - /** - * Access the sample size. - * - * @return the sample size. - */ - public int getSampleSize() { - return sampleSize; - } - - /** - * Return the highest domain value for the given hypergeometric distribution - * parameters. - * - * @param m Number of successes in the population. - * @param k Sample size. - * @return the highest domain value of the hypergeometric distribution. - */ - private int getUpperDomain(int m, int k) { - return FastMath.min(k, m); - } - - /** {@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) { - double ret; - - int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize); - if (x < domain[0] || x > domain[1]) { - ret = Double.NEGATIVE_INFINITY; - } else { - double p = (double) sampleSize / (double) populationSize; - double q = (double) (populationSize - sampleSize) / (double) populationSize; - double p1 = SaddlePointExpansion.logBinomialProbability(x, - numberOfSuccesses, p, q); - double p2 = - SaddlePointExpansion.logBinomialProbability(sampleSize - x, - populationSize - numberOfSuccesses, p, q); - double p3 = - SaddlePointExpansion.logBinomialProbability(sampleSize, populationSize, p, q); - ret = p1 + p2 - p3; - } - - return ret; - } - - /** - * For this distribution, {@code X}, this method returns {@code P(X >= x)}. - * - * @param x Value at which the CDF is evaluated. - * @return the upper tail CDF for this distribution. - * @since 1.1 - */ - public double upperCumulativeProbability(int x) { - double ret; - - final int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize); - if (x <= domain[0]) { - ret = 1.0; - } else if (x > domain[1]) { - ret = 0.0; - } else { - ret = innerCumulativeProbability(domain[1], x, -1); - } - - return ret; - } - - /** - * For this distribution, {@code X}, this method returns - * {@code P(x0 <= X <= x1)}. - * This probability is computed by summing the point probabilities for the - * values {@code x0, x0 + 1, x0 + 2, ..., x1}, in the order directed by - * {@code dx}. - * - * @param x0 Inclusive lower bound. - * @param x1 Inclusive upper bound. - * @param dx Direction of summation (1 indicates summing from x0 to x1, and - * 0 indicates summing from x1 to x0). - * @return {@code P(x0 <= X <= x1)}. - */ - private double innerCumulativeProbability(int x0, int x1, int dx) { - double ret = probability(x0); - while (x0 != x1) { - x0 += dx; - ret += probability(x0); - } - return ret; - } - - /** - * {@inheritDoc} - * - * For population size {@code N}, number of successes {@code m}, and sample - * size {@code n}, the mean is {@code n * m / N}. - */ - @Override - public double getNumericalMean() { - return getSampleSize() * (getNumberOfSuccesses() / (double) getPopulationSize()); - } - - /** - * {@inheritDoc} - * - * For population size {@code N}, number of successes {@code m}, and sample - * size {@code n}, the variance is - * {@code [n * m * (N - n) * (N - m)] / [N^2 * (N - 1)]}. - */ - @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 N = getPopulationSize(); - final double m = getNumberOfSuccesses(); - final double n = getSampleSize(); - return (n * m * (N - n) * (N - m)) / (N * N * (N - 1)); - } - - /** - * {@inheritDoc} - * - * For population size {@code N}, number of successes {@code m}, and sample - * size {@code n}, the lower bound of the support is - * {@code max(0, n + m - N)}. - * - * @return lower bound of the support - */ - @Override - public int getSupportLowerBound() { - return FastMath.max(0, - getSampleSize() + getNumberOfSuccesses() - getPopulationSize()); - } - - /** - * {@inheritDoc} - * - * For number of successes {@code m} and sample size {@code n}, the upper - * bound of the support is {@code min(m, n)}. - * - * @return upper bound of the support - */ - @Override - public int getSupportUpperBound() { - return FastMath.min(getNumberOfSuccesses(), getSampleSize()); - } - - /** - * {@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/LaplaceDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/LaplaceDistribution.java b/src/main/java/org/apache/commons/math4/distribution/LaplaceDistribution.java deleted file mode 100644 index c85618b..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/LaplaceDistribution.java +++ /dev/null @@ -1,135 +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; - -/** - * This class implements the Laplace distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Laplace_distribution">Laplace distribution (Wikipedia)</a> - * - * @since 3.4 - */ -public class LaplaceDistribution extends AbstractRealDistribution { - - /** Serializable version identifier. */ - private static final long serialVersionUID = 20160311L; - - /** 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 LaplaceDistribution(double mu, double beta) { - if (beta <= 0.0) { - throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_SCALE, beta); - } - - this.mu = mu; - this.beta = beta; - } - - /** - * 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) { - return FastMath.exp(-FastMath.abs(x - mu) / beta) / (2.0 * beta); - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(double x) { - if (x <= mu) { - return FastMath.exp((x - mu) / beta) / 2.0; - } else { - return 1.0 - FastMath.exp((mu - x) / beta) / 2.0; - } - } - - /** {@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; - } - double x = (p > 0.5) ? -Math.log(2.0 - 2.0 * p) : Math.log(2.0 * p); - return mu + beta * x; - } - - /** {@inheritDoc} */ - @Override - public double getNumericalMean() { - return mu; - } - - /** {@inheritDoc} */ - @Override - public double getNumericalVariance() { - return 2.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; - } - -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/LevyDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/LevyDistribution.java b/src/main/java/org/apache/commons/math4/distribution/LevyDistribution.java deleted file mode 100644 index 5f99f8e..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/LevyDistribution.java +++ /dev/null @@ -1,166 +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.numbers.gamma.Erfc; -import org.apache.commons.numbers.gamma.InverseErfc; -import org.apache.commons.math4.util.FastMath; - -/** - * This class implements the <a href="http://en.wikipedia.org/wiki/L%C3%A9vy_distribution"> - * Lévy distribution</a>. - * - * @since 3.2 - */ -public class LevyDistribution extends AbstractRealDistribution { - - /** Serializable UID. */ - private static final long serialVersionUID = 20630311L; - - /** Location parameter. */ - private final double mu; - - /** Scale parameter. */ - private final double c; // Setting this to 1 returns a cumProb of 1.0 - - /** Half of c (for calculations). */ - private final double halfC; - - /** - * Creates a distribution. - * - * @param mu location - * @param c scale parameter - */ - public LevyDistribution(final double mu, final double c) { - this.mu = mu; - this.c = c; - this.halfC = 0.5 * c; - } - - /** {@inheritDoc} - * <p> - * From Wikipedia: The probability density function of the Lévy distribution - * over the domain is - * </p> - * <div style="white-space: pre"><code> - * f(x; μ, c) = √(c / 2π) * e<sup>-c / 2 (x - μ)</sup> / (x - μ)<sup>3/2</sup> - * </code></div> - * <p> - * For this distribution, {@code X}, this method returns {@code P(X < x)}. - * If {@code x} is less than location parameter μ, {@code Double.NaN} is - * returned, as in these cases the distribution is not defined. - * </p> - */ - @Override - public double density(final double x) { - if (x < mu) { - return Double.NaN; - } - - final double delta = x - mu; - final double f = halfC / delta; - return FastMath.sqrt(f / FastMath.PI) * FastMath.exp(-f) /delta; - } - - /** {@inheritDoc} - * - * See documentation of {@link #density(double)} for computation details. - */ - @Override - public double logDensity(double x) { - if (x < mu) { - return Double.NaN; - } - - final double delta = x - mu; - final double f = halfC / delta; - return 0.5 * FastMath.log(f / FastMath.PI) - f - FastMath.log(delta); - } - - /** {@inheritDoc} - * <p> - * From Wikipedia: the cumulative distribution function is - * </p> - * <pre> - * f(x; u, c) = erfc (√ (c / 2 (x - u ))) - * </pre> - */ - @Override - public double cumulativeProbability(final double x) { - if (x < mu) { - return Double.NaN; - } - return Erfc.value(FastMath.sqrt(halfC / (x - mu))); - } - - /** {@inheritDoc} */ - @Override - public double inverseCumulativeProbability(final double p) throws OutOfRangeException { - if (p < 0.0 || p > 1.0) { - throw new OutOfRangeException(p, 0, 1); - } - final double t = InverseErfc.value(p); - return mu + halfC / (t * t); - } - - /** Get the scale parameter of the distribution. - * @return scale parameter of the distribution - */ - public double getScale() { - return c; - } - - /** Get the location parameter of the distribution. - * @return location parameter of the distribution - */ - public double getLocation() { - return mu; - } - - /** {@inheritDoc} */ - @Override - public double getNumericalMean() { - return Double.POSITIVE_INFINITY; - } - - /** {@inheritDoc} */ - @Override - public double getNumericalVariance() { - return Double.POSITIVE_INFINITY; - } - - /** {@inheritDoc} */ - @Override - public double getSupportLowerBound() { - return mu; - } - - /** {@inheritDoc} */ - @Override - public double getSupportUpperBound() { - return Double.POSITIVE_INFINITY; - } - - /** {@inheritDoc} */ - @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/LogNormalDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/LogNormalDistribution.java b/src/main/java/org/apache/commons/math4/distribution/LogNormalDistribution.java deleted file mode 100644 index 0060a97..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/LogNormalDistribution.java +++ /dev/null @@ -1,312 +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.NumberIsTooLargeException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.numbers.gamma.Erf; -import org.apache.commons.numbers.gamma.ErfDifference; -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.LogNormalSampler; -import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler; - -/** - * Implementation of the log-normal (gaussian) distribution. - * - * <p> - * <strong>Parameters:</strong> - * {@code X} is log-normally distributed if its natural logarithm {@code log(X)} - * is normally distributed. The probability distribution function of {@code X} - * is given by (for {@code x > 0}) - * </p> - * <p> - * {@code exp(-0.5 * ((ln(x) - m) / s)^2) / (s * sqrt(2 * pi) * x)} - * </p> - * <ul> - * <li>{@code m} is the <em>scale</em> parameter: this is the mean of the - * normally distributed natural logarithm of this distribution,</li> - * <li>{@code s} is the <em>shape</em> parameter: this is the standard - * deviation of the normally distributed natural logarithm of this - * distribution. - * </ul> - * - * @see <a href="http://en.wikipedia.org/wiki/Log-normal_distribution"> - * Log-normal distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/LogNormalDistribution.html"> - * Log Normal distribution (MathWorld)</a> - * - * @since 3.0 - */ -public class LogNormalDistribution extends AbstractRealDistribution { - /** Default inverse cumulative probability accuracy. */ - public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; - - /** Serializable version identifier. */ - private static final long serialVersionUID = 20120112; - - /** √(2 π) */ - private static final double SQRT2PI = FastMath.sqrt(2 * FastMath.PI); - - /** √(2) */ - private static final double SQRT2 = FastMath.sqrt(2.0); - - /** The scale parameter of this distribution. */ - private final double scale; - - /** The shape parameter of this distribution. */ - private final double shape; - /** The value of {@code log(shape) + 0.5 * log(2*PI)} stored for faster computation. */ - private final double logShapePlusHalfLog2Pi; - - /** Inverse cumulative probability accuracy. */ - private final double solverAbsoluteAccuracy; - - /** - * Creates a log-normal distribution, where the mean and standard deviation - * of the {@link NormalDistribution normally distributed} natural - * logarithm of the log-normal distribution are equal to zero and one - * respectively. In other words, the scale of the returned distribution is - * {@code 0}, while its shape is {@code 1}. - */ - public LogNormalDistribution() { - this(0, 1); - } - - /** - * Creates a log-normal distribution. - * - * @param scale Scale parameter of this distribution. - * @param shape Shape parameter of this distribution. - * @throws NotStrictlyPositiveException if {@code shape <= 0}. - */ - public LogNormalDistribution(double scale, double shape) - throws NotStrictlyPositiveException { - this(scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a log-normal distribution. - * - * @param scale Scale parameter of this distribution. - * @param shape Shape parameter of this distribution. - * @param inverseCumAccuracy Inverse cumulative probability accuracy. - * @throws NotStrictlyPositiveException if {@code shape <= 0}. - */ - public LogNormalDistribution(double scale, - double shape, - double inverseCumAccuracy) - throws NotStrictlyPositiveException { - if (shape <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, shape); - } - - this.scale = scale; - this.shape = shape; - this.logShapePlusHalfLog2Pi = FastMath.log(shape) + 0.5 * FastMath.log(2 * FastMath.PI); - this.solverAbsoluteAccuracy = inverseCumAccuracy; - } - - /** - * Returns the scale parameter of this distribution. - * - * @return the scale parameter - */ - public double getScale() { - return scale; - } - - /** - * Returns the shape parameter of this distribution. - * - * @return the shape parameter - */ - public double getShape() { - return shape; - } - - /** - * {@inheritDoc} - * - * For scale {@code m}, and shape {@code s} of this distribution, the PDF - * is given by - * <ul> - * <li>{@code 0} if {@code x <= 0},</li> - * <li>{@code exp(-0.5 * ((ln(x) - m) / s)^2) / (s * sqrt(2 * pi) * x)} - * otherwise.</li> - * </ul> - */ - @Override - public double density(double x) { - if (x <= 0) { - return 0; - } - final double x0 = FastMath.log(x) - scale; - final double x1 = x0 / shape; - return FastMath.exp(-0.5 * x1 * x1) / (shape * SQRT2PI * x); - } - - /** {@inheritDoc} - * - * See documentation of {@link #density(double)} for computation details. - */ - @Override - public double logDensity(double x) { - if (x <= 0) { - return Double.NEGATIVE_INFINITY; - } - final double logX = FastMath.log(x); - final double x0 = logX - scale; - final double x1 = x0 / shape; - return -0.5 * x1 * x1 - (logShapePlusHalfLog2Pi + logX); - } - - /** - * {@inheritDoc} - * - * For scale {@code m}, and shape {@code s} of this distribution, the CDF - * is given by - * <ul> - * <li>{@code 0} if {@code x <= 0},</li> - * <li>{@code 0} if {@code ln(x) - m < 0} and {@code m - ln(x) > 40 * s}, as - * in these cases the actual value is within {@code Double.MIN_VALUE} of 0, - * <li>{@code 1} if {@code ln(x) - m >= 0} and {@code ln(x) - m > 40 * s}, - * as in these cases the actual value is within {@code Double.MIN_VALUE} of - * 1,</li> - * <li>{@code 0.5 + 0.5 * erf((ln(x) - m) / (s * sqrt(2))} otherwise.</li> - * </ul> - */ - @Override - public double cumulativeProbability(double x) { - if (x <= 0) { - return 0; - } - final double dev = FastMath.log(x) - scale; - if (FastMath.abs(dev) > 40 * shape) { - return dev < 0 ? 0.0d : 1.0d; - } - return 0.5 + 0.5 * Erf.value(dev / (shape * SQRT2)); - } - - /** {@inheritDoc} */ - @Override - public double probability(double x0, - double x1) - throws NumberIsTooLargeException { - if (x0 > x1) { - throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, - x0, x1, true); - } - if (x0 <= 0 || x1 <= 0) { - return super.probability(x0, x1); - } - final double denom = shape * SQRT2; - final double v0 = (FastMath.log(x0) - scale) / denom; - final double v1 = (FastMath.log(x1) - scale) / denom; - return 0.5 * ErfDifference.value(v0, v1); - } - - /** {@inheritDoc} */ - @Override - protected double getSolverAbsoluteAccuracy() { - return solverAbsoluteAccuracy; - } - - /** - * {@inheritDoc} - * - * For scale {@code m} and shape {@code s}, the mean is - * {@code exp(m + s^2 / 2)}. - */ - @Override - public double getNumericalMean() { - double s = shape; - return FastMath.exp(scale + (s * s / 2)); - } - - /** - * {@inheritDoc} - * - * For scale {@code m} and shape {@code s}, the variance is - * {@code (exp(s^2) - 1) * exp(2 * m + s^2)}. - */ - @Override - public double getNumericalVariance() { - final double s = shape; - final double ss = s * s; - return (FastMath.expm1(ss)) * FastMath.exp(2 * scale + ss); - } - - /** - * {@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 - * {@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; - } - - /** {@inheritDoc} */ - @Override - public RealDistribution.Sampler createSampler(final UniformRandomProvider rng) { - return new RealDistribution.Sampler() { - /** - * Log normal distribution sampler. - */ - private final ContinuousSampler sampler = - new LogNormalSampler(new ZigguratNormalizedGaussianSampler(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/LogisticDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/LogisticDistribution.java b/src/main/java/org/apache/commons/math4/distribution/LogisticDistribution.java deleted file mode 100644 index 1c69804..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/LogisticDistribution.java +++ /dev/null @@ -1,136 +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 Logistic distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Logistic_distribution">Logistic Distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/LogisticDistribution.html">Logistic Distribution (Mathworld)</a> - * - * @since 3.4 - */ -public class LogisticDistribution extends AbstractRealDistribution { - - /** Serializable version identifier. */ - private static final long serialVersionUID = 20160311L; - - /** The location parameter. */ - private final double mu; - /** The scale parameter. */ - private final double s; - - /** - * Creates a distribution. - * - * @param mu location parameter - * @param s scale parameter (must be positive) - * @throws NotStrictlyPositiveException if {@code beta <= 0} - */ - public LogisticDistribution(double mu, - double s) { - if (s <= 0.0) { - throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_SCALE, s); - } - - this.mu = mu; - this.s = s; - } - - /** - * Access the location parameter, {@code mu}. - * - * @return the location parameter. - */ - public double getLocation() { - return mu; - } - - /** - * Access the scale parameter, {@code s}. - * - * @return the scale parameter. - */ - public double getScale() { - return s; - } - - /** {@inheritDoc} */ - @Override - public double density(double x) { - double z = (x - mu) / s; - double v = FastMath.exp(-z); - return 1 / s * v / ((1.0 + v) * (1.0 + v)); - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(double x) { - double z = 1 / s * (x - mu); - return 1.0 / (1.0 + 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 0.0; - } else if (p == 1) { - return Double.POSITIVE_INFINITY; - } - return s * Math.log(p / (1.0 - p)) + mu; - } - - /** {@inheritDoc} */ - @Override - public double getNumericalMean() { - return mu; - } - - /** {@inheritDoc} */ - @Override - public double getNumericalVariance() { - return (MathUtils.PI_SQUARED / 3.0) * (1.0 / (s * s)); - } - - /** {@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; - } - -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/NakagamiDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/NakagamiDistribution.java b/src/main/java/org/apache/commons/math4/distribution/NakagamiDistribution.java deleted file mode 100644 index c26b2e6..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/NakagamiDistribution.java +++ /dev/null @@ -1,157 +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.NumberIsTooSmallException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.numbers.gamma.Gamma; -import org.apache.commons.numbers.gamma.RegularizedGamma; -import org.apache.commons.math4.util.FastMath; - -/** - * This class implements the Nakagami distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Nakagami_distribution">Nakagami Distribution (Wikipedia)</a> - * - * @since 3.4 - */ -public class NakagamiDistribution extends AbstractRealDistribution { - - /** Default inverse cumulative probability accuracy. */ - public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; - - /** Serializable version identifier. */ - private static final long serialVersionUID = 20160311L; - - /** The shape parameter. */ - private final double mu; - /** The scale parameter. */ - private final double omega; - /** Inverse cumulative probability accuracy. */ - private final double inverseAbsoluteAccuracy; - - /** - * Creates a distribution. - * - * @param mu shape parameter - * @param omega scale parameter (must be positive) - * @throws NumberIsTooSmallException if {@code mu < 0.5} - * @throws NotStrictlyPositiveException if {@code omega <= 0} - */ - public NakagamiDistribution(double mu, - double omega) { - this(mu, omega, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a distribution. - * - * @param mu shape parameter - * @param omega scale parameter (must be positive) - * @param inverseAbsoluteAccuracy the maximum absolute error in inverse - * cumulative probability estimates (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NumberIsTooSmallException if {@code mu < 0.5} - * @throws NotStrictlyPositiveException if {@code omega <= 0} - */ - public NakagamiDistribution(double mu, - double omega, - double inverseAbsoluteAccuracy) { - if (mu < 0.5) { - throw new NumberIsTooSmallException(mu, 0.5, true); - } - if (omega <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_SCALE, omega); - } - - this.mu = mu; - this.omega = omega; - this.inverseAbsoluteAccuracy = inverseAbsoluteAccuracy; - } - - /** - * Access the shape parameter, {@code mu}. - * - * @return the shape parameter. - */ - public double getShape() { - return mu; - } - - /** - * Access the scale parameter, {@code omega}. - * - * @return the scale parameter. - */ - public double getScale() { - return omega; - } - - /** {@inheritDoc} */ - @Override - protected double getSolverAbsoluteAccuracy() { - return inverseAbsoluteAccuracy; - } - - /** {@inheritDoc} */ - @Override - public double density(double x) { - if (x <= 0) { - return 0.0; - } - return 2.0 * FastMath.pow(mu, mu) / (Gamma.value(mu) * FastMath.pow(omega, mu)) * - FastMath.pow(x, 2 * mu - 1) * FastMath.exp(-mu * x * x / omega); - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(double x) { - return RegularizedGamma.P.value(mu, mu * x * x / omega); - } - - /** {@inheritDoc} */ - @Override - public double getNumericalMean() { - return Gamma.value(mu + 0.5) / Gamma.value(mu) * FastMath.sqrt(omega / mu); - } - - /** {@inheritDoc} */ - @Override - public double getNumericalVariance() { - double v = Gamma.value(mu + 0.5) / Gamma.value(mu); - return omega * (1 - 1 / mu * v * v); - } - - /** {@inheritDoc} */ - @Override - public double getSupportLowerBound() { - return 0; - } - - /** {@inheritDoc} */ - @Override - public double getSupportUpperBound() { - return Double.POSITIVE_INFINITY; - } - - /** {@inheritDoc} */ - @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/NormalDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/NormalDistribution.java b/src/main/java/org/apache/commons/math4/distribution/NormalDistribution.java deleted file mode 100644 index 76c41a3..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/NormalDistribution.java +++ /dev/null @@ -1,261 +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.NumberIsTooLargeException; -import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.numbers.gamma.Erfc; -import org.apache.commons.numbers.gamma.InverseErf; -import org.apache.commons.numbers.gamma.ErfDifference; -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.GaussianSampler; -import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler; - -/** - * Implementation of the normal (gaussian) distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Normal_distribution">Normal distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/NormalDistribution.html">Normal distribution (MathWorld)</a> - */ -public class NormalDistribution 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 = 8589540077390120676L; - /** √(2) */ - private static final double SQRT2 = FastMath.sqrt(2.0); - /** Mean of this distribution. */ - private final double mean; - /** Standard deviation of this distribution. */ - private final double standardDeviation; - /** The value of {@code log(sd) + 0.5*log(2*pi)} stored for faster computation. */ - private final double logStandardDeviationPlusHalfLog2Pi; - /** Inverse cumulative probability accuracy. */ - private final double solverAbsoluteAccuracy; - - /** - * Create a normal distribution with mean equal to zero and standard - * deviation equal to one. - */ - public NormalDistribution() { - this(0, 1); - } - - /** - * Creates a distribution. - * - * @param mean Mean for this distribution. - * @param sd Standard deviation for this distribution. - * @throws NotStrictlyPositiveException if {@code sd <= 0}. - */ - public NormalDistribution(double mean, - double sd) - throws NotStrictlyPositiveException { - this(mean, sd, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - - /** - * Creates a distribution. - * - * @param mean Mean for this distribution. - * @param sd Standard deviation for this distribution. - * @param inverseCumAccuracy Inverse cumulative probability accuracy. - * @throws NotStrictlyPositiveException if {@code sd <= 0}. - */ - public NormalDistribution(double mean, - double sd, - double inverseCumAccuracy) - throws NotStrictlyPositiveException { - if (sd <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sd); - } - - this.mean = mean; - standardDeviation = sd; - logStandardDeviationPlusHalfLog2Pi = FastMath.log(sd) + 0.5 * FastMath.log(2 * FastMath.PI); - solverAbsoluteAccuracy = inverseCumAccuracy; - } - - /** - * Access the mean. - * - * @return the mean for this distribution. - */ - public double getMean() { - return mean; - } - - /** - * Access the standard deviation. - * - * @return the standard deviation for this distribution. - */ - public double getStandardDeviation() { - return standardDeviation; - } - - /** {@inheritDoc} */ - @Override - public double density(double x) { - return FastMath.exp(logDensity(x)); - } - - /** {@inheritDoc} */ - @Override - public double logDensity(double x) { - final double x0 = x - mean; - final double x1 = x0 / standardDeviation; - return -0.5 * x1 * x1 - logStandardDeviationPlusHalfLog2Pi; - } - - /** - * {@inheritDoc} - * - * If {@code x} is more than 40 standard deviations from the mean, 0 or 1 - * is returned, as in these cases the actual value is within - * {@code Double.MIN_VALUE} of 0 or 1. - */ - @Override - public double cumulativeProbability(double x) { - final double dev = x - mean; - if (FastMath.abs(dev) > 40 * standardDeviation) { - return dev < 0 ? 0.0d : 1.0d; - } - return 0.5 * Erfc.value(-dev / (standardDeviation * SQRT2)); - } - - /** {@inheritDoc} - * @since 3.2 - */ - @Override - public double inverseCumulativeProbability(final double p) throws OutOfRangeException { - if (p < 0.0 || p > 1.0) { - throw new OutOfRangeException(p, 0, 1); - } - return mean + standardDeviation * SQRT2 * InverseErf.value(2 * p - 1); - } - - /** {@inheritDoc} */ - @Override - public double probability(double x0, - double x1) - throws NumberIsTooLargeException { - if (x0 > x1) { - throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, - x0, x1, true); - } - final double denom = standardDeviation * SQRT2; - final double v0 = (x0 - mean) / denom; - final double v1 = (x1 - mean) / denom; - return 0.5 * ErfDifference.value(v0, v1); - } - - /** {@inheritDoc} */ - @Override - protected double getSolverAbsoluteAccuracy() { - return solverAbsoluteAccuracy; - } - - /** - * {@inheritDoc} - * - * For mean parameter {@code mu}, the mean is {@code mu}. - */ - @Override - public double getNumericalMean() { - return getMean(); - } - - /** - * {@inheritDoc} - * - * For standard deviation parameter {@code s}, the variance is {@code s^2}. - */ - @Override - public double getNumericalVariance() { - final double s = getStandardDeviation(); - return s * s; - } - - /** - * {@inheritDoc} - * - * The lower bound of the support is always negative infinity - * no matter the parameters. - * - * @return lower bound of the support (always - * {@code 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 - * {@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; - } - - /** {@inheritDoc} */ - @Override - public RealDistribution.Sampler createSampler(final UniformRandomProvider rng) { - return new RealDistribution.Sampler() { - /** - * Gaussian distribution sampler. - */ - private final ContinuousSampler sampler = - new GaussianSampler(new MarsagliaNormalizedGaussianSampler(rng), - mean, standardDeviation); - - /**{@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/ParetoDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/ParetoDistribution.java b/src/main/java/org/apache/commons/math4/distribution/ParetoDistribution.java deleted file mode 100644 index e2b350e..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/ParetoDistribution.java +++ /dev/null @@ -1,269 +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.math4.util.FastMath; -import org.apache.commons.rng.UniformRandomProvider; -import org.apache.commons.rng.sampling.distribution.ContinuousSampler; -import org.apache.commons.rng.sampling.distribution.InverseTransformParetoSampler; - -/** - * Implementation of the Pareto distribution. - * - * <p> - * <strong>Parameters:</strong> - * The probability distribution function of {@code X} is given by (for {@code x >= k}): - * <pre> - * α * k^α / x^(α + 1) - * </pre> - * <ul> - * <li>{@code k} is the <em>scale</em> parameter: this is the minimum possible value of {@code X},</li> - * <li>{@code α} is the <em>shape</em> parameter: this is the Pareto index</li> - * </ul> - * - * @see <a href="http://en.wikipedia.org/wiki/Pareto_distribution"> - * Pareto distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/ParetoDistribution.html"> - * Pareto distribution (MathWorld)</a> - * - * @since 3.3 - */ -public class ParetoDistribution extends AbstractRealDistribution { - - /** Default inverse cumulative probability accuracy. */ - public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; - - /** Serializable version identifier. */ - private static final long serialVersionUID = 20160311L; - - /** The scale parameter of this distribution. */ - private final double scale; - - /** The shape parameter of this distribution. */ - private final double shape; - - /** Inverse cumulative probability accuracy. */ - private final double solverAbsoluteAccuracy; - - /** - * Creates a Pareto distribution with a scale of {@code 1} and a shape of {@code 1}. - */ - public ParetoDistribution() { - this(1, 1); - } - - /** - * Creates a Pareto distribution. - * - * @param scale the scale parameter of this distribution - * @param shape the shape parameter of this distribution - * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}. - */ - public ParetoDistribution(double scale, - double shape) - throws NotStrictlyPositiveException { - this(scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a Pareto distribution. - * - * @param scale Scale parameter of this distribution. - * @param shape Shape parameter of this distribution. - * @param inverseCumAccuracy Inverse cumulative probability accuracy. - * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}. - */ - public ParetoDistribution(double scale, - double shape, - double inverseCumAccuracy) - throws NotStrictlyPositiveException { - if (scale <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, scale); - } - - if (shape <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, shape); - } - - this.scale = scale; - this.shape = shape; - this.solverAbsoluteAccuracy = inverseCumAccuracy; - } - - /** - * Returns the scale parameter of this distribution. - * - * @return the scale parameter - */ - public double getScale() { - return scale; - } - - /** - * Returns the shape parameter of this distribution. - * - * @return the shape parameter - */ - public double getShape() { - return shape; - } - - /** - * {@inheritDoc} - * <p> - * For scale {@code k}, and shape {@code α} of this distribution, the PDF - * is given by - * <ul> - * <li>{@code 0} if {@code x < k},</li> - * <li>{@code α * k^α / x^(α + 1)} otherwise.</li> - * </ul> - */ - @Override - public double density(double x) { - if (x < scale) { - return 0; - } - return FastMath.pow(scale, shape) / FastMath.pow(x, shape + 1) * shape; - } - - /** {@inheritDoc} - * - * See documentation of {@link #density(double)} for computation details. - */ - @Override - public double logDensity(double x) { - if (x < scale) { - return Double.NEGATIVE_INFINITY; - } - return FastMath.log(scale) * shape - FastMath.log(x) * (shape + 1) + FastMath.log(shape); - } - - /** - * {@inheritDoc} - * <p> - * For scale {@code k}, and shape {@code α} of this distribution, the CDF is given by - * <ul> - * <li>{@code 0} if {@code x < k},</li> - * <li>{@code 1 - (k / x)^α} otherwise.</li> - * </ul> - */ - @Override - public double cumulativeProbability(double x) { - if (x <= scale) { - return 0; - } - return 1 - FastMath.pow(scale / x, shape); - } - - /** {@inheritDoc} */ - @Override - protected double getSolverAbsoluteAccuracy() { - return solverAbsoluteAccuracy; - } - - /** - * {@inheritDoc} - * <p> - * For scale {@code k} and shape {@code α}, the mean is given by - * <ul> - * <li>{@code â} if {@code α <= 1},</li> - * <li>{@code α * k / (α - 1)} otherwise.</li> - * </ul> - */ - @Override - public double getNumericalMean() { - if (shape <= 1) { - return Double.POSITIVE_INFINITY; - } - return shape * scale / (shape - 1); - } - - /** - * {@inheritDoc} - * <p> - * For scale {@code k} and shape {@code α}, the variance is given by - * <ul> - * <li>{@code â} if {@code 1 < α <= 2},</li> - * <li>{@code k^2 * α / ((α - 1)^2 * (α - 2))} otherwise.</li> - * </ul> - */ - @Override - public double getNumericalVariance() { - if (shape <= 2) { - return Double.POSITIVE_INFINITY; - } - double s = shape - 1; - return scale * scale * shape / (s * s) / (shape - 2); - } - - /** - * {@inheritDoc} - * <p> - * The lower bound of the support is equal to the scale parameter {@code k}. - * - * @return lower bound of the support - */ - @Override - public double getSupportLowerBound() { - return scale; - } - - /** - * {@inheritDoc} - * <p> - * The upper bound of the support is always positive infinity no matter the parameters. - * - * @return upper bound of the support (always {@code Double.POSITIVE_INFINITY}) - */ - @Override - public double getSupportUpperBound() { - return Double.POSITIVE_INFINITY; - } - - /** - * {@inheritDoc} - * <p> - * The support of this distribution is connected. - * - * @return {@code true} - */ - @Override - public boolean isSupportConnected() { - return true; - } - - /** {@inheritDoc} */ - @Override - public RealDistribution.Sampler createSampler(final UniformRandomProvider rng) { - return new RealDistribution.Sampler() { - /** - * Pareto distribution sampler. - */ - private final ContinuousSampler sampler = - new InverseTransformParetoSampler(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/PascalDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/PascalDistribution.java b/src/main/java/org/apache/commons/math4/distribution/PascalDistribution.java deleted file mode 100644 index 9a693d1..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/PascalDistribution.java +++ /dev/null @@ -1,228 +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.numbers.combinatorics.BinomialCoefficientDouble; -import org.apache.commons.numbers.combinatorics.LogBinomialCoefficient; -import org.apache.commons.numbers.gamma.RegularizedBeta; -import org.apache.commons.math4.util.FastMath; - -/** - * <p> - * Implementation of the Pascal distribution. The Pascal distribution is a - * special case of the Negative Binomial distribution where the number of - * successes parameter is an integer. - * </p> - * <p> - * There are various ways to express the probability mass and distribution - * functions for the Pascal distribution. The present implementation represents - * the distribution of the number of failures before {@code r} successes occur. - * This is the convention adopted in e.g. - * <a href="http://mathworld.wolfram.com/NegativeBinomialDistribution.html">MathWorld</a>, - * but <em>not</em> in - * <a href="http://en.wikipedia.org/wiki/Negative_binomial_distribution">Wikipedia</a>. - * </p> - * <p> - * For a random variable {@code X} whose values are distributed according to this - * distribution, the probability mass function is given by<br> - * {@code P(X = k) = C(k + r - 1, r - 1) * p^r * (1 - p)^k,}<br> - * where {@code r} is the number of successes, {@code p} is the probability of - * success, and {@code X} is the total number of failures. {@code C(n, k)} is - * the binomial coefficient ({@code n} choose {@code k}). The mean and variance - * of {@code X} are<br> - * {@code E(X) = (1 - p) * r / p, var(X) = (1 - p) * r / p^2.}<br> - * Finally, the cumulative distribution function is given by<br> - * {@code P(X <= k) = I(p, r, k + 1)}, - * where I is the regularized incomplete Beta function. - * </p> - * - * @see <a href="http://en.wikipedia.org/wiki/Negative_binomial_distribution"> - * Negative binomial distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/NegativeBinomialDistribution.html"> - * Negative binomial distribution (MathWorld)</a> - * @since 1.2 (changed to concrete class in 3.0) - */ -public class PascalDistribution extends AbstractIntegerDistribution { - /** Serializable version identifier. */ - private static final long serialVersionUID = 6751309484392813623L; - /** The number of successes. */ - private final int numberOfSuccesses; - /** The probability of success. */ - private final double probabilityOfSuccess; - /** The value of {@code log(p)}, where {@code p} is the probability of success, - * stored for faster computation. */ - private final double logProbabilityOfSuccess; - /** The value of {@code log(1-p)}, where {@code p} is the probability of success, - * stored for faster computation. */ - private final double log1mProbabilityOfSuccess; - - /** - * Create a Pascal distribution with the given number of successes and - * probability of success. - * - * @param r Number of successes. - * @param p Probability of success. - * @throws NotStrictlyPositiveException if the number of successes is not positive - * @throws OutOfRangeException if the probability of success is not in the - * range {@code [0, 1]}. - */ - public PascalDistribution(int r, - double p) - throws NotStrictlyPositiveException, - OutOfRangeException { - if (r <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES, - r); - } - if (p < 0 || p > 1) { - throw new OutOfRangeException(p, 0, 1); - } - - numberOfSuccesses = r; - probabilityOfSuccess = p; - logProbabilityOfSuccess = FastMath.log(p); - log1mProbabilityOfSuccess = FastMath.log1p(-p); - } - - /** - * Access the number of successes for this distribution. - * - * @return the number of successes. - */ - public int getNumberOfSuccesses() { - return numberOfSuccesses; - } - - /** - * 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) { - double ret; - if (x < 0) { - ret = 0.0; - } else { - ret = BinomialCoefficientDouble.value(x + - numberOfSuccesses - 1, numberOfSuccesses - 1) * - FastMath.pow(probabilityOfSuccess, numberOfSuccesses) * - FastMath.pow(1.0 - probabilityOfSuccess, x); - } - return ret; - } - - /** {@inheritDoc} */ - @Override - public double logProbability(int x) { - double ret; - if (x < 0) { - ret = Double.NEGATIVE_INFINITY; - } else { - ret = LogBinomialCoefficient.value(x + - numberOfSuccesses - 1, numberOfSuccesses - 1) + - logProbabilityOfSuccess * numberOfSuccesses + - log1mProbabilityOfSuccess * x; - } - return ret; - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(int x) { - double ret; - if (x < 0) { - ret = 0.0; - } else { - ret = RegularizedBeta.value(probabilityOfSuccess, - numberOfSuccesses, x + 1.0); - } - return ret; - } - - /** - * {@inheritDoc} - * - * For number of successes {@code r} and probability of success {@code p}, - * the mean is {@code r * (1 - p) / p}. - */ - @Override - public double getNumericalMean() { - final double p = getProbabilityOfSuccess(); - final double r = getNumberOfSuccesses(); - return (r * (1 - p)) / p; - } - - /** - * {@inheritDoc} - * - * For number of successes {@code r} and probability of success {@code p}, - * the variance is {@code r * (1 - p) / p^2}. - */ - @Override - public double getNumericalVariance() { - final double p = getProbabilityOfSuccess(); - final double r = getNumberOfSuccesses(); - return r * (1 - p) / (p * p); - } - - /** - * {@inheritDoc} - * - * The lower bound of the support is always 0 no matter the parameters. - * - * @return lower bound of the support (always 0) - */ - @Override - public int getSupportLowerBound() { - return 0; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is always positive infinity no matter the - * parameters. Positive infinity is symbolized by {@code Integer.MAX_VALUE}. - * - * @return upper bound of the support (always {@code Integer.MAX_VALUE} - * for positive infinity) - */ - @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; - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/PoissonDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/PoissonDistribution.java b/src/main/java/org/apache/commons/math4/distribution/PoissonDistribution.java deleted file mode 100644 index 87cd7ce..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/PoissonDistribution.java +++ /dev/null @@ -1,259 +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.RegularizedGamma; -import org.apache.commons.math4.util.FastMath; -import org.apache.commons.math4.util.MathUtils; -import org.apache.commons.rng.UniformRandomProvider; -import org.apache.commons.rng.sampling.distribution.DiscreteSampler; -import org.apache.commons.rng.sampling.distribution.PoissonSampler; - -/** - * Implementation of the Poisson distribution. - * - * @see <a href="http://en.wikipedia.org/wiki/Poisson_distribution">Poisson distribution (Wikipedia)</a> - * @see <a href="http://mathworld.wolfram.com/PoissonDistribution.html">Poisson distribution (MathWorld)</a> - */ -public class PoissonDistribution extends AbstractIntegerDistribution { - /** - * Default maximum number of iterations for cumulative probability calculations. - * @since 2.1 - */ - private static final int DEFAULT_MAX_ITERATIONS = 10000000; - /** - * Default convergence criterion. - * @since 2.1 - */ - private static final double DEFAULT_EPSILON = 1e-12; - /** Serializable version identifier. */ - private static final long serialVersionUID = -3349935121172596109L; - /** Distribution used to compute normal approximation. */ - private final NormalDistribution normal; - /** Mean of the distribution. */ - private final double mean; - - /** Maximum number of iterations for cumulative probability. */ - private final int maxIterations; - - /** Convergence criterion for cumulative probability. */ - private final double epsilon; - - /** - * Creates a new Poisson distribution with specified mean. - * - * @param p the Poisson mean - * @throws NotStrictlyPositiveException if {@code p <= 0}. - */ - public PoissonDistribution(double p) - throws NotStrictlyPositiveException { - this(p, DEFAULT_EPSILON, DEFAULT_MAX_ITERATIONS); - } - - /** - * Creates a new Poisson distribution with specified mean, convergence - * criterion and maximum number of iterations. - * - * @param p Poisson mean. - * @param epsilon Convergence criterion for cumulative probabilities. - * @param maxIterations the maximum number of iterations for cumulative - * probabilities. - * @throws NotStrictlyPositiveException if {@code p <= 0}. - * @since 2.1 - */ - public PoissonDistribution(double p, - double epsilon, - int maxIterations) - throws NotStrictlyPositiveException { - if (p <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, p); - } - mean = p; - this.epsilon = epsilon; - this.maxIterations = maxIterations; - - normal = new NormalDistribution(p, FastMath.sqrt(p), - NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a new Poisson distribution with the specified mean and - * convergence criterion. - * - * @param p Poisson mean. - * @param epsilon Convergence criterion for cumulative probabilities. - * @throws NotStrictlyPositiveException if {@code p <= 0}. - * @since 2.1 - */ - public PoissonDistribution(double p, double epsilon) - throws NotStrictlyPositiveException { - this(p, epsilon, DEFAULT_MAX_ITERATIONS); - } - - /** - * Creates a new Poisson distribution with the specified mean and maximum - * number of iterations. - * - * @param p Poisson mean. - * @param maxIterations Maximum number of iterations for cumulative - * probabilities. - * @since 2.1 - */ - public PoissonDistribution(double p, int maxIterations) { - this(p, DEFAULT_EPSILON, maxIterations); - } - - /** - * Get the mean for the distribution. - * - * @return the mean for the distribution. - */ - public double getMean() { - return mean; - } - - /** {@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) { - double ret; - if (x < 0 || x == Integer.MAX_VALUE) { - ret = Double.NEGATIVE_INFINITY; - } else if (x == 0) { - ret = -mean; - } else { - ret = -SaddlePointExpansion.getStirlingError(x) - - SaddlePointExpansion.getDeviancePart(x, mean) - - 0.5 * FastMath.log(MathUtils.TWO_PI) - 0.5 * FastMath.log(x); - } - return ret; - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(int x) { - if (x < 0) { - return 0; - } - if (x == Integer.MAX_VALUE) { - return 1; - } - return RegularizedGamma.Q.value((double) x + 1, mean, epsilon, - maxIterations); - } - - /** - * Calculates the Poisson distribution function using a normal - * approximation. The {@code N(mean, sqrt(mean))} distribution is used - * to approximate the Poisson distribution. The computation uses - * "half-correction" (evaluating the normal distribution function at - * {@code x + 0.5}). - * - * @param x Upper bound, inclusive. - * @return the distribution function value calculated using a normal - * approximation. - */ - public double normalApproximateProbability(int x) { - // calculate the probability using half-correction - return normal.cumulativeProbability(x + 0.5); - } - - /** - * {@inheritDoc} - * - * For mean parameter {@code p}, the mean is {@code p}. - */ - @Override - public double getNumericalMean() { - return getMean(); - } - - /** - * {@inheritDoc} - * - * For mean parameter {@code p}, the variance is {@code p}. - */ - @Override - public double getNumericalVariance() { - return getMean(); - } - - /** - * {@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 int getSupportLowerBound() { - return 0; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is positive infinity, - * regardless of the parameter values. There is no integer infinity, - * so this method returns {@code Integer.MAX_VALUE}. - * - * @return upper bound of the support (always {@code Integer.MAX_VALUE} for - * positive infinity) - */ - @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 IntegerDistribution.Sampler createSampler(final UniformRandomProvider rng) { - return new IntegerDistribution.Sampler() { - /** - * Poisson distribution sampler. - */ - private final DiscreteSampler sampler = - new PoissonSampler(rng, mean); - - /**{@inheritDoc} */ - @Override - public int sample() { - return sampler.sample(); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/ef846813/src/main/java/org/apache/commons/math4/distribution/TDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/distribution/TDistribution.java b/src/main/java/org/apache/commons/math4/distribution/TDistribution.java deleted file mode 100644 index 194ce94..0000000 --- a/src/main/java/org/apache/commons/math4/distribution/TDistribution.java +++ /dev/null @@ -1,218 +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.RegularizedBeta; -import org.apache.commons.numbers.gamma.LogGamma; -import org.apache.commons.math4.util.FastMath; - -/** - * Implementation of Student's t-distribution. - * - * @see "<a href='http://en.wikipedia.org/wiki/Student's_t-distribution'>Student's t-distribution (Wikipedia)</a>" - * @see "<a href='http://mathworld.wolfram.com/Studentst-Distribution.html'>Student's t-distribution (MathWorld)</a>" - */ -public class TDistribution 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 degrees of freedom. */ - private final double degreesOfFreedom; - /** Inverse cumulative probability accuracy. */ - private final double solverAbsoluteAccuracy; - /** Static computation factor based on degreesOfFreedom. */ - private final double factor; - - /** - * Creates a distribution. - * - * @param degreesOfFreedom Degrees of freedom. - * @throws NotStrictlyPositiveException if {@code degreesOfFreedom <= 0} - */ - public TDistribution(double degreesOfFreedom) - throws NotStrictlyPositiveException { - this(degreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a distribution. - * - * @param degreesOfFreedom Degrees of freedom. - * @param inverseCumAccuracy the maximum absolute error in inverse - * cumulative probability estimates - * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NotStrictlyPositiveException if {@code degreesOfFreedom <= 0} - */ - public TDistribution(double degreesOfFreedom, - double inverseCumAccuracy) - throws NotStrictlyPositiveException { - if (degreesOfFreedom <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.DEGREES_OF_FREEDOM, - degreesOfFreedom); - } - this.degreesOfFreedom = degreesOfFreedom; - solverAbsoluteAccuracy = inverseCumAccuracy; - - final double n = degreesOfFreedom; - final double nPlus1Over2 = (n + 1) / 2; - factor = LogGamma.value(nPlus1Over2) - - 0.5 * (FastMath.log(FastMath.PI) + FastMath.log(n)) - - LogGamma.value(n / 2); - } - - /** - * Access the degrees of freedom. - * - * @return the degrees of freedom. - */ - public double getDegreesOfFreedom() { - return degreesOfFreedom; - } - - /** {@inheritDoc} */ - @Override - public double density(double x) { - return FastMath.exp(logDensity(x)); - } - - /** {@inheritDoc} */ - @Override - public double logDensity(double x) { - final double n = degreesOfFreedom; - final double nPlus1Over2 = (n + 1) / 2; - return factor - nPlus1Over2 * FastMath.log(1 + x * x / n); - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(double x) { - double ret; - if (x == 0) { - ret = 0.5; - } else { - double t = - RegularizedBeta.value(degreesOfFreedom / (degreesOfFreedom + (x * x)), - 0.5 * degreesOfFreedom, - 0.5); - if (x < 0.0) { - ret = 0.5 * t; - } else { - ret = 1.0 - 0.5 * t; - } - } - - return ret; - } - - /** {@inheritDoc} */ - @Override - protected double getSolverAbsoluteAccuracy() { - return solverAbsoluteAccuracy; - } - - /** - * {@inheritDoc} - * - * For degrees of freedom parameter {@code df}, the mean is - * <ul> - * <li>if {@code df > 1} then {@code 0},</li> - * <li>else undefined ({@code Double.NaN}).</li> - * </ul> - */ - @Override - public double getNumericalMean() { - final double df = getDegreesOfFreedom(); - - if (df > 1) { - return 0; - } - - return Double.NaN; - } - - /** - * {@inheritDoc} - * - * For degrees of freedom parameter {@code df}, the variance is - * <ul> - * <li>if {@code df > 2} then {@code df / (df - 2)},</li> - * <li>if {@code 1 < df <= 2} then positive infinity - * ({@code Double.POSITIVE_INFINITY}),</li> - * <li>else undefined ({@code Double.NaN}).</li> - * </ul> - */ - @Override - public double getNumericalVariance() { - final double df = getDegreesOfFreedom(); - - if (df > 2) { - return df / (df - 2); - } - - if (df > 1 && df <= 2) { - return Double.POSITIVE_INFINITY; - } - - 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 - * {@code 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 - * {@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; - } -}