[MATH-1139] Added Gumbel, Laplace, Logistic and Nakagami distributions. Thanks to Alexey Volkov.

This commit is contained in:
Thomas Neidhart 2014-10-03 23:12:09 +02:00
parent e3dda4407a
commit 58d613bd8d
15 changed files with 1272 additions and 0 deletions

View File

@ -320,6 +320,9 @@
<contributor> <contributor>
<name>Kim van der Linde</name> <name>Kim van der Linde</name>
</contributor> </contributor>
<contributor>
<name>Alexey Volkov</name>
</contributor>
<contributor> <contributor>
<name>Andrew Waterman</name> <name>Andrew Waterman</name>
</contributor> </contributor>

View File

@ -73,6 +73,9 @@ Users are encouraged to upgrade to this version as this release not
2. A few methods in the FastMath class are in fact slower that their 2. A few methods in the FastMath class are in fact slower that their
counterpart in either Math or StrictMath (cf. MATH-740 and MATH-901). counterpart in either Math or StrictMath (cf. MATH-740 and MATH-901).
"> ">
<action dev="tn" type="add" issue="MATH-1139" due-to="Alexey Volkov">
Added Gumbel, Laplace, Logistic and Nakagami distributions.
</action>
<action dev="psteitz" type="fix" issue="MATH-1147"> <action dev="psteitz" type="fix" issue="MATH-1147">
Added statistics missing from toString method in SummaryStatistics. Added statistics missing from toString method in SummaryStatistics.
</action> </action>

View File

@ -0,0 +1,158 @@
/*
* 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 Gumbel distribution.
*
* @see <a href="http://en.wikipedia.org/wiki/Gumbel_distribution">Gumbel Distribution (Wikipedia)</a>
* @see <a href="http://mathworld.wolfram.com/GumbelDistribution.html">Gumbel Distribution (Mathworld)</a>
*
* @since 3.4
*/
public class GumbelDistribution extends AbstractRealDistribution {
/** Serializable version identifier. */
private static final long serialVersionUID = 20141003;
/**
* Approximation of Euler's constant
* see http://mathworld.wolfram.com/Euler-MascheroniConstantApproximations.html
*/
private static final double EULER = FastMath.PI / (2 * FastMath.E);
/** The location parameter. */
private final double mu;
/** The scale parameter. */
private final double beta;
/**
* Build a new instance.
*
* @param mu location parameter
* @param beta scale parameter (must be positive)
* @throws NotStrictlyPositiveException if {@code beta <= 0}
*/
public GumbelDistribution(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 GumbelDistribution(RandomGenerator rng, double mu, double beta) {
super(rng);
if (beta <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, beta);
}
this.beta = beta;
this.mu = mu;
}
/**
* Access the location parameter, {@code mu}.
*
* @return the location parameter.
*/
public double getLocation() {
return mu;
}
/**
* Access the scale parameter, {@code beta}.
*
* @return the scale parameter.
*/
public double getScale() {
return beta;
}
/** {@inheritDoc} */
public double density(double x) {
final double z = (x - mu) / beta;
final double t = FastMath.exp(-z);
return FastMath.exp(-z - t) / beta;
}
/** {@inheritDoc} */
public double cumulativeProbability(double x) {
final double z = (x - mu) / beta;
return FastMath.exp(-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 Double.NEGATIVE_INFINITY;
} else if (p == 1) {
return Double.POSITIVE_INFINITY;
}
return mu - FastMath.log(-FastMath.log(p)) * beta;
}
/** {@inheritDoc} */
public double getNumericalMean() {
return mu + EULER * beta;
}
/** {@inheritDoc} */
public double getNumericalVariance() {
return (FastMath.PI * FastMath.PI) / 6.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;
}
}

View File

@ -0,0 +1,153 @@
/*
* 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.
*
* @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 0.0;
} 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;
}
}

View File

@ -0,0 +1,152 @@
/*
* 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 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.
*
* @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 (FastMath.PI * FastMath.PI / 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;
}
}

View File

@ -0,0 +1,172 @@
/*
* 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>
*/
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.
*
* @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.
*
* @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;
}
}

