~ubuntu-branches/ubuntu/trusty/commons-math/trusty

« back to all changes in this revision

Viewing changes to src/test/R/poissonTestCases

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-03-15 20:20:21 UTC
  • Revision ID: james.westby@ubuntu.com-20090315202021-zto3nmvqgcf3ami4
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
#
 
8
#      http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
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.
 
15
#
 
16
#------------------------------------------------------------------------------
 
17
# R source file to validate Poisson distribution tests in
 
18
# org.apache.commons.math.distribution.PoissonDistributionTest
 
19
#
 
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>")
 
23
#
 
24
# R functions used
 
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
#------------------------------------------------------------------------------
 
29
tol <- 1E-10
 
30
#------------------------------------------------------------------------------
 
31
# Function definitions
 
32
 
 
33
source("testFunctions")           # utility test functions
 
34
 
 
35
# function to verify density computations
 
36
 
 
37
verifyDensity <- function(points, expected, lambda, tol) {
 
38
    rDensityValues <- rep(0, length(points))
 
39
    i <- 0
 
40
    for (point in points) {
 
41
        i <- i + 1
 
42
        rDensityValues[i] <- dpois(point, lambda, log = FALSE)
 
43
    }
 
44
    output <- c("Density test lambda = ", lambda)
 
45
    if (assertEquals(expected, rDensityValues, tol, "Density Values")) {
 
46
        displayPadded(output, SUCCEEDED, WIDTH)
 
47
    } else {
 
48
        displayPadded(output, FAILED, WIDTH)
 
49
    }       
 
50
}
 
51
 
 
52
# function to verify distribution computations
 
53
 
 
54
verifyDistribution <- function(points, expected, lambda, tol) {
 
55
    rDistValues <- rep(0, length(points))
 
56
    i <- 0
 
57
    for (point in points) {
 
58
        i <- i + 1
 
59
        rDistValues[i] <- ppois(point, lambda, log = FALSE)
 
60
    }
 
61
    output <- c("Distribution test lambda = ", lambda)
 
62
    if (assertEquals(expected, rDistValues, tol, "Distribution Values")) {
 
63
        displayPadded(output, SUCCEEDED, WIDTH)
 
64
    } else {
 
65
        displayPadded(output, FAILED, WIDTH)
 
66
    }       
 
67
}
 
68
 
 
69
# function to verify normal approximation
 
70
 
 
71
verifyNormalApproximation <- function(expected, lambda, lower, upper, tol) {
 
72
    rValue <- pnorm(upper, mean=lambda, sd=sqrt(lambda), lower.tail = TRUE,
 
73
               log.p = FALSE) - 
 
74
               pnorm(lower, mean=lambda, sd=sqrt(lambda), lower.tail = TRUE,
 
75
               log.p = FALSE)
 
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)
 
80
    } else {
 
81
        displayPadded(output, FAILED, WIDTH)
 
82
    }       
 
83
}
 
84
 
 
85
cat("Poisson distribution test cases\n")
 
86
 
 
87
# stock tests
 
88
 
 
89
lambda <- 4.0
 
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)
 
95
 
 
96
 
 
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)
 
102
 
 
103
# normal approximation tests
 
104
 
 
105
lambda <- 100
 
106
verifyNormalApproximation(0.706281887248, lambda, 89.5, 110.5, tol)
 
107
 
 
108
lambda <- 10000
 
109
verifyNormalApproximation(0.820070051552, lambda, 9899.5, 10200.5, tol)
 
110
 
 
111
displayDashes(WIDTH)
 
112
 
 
113
 
 
114
 
 
115
 
 
116