• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env Rscript
2#
3# Copyright 2014 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17library(RUnit)
18library(Matrix)  # for sparse matrices
19
20source('tests/gen_counts.R')
21
22TestGenerateCounts <- function() {
23  report_params <- list(k = 4, m = 2)  # 2 cohorts, 4 bits each
24  map <- Matrix(0, nrow = 8, ncol = 3, sparse = TRUE)  # 3 possible values
25  map[1,] <- c(1, 0, 0)
26  map[2,] <- c(0, 1, 0)
27  map[3,] <- c(0, 0, 1)
28  map[4,] <- c(1, 1, 1)  # 4th bit of the first cohort gets signal from all
29  map[5,] <- c(0, 0, 1)  # 1st bit of the second cohort gets signal from v3
30
31  colnames(map) <- c('v1', 'v2', 'v3')
32
33  partition <- c(3, 2, 1) * 10000
34  v <- 100  # reports per client
35
36  noise0 <- list(p = 0, q = 1, f = 0)  # no noise at all
37  counts0 <- GenerateCounts(c(report_params, noise0), map, partition, v)
38
39  checkEqualsNumeric(sum(counts0[1,2:4]), counts0[1,1])
40  checkEqualsNumeric(counts0[1,5], counts0[1,1])
41  checkEqualsNumeric(partition[3] * v, counts0[1,4] + counts0[2,2])
42  checkEqualsNumeric(sum(partition) * v, counts0[1,1] + counts0[2,1])
43
44  pvalues <- chisq.test(counts0[,1] / v, p = c(.5, .5))$p.value
45  for(i in 2:4)
46    pvalues <- c(pvalues,
47                 chisq.test(
48                   c(counts0[1,i] / v, partition[i - 1] - counts0[1,i] / v),
49                   p = c(.5, .5))$p.value)
50
51  noise1 <- list(p = .5, q = .5, f = 0)  # truly random IRRs
52  counts1 <- GenerateCounts(c(report_params, noise1), map, partition, v)
53
54  for(i in 2:5)
55    for(j in 1:2)
56      pvalues <- c(pvalues,
57                   chisq.test(c(counts1[j,1] - counts1[j,i], counts1[j,i]),
58                                p = c(.5, .5))$p.value)
59
60  noise2 <- list(p = 0, q = 1, f = 1.0)  # truly random PRRs
61  counts2 <- GenerateCounts(c(report_params, noise2), map, partition, v)
62
63  checkEqualsNumeric(0, max(counts2 %% v))  # all entries must be divisible by v
64
65  counts2 <- counts2 / v
66
67  for(i in 2:5)
68    for(j in 1:2)
69      pvalues <- c(pvalues,
70                   chisq.test(c(counts2[j,1] - counts2[j,i], counts2[j,i]),
71                              p = c(.5, .5))$p.value)
72
73  checkTrue(min(pvalues) > 1E-9,  "Chi-squared test failed")
74}
75
76TestRandomPartition <- function() {
77
78  p1 <- RandomPartition(total = 100, dgeom(0:999, prob = .1))
79  p2 <- RandomPartition(total = 1000, dnorm(1:1000, mean = 500, sd = 1000 / 6))
80  p3 <- RandomPartition(total = 10000, dunif(1:1000))
81
82  # Totals must check out.
83  checkEqualsNumeric(100, sum(p1))
84  checkEqualsNumeric(1000, sum(p2))
85  checkEqualsNumeric(10000, sum(p3))
86
87  # Initialize the weights vector to 1 0 1 0 1 0 ...
88  weights <- rep(c(1, 0), 100)
89
90  p4 <- RandomPartition(total = 10000, weights)
91
92  # Check that all mass is allocated to non-zero weights.
93  checkEqualsNumeric(10000, sum(p4[weights == 1]))
94  checkTrue(all(p4[weights == 0] == 0))
95
96  p5 <- RandomPartition(total = 1000000, c(1, 2, 3, 4))
97  p.value <- chisq.test(p5, p = c(.1, .2, .3, .4))$p.value
98
99  # Apply the chi squared test and fail if p.value is too high or too low.
100  # Probability of failure is 2 * 1E-9, which should never happen.
101  checkTrue(p.value > 1E-9)
102}
103
104TestAll <- function(){
105  TestRandomPartition()
106  TestGenerateCounts()
107}
108
109TestAll()