View File

@ -0,0 +1,85 @@
# 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.
#
#------------------------------------------------------------------------------
# R source file to validate Gumbel distribution tests in
# org.apache.commons.math3.distribution.GumbelDistributionTest
#
# To run the test, install R, put this file and testFunctions
# into the same directory, launch R from this directory and then enter
# source("<name-of-this-file>")
#
# R functions used
# dgumbel(x, location = 0, scale = 1, log = FALSE, max = TRUE)
# pgumbel(q, location = 0, scale = 1, lower.tail = TRUE, max = TRUE)
#-----------------------------------------------------------------------------
tol <- 1E-9
# Function definitions
source("testFunctions") # utility test functions
library("VGAM")
# function to verify distribution computations
verifyDistribution <- function(points, expected, m, s, tol) {
rDistValues <- rep(0, length(points))
i <- 0
for (point in points) {
i <- i + 1
rDistValues[i] <- pgumbel(point, m, s)
}
output <- c("Distribution test m = ",m,", s = ", s)
if (assertEquals(expected, rDistValues, tol, "Distribution Values")) {
displayPadded(output, SUCCEEDED, WIDTH)
} else {
displayPadded(output, FAILED, WIDTH)
}
}
# function to verify density computations
verifyDensity <- function(points, expected, m, s, tol) {
rDensityValues <- rep(0, length(points))
i <- 0
for (point in points) {
i <- i + 1
rDensityValues[i] <- dgumbel(point, m, s)
}
output <- c("Density test m = ",m,", s = ", s)
if (assertEquals(expected, rDensityValues, tol, "Density Values")) {
displayPadded(output, SUCCEEDED, WIDTH)
} else {
displayPadded(output, FAILED, WIDTH)
}
}
#--------------------------------------------------------------------------
cat("Gumbel test cases\n")
m <- 0.5
s <- 2
distributionPoints <- c(-5:5)
densityValues <- c(1.2582621126545528E-6,3.5946885566568164E-4,0.009115765822384943,0.05321099995044945,0.12743521041151834,0.17778637369097208,0.17871767308609124,0.14726615762017733,0.10756585897012155,0.07302735923472656,0.047427815138561126)
distributionValues <- c(1.6087601139887782E-7,7.577547728260715E-5,0.003168165149053243,0.03049041346306221,0.12039226207982957,0.27692033409990896,0.4589560693076638,0.6235249162568004,0.7508834766393948,0.8404868737475784,0.8999651626606278)
verifyDistribution(distributionPoints, distributionValues, m, s, tol)
verifyDensity(distributionPoints, densityValues, m, s, tol)
m <- 1.5
s <- 3
distributionPoints <- c(-5:5)
densityValues <- c(4.707967970909721E-4,0.004005928431315734,0.01690237120332691,0.04314381688828758,0.07682272023182242,0.10568064035931406,0.12083781131699158,0.12102469295161239,0.11023476629680602,0.09380437047469757,0.07602582610501195)
distributionValues <- c(1.6180181049060529E-4,0.0019214004612365587,0.011314286380459627,0.04030537101298144,0.10016104975617146,0.1922956455479649,0.30686099686684076,0.42892134369676455,0.545239211892605,0.6475247847679736,0.7324184876698198)
verifyDistribution(distributionPoints, distributionValues, m, s, tol)
verifyDensity(distributionPoints, densityValues, m, s, tol)

View File

