1
# Licensed to the Apache Software Foundation (ASF) under one or more
2
# contributor license agreements. See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License. You may obtain a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
16
#------------------------------------------------------------------------------
17
# R source file to validate Poisson distribution tests in
18
# org.apache.commons.math.distribution.PoissonDistributionTest
20
# To run the test, install R, put this file and testFunctions
21
# into the same directory, launch R from this directory and then enter
22
# source("<name-of-this-file>")
25
# dpois(x, lambda, log = FALSE) <-- density
26
# ppois(q, lambda, lower.tail = TRUE, log.p = FALSE) <-- distribution
27
# pnorm(q, mean=0, sd=1, lower.tail = TRUE, log.p = FALSE) <-- normal dist.
28
#------------------------------------------------------------------------------
30
#------------------------------------------------------------------------------
31
# Function definitions
33
source("testFunctions") # utility test functions
35
# function to verify density computations
37
verifyDensity <- function(points, expected, lambda, tol) {
38
rDensityValues <- rep(0, length(points))
40
for (point in points) {
42
rDensityValues[i] <- dpois(point, lambda, log = FALSE)
44
output <- c("Density test lambda = ", lambda)
45
if (assertEquals(expected, rDensityValues, tol, "Density Values")) {
46
displayPadded(output, SUCCEEDED, WIDTH)
48
displayPadded(output, FAILED, WIDTH)
52
# function to verify distribution computations
54
verifyDistribution <- function(points, expected, lambda, tol) {
55
rDistValues <- rep(0, length(points))
57
for (point in points) {
59
rDistValues[i] <- ppois(point, lambda, log = FALSE)
61
output <- c("Distribution test lambda = ", lambda)
62
if (assertEquals(expected, rDistValues, tol, "Distribution Values")) {
63
displayPadded(output, SUCCEEDED, WIDTH)
65
displayPadded(output, FAILED, WIDTH)
69
# function to verify normal approximation
71
verifyNormalApproximation <- function(expected, lambda, lower, upper, tol) {
72
rValue <- pnorm(upper, mean=lambda, sd=sqrt(lambda), lower.tail = TRUE,
74
pnorm(lower, mean=lambda, sd=sqrt(lambda), lower.tail = TRUE,
76
output <- c("Normal approx. test lambda = ", lambda, " upper = ",
77
upper, " lower = ", lower)
78
if (assertEquals(expected, rValue, tol, "Distribution Values")) {
79
displayPadded(output, SUCCEEDED, WIDTH)
81
displayPadded(output, FAILED, WIDTH)
85
cat("Poisson distribution test cases\n")
90
densityPoints <- c(-1,0,1,2,3,4,5,10,20)
91
densityValues <- c(0, 0.0183156388887, 0.073262555555, 0.14652511111,
92
0.195366814813, 0.195366814813, 0.156293451851,
93
0.00529247667642, 8.27746364655e-09)
94
verifyDensity(densityPoints, densityValues, lambda, tol)
97
distributionPoints <- c(-1, 0, 1, 2, 3, 4, 5, 10, 20)
98
distributionValues <- c(0, 0.0183156388887, 0.0915781944437, 0.238103305554,
99
0.433470120367, 0.62883693518, 0.78513038703,
100
0.99716023388, 0.999999998077)
101
verifyDistribution(distributionPoints, distributionValues, lambda, tol)
103
# normal approximation tests
106
verifyNormalApproximation(0.706281887248, lambda, 89.5, 110.5, tol)
109
verifyNormalApproximation(0.820070051552, lambda, 9899.5, 10200.5, tol)