Skip to content

Commit 5845582

Browse files
committed
Add 2 options to handle empty partitions in Issue #49
Will discuss with Ira which option is best
1 parent bfc6b85 commit 5845582

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

R/mechanism-bootstrap.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#' @import stats
1111
#' @export
1212

13+
# There are 2 options for handling empty partitions:
14+
15+
# 1: skip it entirely, and say the total number of partitions is just the number of partitions that are not empty
16+
1317
bootstrap.replication <- function(x, n, sensitivity, epsilon, fun, inputObject, ...) {
1418
partition <- rmultinom(n=1, size=n, prob=rep(1 / n, n))
1519
# make a sorted vector of the partitions of the data
@@ -33,6 +37,34 @@ bootstrap.replication <- function(x, n, sensitivity, epsilon, fun, inputObject,
3337
return(apply(stat.out, 2, sum))
3438
}
3539

40+
# 2: treat it as a partition with a mean of 0 and keep it in the calculation, adding noise and adding it to the final calculation
41+
42+
# bootstrap.replication <- function(x, n, sensitivity, epsilon, fun, inputObject, ...) {
43+
# partition <- rmultinom(n=1, size=n, prob=rep(1 / n, n))
44+
# # make a sorted vector of the partitions of the data
45+
# # because it is not guaranteed that every partition from 1:max.appearances will have a value in it
46+
# validPartitions <- validPartitions <- sort(unique(partition[,1]))
47+
# # print the unique values of the partition, to track which entries may result in NaN
48+
# print(validPartitions)
49+
# max.appearances <- max(partition)
50+
# probs <- sapply(1:max.appearances, dbinom, size=n, prob=(1 / n))
51+
# stat.partitions <- vector('list', max.appearances)
52+
# for (i in 1:max.appearances) {
53+
# variance.i <- (i * probs[i] * (sensitivity^2)) / (2 * epsilon)
54+
# if (i %in% validPartitions) {
55+
# stat.i <- fun(x[partition == i])
56+
# noise.i <- dpNoise(n=length(stat.i), scale=sqrt(variance.i), dist='gaussian')
57+
# stat.partitions[[i]] <- i * stat.i + noise.i
58+
# } else {
59+
# stat.i <- 0
60+
# noise.i <- dpNoise(n=length(stat.i), scale=sqrt(variance.i), dist='gaussian')
61+
# stat.partitions[[i]] <- i * stat.i + noise.i
62+
# }
63+
# }
64+
# stat.out <- do.call(rbind, stat.partitions)
65+
# return(apply(stat.out, 2, sum))
66+
# }
67+
3668

3769
#' Bootstrap mechanism
3870
#'

0 commit comments

Comments
 (0)