@ -0,0 +1,85 @@
# 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.
#
#------------------------------------------------------------------------------
# R source file to validate Laplace distribution tests in
# org.apache.commons.math3.distribution.LaplaceDistributionTest
#
# To run the test, install R, put this file and testFunctions
# into the same directory, launch R from this directory and then enter
# source("<name-of-this-file>")
#
# R functions used
# dlaplace(x, location = 0, scale = 1, log = FALSE, max = TRUE)
# plaplace(q, location = 0, scale = 1, lower.tail = TRUE, max = TRUE)
#-----------------------------------------------------------------------------
tol <- 1E-9
# Function definitions
source("testFunctions") # utility test functions
library("VGAM")
# function to verify distribution computations
verifyDistribution <- function(points, expected, m, s, tol) {
rDistValues <- rep(0, length(points))
i <- 0
for (point in points) {
i <- i + 1
rDistValues[i] <- plaplace(point, m, s)
}
output <- c("Distribution test m = ",m,", s = ", s)
if (assertEquals(expected, rDistValues, tol, "Distribution Values")) {
displayPadded(output, SUCCEEDED, WIDTH)
} else {
displayPadded(output, FAILED, WIDTH)
}
}
# function to verify density computations
verifyDensity <- function(points, expected, m, s, tol) {
rDensityValues <- rep(0, length(points))
i <- 0
for (point in points) {
i <- i + 1
rDensityValues[i] <- dlaplace(point, m, s)
}
output <- c("Density test m = ",m,", s = ", s)
if (assertEquals(expected, rDensityValues, tol, "Density Values")) {
displayPadded(output, SUCCEEDED, WIDTH)
} else {
displayPadded(output, FAILED, WIDTH)
}
}
#--------------------------------------------------------------------------
cat("Laplace test cases\n")
m <- 0
s <- 1
distributionPoints <- c(-5:5)
densityValues <- c(0.0033689734995427335,0.00915781944436709,0.024893534183931972,0.06766764161830635,0.18393972058572117,0.5,0.18393972058572117,0.06766764161830635,0.024893534183931972,0.00915781944436709,0.0033689734995427335)
distributionValues <- c(0.0033689734995427335,0.00915781944436709,0.024893534183931972,0.06766764161830635,0.18393972058572117,0.5,0.8160602794142788,0.9323323583816936,0.9751064658160681,0.9908421805556329,0.9966310265004573)
verifyDistribution(distributionPoints, distributionValues, m, s, tol)
verifyDensity(distributionPoints, densityValues, m, s, tol)
m <- -5
s <- 4
distributionPoints <- c(-5:5)
densityValues <- c(0.125,0.09735009788392561,0.07581633246407918,0.059045819092626836,0.04598493014643029,0.03581309960752376,0.027891270018553727,0.021721742931305642,0.016916910404576588,0.013174903070233042,0.01026062482798735)
distributionValues <- c(0.5,0.6105996084642975,0.6967346701436833,0.7638167236294926,0.8160602794142788,0.8567476015699049,0.888434919925785,0.9131130282747775,0.9323323583816936,0.9473003877190679,0.9589575006880506)
verifyDistribution(distributionPoints, distributionValues, m, s, tol)
verifyDensity(distributionPoints, densityValues, m, s, tol)

View File

