You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2015/02/16 23:40:02 UTC
[32/82] [partial] [math] Update for next development iteration:
commons-math4
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/distribution/LaplaceDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/distribution/LaplaceDistribution.java b/src/main/java/org/apache/commons/math3/distribution/LaplaceDistribution.java
deleted file mode 100644
index 0514bff..0000000
--- a/src/main/java/org/apache/commons/math3/distribution/LaplaceDistribution.java
+++ /dev/null
@@ -1,160 +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.math3.distribution;
-
-import org.apache.commons.math3.exception.NotStrictlyPositiveException;
-import org.apache.commons.math3.exception.OutOfRangeException;
-import org.apache.commons.math3.exception.util.LocalizedFormats;
-import org.apache.commons.math3.random.RandomGenerator;
-import org.apache.commons.math3.random.Well19937c;
-import org.apache.commons.math3.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 = 20141003;
-
- /** The location parameter. */
- private final double mu;
- /** The scale parameter. */
- private final double beta;
-
- /**
- * Build a new instance.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @param mu location parameter
- * @param beta scale parameter (must be positive)
- * @throws NotStrictlyPositiveException if {@code beta <= 0}
- */
- public LaplaceDistribution(double mu, double beta) {
- this(new Well19937c(), mu, beta);
- }
-
- /**
- * Build a new instance.
- *
- * @param rng Random number generator
- * @param mu location parameter
- * @param beta scale parameter (must be positive)
- * @throws NotStrictlyPositiveException if {@code beta <= 0}
- */
- public LaplaceDistribution(RandomGenerator rng, double mu, double beta) {
- super(rng);
-
- 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} */
- public double density(double x) {
- return FastMath.exp(-FastMath.abs(x - mu) / beta) / (2.0 * beta);
- }
-
- /** {@inheritDoc} */
- 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;
- }
- }
-
- @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} */
- public double getNumericalMean() {
- return mu;
- }
-
- /** {@inheritDoc} */
- public double getNumericalVariance() {
- return 2.0 * beta * beta;
- }
-
- /** {@inheritDoc} */
- public double getSupportLowerBound() {
- return Double.NEGATIVE_INFINITY;
- }
-
- /** {@inheritDoc} */
- public double getSupportUpperBound() {
- return Double.POSITIVE_INFINITY;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportLowerBoundInclusive() {
- return false;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportUpperBoundInclusive() {
- return false;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportConnected() {
- return true;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/distribution/LevyDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/distribution/LevyDistribution.java b/src/main/java/org/apache/commons/math3/distribution/LevyDistribution.java
deleted file mode 100644
index 4580e50..0000000
--- a/src/main/java/org/apache/commons/math3/distribution/LevyDistribution.java
+++ /dev/null
@@ -1,192 +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.math3.distribution;
-
-import org.apache.commons.math3.exception.OutOfRangeException;
-import org.apache.commons.math3.random.RandomGenerator;
-import org.apache.commons.math3.random.Well19937c;
-import org.apache.commons.math3.special.Erf;
-import org.apache.commons.math3.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 = 20130314L;
-
- /** 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;
-
- /**
- * Build a new instance.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @param mu location parameter
- * @param c scale parameter
- * @since 3.4
- */
- public LevyDistribution(final double mu, final double c) {
- this(new Well19937c(), mu, c);
- }
-
- /**
- * Creates a LevyDistribution.
- * @param rng random generator to be used for sampling
- * @param mu location
- * @param c scale parameter
- */
- public LevyDistribution(final RandomGenerator rng, final double mu, final double c) {
- super(rng);
- 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>
- * <pre>
- * f(x; μ, c) = √(c / 2π) * e<sup>-c / 2 (x - μ)</sup> / (x - μ)<sup>3/2</sup>
- * </pre>
- * <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>
- */
- 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>
- */
- public double cumulativeProbability(final double x) {
- if (x < mu) {
- return Double.NaN;
- }
- return Erf.erfc(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 = Erf.erfcInv(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} */
- public double getNumericalMean() {
- return Double.POSITIVE_INFINITY;
- }
-
- /** {@inheritDoc} */
- public double getNumericalVariance() {
- return Double.POSITIVE_INFINITY;
- }
-
- /** {@inheritDoc} */
- public double getSupportLowerBound() {
- return mu;
- }
-
- /** {@inheritDoc} */
- public double getSupportUpperBound() {
- return Double.POSITIVE_INFINITY;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportLowerBoundInclusive() {
- // there is a division by x-mu in the computation, so density
- // is not finite at lower bound, bound must be excluded
- return false;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportUpperBoundInclusive() {
- // upper bound is infinite, so it must be excluded
- return false;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportConnected() {
- return true;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/distribution/LogNormalDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/distribution/LogNormalDistribution.java b/src/main/java/org/apache/commons/math3/distribution/LogNormalDistribution.java
deleted file mode 100644
index b8148b0..0000000
--- a/src/main/java/org/apache/commons/math3/distribution/LogNormalDistribution.java
+++ /dev/null
@@ -1,366 +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.math3.distribution;
-
-import org.apache.commons.math3.exception.NotStrictlyPositiveException;
-import org.apache.commons.math3.exception.NumberIsTooLargeException;
-import org.apache.commons.math3.exception.util.LocalizedFormats;
-import org.apache.commons.math3.random.RandomGenerator;
-import org.apache.commons.math3.random.Well19937c;
-import org.apache.commons.math3.special.Erf;
-import org.apache.commons.math3.util.FastMath;
-
-/**
- * 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;
-
- /**
- * Create 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}.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- */
- public LogNormalDistribution() {
- this(0, 1);
- }
-
- /**
- * Create a log-normal distribution using the specified scale and shape.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @param scale the scale parameter of this distribution
- * @param shape the 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);
- }
-
- /**
- * Create a log-normal distribution using the specified scale, shape and
- * inverse cumulative distribution accuracy.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @param scale the scale parameter of this distribution
- * @param shape the 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 {
- this(new Well19937c(), scale, shape, inverseCumAccuracy);
- }
-
- /**
- * Creates a log-normal distribution.
- *
- * @param rng Random number generator.
- * @param scale Scale parameter of this distribution.
- * @param shape Shape parameter of this distribution.
- * @throws NotStrictlyPositiveException if {@code shape <= 0}.
- * @since 3.3
- */
- public LogNormalDistribution(RandomGenerator rng, double scale, double shape)
- throws NotStrictlyPositiveException {
- this(rng, scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
- }
-
- /**
- * Creates a log-normal distribution.
- *
- * @param rng Random number generator.
- * @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}.
- * @since 3.1
- */
- public LogNormalDistribution(RandomGenerator rng,
- double scale,
- double shape,
- double inverseCumAccuracy)
- throws NotStrictlyPositiveException {
- super(rng);
-
- 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>
- */
- 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>
- */
- 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.erf(dev / (shape * SQRT2));
- }
-
- /**
- * {@inheritDoc}
- *
- * @deprecated See {@link RealDistribution#cumulativeProbability(double,double)}
- */
- @Override@Deprecated
- public double cumulativeProbability(double x0, double x1)
- throws NumberIsTooLargeException {
- return probability(x0, x1);
- }
-
- /** {@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 * Erf.erf(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)}.
- */
- 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)}.
- */
- 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)
- */
- 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})
- */
- public double getSupportUpperBound() {
- return Double.POSITIVE_INFINITY;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportLowerBoundInclusive() {
- return true;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportUpperBoundInclusive() {
- return false;
- }
-
- /**
- * {@inheritDoc}
- *
- * The support of this distribution is connected.
- *
- * @return {@code true}
- */
- public boolean isSupportConnected() {
- return true;
- }
-
- /** {@inheritDoc} */
- @Override
- public double sample() {
- final double n = random.nextGaussian();
- return FastMath.exp(scale + shape * n);
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/distribution/LogisticDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/distribution/LogisticDistribution.java b/src/main/java/org/apache/commons/math3/distribution/LogisticDistribution.java
deleted file mode 100644
index 59313f5..0000000
--- a/src/main/java/org/apache/commons/math3/distribution/LogisticDistribution.java
+++ /dev/null
@@ -1,160 +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.math3.distribution;
-
-import org.apache.commons.math3.exception.NotStrictlyPositiveException;
-import org.apache.commons.math3.exception.OutOfRangeException;
-import org.apache.commons.math3.exception.util.LocalizedFormats;
-import org.apache.commons.math3.random.RandomGenerator;
-import org.apache.commons.math3.random.Well19937c;
-import org.apache.commons.math3.util.FastMath;
-import org.apache.commons.math3.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 = 20141003;
-
- /** The location parameter. */
- private final double mu;
- /** The scale parameter. */
- private final double s;
-
- /**
- * Build a new instance.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @param mu location parameter
- * @param s scale parameter (must be positive)
- * @throws NotStrictlyPositiveException if {@code beta <= 0}
- */
- public LogisticDistribution(double mu, double s) {
- this(new Well19937c(), mu, s);
- }
-
- /**
- * Build a new instance.
- *
- * @param rng Random number generator
- * @param mu location parameter
- * @param s scale parameter (must be positive)
- * @throws NotStrictlyPositiveException if {@code beta <= 0}
- */
- public LogisticDistribution(RandomGenerator rng, double mu, double s) {
- super(rng);
-
- 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} */
- 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} */
- public double cumulativeProbability(double x) {
- double z = 1 / s * (x - mu);
- return 1.0 / (1.0 + FastMath.exp(-z));
- }
-
- @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} */
- public double getNumericalMean() {
- return mu;
- }
-
- /** {@inheritDoc} */
- public double getNumericalVariance() {
- return (MathUtils.PI_SQUARED / 3.0) * (1.0 / (s * s));
- }
-
- /** {@inheritDoc} */
- public double getSupportLowerBound() {
- return Double.NEGATIVE_INFINITY;
- }
-
- /** {@inheritDoc} */
- public double getSupportUpperBound() {
- return Double.POSITIVE_INFINITY;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportLowerBoundInclusive() {
- return false;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportUpperBoundInclusive() {
- return false;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportConnected() {
- return true;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateNormalDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateNormalDistribution.java b/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateNormalDistribution.java
deleted file mode 100644
index 0cf88c2..0000000
--- a/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateNormalDistribution.java
+++ /dev/null
@@ -1,113 +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.math3.distribution;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.math3.exception.DimensionMismatchException;
-import org.apache.commons.math3.exception.NotPositiveException;
-import org.apache.commons.math3.random.RandomGenerator;
-import org.apache.commons.math3.util.Pair;
-
-/**
- * Multivariate normal mixture distribution.
- * This class is mainly syntactic sugar.
- *
- * @see MixtureMultivariateRealDistribution
- * @since 3.2
- */
-public class MixtureMultivariateNormalDistribution
- extends MixtureMultivariateRealDistribution<MultivariateNormalDistribution> {
-
- /**
- * Creates a multivariate normal mixture distribution.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link org.apache.commons.math3.random.Well19937c Well19937c} as random
- * generator to be used for sampling only (see {@link #sample()} and
- * {@link #sample(int)}). In case no sampling is needed for the created
- * distribution, it is advised to pass {@code null} as random generator via
- * the appropriate constructors to avoid the additional initialisation
- * overhead.
- *
- * @param weights Weights of each component.
- * @param means Mean vector for each component.
- * @param covariances Covariance matrix for each component.
- */
- public MixtureMultivariateNormalDistribution(double[] weights,
- double[][] means,
- double[][][] covariances) {
- super(createComponents(weights, means, covariances));
- }
-
- /**
- * Creates a mixture model from a list of distributions and their
- * associated weights.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link org.apache.commons.math3.random.Well19937c Well19937c} as random
- * generator to be used for sampling only (see {@link #sample()} and
- * {@link #sample(int)}). In case no sampling is needed for the created
- * distribution, it is advised to pass {@code null} as random generator via
- * the appropriate constructors to avoid the additional initialisation
- * overhead.
- *
- * @param components List of (weight, distribution) pairs from which to sample.
- */
- public MixtureMultivariateNormalDistribution(List<Pair<Double, MultivariateNormalDistribution>> components) {
- super(components);
- }
-
- /**
- * Creates a mixture model from a list of distributions and their
- * associated weights.
- *
- * @param rng Random number generator.
- * @param components Distributions from which to sample.
- * @throws NotPositiveException if any of the weights is negative.
- * @throws DimensionMismatchException if not all components have the same
- * number of variables.
- */
- public MixtureMultivariateNormalDistribution(RandomGenerator rng,
- List<Pair<Double, MultivariateNormalDistribution>> components)
- throws NotPositiveException, DimensionMismatchException {
- super(rng, components);
- }
-
- /**
- * @param weights Weights of each component.
- * @param means Mean vector for each component.
- * @param covariances Covariance matrix for each component.
- * @return the list of components.
- */
- private static List<Pair<Double, MultivariateNormalDistribution>> createComponents(double[] weights,
- double[][] means,
- double[][][] covariances) {
- final List<Pair<Double, MultivariateNormalDistribution>> mvns
- = new ArrayList<Pair<Double, MultivariateNormalDistribution>>(weights.length);
-
- for (int i = 0; i < weights.length; i++) {
- final MultivariateNormalDistribution dist
- = new MultivariateNormalDistribution(means[i], covariances[i]);
-
- mvns.add(new Pair<Double, MultivariateNormalDistribution>(weights[i], dist));
- }
-
- return mvns;
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateRealDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateRealDistribution.java b/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateRealDistribution.java
deleted file mode 100644
index f0939f6..0000000
--- a/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateRealDistribution.java
+++ /dev/null
@@ -1,171 +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.math3.distribution;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.math3.exception.DimensionMismatchException;
-import org.apache.commons.math3.exception.MathArithmeticException;
-import org.apache.commons.math3.exception.NotPositiveException;
-import org.apache.commons.math3.exception.util.LocalizedFormats;
-import org.apache.commons.math3.random.RandomGenerator;
-import org.apache.commons.math3.random.Well19937c;
-import org.apache.commons.math3.util.Pair;
-
-/**
- * Class for representing <a href="http://en.wikipedia.org/wiki/Mixture_model">
- * mixture model</a> distributions.
- *
- * @param <T> Type of the mixture components.
- *
- * @since 3.1
- */
-public class MixtureMultivariateRealDistribution<T extends MultivariateRealDistribution>
- extends AbstractMultivariateRealDistribution {
- /** Normalized weight of each mixture component. */
- private final double[] weight;
- /** Mixture components. */
- private final List<T> distribution;
-
- /**
- * Creates a mixture model from a list of distributions and their
- * associated weights.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @param components List of (weight, distribution) pairs from which to sample.
- */
- public MixtureMultivariateRealDistribution(List<Pair<Double, T>> components) {
- this(new Well19937c(), components);
- }
-
- /**
- * Creates a mixture model from a list of distributions and their
- * associated weights.
- *
- * @param rng Random number generator.
- * @param components Distributions from which to sample.
- * @throws NotPositiveException if any of the weights is negative.
- * @throws DimensionMismatchException if not all components have the same
- * number of variables.
- */
- public MixtureMultivariateRealDistribution(RandomGenerator rng,
- List<Pair<Double, T>> components) {
- super(rng, components.get(0).getSecond().getDimension());
-
- final int numComp = components.size();
- final int dim = getDimension();
- double weightSum = 0;
- for (int i = 0; i < numComp; i++) {
- final Pair<Double, T> comp = components.get(i);
- if (comp.getSecond().getDimension() != dim) {
- throw new DimensionMismatchException(comp.getSecond().getDimension(), dim);
- }
- if (comp.getFirst() < 0) {
- throw new NotPositiveException(comp.getFirst());
- }
- weightSum += comp.getFirst();
- }
-
- // Check for overflow.
- if (Double.isInfinite(weightSum)) {
- throw new MathArithmeticException(LocalizedFormats.OVERFLOW);
- }
-
- // Store each distribution and its normalized weight.
- distribution = new ArrayList<T>();
- weight = new double[numComp];
- for (int i = 0; i < numComp; i++) {
- final Pair<Double, T> comp = components.get(i);
- weight[i] = comp.getFirst() / weightSum;
- distribution.add(comp.getSecond());
- }
- }
-
- /** {@inheritDoc} */
- public double density(final double[] values) {
- double p = 0;
- for (int i = 0; i < weight.length; i++) {
- p += weight[i] * distribution.get(i).density(values);
- }
- return p;
- }
-
- /** {@inheritDoc} */
- @Override
- public double[] sample() {
- // Sampled values.
- double[] vals = null;
-
- // Determine which component to sample from.
- final double randomValue = random.nextDouble();
- double sum = 0;
-
- for (int i = 0; i < weight.length; i++) {
- sum += weight[i];
- if (randomValue <= sum) {
- // pick model i
- vals = distribution.get(i).sample();
- break;
- }
- }
-
- if (vals == null) {
- // This should never happen, but it ensures we won't return a null in
- // case the loop above has some floating point inequality problem on
- // the final iteration.
- vals = distribution.get(weight.length - 1).sample();
- }
-
- return vals;
- }
-
- /** {@inheritDoc} */
- @Override
- public void reseedRandomGenerator(long seed) {
- // Seed needs to be propagated to underlying components
- // in order to maintain consistency between runs.
- super.reseedRandomGenerator(seed);
-
- for (int i = 0; i < distribution.size(); i++) {
- // Make each component's seed different in order to avoid
- // using the same sequence of random numbers.
- distribution.get(i).reseedRandomGenerator(i + 1 + seed);
- }
- }
-
- /**
- * Gets the distributions that make up the mixture model.
- *
- * @return the component distributions and associated weights.
- */
- public List<Pair<Double, T>> getComponents() {
- final List<Pair<Double, T>> list = new ArrayList<Pair<Double, T>>(weight.length);
-
- for (int i = 0; i < weight.length; i++) {
- list.add(new Pair<Double, T>(weight[i], distribution.get(i)));
- }
-
- return list;
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java b/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java
deleted file mode 100644
index 7fc8b74..0000000
--- a/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java
+++ /dev/null
@@ -1,247 +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.math3.distribution;
-
-import org.apache.commons.math3.exception.DimensionMismatchException;
-import org.apache.commons.math3.linear.Array2DRowRealMatrix;
-import org.apache.commons.math3.linear.EigenDecomposition;
-import org.apache.commons.math3.linear.NonPositiveDefiniteMatrixException;
-import org.apache.commons.math3.linear.RealMatrix;
-import org.apache.commons.math3.linear.SingularMatrixException;
-import org.apache.commons.math3.random.RandomGenerator;
-import org.apache.commons.math3.random.Well19937c;
-import org.apache.commons.math3.util.FastMath;
-import org.apache.commons.math3.util.MathArrays;
-
-/**
- * Implementation of the multivariate normal (Gaussian) distribution.
- *
- * @see <a href="http://en.wikipedia.org/wiki/Multivariate_normal_distribution">
- * Multivariate normal distribution (Wikipedia)</a>
- * @see <a href="http://mathworld.wolfram.com/MultivariateNormalDistribution.html">
- * Multivariate normal distribution (MathWorld)</a>
- *
- * @since 3.1
- */
-public class MultivariateNormalDistribution
- extends AbstractMultivariateRealDistribution {
- /** Vector of means. */
- private final double[] means;
- /** Covariance matrix. */
- private final RealMatrix covarianceMatrix;
- /** The matrix inverse of the covariance matrix. */
- private final RealMatrix covarianceMatrixInverse;
- /** The determinant of the covariance matrix. */
- private final double covarianceMatrixDeterminant;
- /** Matrix used in computation of samples. */
- private final RealMatrix samplingMatrix;
-
- /**
- * Creates a multivariate normal distribution with the given mean vector and
- * covariance matrix.
- * <br/>
- * The number of dimensions is equal to the length of the mean vector
- * and to the number of rows and columns of the covariance matrix.
- * It is frequently written as "p" in formulae.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @param means Vector of means.
- * @param covariances Covariance matrix.
- * @throws DimensionMismatchException if the arrays length are
- * inconsistent.
- * @throws SingularMatrixException if the eigenvalue decomposition cannot
- * be performed on the provided covariance matrix.
- * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is
- * negative.
- */
- public MultivariateNormalDistribution(final double[] means,
- final double[][] covariances)
- throws SingularMatrixException,
- DimensionMismatchException,
- NonPositiveDefiniteMatrixException {
- this(new Well19937c(), means, covariances);
- }
-
- /**
- * Creates a multivariate normal distribution with the given mean vector and
- * covariance matrix.
- * <br/>
- * The number of dimensions is equal to the length of the mean vector
- * and to the number of rows and columns of the covariance matrix.
- * It is frequently written as "p" in formulae.
- *
- * @param rng Random Number Generator.
- * @param means Vector of means.
- * @param covariances Covariance matrix.
- * @throws DimensionMismatchException if the arrays length are
- * inconsistent.
- * @throws SingularMatrixException if the eigenvalue decomposition cannot
- * be performed on the provided covariance matrix.
- * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is
- * negative.
- */
- public MultivariateNormalDistribution(RandomGenerator rng,
- final double[] means,
- final double[][] covariances)
- throws SingularMatrixException,
- DimensionMismatchException,
- NonPositiveDefiniteMatrixException {
- super(rng, means.length);
-
- final int dim = means.length;
-
- if (covariances.length != dim) {
- throw new DimensionMismatchException(covariances.length, dim);
- }
-
- for (int i = 0; i < dim; i++) {
- if (dim != covariances[i].length) {
- throw new DimensionMismatchException(covariances[i].length, dim);
- }
- }
-
- this.means = MathArrays.copyOf(means);
-
- covarianceMatrix = new Array2DRowRealMatrix(covariances);
-
- // Covariance matrix eigen decomposition.
- final EigenDecomposition covMatDec = new EigenDecomposition(covarianceMatrix);
-
- // Compute and store the inverse.
- covarianceMatrixInverse = covMatDec.getSolver().getInverse();
- // Compute and store the determinant.
- covarianceMatrixDeterminant = covMatDec.getDeterminant();
-
- // Eigenvalues of the covariance matrix.
- final double[] covMatEigenvalues = covMatDec.getRealEigenvalues();
-
- for (int i = 0; i < covMatEigenvalues.length; i++) {
- if (covMatEigenvalues[i] < 0) {
- throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0);
- }
- }
-
- // Matrix where each column is an eigenvector of the covariance matrix.
- final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim);
- for (int v = 0; v < dim; v++) {
- final double[] evec = covMatDec.getEigenvector(v).toArray();
- covMatEigenvectors.setColumn(v, evec);
- }
-
- final RealMatrix tmpMatrix = covMatEigenvectors.transpose();
-
- // Scale each eigenvector by the square root of its eigenvalue.
- for (int row = 0; row < dim; row++) {
- final double factor = FastMath.sqrt(covMatEigenvalues[row]);
- for (int col = 0; col < dim; col++) {
- tmpMatrix.multiplyEntry(row, col, factor);
- }
- }
-
- samplingMatrix = covMatEigenvectors.multiply(tmpMatrix);
- }
-
- /**
- * Gets the mean vector.
- *
- * @return the mean vector.
- */
- public double[] getMeans() {
- return MathArrays.copyOf(means);
- }
-
- /**
- * Gets the covariance matrix.
- *
- * @return the covariance matrix.
- */
- public RealMatrix getCovariances() {
- return covarianceMatrix.copy();
- }
-
- /** {@inheritDoc} */
- public double density(final double[] vals) throws DimensionMismatchException {
- final int dim = getDimension();
- if (vals.length != dim) {
- throw new DimensionMismatchException(vals.length, dim);
- }
-
- return FastMath.pow(2 * FastMath.PI, -0.5 * dim) *
- FastMath.pow(covarianceMatrixDeterminant, -0.5) *
- getExponentTerm(vals);
- }
-
- /**
- * Gets the square root of each element on the diagonal of the covariance
- * matrix.
- *
- * @return the standard deviations.
- */
- public double[] getStandardDeviations() {
- final int dim = getDimension();
- final double[] std = new double[dim];
- final double[][] s = covarianceMatrix.getData();
- for (int i = 0; i < dim; i++) {
- std[i] = FastMath.sqrt(s[i][i]);
- }
- return std;
- }
-
- /** {@inheritDoc} */
- @Override
- public double[] sample() {
- final int dim = getDimension();
- final double[] normalVals = new double[dim];
-
- for (int i = 0; i < dim; i++) {
- normalVals[i] = random.nextGaussian();
- }
-
- final double[] vals = samplingMatrix.operate(normalVals);
-
- for (int i = 0; i < dim; i++) {
- vals[i] += means[i];
- }
-
- return vals;
- }
-
- /**
- * Computes the term used in the exponent (see definition of the distribution).
- *
- * @param values Values at which to compute density.
- * @return the multiplication factor of density calculations.
- */
- private double getExponentTerm(final double[] values) {
- final double[] centered = new double[values.length];
- for (int i = 0; i < centered.length; i++) {
- centered[i] = values[i] - getMeans()[i];
- }
- final double[] preMultiplied = covarianceMatrixInverse.preMultiply(centered);
- double sum = 0;
- for (int i = 0; i < preMultiplied.length; i++) {
- sum += preMultiplied[i] * centered[i];
- }
- return FastMath.exp(-0.5 * sum);
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/distribution/MultivariateRealDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/distribution/MultivariateRealDistribution.java b/src/main/java/org/apache/commons/math3/distribution/MultivariateRealDistribution.java
deleted file mode 100644
index cde1f74..0000000
--- a/src/main/java/org/apache/commons/math3/distribution/MultivariateRealDistribution.java
+++ /dev/null
@@ -1,78 +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.math3.distribution;
-
-import org.apache.commons.math3.exception.NotStrictlyPositiveException;
-
-/**
- * Base interface for multivariate distributions on the reals.
- *
- * This is based largely on the RealDistribution interface, but cumulative
- * distribution functions are not required because they are often quite
- * difficult to compute for multivariate distributions.
- *
- * @since 3.1
- */
-public interface MultivariateRealDistribution {
- /**
- * Returns the probability density function (PDF) of this distribution
- * evaluated at the specified point {@code x}. In general, the PDF is the
- * derivative of the cumulative distribution function. If the derivative
- * does not exist at {@code x}, then an appropriate replacement should be
- * returned, e.g. {@code Double.POSITIVE_INFINITY}, {@code Double.NaN}, or
- * the limit inferior or limit superior of the difference quotient.
- *
- * @param x Point at which the PDF is evaluated.
- * @return the value of the probability density function at point {@code x}.
- */
- double density(double[] x);
-
- /**
- * Reseeds the random generator used to generate samples.
- *
- * @param seed Seed with which to initialize the random number generator.
- */
- void reseedRandomGenerator(long seed);
-
- /**
- * Gets the number of random variables of the distribution.
- * It is the size of the array returned by the {@link #sample() sample}
- * method.
- *
- * @return the number of variables.
- */
- int getDimension();
-
- /**
- * Generates a random value vector sampled from this distribution.
- *
- * @return a random value vector.
- */
- double[] sample();
-
- /**
- * Generates a list of a random value vectors from the distribution.
- *
- * @param sampleSize the number of random vectors to generate.
- * @return an array representing the random samples.
- * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
- * if {@code sampleSize} is not positive.
- *
- * @see #sample()
- */
- double[][] sample(int sampleSize) throws NotStrictlyPositiveException;
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/distribution/NakagamiDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/distribution/NakagamiDistribution.java b/src/main/java/org/apache/commons/math3/distribution/NakagamiDistribution.java
deleted file mode 100644
index 2b1f81f..0000000
--- a/src/main/java/org/apache/commons/math3/distribution/NakagamiDistribution.java
+++ /dev/null
@@ -1,188 +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.math3.distribution;
-
-import org.apache.commons.math3.exception.NotStrictlyPositiveException;
-import org.apache.commons.math3.exception.NumberIsTooSmallException;
-import org.apache.commons.math3.exception.util.LocalizedFormats;
-import org.apache.commons.math3.random.RandomGenerator;
-import org.apache.commons.math3.random.Well19937c;
-import org.apache.commons.math3.special.Gamma;
-import org.apache.commons.math3.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 = 20141003;
-
- /** The shape parameter. */
- private final double mu;
- /** The scale parameter. */
- private final double omega;
- /** Inverse cumulative probability accuracy. */
- private final double inverseAbsoluteAccuracy;
-
- /**
- * Build a new instance.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @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);
- }
-
- /**
- * Build a new instance.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @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) {
- this(new Well19937c(), mu, omega, inverseAbsoluteAccuracy);
- }
-
- /**
- * Build a new instance.
- *
- * @param rng Random number generator
- * @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(RandomGenerator rng, double mu, double omega, double inverseAbsoluteAccuracy) {
- super(rng);
-
- 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;
- }
-
- @Override
- protected double getSolverAbsoluteAccuracy() {
- return inverseAbsoluteAccuracy;
- }
-
- /** {@inheritDoc} */
- public double density(double x) {
- if (x <= 0) {
- return 0.0;
- }
- return 2.0 * FastMath.pow(mu, mu) / (Gamma.gamma(mu) * FastMath.pow(omega, mu)) *
- FastMath.pow(x, 2 * mu - 1) * FastMath.exp(-mu * x * x / omega);
- }
-
- /** {@inheritDoc} */
- public double cumulativeProbability(double x) {
- return Gamma.regularizedGammaP(mu, mu * x * x / omega);
- }
-
- /** {@inheritDoc} */
- public double getNumericalMean() {
- return Gamma.gamma(mu + 0.5) / Gamma.gamma(mu) * FastMath.sqrt(omega / mu);
- }
-
- /** {@inheritDoc} */
- public double getNumericalVariance() {
- double v = Gamma.gamma(mu + 0.5) / Gamma.gamma(mu);
- return omega * (1 - 1 / mu * v * v);
- }
-
- /** {@inheritDoc} */
- public double getSupportLowerBound() {
- return 0;
- }
-
- /** {@inheritDoc} */
- public double getSupportUpperBound() {
- return Double.POSITIVE_INFINITY;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportLowerBoundInclusive() {
- return true;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportUpperBoundInclusive() {
- return false;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportConnected() {
- return true;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/distribution/NormalDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/distribution/NormalDistribution.java b/src/main/java/org/apache/commons/math3/distribution/NormalDistribution.java
deleted file mode 100644
index 0fc839f..0000000
--- a/src/main/java/org/apache/commons/math3/distribution/NormalDistribution.java
+++ /dev/null
@@ -1,311 +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.math3.distribution;
-
-import org.apache.commons.math3.exception.NotStrictlyPositiveException;
-import org.apache.commons.math3.exception.NumberIsTooLargeException;
-import org.apache.commons.math3.exception.OutOfRangeException;
-import org.apache.commons.math3.exception.util.LocalizedFormats;
-import org.apache.commons.math3.random.RandomGenerator;
-import org.apache.commons.math3.random.Well19937c;
-import org.apache.commons.math3.special.Erf;
-import org.apache.commons.math3.util.FastMath;
-
-/**
- * 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.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- */
- public NormalDistribution() {
- this(0, 1);
- }
-
- /**
- * Create a normal distribution using the given mean and standard deviation.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @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);
- }
-
- /**
- * Create a normal distribution using the given mean, standard deviation and
- * inverse cumulative distribution accuracy.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @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}.
- * @since 2.1
- */
- public NormalDistribution(double mean, double sd, double inverseCumAccuracy)
- throws NotStrictlyPositiveException {
- this(new Well19937c(), mean, sd, inverseCumAccuracy);
- }
-
- /**
- * Creates a normal distribution.
- *
- * @param rng Random number generator.
- * @param mean Mean for this distribution.
- * @param sd Standard deviation for this distribution.
- * @throws NotStrictlyPositiveException if {@code sd <= 0}.
- * @since 3.3
- */
- public NormalDistribution(RandomGenerator rng, double mean, double sd)
- throws NotStrictlyPositiveException {
- this(rng, mean, sd, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
- }
-
- /**
- * Creates a normal distribution.
- *
- * @param rng Random number generator.
- * @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}.
- * @since 3.1
- */
- public NormalDistribution(RandomGenerator rng,
- double mean,
- double sd,
- double inverseCumAccuracy)
- throws NotStrictlyPositiveException {
- super(rng);
-
- 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} */
- 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.
- */
- 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 * (1 + Erf.erf(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 * Erf.erfInv(2 * p - 1);
- }
-
- /**
- * {@inheritDoc}
- *
- * @deprecated See {@link RealDistribution#cumulativeProbability(double,double)}
- */
- @Override@Deprecated
- public double cumulativeProbability(double x0, double x1)
- throws NumberIsTooLargeException {
- return probability(x0, x1);
- }
-
- /** {@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 * Erf.erf(v0, v1);
- }
-
- /** {@inheritDoc} */
- @Override
- protected double getSolverAbsoluteAccuracy() {
- return solverAbsoluteAccuracy;
- }
-
- /**
- * {@inheritDoc}
- *
- * For mean parameter {@code mu}, the mean is {@code mu}.
- */
- public double getNumericalMean() {
- return getMean();
- }
-
- /**
- * {@inheritDoc}
- *
- * For standard deviation parameter {@code s}, the variance is {@code s^2}.
- */
- 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})
- */
- 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})
- */
- public double getSupportUpperBound() {
- return Double.POSITIVE_INFINITY;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportLowerBoundInclusive() {
- return false;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportUpperBoundInclusive() {
- return false;
- }
-
- /**
- * {@inheritDoc}
- *
- * The support of this distribution is connected.
- *
- * @return {@code true}
- */
- public boolean isSupportConnected() {
- return true;
- }
-
- /** {@inheritDoc} */
- @Override
- public double sample() {
- return standardDeviation * random.nextGaussian() + mean;
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java b/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java
deleted file mode 100644
index 3c4d77a..0000000
--- a/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java
+++ /dev/null
@@ -1,318 +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.math3.distribution;
-
-import org.apache.commons.math3.exception.NotStrictlyPositiveException;
-import org.apache.commons.math3.exception.NumberIsTooLargeException;
-import org.apache.commons.math3.exception.util.LocalizedFormats;
-import org.apache.commons.math3.random.RandomGenerator;
-import org.apache.commons.math3.random.Well19937c;
-import org.apache.commons.math3.util.FastMath;
-
-/**
- * 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>
- * <p>
- * <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 = 20130424;
-
- /** 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;
-
- /**
- * Create a Pareto distribution with a scale of {@code 1} and a shape of {@code 1}.
- */
- public ParetoDistribution() {
- this(1, 1);
- }
-
- /**
- * Create a Pareto distribution using the specified scale and shape.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @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);
- }
-
- /**
- * Create a Pareto distribution using the specified scale, shape and
- * inverse cumulative distribution accuracy.
- * <p>
- * <b>Note:</b> this constructor will implicitly create an instance of
- * {@link Well19937c} as random generator to be used for sampling only (see
- * {@link #sample()} and {@link #sample(int)}). In case no sampling is
- * needed for the created distribution, it is advised to pass {@code null}
- * as random generator via the appropriate constructors to avoid the
- * additional initialisation overhead.
- *
- * @param scale the scale parameter of this distribution
- * @param shape the 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 {
- this(new Well19937c(), scale, shape, inverseCumAccuracy);
- }
-
- /**
- * Creates a Pareto distribution.
- *
- * @param rng Random number generator.
- * @param scale Scale parameter of this distribution.
- * @param shape Shape parameter of this distribution.
- * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}.
- */
- public ParetoDistribution(RandomGenerator rng, double scale, double shape)
- throws NotStrictlyPositiveException {
- this(rng, scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
- }
-
- /**
- * Creates a Pareto distribution.
- *
- * @param rng Random number generator.
- * @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(RandomGenerator rng,
- double scale,
- double shape,
- double inverseCumAccuracy)
- throws NotStrictlyPositiveException {
- super(rng);
-
- 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>
- */
- 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>
- */
- public double cumulativeProbability(double x) {
- if (x <= scale) {
- return 0;
- }
- return 1 - FastMath.pow(scale / x, shape);
- }
-
- /**
- * {@inheritDoc}
- *
- * @deprecated See {@link RealDistribution#cumulativeProbability(double,double)}
- */
- @Override
- @Deprecated
- public double cumulativeProbability(double x0, double x1)
- throws NumberIsTooLargeException {
- return probability(x0, x1);
- }
-
- /** {@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>
- */
- 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>
- */
- 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
- */
- 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})
- */
- public double getSupportUpperBound() {
- return Double.POSITIVE_INFINITY;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportLowerBoundInclusive() {
- return true;
- }
-
- /** {@inheritDoc} */
- public boolean isSupportUpperBoundInclusive() {
- return false;
- }
-
- /**
- * {@inheritDoc}
- * <p>
- * The support of this distribution is connected.
- *
- * @return {@code true}
- */
- public boolean isSupportConnected() {
- return true;
- }
-
- /** {@inheritDoc} */
- @Override
- public double sample() {
- final double n = random.nextDouble();
- return scale / FastMath.pow(n, 1 / shape);
- }
-}