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 covariance tests in
18
# org.apache.commons.math.stat.correlation.CovarianceTest
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>")
24
#------------------------------------------------------------------------------
25
tol <- 1E-9 # error tolerance for tests
26
#------------------------------------------------------------------------------
27
# Function definitions
29
source("testFunctions") # utility test functions
30
options(digits=16) # override number of digits displayed
32
# function to verify covariance computations
33
verifyCovariance <- function(matrix, expectedCovariance, name) {
34
covariance <- cov(matrix)
35
output <- c("Covariance test dataset = ", name)
36
if (assertEquals(expectedCovariance,covariance,tol,"Covariances")) {
37
displayPadded(output, SUCCEEDED, WIDTH)
39
displayPadded(output, FAILED, WIDTH)
43
#--------------------------------------------------------------------------
44
cat("Covariance test cases\n")
48
longley <- matrix(c(60323,83.0,234289,2356,1590,107608,1947,
49
61122,88.5,259426,2325,1456,108632,1948,
50
60171,88.2,258054,3682,1616,109773,1949,
51
61187,89.5,284599,3351,1650,110929,1950,
52
63221,96.2,328975,2099,3099,112075,1951,
53
63639,98.1,346999,1932,3594,113270,1952,
54
64989,99.0,365385,1870,3547,115094,1953,
55
63761,100.0,363112,3578,3350,116219,1954,
56
66019,101.2,397469,2904,3048,117388,1955,
57
67857,104.6,419180,2822,2857,118734,1956,
58
68169,108.4,442769,2936,2798,120445,1957,
59
66513,110.8,444546,4681,2637,121950,1958,
60
68655,112.6,482704,3813,2552,123366,1959,
61
69564,114.2,502601,3931,2514,125368,1960,
62
69331,115.7,518173,4806,2572,127852,1961,
63
70551,116.9,554894,4007,2827,130081,1962),
64
nrow = 16, ncol = 7, byrow = TRUE)
66
expectedCovariance <- matrix(c(
67
12333921.73333333246, 3.679666000000000e+04, 343330206.333333313,
68
1649102.666666666744, 1117681.066666666651, 23461965.733333334, 16240.93333333333248,
69
36796.66000000000, 1.164576250000000e+02, 1063604.115416667,
70
6258.666250000000, 3490.253750000000, 73503.000000000, 50.92333333333334,
71
343330206.33333331347, 1.063604115416667e+06, 9879353659.329166412,
72
56124369.854166664183, 30880428.345833335072, 685240944.600000024, 470977.90000000002328,
73
1649102.66666666674, 6.258666250000000e+03, 56124369.854166664,
74
873223.429166666698, -115378.762499999997, 4462741.533333333, 2973.03333333333330,
75
1117681.06666666665, 3.490253750000000e+03, 30880428.345833335,
76
-115378.762499999997, 484304.095833333326, 1764098.133333333, 1382.43333333333339,
77
23461965.73333333433, 7.350300000000000e+04, 685240944.600000024,
78
4462741.533333333209, 1764098.133333333302, 48387348.933333330, 32917.40000000000146,
79
16240.93333333333, 5.092333333333334e+01, 470977.900000000,
80
2973.033333333333, 1382.433333333333, 32917.40000000, 22.66666666666667),
81
nrow = 7, ncol = 7, byrow = TRUE)
83
verifyCovariance(longley, expectedCovariance, "longley")
87
fertility <- matrix(c(80.2,17.0,15,12,9.96,
96
82.9,45.2,16,13,91.38,
100
68.9,60.7,19,12,4.43,
102
68.3,72.6,18,2,24.20,
104
55.7,19.4,26,28,12.11,
105
54.3,15.2,31,20,2.15,
107
65.5,59.8,22,10,5.23,
109
56.6,50.9,22,12,15.14,
114
60.5,60.8,16,10,7.72,
115
58.3,26.8,25,19,18.46,
119
77.3,89.7,5,2,100.00,
120
70.5,78.2,12,6,98.96,
124
79.3,63.1,13,13,96.83,
125
70.4,38.4,26,12,5.62,
126
65.7,7.7,29,11,13.79,
127
72.7,16.7,22,13,11.22,
128
64.4,17.6,35,32,16.92,
131
35.0,1.2,37,53,42.34,
132
44.7,46.6,16,29,50.43,
133
42.8,27.7,22,29,58.33),
134
nrow = 47, ncol = 5, byrow = TRUE)
136
expectedCovariance <- matrix(c(
137
156.0424976873265, 100.1691489361702, -64.36692876965772, -79.7295097132285, 241.5632030527289,
138
100.169148936170251, 515.7994172062905, -124.39283071230344, -139.6574005550416, 379.9043755781684,
139
-64.3669287696577, -124.3928307123034, 63.64662349676226, 53.5758556891767, -190.5606105457909,
140
-79.7295097132285, -139.6574005550416, 53.57585568917669, 92.4560592044403, -61.6988297872340,
141
241.5632030527289, 379.9043755781684, -190.56061054579092, -61.6988297872340, 1739.2945371877890),
142
nrow = 5, ncol = 5, byrow = TRUE)
144
verifyCovariance(fertility, expectedCovariance, "swiss fertility")
b'\\ No newline at end of file'