@ -0,0 +1,85 @@
# 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.
#
#------------------------------------------------------------------------------
# R source file to validate Logistics distribution tests in
# org.apache.commons.math3.distribution.LogisticsDistributionTest
#
# To run the test, install R, put this file and testFunctions
# into the same directory, launch R from this directory and then enter
# source("<name-of-this-file>")
#
# R functions used
# dlogis(x, location = 0, scale = 1, log = FALSE, max = TRUE)
# plogis(q, location = 0, scale = 1, lower.tail = TRUE, max = TRUE)
#-----------------------------------------------------------------------------
tol <- 1E-9
# Function definitions
source("testFunctions") # utility test functions
library("VGAM")
# function to verify distribution computations
verifyDistribution <- function(points, expected, m, s, tol) {
rDistValues <- rep(0, length(points))
i <- 0
for (point in points) {
i <- i + 1
rDistValues[i] <- plogis(point, m, s)
}
output <- c("Distribution test m = ",m,", s = ", s)
if (assertEquals(expected, rDistValues, tol, "Distribution Values")) {
displayPadded(output, SUCCEEDED, WIDTH)
} else {
displayPadded(output, FAILED, WIDTH)
}
}
# function to verify density computations
verifyDensity <- function(points, expected, m, s, tol) {
rDensityValues <- rep(0, length(points))
i <- 0
for (point in points) {
i <- i + 1
rDensityValues[i] <- dlogis(point, m, s)
}
output <- c("Density test m = ",m,", s = ", s)
if (assertEquals(expected, rDensityValues, tol, "Density Values")) {
displayPadded(output, SUCCEEDED, WIDTH)
} else {
displayPadded(output, FAILED, WIDTH)
}
}
#--------------------------------------------------------------------------
cat("Logistics test cases\n")
m <- 5
s <- 2
distributionPoints <- c(-5:5)
densityValues <- c(0.0033240283353950773,0.005433114861112618,0.008831353106645559,0.014226511939867782,0.022588329865456065,0.03505185827255408,0.05249679270175326,0.07457322603516643,0.09830596662074093,0.11750185610079725,0.125)
distributionValues <- c(0.0066928509242848554,0.01098694263059318,0.01798620996209156,0.02931223075135632,0.04742587317756678,0.07585818002124355,0.11920292202211755,0.18242552380635635,0.2689414213699951,0.3775406687981454,0.5)
verifyDistribution(distributionPoints, distributionValues, m, s, tol)
verifyDensity(distributionPoints, densityValues, m, s, tol)
m <- 9
s <- 3
distributionPoints <- c(-5:5)
densityValues <- c(0.0030763907488492496,0.004261976157918787,0.005887568737763705,0.008101066534475556,0.01108624138724253,0.01505888657697071,0.020249392063798367,0.026861724758915424,0.03499786180116884,0.04454323746508397,0.0550303365509679)
distributionValues <- c(0.009315959345066693,0.012953727530695871,0.01798620996209156,0.02492442664711404,0.03444519566621118,0.04742587317756678,0.06496916912866407,0.08839967720705845,0.11920292202211755,0.15886910488091516,0.20860852732604496)
verifyDistribution(distributionPoints, distributionValues, m, s, tol)
verifyDensity(distributionPoints, densityValues, m, s, tol)

View File

@ -0,0 +1,85 @@
# 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.
#
#------------------------------------------------------------------------------
# R source file to validate Nakagami distribution tests in
# org.apache.commons.math3.distribution.NakagamiDistributionTest
#
# To run the test, install R, put this file and testFunctions
# into the same directory, launch R from this directory and then enter
# source("<name-of-this-file>")
#
# R functions used
# dnaka(x, location = 0, scale = 1, log = FALSE, max = TRUE)
# pnaka(q, location = 0, scale = 1, lower.tail = TRUE, max = TRUE)
#-----------------------------------------------------------------------------
tol <- 1E-9
# Function definitions
source("testFunctions") # utility test functions
library("VGAM")
# function to verify distribution computations
verifyDistribution <- function(points, expected, m, s, tol) {
rDistValues <- rep(0, length(points))
i <- 0
for (point in points) {
i <- i + 1
rDistValues[i] <- pnaka(point, m, s)
}
output <- c("Distribution test m = ",m,", s = ", s)
if (assertEquals(expected, rDistValues, tol, "Distribution Values")) {
displayPadded(output, SUCCEEDED, WIDTH)
} else {
displayPadded(output, FAILED, WIDTH)
}
}
# function to verify density computations
verifyDensity <- function(points, expected, m, s, tol) {
rDensityValues <- rep(0, length(points))
i <- 0
for (point in points) {
i <- i + 1
rDensityValues[i] <- dnaka(point, m, s)
}
output <- c("Density test m = ",m,", s = ", s)
if (assertEquals(expected, rDensityValues, tol, "Density Values")) {
displayPadded(output, SUCCEEDED, WIDTH)
} else {
displayPadded(output, FAILED, WIDTH)
}
}
#--------------------------------------------------------------------------
cat("Nakagami test cases\n")
m <- 0.5
s <- 1
distributionPoints <- seq(from = 0, to = 2, by = 0.2)
densityValues <- c(0.0,0.7820853879509118,0.7365402806066467,0.6664492057835993,0.5793831055229655,0.48394144903828673,0.38837210996642585,0.29945493127148964,0.2218416693589111,0.1579003166017883,0.10798193302637613)
distributionValues <- c(0.0,0.15851941887820603,0.3108434832206483,0.45149376449985296,0.5762892028332065,0.6826894921370859,0.7698606595565836,0.8384866815324576,0.8904014166008841,0.9281393617741498,0.9544997361036424)
verifyDistribution(distributionPoints, distributionValues, m, s, tol)
verifyDensity(distributionPoints, densityValues, m, s, tol)
m <- 1
s <- 2
distributionPoints <- seq(from = 0, to = 2, by = 0.2)
densityValues <- c(0.0,0.19603973466135105,0.36924653855465434,0.5011621268467633,0.5809192296589527,0.6065306597126334,0.584102707151966,0.5254355383919593,0.44485968072511056,0.3562176583505064,0.2706705664732254)
distributionValues <- c(0.0,0.0198013266932447,0.07688365361336423,0.16472978858872803,0.273850962926309,0.3934693402873665,0.5132477440400285,0.6246889011486005,0.7219626995468056,0.8021013009163853,0.8646647167633873)
verifyDistribution(distributionPoints, distributionValues, m, s, tol)
verifyDensity(distributionPoints, densityValues, m, s, tol)

