#simulate confidential data
#privacy mechanism adds gaussian noise to each observation.
set.seed(1)
n <- 100
eps <- 3
y <- rnorm(n, mean = -2, sd = 1)
sdp <- mean(y) + rnorm(1, 0, 1/eps)
posterior_f <- function(dmat, theta) {
x <- c(dmat)
xbar <- mean(x)
n <- length(x)
pr_m <- 0
pr_s2 <- 4
ps_s2 <- 1/(1/pr_s2 + n)
ps_m <- ps_s2 * ((1/pr_s2)*pr_m + n * xbar)
rnorm(1, mean = ps_m, sd = sqrt(ps_s2))
}
latent_f <- function(theta) {
matrix(rnorm(100, mean = theta, sd = 1), ncol = 1)
}
statistic_f <- function(xi, sdp, i) {
xi
}
mechanism_f <- function(sdp, sx) {
sum(dnorm(sdp - sx/n, 0, 1/eps, TRUE))
}
dmod <- new_privacy(posterior_f = posterior_f,
latent_f = latent_f,
mechanism_f = mechanism_f,
statistic_f = statistic_f,
npar = 1)
out <- dapper_sample(dmod,
sdp = sdp,
init_par = -2,
niter = 500)
summary(out)
# for parallel computing we 'plan' a session
# the code below uses 2 CPU cores for parallel computing
library(furrr)
plan(multisession, workers = 2)
out <- dapper_sample(dmod,
sdp = sdp,
init_par = -2,
niter = 500,
chains = 2)
# to go back to sequential computing we use
plan(sequential)
Run the code above in your browser using DataLab