http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/main/java/org/apache/commons/complex/Quaternion.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/complex/Quaternion.java b/src/main/java/org/apache/commons/complex/Quaternion.java deleted file mode 100644 index 6fc29a3..0000000 --- a/src/main/java/org/apache/commons/complex/Quaternion.java +++ /dev/null @@ -1,467 +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.complex; - -import java.io.Serializable; - -/** - * This class implements <a href="http://mathworld.wolfram.com/Quaternion.html"> - * quaternions</a> (Hamilton's hypercomplex numbers). - * <br/> - * Instance of this class are guaranteed to be immutable. - * - * @since 3.1 - */ -public final class Quaternion implements Serializable { - /** Identity quaternion. */ - public static final Quaternion IDENTITY = new Quaternion(1, 0, 0, 0); - /** Zero quaternion. */ - public static final Quaternion ZERO = new Quaternion(0, 0, 0, 0); - /** i */ - public static final Quaternion I = new Quaternion(0, 1, 0, 0); - /** j */ - public static final Quaternion J = new Quaternion(0, 0, 1, 0); - /** k */ - public static final Quaternion K = new Quaternion(0, 0, 0, 1); - - /** Serializable version identifier. */ - private static final long serialVersionUID = 20092012L; - - /** First component (scalar part). */ - private final double q0; - /** Second component (first vector part). */ - private final double q1; - /** Third component (second vector part). */ - private final double q2; - /** Fourth component (third vector part). */ - private final double q3; - - /** Exception message strings */ - private static final String zeroNormException = "Norm is zero"; - - /** Exponent offset in IEEE754 representation. */ - private static final long EXPONENT_OFFSET = 1023l; - /** Safe minimums such that 1/N does not overflow */ - private static final double SAFE_MIN = Double.longBitsToDouble((EXPONENT_OFFSET - 1022l) << 52); - - /** - * Builds a quaternion from its components. - * - * @param a Scalar component. - * @param b First vector component. - * @param c Second vector component. - * @param d Third vector component. - */ - public Quaternion(final double a, - final double b, - final double c, - final double d) { - this.q0 = a; - this.q1 = b; - this.q2 = c; - this.q3 = d; - } - - /** - * Builds a quaternion from scalar and vector parts. - * - * @param scalar Scalar part of the quaternion. - * @param v Components of the vector part of the quaternion. - * - * @throws IllegalArgumentException if the array length is not 3. - */ - public Quaternion(final double scalar, - final double[] v) { - try { - this.q0 = scalar; - this.q1 = v[0]; - this.q2 = v[1]; - this.q3 = v[2]; - } catch (Exception e) { - throw new IllegalArgumentException(zeroNormException); - } - } - - /** - * Builds a pure quaternion from a vector (assuming that the scalar - * part is zero). - * - * @param v Components of the vector part of the pure quaternion. - */ - public Quaternion(final double[] v) { - this(0, v); - } - - /** - * Returns the conjugate quaternion of the instance. - * - * @return the conjugate quaternion - */ - public Quaternion getConjugate() { - return new Quaternion(q0, -q1, -q2, -q3); - } - - /** - * Returns the Hamilton product of two quaternions. - * - * @param q1 First quaternion. - * @param q2 Second quaternion. - * @return the product {@code q1} and {@code q2}, in that order. - */ - public static Quaternion multiply(final Quaternion q1, final Quaternion q2) { - // Components of the first quaternion. - final double q1a = q1.getQ0(); - final double q1b = q1.getQ1(); - final double q1c = q1.getQ2(); - final double q1d = q1.getQ3(); - - // Components of the second quaternion. - final double q2a = q2.getQ0(); - final double q2b = q2.getQ1(); - final double q2c = q2.getQ2(); - final double q2d = q2.getQ3(); - - // Components of the product. - final double w = q1a * q2a - q1b * q2b - q1c * q2c - q1d * q2d; - final double x = q1a * q2b + q1b * q2a + q1c * q2d - q1d * q2c; - final double y = q1a * q2c - q1b * q2d + q1c * q2a + q1d * q2b; - final double z = q1a * q2d + q1b * q2c - q1c * q2b + q1d * q2a; - - return new Quaternion(w, x, y, z); - } - - /** - * Returns the Hamilton product of the instance by a quaternion. - * - * @param q Quaternion. - * @return the product of this instance with {@code q}, in that order. - */ - public Quaternion multiply(final Quaternion q) { - return multiply(this, q); - } - - /** - * Computes the sum of two quaternions. - * - * @param q1 Quaternion. - * @param q2 Quaternion. - * @return the sum of {@code q1} and {@code q2}. - */ - public static Quaternion add(final Quaternion q1, - final Quaternion q2) { - return new Quaternion(q1.getQ0() + q2.getQ0(), - q1.getQ1() + q2.getQ1(), - q1.getQ2() + q2.getQ2(), - q1.getQ3() + q2.getQ3()); - } - - /** - * Computes the sum of the instance and another quaternion. - * - * @param q Quaternion. - * @return the sum of this instance and {@code q} - */ - public Quaternion add(final Quaternion q) { - return add(this, q); - } - - /** - * Subtracts two quaternions. - * - * @param q1 First Quaternion. - * @param q2 Second quaternion. - * @return the difference between {@code q1} and {@code q2}. - */ - public static Quaternion subtract(final Quaternion q1, - final Quaternion q2) { - return new Quaternion(q1.getQ0() - q2.getQ0(), - q1.getQ1() - q2.getQ1(), - q1.getQ2() - q2.getQ2(), - q1.getQ3() - q2.getQ3()); - } - - /** - * Subtracts a quaternion from the instance. - * - * @param q Quaternion. - * @return the difference between this instance and {@code q}. - */ - public Quaternion subtract(final Quaternion q) { - return subtract(this, q); - } - - /** - * Computes the dot-product of two quaternions. - * - * @param q1 Quaternion. - * @param q2 Quaternion. - * @return the dot product of {@code q1} and {@code q2}. - */ - public static double dotProduct(final Quaternion q1, - final Quaternion q2) { - return q1.getQ0() * q2.getQ0() + - q1.getQ1() * q2.getQ1() + - q1.getQ2() * q2.getQ2() + - q1.getQ3() * q2.getQ3(); - } - - /** - * Computes the dot-product of the instance by a quaternion. - * - * @param q Quaternion. - * @return the dot product of this instance and {@code q}. - */ - public double dotProduct(final Quaternion q) { - return dotProduct(this, q); - } - - /** - * Computes the norm of the quaternion. - * - * @return the norm. - */ - public double getNorm() { - return Math.sqrt(q0 * q0 + - q1 * q1 + - q2 * q2 + - q3 * q3); - } - - /** - * Computes the normalized quaternion (the versor of the instance). - * The norm of the quaternion must not be zero. - * - * @return a normalized quaternion. - * @throws IllegalArgumentException if the norm of the quaternion is zero. - */ - public Quaternion normalize() { - final double norm = getNorm(); - - if (norm < SAFE_MIN) { - throw new IllegalStateException(zeroNormException); - } - - return new Quaternion(q0 / norm, - q1 / norm, - q2 / norm, - q3 / norm); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean equals(Object other) { - if (this == other) { - return true; - } - if (other instanceof Quaternion) { - final Quaternion q = (Quaternion) other; - return q0 == q.getQ0() && - q1 == q.getQ1() && - q2 == q.getQ2() && - q3 == q.getQ3(); - } - - return false; - } - - /** - * {@inheritDoc} - */ - @Override - public int hashCode() { - // "Effective Java" (second edition, p. 47). - int result = 17; - for (double comp : new double[] { q0, q1, q2, q3 }) { - final int c = new Double(comp).hashCode(); - result = 31 * result + c; - } - return result; - } - - /** - * Checks whether this instance is equal to another quaternion - * within a given tolerance. - * - * @param q Quaternion with which to compare the current quaternion. - * @param eps Tolerance. - * @return {@code true} if the each of the components are equal - * within the allowed absolute error. - */ - public boolean equals(final Quaternion q, - final double eps) { - return Precision.equals(q0, q.getQ0(), eps) && - Precision.equals(q1, q.getQ1(), eps) && - Precision.equals(q2, q.getQ2(), eps) && - Precision.equals(q3, q.getQ3(), eps); - } - - /** - * Checks whether the instance is a unit quaternion within a given - * tolerance. - * - * @param eps Tolerance (absolute error). - * @return {@code true} if the norm is 1 within the given tolerance, - * {@code false} otherwise - */ - public boolean isUnitQuaternion(double eps) { - return Precision.equals(getNorm(), 1d, eps); - } - - /** - * Checks whether the instance is a pure quaternion within a given - * tolerance. - * - * @param eps Tolerance (absolute error). - * @return {@code true} if the scalar part of the quaternion is zero. - */ - public boolean isPureQuaternion(double eps) { - return Math.abs(getQ0()) <= eps; - } - - /** - * Returns the polar form of the quaternion. - * - * @return the unit quaternion with positive scalar part. - */ - public Quaternion getPositivePolarForm() { - if (getQ0() < 0) { - final Quaternion unitQ = normalize(); - // The quaternion of rotation (normalized quaternion) q and -q - // are equivalent (i.e. represent the same rotation). - return new Quaternion(-unitQ.getQ0(), - -unitQ.getQ1(), - -unitQ.getQ2(), - -unitQ.getQ3()); - } else { - return this.normalize(); - } - } - - /** - * Returns the inverse of this instance. - * The norm of the quaternion must not be zero. - * - * @return the inverse. - * @throws IllegalArgumentException if the norm (squared) of the quaternion is zero. - */ - public Quaternion getInverse() { - final double squareNorm = q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3; - if (squareNorm < SAFE_MIN) { - throw new IllegalStateException("Norm of the Quarterion is zero"); - } - - return new Quaternion(q0 / squareNorm, - -q1 / squareNorm, - -q2 / squareNorm, - -q3 / squareNorm); - } - - /** - * Gets the first component of the quaternion (scalar part). - * - * @return the scalar part. - */ - public double getQ0() { - return q0; - } - - /** - * Gets the second component of the quaternion (first component - * of the vector part). - * - * @return the first component of the vector part. - */ - public double getQ1() { - return q1; - } - - /** - * Gets the third component of the quaternion (second component - * of the vector part). - * - * @return the second component of the vector part. - */ - public double getQ2() { - return q2; - } - - /** - * Gets the fourth component of the quaternion (third component - * of the vector part). - * - * @return the third component of the vector part. - */ - public double getQ3() { - return q3; - } - - /** - * Gets the scalar part of the quaternion. - * - * @return the scalar part. - * @see #getQ0() - */ - public double getScalarPart() { - return getQ0(); - } - - /** - * Gets the three components of the vector part of the quaternion. - * - * @return the vector part. - * @see #getQ1() - * @see #getQ2() - * @see #getQ3() - */ - public double[] getVectorPart() { - return new double[] { getQ1(), getQ2(), getQ3() }; - } - - /** - * Multiplies the instance by a scalar. - * - * @param alpha Scalar factor. - * @return a scaled quaternion. - */ - public Quaternion multiply(final double alpha) { - return new Quaternion(alpha * q0, - alpha * q1, - alpha * q2, - alpha * q3); - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - final String sp = " "; - final StringBuilder s = new StringBuilder(); - s.append("[") - .append(q0).append(sp) - .append(q1).append(sp) - .append(q2).append(sp) - .append(q3) - .append("]"); - - return s.toString(); - } - -}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/main/java/org/apache/commons/complex/RootsOfUnity.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/complex/RootsOfUnity.java b/src/main/java/org/apache/commons/complex/RootsOfUnity.java deleted file mode 100644 index 7a06a69..0000000 --- a/src/main/java/org/apache/commons/complex/RootsOfUnity.java +++ /dev/null @@ -1,116 +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.complex; - -/** - * Computation of the {@code n}-th roots of unity. - */ -public class RootsOfUnity { - /** 2 * π */ - private static final double TWO_PI = 2 * Math.PI; - /** Number of roots of unity. */ - private final int omegaCount; - /** The roots. */ - private final Complex[] omega; - /** - * {@code true} if the constructor was called with a positive - * value of its argument {@code n}. - */ - private final boolean isCounterClockwise; - - /** - * Computes the {@code n}-th roots of unity. - * - * The roots are stored in an array - * {@code omega[]}, such that {@code omega[k] = w ^ k}, where - * {@code k = 0, ..., n - 1}, {@code w = exp(2 * pi * i / n)} and - * {@code i = sqrt(-1)}. - * - * <p>{@code n} can be positive of negative ({@code abs(n)} is always - * the number of roots of unity):</p> - * <ul> - * <li>If {@code n > 0}, then the roots are stored in counter-clockwise order.</li> - * <li>If {@code n < 0}, then the roots are stored in clockwise order.</li> - * </ul> - * - * @param n The (signed) number of roots of unity to be computed. - * @throws IllegalArgumentException if {@code n == 0}? - */ - public RootsOfUnity(int n) { - if (n == 0) { - throw new IllegalArgumentException("Zero-th root"); - } - - omegaCount = Math.abs(n); - isCounterClockwise = n > 0; - - omega = new Complex[omegaCount]; - final double t = TWO_PI / omegaCount; - final double cosT = Math.cos(t); - final double sinT = Math.sin(t); - - double previousReal = 1; - double previousImag = 0; - omega[0] = new Complex(previousReal, previousImag); - for (int i = 1; i < omegaCount; i++) { - final double real = previousReal * cosT - previousImag * sinT; - final double imag = previousReal * sinT + previousImag * cosT; - - omega[i] = isCounterClockwise ? - new Complex(real, imag) : - new Complex(real, -imag); - - previousReal = real; - previousImag = imag; - } - } - - /** - * Returns {@code true} if {@link #RootsOfUnity(int)} was called with a - * positive value of its argument {@code n}. - * If {@code true}, then the imaginary parts of the successive roots are - * {@link #getRoot(int) returned} in counter-clockwise order. - * - * @return {@code true} if the roots of unity are stored in - * counter-clockwise order. - */ - public boolean isCounterClockwise() { - return isCounterClockwise; - } - - /** - * Gets the {@code k}-th among the computed roots of unity. - * - * @param k Index of the requested value. - * @return the {@code k}-th among the {@code N}-th root of unity - * where {@code N} is the absolute value of the argument passed - * to the constructor. - * @throws IndexOutOfBoundsException if {@code k} is out of range. - */ - public Complex getRoot(int k) { - return omega[k]; - } - - /** - * Gets the number of roots of unity. - * - * @return the number of roots of unity. - */ - public int getNumberOfRoots() { - return omegaCount; - } -} http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/main/java/org/apache/commons/complex/package-info.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/complex/package-info.java b/src/main/java/org/apache/commons/complex/package-info.java deleted file mode 100644 index 88a0f54..0000000 --- a/src/main/java/org/apache/commons/complex/package-info.java +++ /dev/null @@ -1,23 +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. - */ -/** - * - * Complex number type and implementations of complex transcendental - * functions. - * - */ -package org.apache.commons.complex; http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/ChiSquareDistributionTestCases.R ---------------------------------------------------------------------- diff --git a/src/test/R/ChiSquareDistributionTestCases.R b/src/test/R/ChiSquareDistributionTestCases.R deleted file mode 100644 index 3df58ba..0000000 --- a/src/test/R/ChiSquareDistributionTestCases.R +++ /dev/null @@ -1,97 +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. -# -#------------------------------------------------------------------------------ -# R source file to validate ChiSquare distribution tests in -# org.apache.commons.math.distribution.ChiSquareDistributionTest -# -# 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>") -# -#----------------------------------------------------------------------------- -tol <- 1E-9 - -# Function definitions -source("testFunctions") # utility test functions - -# function to verify distribution computations -verifyDistribution <- function(points, expected, df, tol) { - rDistValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDistValues[i] <- pchisq(point, df, log = FALSE) - } - output <- c("Distribution test df = ", df) - 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, df, tol) { - rDensityValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDensityValues[i] <- dchisq(point, df, log = FALSE) - } - output <- c("Density test df = ", df) - if (assertEquals(expected, rDensityValues, tol, "Density Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -# function to verify quantiles -verifyQuantiles <- function(points, expected, df, tol) { - rQuantileValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rQuantileValues[i] <- qchisq(point, df, log = FALSE) - } - output <- c("Quantile test df = ", df) - if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -#-------------------------------------------------------------------------- -cat("ChiSquare Distribution test cases\n") - -df <- 5 -distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) -densityValues <- c(0.0115379817652, 0.0415948507811, 0.0665060119842, 0.0919455953114, 0.121472591024, - 0.000433630076361, 0.00412780610309, 0.00999340341045, 0.0193246438937, 0.0368460089216) -distributionPoints <- c(0.210212602629, 0.554298076728, 0.831211613487, 1.14547622606, 1.61030798696, - 20.5150056524, 15.0862724694, 12.8325019940, 11.0704976935, 9.23635689978) -verifyQuantiles(distributionValues, distributionPoints, df, tol) -verifyDistribution(distributionPoints, distributionValues, df, tol) -verifyDensity(distributionPoints, densityValues, df, tol) - -df <- .1 -distributionPoints <- c(1.16892641146e-60, 1.16892641146e-40, 1.06313237798e-32, 1.11477509638e-26, 1.16892641146e-20, - 5.47291719746, 2.17525480018, 1.13434752351, 0.531864604852, 0.152634227818) -verifyQuantiles(distributionValues, distributionPoints, df, tol) -verifyDistribution(distributionPoints, distributionValues, df, tol) - -displayDashes(WIDTH) http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/FDistributionTestCases.R ---------------------------------------------------------------------- diff --git a/src/test/R/FDistributionTestCases.R b/src/test/R/FDistributionTestCases.R deleted file mode 100644 index ee7c199..0000000 --- a/src/test/R/FDistributionTestCases.R +++ /dev/null @@ -1,92 +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. -# -#------------------------------------------------------------------------------ -# R source file to validate F distribution tests in -# org.apache.commons.math.distribution.TDistributionTest -# -# 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>") -# -#----------------------------------------------------------------------------- -tol <- 1E-9 - -# Function definitions -source("testFunctions") # utility test functions - -# function to verify distribution computations -verifyDistribution <- function(points, expected, numeratorDf, denominatorDf, tol) { - rDistValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDistValues[i] <- pf(point, numeratorDf, denominatorDf, log = FALSE) - } - output <- c("Distribution test numerator df = ", numeratorDf, " denominator df = ", denominatorDf) - 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, numeratorDf, denominatorDf, tol) { - rDensityValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDensityValues[i] <- df(point, numeratorDf, denominatorDf, log = FALSE) - } - output <- c("Density test numerator df = ", numeratorDf, " denominator df = ", denominatorDf) - if (assertEquals(expected, rDensityValues, tol, "Density Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -# function to verify quantiles -verifyQuantiles <- function(points, expected, numeratorDf, denominatorDf, tol) { - rQuantileValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rQuantileValues[i] <- qf(point, numeratorDf, denominatorDf, log = FALSE) - } - output <- c("Quantile test numerator df = ", numeratorDf, " denominator df = ", denominatorDf) - if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -#-------------------------------------------------------------------------- -cat("F Distribution test cases\n") - -numeratorDf <- 5 -denominatorDf <- 6 -distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) -densityValues <- c(0.0689156576706, 0.236735653193, 0.364074131941, 0.481570789649, 0.595880479994, - 0.000133443915657, 0.00286681303403, 0.00969192007502, 0.0242883861471, 0.0605491314658) -distributionPoints <- c(0.0346808448626, 0.0937009113303, 0.143313661184, 0.202008445998, 0.293728320107, - 20.8026639595, 8.74589525602, 5.98756512605, 4.38737418741, 3.10751166664) -verifyQuantiles(distributionValues, distributionPoints, numeratorDf, denominatorDf, tol) -verifyDistribution(distributionPoints, distributionValues, numeratorDf, denominatorDf, tol) -verifyDensity(distributionPoints, densityValues, numeratorDf, denominatorDf, tol) - -displayDashes(WIDTH) http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/GammaDistributionTestCases.R ---------------------------------------------------------------------- diff --git a/src/test/R/GammaDistributionTestCases.R b/src/test/R/GammaDistributionTestCases.R deleted file mode 100644 index 2ed7c95..0000000 --- a/src/test/R/GammaDistributionTestCases.R +++ /dev/null @@ -1,92 +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. -# -#------------------------------------------------------------------------------ -# R source file to validate Gamma distribution tests in -# org.apache.commons.math.distribution.GammaDistributionTest -# -# 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>") -# -#----------------------------------------------------------------------------- -tol <- 1E-9 - -# Function definitions -source("testFunctions") # utility test functions - -# function to verify distribution computations -verifyDistribution <- function(points, expected, alpha, beta, tol) { - rDistValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDistValues[i] <- pgamma(point, shape=alpha, scale=beta, log = FALSE) - } - output <- c("Distribution test shape = ", shape, " scale = ", scale) - 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, alpha, beta, tol) { - rDensityValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDensityValues[i] <- dgamma(point, shape=alpha, scale=beta, log = FALSE) - } - output <- c("Density test shape = ", shape, " scale = ", scale) - if (assertEquals(expected, rDensityValues, tol, "Density Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -# function to verify quantiles -verifyQuantiles <- function(points, expected, alpha, beta, tol) { - rQuantileValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rQuantileValues[i] <- qgamma(point, shape=alpha, scale=beta, log = FALSE) - } - output <- c("Quantile test shape = ", shape, " scale = ", scale) - if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -#-------------------------------------------------------------------------- -cat("Gamma Distribution test cases\n") - -shape <- 4 -scale <- 2 -distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) -densityValues <- c(0.00427280075546, 0.0204117166709, 0.0362756163658, 0.0542113174239, 0.0773195272491, - 0.000394468852816, 0.00366559696761, 0.00874649473311, 0.0166712508128, 0.0311798227954) -distributionPoints <- c(0.857104827257, 1.64649737269, 2.17973074725, 2.7326367935, 3.48953912565, - 26.1244815584, 20.0902350297, 17.5345461395, 15.5073130559, 13.3615661365) -verifyQuantiles(distributionValues, distributionPoints, shape, scale, tol) -verifyDistribution(distributionPoints, distributionValues, shape, scale, tol) -verifyDensity(distributionPoints, densityValues, shape, scale, tol) - -displayDashes(WIDTH) http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/KolmogorovSmirnovDistributionTestCases.R ---------------------------------------------------------------------- diff --git a/src/test/R/KolmogorovSmirnovDistributionTestCases.R b/src/test/R/KolmogorovSmirnovDistributionTestCases.R deleted file mode 100644 index 19f74b7..0000000 --- a/src/test/R/KolmogorovSmirnovDistributionTestCases.R +++ /dev/null @@ -1,37 +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. -# -cat("/* ", version$version.string, " */\n\n\n", sep = "") - -ns <- c(200, 341, 389) -ps <- c(0.005, 0.02, 0.031111, 0.04) - -for (n in ns) { - for (p in ps) { - res <- .C("pkolmogorov2x", p = as.double(p), n = as.integer(n), PACKAGE = "stats")$p - - cat("/* formatC(.C(\"pkolmogorov2x\", p = as.double(", p, "), n = as.integer(", n, "), PACKAGE = \"stats\")$p, 40) gives\n", sep = "") - cat(" * ", formatC(res, digits = 40), "\n", sep = "") - cat(" */\n") - - cat("dist = new KolmogorovSmirnovDistributionImpl(", n, ");\n", sep = "") - #cat("Assert.assertEquals(", formatC(res, digits = 40), ", dist.cdf(", p, ", true), TOLERANCE);\n", sep = "") - cat("Assert.assertEquals(", formatC(res, digits = 40), ", dist.cdf(", p, ", false), TOLERANCE);\n", sep = "") - cat("\n") - - #cat("System.out.println(\"", formatC(res, digits = 20), " - \" + dist.cdf(", p, ", false) + \" = \" + (", res, " - dist.cdf(", p, ", false)));\n", sep = "") - } -} - http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/KolmogorovSmirnovTestCases.R ---------------------------------------------------------------------- diff --git a/src/test/R/KolmogorovSmirnovTestCases.R b/src/test/R/KolmogorovSmirnovTestCases.R deleted file mode 100644 index 19b3c13..0000000 --- a/src/test/R/KolmogorovSmirnovTestCases.R +++ /dev/null @@ -1,208 +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. -# -#------------------------------------------------------------------------------ -# R source file to validate KolmogorovSmirnov tests in -# org.apache.commons.complex.stat.inference.KolmogorovSmirnovTest -# -# 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>") -# -# NOTE: the 2-sample bootstrap test requires the "Matching" library -## https://cran.r-project.org/web/packages/Matching/index.html -## See http://sekhon.berkeley.edu/matching for additional documentation. -## Jasjeet S. Sekhon. 2011. ``Multivariate and Propensity Score Matching -## Software with Automated Balance Optimization: The Matching package for R.'' -## Journal of Statistical Software, 42(7): 1-52. -# -#------------------------------------------------------------------------------ -tol <- 1E-14 # error tolerance for tests -#------------------------------------------------------------------------------ -# Function definitions - -source("testFunctions") # utility test functions -require("Matching") # for ks.boot - -verifyOneSampleGaussian <- function(data, expectedP, expectedD, mean, sigma, exact, tol, desc) { - results <- ks.test(data, "pnorm", mean, sigma, exact = exact) - if (assertEquals(expectedP, results$p.value, tol, "p-value")) { - displayPadded(c(desc," p-value test"), SUCCEEDED, WIDTH) - } else { - displayPadded(c(desc, " p-value test"), FAILED, WIDTH) - } - if (assertEquals(expectedD, results$statistic, tol, "D statistic value")) { - displayPadded(c(desc," D statistic test"), SUCCEEDED, WIDTH) - } else { - displayPadded(c(desc, " D statistic test"), FAILED, WIDTH) - } -} - -verifyOneSampleUniform <- function(data, expectedP, expectedD, min, max, exact, tol, desc) { - results <- ks.test(data, "punif", min, max, exact = exact) - if (assertEquals(expectedP, results$p.value, tol, "p-value")) { - displayPadded(c(desc," p-value test"), SUCCEEDED, WIDTH) - } else { - displayPadded(c(desc, " p-value test"), FAILED, WIDTH) - } - if (assertEquals(expectedD, results$statistic, tol, "D statistic value")) { - displayPadded(c(desc," D statistic test"), SUCCEEDED, WIDTH) - } else { - displayPadded(c(desc, " D statistic test"), FAILED, WIDTH) - } -} - -verifyTwoSampleLargeSamples <- function(sample1, sample2, expectedP, expectedD, tol, desc) { - results <- ks.test(sample1, sample2) - if (assertEquals(expectedP, results$p.value, tol, "p-value")) { - displayPadded(c(desc," p-value test"), SUCCEEDED, WIDTH) - } else { - displayPadded(c(desc, " p-value test"), FAILED, WIDTH) - } - if (assertEquals(expectedD, results$statistic, tol, "D statistic value")) { - displayPadded(c(desc," D statistic test"), SUCCEEDED, WIDTH) - } else { - displayPadded(c(desc, " D statistic test"), FAILED, WIDTH) - } -} - -verifyTwoSampleSmallSamplesExact <- function(sample1, sample2, expectedP, expectedD, tol, desc) { - results <- ks.test(sample1, sample2, exact = TRUE) - if (assertEquals(expectedP, results$p.value, tol, "p-value")) { - displayPadded(c(desc," p-value test"), SUCCEEDED, WIDTH) - } else { - displayPadded(c(desc, " p-value test"), FAILED, WIDTH) - } - if (assertEquals(expectedD, results$statistic, tol, "D statistic value")) { - displayPadded(c(desc," D statistic test"), SUCCEEDED, WIDTH) - } else { - displayPadded(c(desc, " D statistic test"), FAILED, WIDTH) - } -} - -verifyTwoSampleBootstrap <- function(sample1, sample2, expectedP, tol, desc) { - results <- ks.boot(sample1, sample2,nboots=10000 ) - if (assertEquals(expectedP, results$ks.boot.pvalue, tol, "p-value")) { - displayPadded(c(desc, " p-value test"), SUCCEEDED, WIDTH) - } else { - displayPadded(c(desc, " p-value test"), FAILED, WIDTH) - } -} - -cat("KolmogorovSmirnovTest test cases\n") - -gaussian <- c(0.26055895, -0.63665233, 1.51221323, 0.61246988, -0.03013003, -1.73025682, - -0.51435805, 0.70494168, 0.18242945, 0.94734336, -0.04286604, -0.37931719, -1.07026403, -2.05861425, - 0.11201862, 0.71400136, -0.52122185, -0.02478725, -1.86811649, -1.79907688, 0.15046279, 1.32390193, - 1.55889719, 1.83149171, -0.03948003, -0.98579207, -0.76790540, 0.89080682, 0.19532153, 0.40692841, - 0.15047336, -0.58546562, -0.39865469, 0.77604271, -0.65188221, -1.80368554, 0.65273365, -0.75283102, - -1.91022150, -0.07640869, -1.08681188, -0.89270600, 2.09017508, 0.43907981, 0.10744033, -0.70961218, - 1.15707300, 0.44560525, -2.04593349, 0.53816843, -0.08366640, 0.24652218, 1.80549401, -0.99220707, - -1.14589408, -0.27170290, -0.49696855, 0.00968353, -1.87113545, -1.91116529, 0.97151891, -0.73576115, - -0.59437029, 0.72148436, 0.01747695, -0.62601157, -1.00971538, -1.42691397, 1.03250131, -0.30672627, - -0.15353992, -1.19976069, -0.68364218, 0.37525652, -0.46592881, -0.52116168, -0.17162202, 1.04679215, - 0.25165971, -0.04125231, -0.23756244, -0.93389975, 0.75551407, 0.08347445, -0.27482228, -0.4717632, - -0.1867746, -0.1166976, 0.5763333, 0.1307952, 0.7630584, -0.3616248, 2.1383790,-0.7946630, - 0.0231885, 0.7919195, 1.6057144, -0.3802508, 0.1229078, 1.5252901, -0.8543149, 0.3025040) - -shortGaussian <- gaussian[1:50] - -gaussian2 <- c(2.88041498038308, -0.632349445671017, 0.402121295225571, 0.692626364613243, 1.30693446815426, - -0.714176317131286, -0.233169206599583, 1.09113298322107, -1.53149079994305, 1.23259966205809, - 1.01389927412503, 0.0143898711497477, -0.512813545447559, 2.79364360835469, 0.662008875538092, - 1.04861546834788, -0.321280099931466, 0.250296656278743, 1.75820367603736, -2.31433523590905, - -0.462694696086403, 0.187725700950191, -2.24410950019152, 2.83473751105445, 0.252460174391016, - 1.39051945380281, -1.56270144203134, 0.998522814471644, -1.50147469080896, 0.145307533554146, - 0.469089457043406, -0.0914780723809334, -0.123446939266548, -0.610513388160565, -3.71548343891957, - -0.329577317349478, -0.312973794075871, 2.02051909758923, 2.85214308266271, 0.0193222002327237, - -0.0322422268266562, 0.514736012106768, 0.231484953375887, -2.22468798953629, 1.42197716075595, - 2.69988043856357, 0.0443757119128293, 0.721536984407798, -0.0445688839903234, -0.294372724550705, - 0.234041580912698, -0.868973119365727, 1.3524893453845, -0.931054600134503, -0.263514296006792, - 0.540949457402918, -0.882544288773685, -0.34148675747989, 1.56664494810034, 2.19850536566584, - -0.667972122928022, -0.70889669526203, -0.00251758193079668, 2.39527162977682, -2.7559594317269, - -0.547393502656671, -2.62144031572617, 2.81504147017922, -1.02036850201042, -1.00713927602786, - -0.520197775122254, 1.00625480138649, 2.46756916531313, 1.64364743727799, 0.704545210648595, - -0.425885789416992, -1.78387854908546, -0.286783886710481, 0.404183648369076, -0.369324280845769, - -0.0391185138840443, 2.41257787857293, 2.49744281317859, -0.826964496939021, -0.792555379958975, - 1.81097685787403, -0.475014580016638, 1.23387615291805, 0.646615294802053, 1.88496377454523, 1.20390698380814, - -0.27812153371728, 2.50149494533101, 0.406964323253817, -1.72253451309982, 1.98432494184332, 2.2223658560333, - 0.393086362404685, -0.504073151377089, -0.0484610869883821) - -uniform <- c(0.7930305, 0.6424382, 0.8747699, 0.7156518, 0.1845909, 0.2022326, - 0.4877206, 0.8928752, 0.2293062, 0.4222006, 0.1610459, 0.2830535, 0.9946345, 0.7329499, - 0.26411126, 0.87958133, 0.29827437, 0.39185988, 0.38351185, 0.36359611, 0.48646472, 0.05577866, - 0.56152250, 0.52672013, 0.13171783, 0.95864085, 0.03060207, 0.33514887, 0.72508148, 0.38901437, - 0.9978665, 0.5981300, 0.1065388, 0.7036991, 0.1071584, 0.4423963, 0.1107071, 0.6437221, - 0.58523872, 0.05044634, 0.65999539, 0.37367260, 0.73270024, 0.47473755, 0.74661163, 0.50765549, - 0.05377347, 0.40998009, 0.55235182, 0.21361998, 0.63117971, 0.18109222, 0.89153510, 0.23203248, - 0.6177106, 0.6856418, 0.2158557, 0.9870501, 0.2036914, 0.2100311, 0.9065020, 0.7459159, - 0.56631790, 0.06753629, 0.39684629, 0.52504615, 0.14199103, 0.78551120, 0.90503321, 0.80452362, - 0.9960115, 0.8172592, 0.5831134, 0.8794187, 0.2021501, 0.2923505, 0.9561824, 0.8792248, - 0.85201008, 0.02945562, 0.26200374, 0.11382818, 0.17238856, 0.36449473, 0.69688273, 0.96216330, - 0.4859432,0.4503438, 0.1917656, 0.8357845, 0.9957812, 0.4633570, 0.8654599, 0.4597996, - 0.68190289, 0.58887855, 0.09359396, 0.98081979, 0.73659533, 0.89344777, 0.18903099, 0.97660425) - -smallSample1 <- c(6, 7, 9, 13, 19, 21, 22, 23, 24) -smallSample2 <- c(10, 11, 12, 16, 20, 27, 28, 32, 44, 54) -smallSample3 <- c(6, 7, 9, 13, 19, 21, 22, 23, 24, 29, 30, 34, 36, 41, 45, 47, 51, 63, 33, 91) -smallSample4 <- c(10, 11, 12, 16, 20, 27, 28, 32, 44, 54, 56, 57, 64, 69, 71, 80, 81, 88, 90) -smallSample5 <- c(-10, -5, 17, 21, 22, 23, 24, 30, 44, 50, 56, 57, 59, 67, 73, 75, 77, 78, 79, 80, 81, 83, 84, 85, 88, 90, - 92, 93, 94, 95, 98, 100, 101, 103, 105, 110) -smallSample6 <- c(-2, -1, 0, 10, 14, 15, 16, 20, 25, 26, 27, 31, 32, 33, 34, 45, 47, 48, 51, 52, 53, 54, 60, 61, 62, 63, - 74, 82, 106, 107, 109, 11, 112, 113, 114) -bootSample1 <- c(0, 2, 4, 6, 8, 8, 10, 15, 22, 30, 33, 36, 38) -bootSample2 <- c(9, 17, 20, 33, 40, 51, 60, 60, 72, 90, 101) -roundingSample1 <- c(2,4,6,8,9,10,11,12,13) -roundingSample2 <- c(0,1,3,5,7) - -shortUniform <- uniform[1:20] - -verifyOneSampleGaussian(gaussian, 0.3172069207622391, 0.0932947561266756, 0, 1, -TRUE, tol, "One sample gaussian - gaussian values") - -verifyOneSampleGaussian(shortGaussian, 0.683736463728347, 0.09820779969463278, 0, 1, -TRUE, tol, "One sample gaussian - gaussian values - small sample") - -verifyOneSampleGaussian(uniform, 8.881784197001252E-16, 0.5117493931609258, 0, 1, -TRUE, tol, "One sample gaussian - uniform values") - -verifyOneSampleUniform(uniform, 8.881784197001252E-16, 0.5400666982352942, -0.5, 0.5, -TRUE, tol, "One sample uniform - uniform values") - -verifyOneSampleUniform(shortUniform, 4.117594598618268E-9, 0.6610459, -0.5, 0.5, -TRUE, tol, "One sample uniform - uniform values - small sample") - -verifyOneSampleUniform(gaussian, 4.9405812774239166E-11, 0.3401058049019608, -0.5, 0.5, -TRUE, tol, "One sample uniform - unit normal values") - -verifyTwoSampleLargeSamples(gaussian, gaussian2, 0.0319983962391632, 0.202352941176471, tol, -"Two sample N(0, 1) vs N(0, 1.6)") - -verifyTwoSampleSmallSamplesExact(smallSample1, smallSample2, 0.105577085453247, .5, tol, -"Two sample small samples exact 1") - -verifyTwoSampleSmallSamplesExact(smallSample3, smallSample4, 0.046298660942952, 0.426315789473684, tol, -"Two sample small samples exact 2") - -verifyTwoSampleSmallSamplesExact(smallSample5, smallSample6, 0.00300743602233366, 0.41031746031746, tol, -"Two sample small samples exact 3") - -verifyTwoSampleBootstrap(bootSample1, bootSample2, 0.0059, 1E-3, "Two sample bootstrap - isolated failures possible") -verifyTwoSampleBootstrap(gaussian, gaussian2, 0.0237, 1E-2, "Two sample bootstrap - isolated failures possible") -verifyTwoSampleBootstrap(roundingSample1, roundingSample2, 0.06303, 1E-2, "Two sample bootstrap - isolated failures possible") - -displayDashes(WIDTH) - - http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/LevyDistributionTestCases.R ---------------------------------------------------------------------- diff --git a/src/test/R/LevyDistributionTestCases.R b/src/test/R/LevyDistributionTestCases.R deleted file mode 100644 index e1300a7..0000000 --- a/src/test/R/LevyDistributionTestCases.R +++ /dev/null @@ -1,88 +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. -# -#------------------------------------------------------------------------------ -# R source file to validate Lévy distribution tests in -# org.apache.commons.complex.distribution.LevyDistributionTest -# -# 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 -# dlevy(q, m=0, s=1, log=FALSE) -# plevy(q, m=0, s=1) -# qlevy(p, m=0, s=1) -#----------------------------------------------------------------------------- -tol <- 1E-9 - -# Function definitions - -source("testFunctions") # utility test functions - -# 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] <- plevy(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] <- dlevy(point, m, s, log=FALSE) - } - 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("Levy test cases\n") - -m <- 1.2 -s <- 0.4 -distributionPoints <- c(1.2001, 1.21, 1.225, 1.25, 1.3, 1.9, 3.4, 5.6) -densityValues <- c(0.0, 5.200563737654472E-7, 0.021412836122382383, 0.4133397070818418, 1.0798193302637613, 0.3237493191610873, 0.07060325500936372, 0.026122839883975738) -distributionValues <- c(0.0, 2.539628589470901E-10, 6.334248366624259E-5, 0.004677734981047284, 0.04550026389635843, 0.4496917979688907, 0.6698153575994166, 0.763024600552995) -verifyDistribution(distributionPoints, distributionValues, m, s, tol) -verifyDensity(distributionPoints, densityValues, m, s, tol) - -m <- 5 -s <- 1.3 -distributionPoints <- c(5.0001, 6, 7, 8, 9, 10, 11, 12, 13, 14) -densityValues <- c(0.0, 0.23745992633364185, 0.1161959636020616, 0.07048597672583455, 0.04833023442399538, 0.03572468867742048, 0.02777194506550441, 0.022382435270909086, 0.018533623436073274, 0.0156730047506865) -distributionValues <- c(0.0, 0.25421322360396437, 0.42011267955064, 0.5103578488686935, 0.5686182086635944, 0.6101201547975077, 0.6415915735304425, 0.6665077778509312, 0.6868651803414656, 0.7039020091632311) -verifyDistribution(distributionPoints, distributionValues, m, s, tol) -verifyDensity(distributionPoints, densityValues, m, s, tol) - -displayDashes(WIDTH) http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/README.txt ---------------------------------------------------------------------- diff --git a/src/test/R/README.txt b/src/test/R/README.txt deleted file mode 100644 index eb83ecc..0000000 --- a/src/test/R/README.txt +++ /dev/null @@ -1,168 +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. -# -#------------------------------------------------------------------------------ - -INTRODUCTION - -The purpose of the R programs included in this directory is to validate -the target values used in Apache commons math unit tests. Success running the -R and commons-math tests on a platform (OS and R version) means that R and -commons-math give results for the test cases that are close in value. The -tests include configurable tolerance levels; but care must be taken in changing -these, since in most cases the pre-set tolerance is close to the number of -decimal digits used in expressing the expected values (both here and in the -corresponding commons-math unit tests). - -Of course it is always possible that both R and commons-math give incorrect -values for test cases, so these tests should not be interpreted as definitive -in any absolute sense. The value of developing and running the tests is really -to generate questions (and answers!) when the two systems give different -results. - -Contributions of additional test cases (both R and Junit code) or just -R programs to validate commons-math tests that are not covered here would be -greatly appreciated. - -SETUP - -0) Download and install R. You can get R here -http://www.r-project.org/ -Follow the install instructions and make sure that you can launch R from this -(i.e., either explitly add R to your OS path or let the install package do it -for you). - -1) Launch R from this directory and type -> source("testAll") -to an R prompt. This should produce output to the console similar to this: - -Binomial test cases -Density test n = 10, p = 0.7...........................................SUCCEEDED -Distribution test n = 10, p = 0.7......................................SUCCEEDED -Inverse Distribution test n = 10, p = 0.7..............................SUCCEEDED -Density test n = 5, p = 0..............................................SUCCEEDED -Distribution test n = 5, p = 0.........................................SUCCEEDED -Density test n = 5, p = 1..............................................SUCCEEDED -Distribution test n = 5, p = 1.........................................SUCCEEDED --------------------------------------------------------------------------------- -Normal test cases -Distribution test mu = 2.1, sigma = 1.4................................SUCCEEDED -Distribution test mu = 2.1, sigma = 1.4................................SUCCEEDED -Distribution test mu = 0, sigma = 1....................................SUCCEEDED -Distribution test mu = 0, sigma = 0.1..................................SUCCEEDED --------------------------------------------------------------------------------- -... -<more test reports> - - -WORKING WITH THE TESTS - -The R distribution comes with online manuals that you can view by launching -a browser instance and then entering - -> help.start() - -at an R prompt. Poking about in the test case files and the online docs should -bring you up to speed fairly quickly. Here are some basic things to get -you started. I should note at this point that I am by no means an expert R -programmer, so some things may not be implemented in the the nicest way. -Comments / suggestions for improvement are welcome! - -All of the test cases use some basic functions and global constants (screen -width and success / failure strings) defined in "testFunctions." The -R "source" function is used to "import" these functions into each of the test -programs. The "testAll" program pulls together and executes all of the -individual test programs. You can execute any one of them by just entering - -> source(<program-name>). - -The "assertEquals" function in the testFunctions file mimics the similarly -named function used by Junit: - -assertEquals <- function(expected, observed, tol, message) { - if(any(abs(expected - observed) > tol)) { - cat("FAILURE: ",message,"\n") - cat("EXPECTED: ",expected,"\n") - cat("OBSERVED: ",observed,"\n") - return(0) - } else { - return(1) - } -} - -The <expected> and <observed> arguments can be scalar values, vectors or -matrices. If the arguments are vectors or matrices, corresponding entries -are compared. - -The standard pattern used throughout the tests looks like this (from -binomialTestCases): - -Start by defining a "verification function" -- in this example a function to -verify computation of binomial probabilities. The <points> argument is a vector -of integer values to feed into the density function, <expected> is a vector of -the computed probabilies from the commons-math Junit tests, <n> and <p> are -parameters of the distribution and <tol> is the error tolerance of the test. -The function computes the probabilities using R and compares the values that -R produces with those in the <expected> vector. - -verifyDensity <- function(points, expected, n, p, tol) { - rDensityValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDensityValues[i] <- dbinom(point, n, p, log = FALSE) - } - output <- c("Density test n = ", n, ", p = ", p) - if (assertEquals(expected,rDensityValues,tol,"Density Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -The displayPadded function just displays its first and second arguments with -enough dots in between to make the whole string WIDTH characters long. It is -defined in testFunctions. - -Then call this function with different parameters corresponding to the different -Junit test cases: - -size <- 10.0 -probability <- 0.70 - -densityPoints <- c(-1,0,1,2,3,4,5,6,7,8,9,10,11) -densityValues <- c(0, 0.0000, 0.0001, 0.0014, 0.0090, 0.0368, 0.1029, - 0.2001, 0.2668, 0.2335, 0.1211, 0.0282, 0) -... -verifyDensity(densityPoints, densityValues, size, probability, tol) - -If the values computed by R match the target values in densityValues, this will -produce one line of output to the console: - -Density test n = 10, p = 0.7...........................................SUCCEEDED - -If you modify the value of tol set at the top of binomialTestCases to make the -test more sensitive than the number of digits specified in the densityValues -vector, it will fail, producing the following output, showing the failure and -the expected and observed values: - -FAILURE: Density Values -EXPECTED: 0 0 1e-04 0.0014 0.009 0.0368 0.1029 0.2001 0.2668 0.2335 0.1211 / - 0.0282 0 -OBSERVED: 0 5.9049e-06 0.000137781 0.0014467005 0.009001692 0.036756909 / -0.1029193452 0.200120949 0.266827932 0.2334744405 0.121060821 0.0282475249 0 -Density test n = 10, p = 0.7..............................................FAILED - - http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/TDistributionTestCases.R ---------------------------------------------------------------------- diff --git a/src/test/R/TDistributionTestCases.R b/src/test/R/TDistributionTestCases.R deleted file mode 100644 index 6f91269..0000000 --- a/src/test/R/TDistributionTestCases.R +++ /dev/null @@ -1,100 +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. -# -#------------------------------------------------------------------------------ -# R source file to validate T distribution tests in -# org.apache.commons.math.distribution.TDistributionTest -# -# 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>") -# -#----------------------------------------------------------------------------- -tol <- 1E-9 - -# Function definitions -source("testFunctions") # utility test functions - -# function to verify distribution computations -verifyDistribution <- function(points, expected, df, tol) { - rDistValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDistValues[i] <- pt(point, df, log = FALSE) - } - output <- c("Distribution test df = ", df) - 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, df, tol) { - rDensityValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDensityValues[i] <- dt(point, df, log = FALSE) - } - output <- c("Density test df = ", df) - if (assertEquals(expected, rDensityValues, tol, "Density Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -# function to verify quantiles -verifyQuantiles <- function(points, expected, df, tol) { - rQuantileValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rQuantileValues[i] <- qt(point, df, log = FALSE) - } - output <- c("Quantile test df = ", df) - if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -#-------------------------------------------------------------------------- -cat("T Distribution test cases\n") - -df <- 5 -distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) -densityValues <- c(0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005, - 0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005) -distributionPoints <- c(-5.89342953136, -3.36492999891, -2.57058183564, -2.01504837333, -1.47588404882, - 5.89342953136, 3.36492999891, 2.57058183564, 2.01504837333, 1.47588404882) -verifyQuantiles(distributionValues, distributionPoints, df, tol) -verifyDistribution(distributionPoints, distributionValues, df, tol) -verifyDensity(distributionPoints, densityValues, df, tol) - -df <- 1 -densityValues <- c(3.14158231817e-06, 0.000314055924703, 0.00195946145194, 0.00778959736375, 0.0303958893917, - 3.14158231817e-06, 0.000314055924703, 0.00195946145194, 0.00778959736375, 0.0303958893917) -distributionPoints <- c(-318.308838986, -31.8205159538, -12.7062047362, -6.31375151468, -3.07768353718, - 318.308838986, 31.8205159538, 12.7062047362, 6.31375151468, 3.07768353718) -verifyQuantiles(distributionValues, distributionPoints, df, tol) -verifyDistribution(distributionPoints, distributionValues, df, tol) -verifyDensity(distributionPoints, densityValues, df, tol) - -displayDashes(WIDTH) http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/TTestCases ---------------------------------------------------------------------- diff --git a/src/test/R/TTestCases b/src/test/R/TTestCases deleted file mode 100644 index 38adf7b..0000000 --- a/src/test/R/TTestCases +++ /dev/null @@ -1,106 +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. -# -#------------------------------------------------------------------------------ -# R source file to validate TTest tests in -# org.apache.commons.math.inference.TTestImpl -# -# 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 -# t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), -# mu = 0, paired = FALSE, var.equal = FALSE, ... ) -# Arguments -# x a numeric vector of data values. -# y an optional numeric vector data values. -# alternative a character string specifying the alternative hypothesis, -# must be one of "two.sided" (default), "greater" or "less". You can specify -# just the initial letter. -# mu a number indicating the true value of the mean (or difference in means -# if you are performing a two sample test). -# paired a logical indicating whether you want a paired t-test. -# var.equal a logical variable indicating whether to treat the two -# variances as being equal. -# If TRUE then the pooled variance is used to estimate the variance, -# otherwise the Welch (or Satterthwaite) approximation to the degrees -# of freedom is used. -#------------------------------------------------------------------------------ -tol <- 1E-10 # error tolerance for tests -#------------------------------------------------------------------------------ -# Function definitions -#------------------------------------------------------------------------------ -source("testFunctions") # utility test functions -#------------------------------------------------------------------------------ -# Verification function -# -verifyTest <- function(out,expectedP, expectedT, - tol) { - if (assertEquals(expectedP, out$p.value, tol, - "Ttest p value")) { - displayPadded(output, SUCCEEDED, 80) - } else { - displayPadded(output, FAILED, 80) - } - output <- c("t test test statistic") - if (assertEquals(expectedT, out$statistic, tol, - "Ttest t statistic")) { - displayPadded(output, SUCCEEDED, 80) - } else { - displayPadded(output, FAILED, 80) - } - displayDashes(WIDTH) -} - -cat("One-sample, two-sided TTest test cases \n") -sample1 <- c(93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0, 88.0, - 98.0, 94.0, 101.0, 92.0, 95.0) -out <- t.test(sample1, mu=100.0) -expectedP <- 0.0136390585873 -expectedT<- -2.81976445346 -verifyTest(out,expectedP, expectedT, tol) - -cat("One-sample, one-sided TTest test cases \n") -sample1 <- c(2, 0, 6, 6, 3, 3, 2, 3, -6, 6, 6, 6, 3, 0, 1, 1, 0, 2, 3, 3) -out <- t.test(sample1, mu=0.0, alternative="g") -expectedP <- 0.000521637019637 -expectedT<- 3.86485535541 -verifyTest(out,expectedP, expectedT, tol) - -cat("Homoscedastic TTest test cases \n") -sample1 <- c(2, 4, 6, 8, 10, 97) -sample2 <- c(4, 6, 8, 10, 16) -out <- t.test(sample1,sample2,var.equal = TRUE) -expectedP <- 0.4833963785 -expectedT<- 0.73096310086 -verifyTest(out,expectedP, expectedT, tol) - -cat("Heteroscedastic TTest test cases \n") -sample1 <- c(7, -4, 18, 17, -3, -5, 1, 10, 11, -2) -sample2 <- c(-1, 12, -1, -3, 3, -5, 5, 2, -11, -1, -3) -out <- t.test(sample1,sample2,var.equal = FALSE) -expectedP <- 0.128839369622 -expectedT<- 1.60371728768 -verifyTest(out,expectedP, expectedT, tol) - -cat("Small sample, heteroscedastic test cases \n") -sample1 <- c(1,3) -sample2 <- c(4,5) -out <- t.test(sample1,sample2,var.equal = FALSE) -expectedP <- 0.198727388935 -expectedT<- -2.2360679775 -verifyTest(out,expectedP, expectedT, tol) - http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/WeibullDistributionTestCases.R ---------------------------------------------------------------------- diff --git a/src/test/R/WeibullDistributionTestCases.R b/src/test/R/WeibullDistributionTestCases.R deleted file mode 100644 index 7228641..0000000 --- a/src/test/R/WeibullDistributionTestCases.R +++ /dev/null @@ -1,92 +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. -# -#------------------------------------------------------------------------------ -# R source file to validate Weibull distribution tests in -# org.apache.commons.math.distribution.GammaDistributionTest -# -# 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>") -# -#----------------------------------------------------------------------------- -tol <- 1E-9 - -# Function definitions -source("testFunctions") # utility test functions - -# function to verify distribution computations -verifyDistribution <- function(points, expected, alpha, beta, tol) { - rDistValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDistValues[i] <- pweibull(point, shape=alpha, scale=beta, log = FALSE) - } - output <- c("Distribution test shape = ", shape, " scale = ", scale) - 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, alpha, beta, tol) { - rDensityValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDensityValues[i] <- dweibull(point, shape=alpha, scale=beta, log = FALSE) - } - output <- c("Density test shape = ", shape, " scale = ", scale) - if (assertEquals(expected, rDensityValues, tol, "Density Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -# function to verify quantiles -verifyQuantiles <- function(points, expected, alpha, beta, tol) { - rQuantileValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rQuantileValues[i] <- qweibull(point, shape=alpha, scale=beta, log = FALSE) - } - output <- c("Quantile test shape = ", shape, " scale = ", scale) - if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -#-------------------------------------------------------------------------- -cat("Weibull Distribution test cases\n") - -shape <- 1.2 -scale <- 2.1 -distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) -densityValues <- c(0.180535929306, 0.262801138133, 0.301905425199, 0.330899152971, 0.353441418887, 0.000788590320203, - 0.00737060094841, 0.0177576041516, 0.0343043442574, 0.065664589369) -distributionPoints <- c(0.00664355180993, 0.0454328283309, 0.0981162737374, 0.176713524579, 0.321946865392, - 10.5115496887, 7.4976304671, 6.23205600701, 5.23968436955, 4.20790282578) -verifyQuantiles(distributionValues, distributionPoints, shape, scale, tol) -verifyDistribution(distributionPoints, distributionValues, shape, scale, tol) -verifyDensity(distributionPoints, densityValues, shape, scale, tol) - -displayDashes(WIDTH) http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/anovaTestCases ---------------------------------------------------------------------- diff --git a/src/test/R/anovaTestCases b/src/test/R/anovaTestCases deleted file mode 100644 index 1c1dd7c..0000000 --- a/src/test/R/anovaTestCases +++ /dev/null @@ -1,72 +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. -# -#------------------------------------------------------------------------------ -# R source file to validate Binomial distribution tests in -# org.apache.commons.math.distribution.BinomialDistributionTest -# -# 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 -# anova(model) <- anova -# lm(frame) <- linear model -#------------------------------------------------------------------------------ -tol <- 1E-12 # error tolerance for tests -#------------------------------------------------------------------------------ -# Function definitions - -source("testFunctions") # utility test functions -options(digits=16) # override number of digits displayed - -# function to verify anova computations - -verifyAnova <- function(frame, expectedP, expectedF, frameName) { - a <- anova(lm(frame)) - p <- a$"Pr(>F)"[1] - f <- a$"F value"[1] - output <- c("P-value test frame = ", frameName) - if (assertEquals(expectedP,p,tol,"P value")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } - output <- c("F-value test frame = ", frameName) - if (assertEquals(expectedF,f,tol,"F value")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -#-------------------------------------------------------------------------- -cat("Anova test cases\n") -classA <- c(93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0) -classB <- c(99.0, 92.0, 102.0, 100.0, 102.0, 89.0) -classC <- c(110.0, 115.0, 111.0, 117.0, 128.0, 117.0) - -threeClasses = data.frame(val = c(classA, classB, classC), -class=c(rep("classA", length(classA)), - rep("classB", length(classB)), - rep("classC", length(classC)))) - -verifyAnova(threeClasses,6.959446e-06, 24.67361709460624, "Three classes") - -twoClasses = data.frame(val = c(classA, classB), -class=c(rep("classA", length(classA)), rep("classB", length(classB)))) -verifyAnova(twoClasses, 0.904212960464, 0.0150579150579, "Two classes") - -displayDashes(WIDTH) http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/binomialTestCases ---------------------------------------------------------------------- diff --git a/src/test/R/binomialTestCases b/src/test/R/binomialTestCases deleted file mode 100644 index 144a221..0000000 --- a/src/test/R/binomialTestCases +++ /dev/null @@ -1,127 +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. -# -#------------------------------------------------------------------------------ -# R source file to validate Binomial distribution tests in -# org.apache.commons.math.distribution.BinomialDistributionTest -# -# 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 -# dbinom(x, size, prob, log = FALSE) <- density -# pbinom(q, size, prob, lower.tail = TRUE, log.p = FALSE) <- distribution -# qbinom(p, size, prob, lower.tail = TRUE, log.p = FALSE) <- quantiles -#------------------------------------------------------------------------------ -tol <- 1E-4 # error tolerance for tests -#------------------------------------------------------------------------------ -# Function definitions - -source("testFunctions") # utility test functions - -# function to verify density computations - -verifyDensity <- function(points, expected, n, p, tol) { - rDensityValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDensityValues[i] <- dbinom(point, n, p, log = FALSE) - } - output <- c("Density test n = ", n, ", p = ", p) - if (assertEquals(expected,rDensityValues,tol,"Density Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -# function to verify distribution computations - -verifyDistribution <- function(points, expected, n, p, tol) { - rDistValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDistValues[i] <- pbinom(point, n, p, log = FALSE) - } - output <- c("Distribution test n = ", n, ", p = ", p) - if (assertEquals(expected,rDistValues,tol,"Distribution Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -#-------------------------------------------------------------------------- -cat("Binomial test cases\n") - -size <- 10.0 -probability <- 0.70 - -densityPoints <- c(-1,0,1,2,3,4,5,6,7,8,9,10,11) -densityValues <- c(0, 0.0000, 0.0001, 0.0014, 0.0090, 0.0368, 0.1029, - 0.2001, 0.2668, 0.2335, 0.1211, 0.0282, 0) -distributionValues <- c(0, 0.0000, 0.0001, 0.0016, 0.0106, 0.0473, - 0.1503, 0.3504, 0.6172, 0.8507, 0.9718, 1, 1) -inverseCumPoints <- c( 0.001, 0.010, 0.025, 0.050, 0.100, 0.999, - 0.990, 0.975, 0.950, 0.900) -inverseCumValues <- c(1, 2, 3, 4, 4, 9, 9, 9, 8, 8) - -verifyDensity(densityPoints,densityValues,size,probability,tol) -verifyDistribution(densityPoints, distributionValues, size, probability, tol) - -i <- 0 -rInverseCumValues <- rep(0,length(inverseCumPoints)) -for (point in inverseCumPoints) { - i <- i + 1 - rInverseCumValues[i] <- qbinom(point, size, probability, log = FALSE) -} - -output <- c("Inverse Distribution test n = ", size, ", p = ", probability) -# R defines quantiles from the right, need to subtract one -if (assertEquals(inverseCumValues, rInverseCumValues-1, tol, - "Inverse Dist Values")) { - displayPadded(output, SUCCEEDED, 80) -} else { - displayPadded(output, FAILED, 80) -} - -# Degenerate cases - -size <- 5 -probability <- 0.0 - -densityPoints <- c(-1, 0, 1, 10, 11) -densityValues <- c(0, 1, 0, 0, 0) -distributionPoints <- c(-1, 0, 1, 5, 10) -distributionValues <- c(0, 1, 1, 1, 1) - -verifyDensity(densityPoints,densityValues,size,probability,tol) -verifyDistribution(distributionPoints,distributionValues,size,probability,tol) - -size <- 5 -probability <- 1.0 - -densityPoints <- c(-1, 0, 1, 2, 5, 10) -densityValues <- c(0, 0, 0, 0, 1, 0) -distributionPoints <- c(-1, 0, 1, 2, 5, 10) -distributionValues <- c(0, 0, 0, 0, 1, 1) - -verifyDensity(densityPoints,densityValues,size,probability,tol) -verifyDistribution(distributionPoints,distributionValues,size,probability,tol) - -displayDashes(WIDTH) http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/R/cauchyTestCases.R ---------------------------------------------------------------------- diff --git a/src/test/R/cauchyTestCases.R b/src/test/R/cauchyTestCases.R deleted file mode 100644 index d0fe442..0000000 --- a/src/test/R/cauchyTestCases.R +++ /dev/null @@ -1,97 +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. -# -#------------------------------------------------------------------------------ -# R source file to validate Cauchy distribution tests in -# org.apache.commons.math.distribution.CauchyDistributionTest -# -# 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>") -# -#----------------------------------------------------------------------------- -tol <- 1E-9 - -# Function definitions - -source("testFunctions") # utility test functions - -# function to verify distribution computations - -verifyDistribution <- function(points, expected, median, scale, tol) { - rDistValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDistValues[i] <- pcauchy(point, median, scale, log = FALSE) - } - output <- c("Distribution test median = ",median,", scale = ", scale) - 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, median, scale, tol) { - rDensityValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rDensityValues[i] <- dcauchy(point, median, scale, log = FALSE) - } - output <- c("Density test median = ",median,", scale = ", scale) - if (assertEquals(expected, rDensityValues, tol, "Density Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -# function to verify quantiles - -verifyQuantiles <- function(points, expected, median, scale, tol) { - rQuantileValues <- rep(0, length(points)) - i <- 0 - for (point in points) { - i <- i + 1 - rQuantileValues[i] <- qcauchy(point, median, scale, log = FALSE) - } - output <- c("Quantile test median = ",median,", scale = ", scale) - if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { - displayPadded(output, SUCCEEDED, WIDTH) - } else { - displayPadded(output, FAILED, WIDTH) - } -} - -#-------------------------------------------------------------------------- -cat("Cauchy test cases\n") - -median <- 1.2 -scale <- 2.1 -distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, - 0.990, 0.975, 0.950, 0.900) -densityValues <- c(1.49599158008e-06, 0.000149550440335, 0.000933076881878, 0.00370933207799, 0.0144742330437, - 1.49599158008e-06, 0.000149550440335, 0.000933076881878, 0.00370933207799, 0.0144742330437) -distributionPoints <- c(-667.24856187, -65.6230835029, -25.4830299460, -12.0588781808, -5.26313542807, - 669.64856187, 68.0230835029, 27.8830299460, 14.4588781808, 7.66313542807) -verifyDistribution(distributionPoints, distributionValues, median, scale, tol) -verifyDensity(distributionPoints, densityValues, median, scale, tol) -verifyQuantiles(distributionValues, distributionPoints, median, scale, tol) - -displayDashes(WIDTH)
