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

« back to all changes in this revision

Viewing changes to src/test/R/README.txt

  • 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
 
 
18
INTRODUCTION
 
19
 
 
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).
 
28
 
 
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 
 
33
results.
 
34
 
 
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 
 
37
greatly appreciated.
 
38
 
 
39
SETUP
 
40
 
 
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 
 
45
for you).  
 
46
 
 
47
1) Launch R from this directory and type 
 
48
> source("testAll")
 
49
to an R prompt.  This should produce output to the console similar to this:
 
50
 
 
51
Binomial test cases
 
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
--------------------------------------------------------------------------------
 
60
Normal test cases
 
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
--------------------------------------------------------------------------------
 
66
...
 
67
<more test reports>
 
68
 
 
69
 
 
70
WORKING WITH THE TESTS
 
71
 
 
72
The R distribution comes with online manuals that you can view by launching 
 
73
a browser instance and then entering
 
74
 
 
75
> help.start()
 
76
 
 
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!
 
82
 
 
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
 
88
 
 
89
> source(<program-name>).
 
90
 
 
91
The "assertEquals" function in the testFunctions file mimics the similarly 
 
92
named function used by Junit:
 
93
 
 
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")
 
99
        return(0)
 
100
    } else {
 
101
        return(1)
 
102
    }
 
103
}
 
104
 
 
105
The <expected> and <observed> arguments can be scalar values, vectors or 
 
106
matrices. If the arguments are vectors or matrices, corresponding entries
 
107
are compared.
 
108
 
 
109
The standard pattern used throughout the tests looks like this (from 
 
110
binomialTestCases):
 
111
 
 
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.
 
119
 
 
120
verifyDensity <- function(points, expected, n, p, tol) {
 
121
    rDensityValues <- rep(0, length(points))
 
122
    i <- 0
 
123
    for (point in points) {
 
124
        i <- i + 1
 
125
        rDensityValues[i] <- dbinom(point, n, p, log = FALSE)
 
126
    }
 
127
    output <- c("Density test n = ", n, ", p = ", p)
 
128
    if (assertEquals(expected,rDensityValues,tol,"Density Values")) {
 
129
        displayPadded(output, SUCCEEDED, WIDTH)
 
130
    } else {
 
131
        displayPadded(output, FAILED, WIDTH)
 
132
    }       
 
133
}
 
134
 
 
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.
 
138
 
 
139
Then call this function with different parameters corresponding to the different
 
140
Junit test cases:
 
141
 
 
142
size <- 10.0
 
143
probability <- 0.70
 
144
 
 
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)
 
148
...
 
149
verifyDensity(densityPoints, densityValues, size, probability, tol)
 
150
 
 
151
If the values computed by R match the target values in densityValues, this will
 
152
produce one line of output to the console:
 
153
 
 
154
Density test n = 10, p = 0.7...........................................SUCCEEDED
 
155
 
 
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:
 
160
 
 
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 /
 
163
 0.0282 0 
 
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
 
167
 
 
168