View File

@ -41,6 +41,10 @@ source("GammaDistributionTestCases.R")
source("WeibullDistributionTestCases.R") source("WeibullDistributionTestCases.R")
source("ChiSquareDistributionTestCases.R") source("ChiSquareDistributionTestCases.R")
source("LevyDistributionTestCases.R") source("LevyDistributionTestCases.R")
source("gumbelTestCases.R")
source("laplaceTestCases.R")
source("logisticsTestCases.R")
source("nakagamiTestCases.R")
# regression # regression
source("regressionTestCases") source("regressionTestCases")

View File

@ -0,0 +1,71 @@
/*
* 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.util.Precision;
import org.junit.Assert;
import org.junit.Test;
/**
* Test cases for GumbelDistribution.
*/
public class GumbelDistributionTest extends RealDistributionAbstractTest {
@Test
public void testParameters() {
GumbelDistribution d = makeDistribution();
Assert.assertEquals(0.5, d.getLocation(), Precision.EPSILON);
Assert.assertEquals(2, d.getScale(), Precision.EPSILON);
}
@Test
public void testSupport() {
GumbelDistribution d = makeDistribution();
Assert.assertTrue(Double.isInfinite(d.getSupportLowerBound()));
Assert.assertTrue(Double.isInfinite(d.getSupportUpperBound()));
Assert.assertTrue(d.isSupportConnected());
}
@Override
public GumbelDistribution makeDistribution() {
return new GumbelDistribution(0.5, 2);
}
@Override
public double[] makeCumulativeTestPoints() {
return new double[] {
-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5
};
}
@Override
public double[] makeDensityTestValues() {
return new double[] {
1.258262e-06, 3.594689e-04, 9.115766e-03, 5.321100e-02, 1.274352e-01, 1.777864e-01,
1.787177e-01, 1.472662e-01, 1.075659e-01, 7.302736e-02, 4.742782e-02
};
}
@Override
public double[] makeCumulativeTestValues() {
return new double[] {
1.608760e-07, 7.577548e-05, 3.168165e-03, 3.049041e-02, 1.203923e-01, 2.769203e-01,
4.589561e-01, 6.235249e-01, 7.508835e-01, 8.404869e-01, 8.999652e-01
};
}
}

View File

