From 082baefb06ab0ceb31326bb247521b5729a1c108 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 21 Oct 2013 21:36:37 +0000 Subject: [PATCH] Add reference test values for ZipfDistribution.logProbability. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1534389 13f79535-47bb-0310-9956-ffa450edef68 --- src/test/R/zipfTestCases | 89 +++++++++++++++++++ .../distribution/ZipfDistributionTest.java | 5 +- 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/test/R/zipfTestCases diff --git a/src/test/R/zipfTestCases b/src/test/R/zipfTestCases new file mode 100644 index 000000000..f19854832 --- /dev/null +++ b/src/test/R/zipfTestCases @@ -0,0 +1,89 @@ +# 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 Zipf distribution tests in +# org.apache.commons.math.distribution.ZipfDistributionTest +# +# To run the test, install R, put this file and testFunctions +# into the same directory, launch R from this directory and then enter +# source("") +# +# R functions used +# dzipf(x, N, s, log = FALSE) <- density +# pzipf(q, N, s) <- distribution +#------------------------------------------------------------------------------ +tol <- 1E-6 # error tolerance for tests +#------------------------------------------------------------------------------ +# Function definitions + +library(VGAM) +source("testFunctions") # utility test functions + +# function to verify density computations + +verifyDensity <- function(points, expected, N, s, tol, log = FALSE) { + rDensityValues <- rep(0, length(points)) + i <- 0 + for (point in points) { + i <- i + 1 + rDensityValues[i] <- dzipf(point, N, s, log) + } + output <- c("Density test N = ", N, ", s = ", s) + 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, s, tol) { + rDistValues <- rep(0, length(points)) + i <- 0 + for (point in points) { + i <- i + 1 + rDistValues[i] <- pzipf(point, N, s) + } + output <- c("Distribution test N = ", N, ", s = ", s) + if (assertEquals(expected,rDistValues,tol,"Distribution Values")) { + displayPadded(output, SUCCEEDED, WIDTH) + } else { + displayPadded(output, FAILED, WIDTH) + } +} + +#-------------------------------------------------------------------------- +cat("Zipf test cases\n") + +N <- 10 +s <- 1 + +densityPoints <- c(-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) +densityValues <- c(0, 0, 0.341417152147406, 0.170708576073703, 0.113805717382469, 0.0853542880368514, + 0.0682834304294811, 0.0569028586912343, 0.0487738788782008, 0.0426771440184257, + 0.0379352391274895, 0.0341417152147406, 0) +logDensityValues <- c(Inf, NaN, -1.07465022926458, -1.76779740982453, -2.17326251793269, -2.46094459038447, + -2.68408814169868, -2.86640969849264, -3.0205603783199, -3.15409177094442, -3.2718748066008, + -3.37723532225863, -Inf) +distributionValues <- c(0, 0, 0.341417152147406, 0.512125728221108, 0.625931445603577, 0.711285733640428, + 0.779569164069909, 0.836472022761144, 0.885245901639344, 0.92792304565777, + 0.96585828478526, 1, 1) + +verifyDensity(densityPoints, densityValues, N, s, tol) +verifyDensity(densityPoints, logDensityValues, N, s, tol, TRUE) +verifyDistribution(densityPoints, distributionValues, N, s, tol) + diff --git a/src/test/java/org/apache/commons/math3/distribution/ZipfDistributionTest.java b/src/test/java/org/apache/commons/math3/distribution/ZipfDistributionTest.java index 7c68f01da..ac4949d4e 100644 --- a/src/test/java/org/apache/commons/math3/distribution/ZipfDistributionTest.java +++ b/src/test/java/org/apache/commons/math3/distribution/ZipfDistributionTest.java @@ -80,8 +80,9 @@ public class ZipfDistributionTest extends IntegerDistributionAbstractTest { @Override public double[] makeLogDensityTestValues() { return new double[] {Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, - -1.0746d, -1.7678d, -2.1733d, -2.4609d, -2.6841d, -2.8664d, -3.0206d, -3.1541d, - -3.2719d, -3.3772d, Double.NEGATIVE_INFINITY}; + -1.07465022926458, -1.76779740982453, -2.17326251793269, -2.46094459038447, + -2.68408814169868, -2.86640969849264, -3.0205603783199, -3.15409177094442, + -3.2718748066008, -3.37723532225863, Double.NEGATIVE_INFINITY}; } /** Creates the default cumulative probability density test input values */