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
#------------------------------------------------------------------------------
20
The purpose of the R programs included in this directory is to validate
21
the target values used in Apache commons math unit tests. Success running the
22
R and commons-math tests on a platform (OS and R version) means that R and
23
commons-math give results for the test cases that are close in value. The
24
tests include configurable tolerance levels; but care must be taken in changing
25
these, since in most cases the pre-set tolerance is close to the number of
26
decimal digits used in expressing the expected values (both here and in the
27
corresponding commons-math unit tests).
29
Of course it is always possible that both R and commons-math give incorrect
30
values for test cases, so these tests should not be interpreted as definitive
31
in any absolute sense. The value of developing and running the tests is really
32
to generate questions (and answers!) when the two systems give different
35
Contributions of additional test cases (both R and Junit code) or just
36
R programs to validate commons-math tests that are not covered here would be
41
0) Download and install R. You can get R here
42
http://www.r-project.org/
43
Follow the install instructions and make sure that you can launch R from this
44
(i.e., either explitly add R to your OS path or let the install package do it
47
1) Launch R from this directory and type
49
to an R prompt. This should produce output to the console similar to this:
52
Density test n = 10, p = 0.7...........................................SUCCEEDED
53
Distribution test n = 10, p = 0.7......................................SUCCEEDED
54
Inverse Distribution test n = 10, p = 0.7..............................SUCCEEDED
55
Density test n = 5, p = 0..............................................SUCCEEDED
56
Distribution test n = 5, p = 0.........................................SUCCEEDED
57
Density test n = 5, p = 1..............................................SUCCEEDED
58
Distribution test n = 5, p = 1.........................................SUCCEEDED
59
--------------------------------------------------------------------------------
61
Distribution test mu = 2.1, sigma = 1.4................................SUCCEEDED
62
Distribution test mu = 2.1, sigma = 1.4................................SUCCEEDED
63
Distribution test mu = 0, sigma = 1....................................SUCCEEDED
64
Distribution test mu = 0, sigma = 0.1..................................SUCCEEDED
65
--------------------------------------------------------------------------------
70
WORKING WITH THE TESTS
72
The R distribution comes with online manuals that you can view by launching
73
a browser instance and then entering
77
at an R prompt. Poking about in the test case files and the online docs should
78
bring you up to speed fairly quickly. Here are some basic things to get
79
you started. I should note at this point that I am by no means an expert R
80
programmer, so some things may not be implemented in the the nicest way.
81
Comments / suggestions for improvement are welcome!
83
All of the test cases use some basic functions and global constants (screen
84
width and success / failure strings) defined in "testFunctions." The
85
R "source" function is used to "import" these functions into each of the test
86
programs. The "testAll" program pulls together and executes all of the
87
individual test programs. You can execute any one of them by just entering
89
> source(<program-name>).
91
The "assertEquals" function in the testFunctions file mimics the similarly
92
named function used by Junit:
94
assertEquals <- function(expected, observed, tol, message) {
95
if(any(abs(expected - observed) > tol)) {
96
cat("FAILURE: ",message,"\n")
97
cat("EXPECTED: ",expected,"\n")
98
cat("OBSERVED: ",observed,"\n")
105
The <expected> and <observed> arguments can be scalar values, vectors or
106
matrices. If the arguments are vectors or matrices, corresponding entries
109
The standard pattern used throughout the tests looks like this (from
112
Start by defining a "verification function" -- in this example a function to
113
verify computation of binomial probabilities. The <points> argument is a vector
114
of integer values to feed into the density function, <expected> is a vector of
115
the computed probabilies from the commons-math Junit tests, <n> and <p> are
116
parameters of the distribution and <tol> is the error tolerance of the test.
117
The function computes the probabilities using R and compares the values that
118
R produces with those in the <expected> vector.
120
verifyDensity <- function(points, expected, n, p, tol) {
121
rDensityValues <- rep(0, length(points))
123
for (point in points) {
125
rDensityValues[i] <- dbinom(point, n, p, log = FALSE)
127
output <- c("Density test n = ", n, ", p = ", p)
128
if (assertEquals(expected,rDensityValues,tol,"Density Values")) {
129
displayPadded(output, SUCCEEDED, WIDTH)
131
displayPadded(output, FAILED, WIDTH)
135
The displayPadded function just displays its first and second arguments with
136
enough dots in between to make the whole string WIDTH characters long. It is
137
defined in testFunctions.
139
Then call this function with different parameters corresponding to the different
145
densityPoints <- c(-1,0,1,2,3,4,5,6,7,8,9,10,11)
146
densityValues <- c(0, 0.0000, 0.0001, 0.0014, 0.0090, 0.0368, 0.1029,
147
0.2001, 0.2668, 0.2335, 0.1211, 0.0282, 0)
149
verifyDensity(densityPoints, densityValues, size, probability, tol)
151
If the values computed by R match the target values in densityValues, this will
152
produce one line of output to the console:
154
Density test n = 10, p = 0.7...........................................SUCCEEDED
156
If you modify the value of tol set at the top of binomialTestCases to make the
157
test more sensitive than the number of digits specified in the densityValues
158
vector, it will fail, producing the following output, showing the failure and
159
the expected and observed values:
161
FAILURE: Density Values
162
EXPECTED: 0 0 1e-04 0.0014 0.009 0.0368 0.1029 0.2001 0.2668 0.2335 0.1211 /
164
OBSERVED: 0 5.9049e-06 0.000137781 0.0014467005 0.009001692 0.036756909 /
165
0.1029193452 0.200120949 0.266827932 0.2334744405 0.121060821 0.0282475249 0
166
Density test n = 10, p = 0.7..............................................FAILED