@ -0,0 +1,72 @@
/*
* 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.util.Precision;
import org.junit.Assert;
import org.junit.Test;
/**
* Test cases for LaplaceDistribution.
*/
public class LaplaceDistributionTest extends RealDistributionAbstractTest {
@Test
public void testParameters() {
LaplaceDistribution d = makeDistribution();
Assert.assertEquals(0, d.getLocation(), Precision.EPSILON);
Assert.assertEquals(1, d.getScale(), Precision.EPSILON);
}
@Test
public void testSupport() {
LaplaceDistribution d = makeDistribution();
Assert.assertTrue(Double.isInfinite(d.getSupportLowerBound()));
Assert.assertTrue(Double.isInfinite(d.getSupportUpperBound()));
Assert.assertTrue(d.isSupportConnected());
}
@Override
public LaplaceDistribution makeDistribution() {
return new LaplaceDistribution(0, 1);
}
@Override
public double[] makeCumulativeTestPoints() {
return new double[] {
-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5
};
}
@Override
public double[] makeDensityTestValues() {
return new double[] {
0.003368973, 0.009157819, 0.024893534, 0.067667642, 0.183939721,
0.500000000, 0.183939721, 0.067667642, 0.024893534, 0.009157819, 0.003368973
};
}
@Override
public double[] makeCumulativeTestValues() {
return new double[] {
0.003368973, 0.009157819, 0.024893534, 0.067667642, 0.183939721,
0.500000000, 0.816060279, 0.932332358, 0.975106466, 0.990842181, 0.996631027
};
}
}

View File

@ -0,0 +1,72 @@
/*
* 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.util.Precision;
import org.junit.Assert;
import org.junit.Test;
/**
* Test cases for LogisticsDistribution.
*/
public class LogisticsDistributionTest extends RealDistributionAbstractTest {
@Test
public void testParameters() {
LogisticDistribution d = makeDistribution();
Assert.assertEquals(2, d.getLocation(), Precision.EPSILON);
Assert.assertEquals(5, d.getScale(), Precision.EPSILON);
}
@Test
public void testSupport() {
LogisticDistribution d = makeDistribution();
Assert.assertTrue(Double.isInfinite(d.getSupportLowerBound()));
Assert.assertTrue(Double.isInfinite(d.getSupportUpperBound()));
Assert.assertTrue(d.isSupportConnected());
}
@Override
public LogisticDistribution makeDistribution() {
return new LogisticDistribution(2, 5);
}
@Override
public double[] makeCumulativeTestPoints() {
return new double[] {
-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5
};
}
@Override
public double[] makeDensityTestValues() {
return new double[] {
0.03173698, 0.03557889, 0.03932239, 0.04278194, 0.04575685, 0.04805215,
0.04950331, 0.05000000, 0.04950331, 0.04805215, 0.04575685
};
}
@Override
public double[] makeCumulativeTestValues() {
return new double[] {
0.1978161, 0.2314752, 0.2689414, 0.3100255, 0.3543437, 0.4013123,
0.4501660, 0.5000000, 0.5498340, 0.5986877, 0.6456563
};
}
}

View File

@ -0,0 +1,72 @@
/*
* 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.util.Precision;
import org.junit.Assert;
import org.junit.Test;
/**
* Test cases for NakagamiDistribution.
*/
public class NakagamiDistributionTest extends RealDistributionAbstractTest {
@Test
public void testParameters() {
NakagamiDistribution d = makeDistribution();
Assert.assertEquals(0.5, d.getShape(), Precision.EPSILON);
Assert.assertEquals(1, d.getScale(), Precision.EPSILON);
}
@Test
public void testSupport() {
NakagamiDistribution d = makeDistribution();
Assert.assertEquals(d.getSupportLowerBound(), 0, Precision.EPSILON);
Assert.assertTrue(Double.isInfinite(d.getSupportUpperBound()));
Assert.assertTrue(d.isSupportConnected());
}
@Override
public NakagamiDistribution makeDistribution() {
return new NakagamiDistribution(0.5, 1);
}
@Override
public double[] makeCumulativeTestPoints() {
return new double[] {
0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2
};
}
@Override
public double[] makeDensityTestValues() {
return new double[] {
0.0000000, 0.7820854, 0.7365403, 0.6664492, 0.5793831, 0.4839414,
0.3883721, 0.2994549, 0.2218417, 0.1579003, 0.1079819
};
}
@Override
public double[] makeCumulativeTestValues() {
return new double[] {
0.0000000, 0.1585194, 0.3108435, 0.4514938, 0.5762892, 0.6826895,
0.7698607, 0.8384867, 0.8904014, 0.9281394, 0.9544997
};
}
}