| Title: | Differentially Private Classical Statistical Inference |
|---|---|
| Description: | Implements differentially private (DP) versions of common classical statistical procedures, including descriptive statistics (mean, variance, quantiles, histograms), hypothesis tests (t-test, chi-square, Kolmogorov-Smirnov, one-way ANOVA), and regression (closed-form DP linear regression and DP-SGD for generalized linear models). Provides Laplace and Gaussian mechanisms with analytic calibration, exponential mechanism for medians, privacy-aware confidence intervals that account for both sampling and privacy noise, and privacy budget accounting via basic, advanced, and Renyi differential privacy (RDP) composition. Designed for official statistics and privacy-preserving data analysis research. |
| Authors: | Mukul Bijalwan [aut, cre] |
| Maintainer: | Mukul Bijalwan <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-10-09 09:23:25 UTC |
| Source: | https://github.com/mukulbijalwan/dprivstats |
Computes the advanced-composition bound for homogeneous releases:
advanced_composition_epsilon(prior_epsilons, new_epsilon, delta_prime = 1e-06)advanced_composition_epsilon(prior_epsilons, new_epsilon, delta_prime = 1e-06)
prior_epsilons |
Numeric vector of epsilons already spent. |
new_epsilon |
Epsilon of the new release. |
delta_prime |
Target overall failure parameter |
Total epsilon under advanced composition.
Releases f(x) plus Gaussian noise calibrated with the analytic
Gaussian mechanism (Balle & Wang, 2018).
analytic_gaussian_mechanism(f, x, epsilon, delta, l2_sensitivity)analytic_gaussian_mechanism(f, x, epsilon, delta, l2_sensitivity)
f |
Function computing the target statistic on data |
x |
The private dataset. |
epsilon |
Privacy parameter epsilon (> 0). |
delta |
Privacy parameter delta in (0, 1). |
l2_sensitivity |
L2 sensitivity of |
The privatized value of f(x) (scalar or vector).
Solves for the smallest sigma satisfying the analytic Gaussian mechanism privacy guarantee:
via bisection. Always at least as tight as the classic calibration.
analytic_gaussian_sigma(epsilon, delta, l2_sensitivity)analytic_gaussian_sigma(epsilon, delta, l2_sensitivity)
epsilon |
Privacy parameter epsilon (> 0). |
delta |
Privacy parameter delta in (0, 1). |
l2_sensitivity |
L2 sensitivity of |
Scalar noise standard deviation.
Balle, B., & Wang, Y. (2018). Improving the Gaussian Mechanism for Differential Privacy via Analytic Concentration of the Gaussian Distribution. NeurIPS 2018.
analytic_gaussian_sigma(1.0, 1e-6, 1.0) gaussian_sigma(1.0, 1e-6, 1.0) # looseranalytic_gaussian_sigma(1.0, 1e-6, 1.0) gaussian_sigma(1.0, 1e-6, 1.0) # looser
Basic (sequential) composition
basic_composition(epsilons, deltas = rep(0, length(epsilons)))basic_composition(epsilons, deltas = rep(0, length(epsilons)))
epsilons |
Numeric vector of per-release epsilons. |
deltas |
Numeric vector of per-release deltas. |
List with total epsilon and delta.
Check whether a release fits in the remaining budget
can_spend(budget, epsilon, delta = 0)can_spend(budget, epsilon, delta = 0)
budget |
A |
epsilon |
Proposed epsilon spend. |
delta |
Proposed delta spend (default 0). |
Logical; TRUE if the proposed release fits.
Clips each row of a gradient matrix to a maximum L2 norm:
. Used by DP-SGD.
clip_gradients(grads, max_grad_norm)clip_gradients(grads, max_grad_norm)
grads |
Numeric matrix (n x p) of per-sample gradients. |
max_grad_norm |
Positive clipping constant C. |
List with clipped (the clipped gradients) and
norms (original norms).
g <- matrix(rnorm(30), 10, 3) clip_gradients(g, 1)$normsg <- matrix(rnorm(30), 10, 3) clip_gradients(g, 1)$norms
Given a sequence of per-release epsilons, returns total privacy cost under basic, advanced, and RDP composition — useful for the research question of which rule dominates in practice.
compare_composition(epsilons, delta = 1e-06)compare_composition(epsilons, delta = 1e-06)
epsilons |
Numeric vector of per-release epsilons. |
delta |
Target delta in (0, 1). |
Data frame with one row per composition rule and total epsilon.
compare_composition(c(1, 0.5, 0.5, 0.25), 1e-6)compare_composition(c(1, 0.5, 0.5, 0.25), 1e-6)
Fits dp_lm at each epsilon on an epsilon grid and reports MSE
of coefficients relative to the ordinary least squares fit.
compare_utility( formula, data, epsilon_grid, delta = 1e-06, bounds = NULL, n_reps = 10 )compare_utility( formula, data, epsilon_grid, delta = 1e-06, bounds = NULL, n_reps = 10 )
formula |
Model formula. |
data |
Data frame. |
epsilon_grid |
Numeric vector of epsilon values to compare. |
delta |
Delta for Gaussian mechanism. |
bounds |
Bounds list passed to |
n_reps |
Number of repetitions per epsilon (default 10). |
Data frame with columns epsilon, rep,
mse_coef, max_abs_error.
set.seed(1) d <- data.frame(x = rnorm(200)); d$y <- 1 + d$x + rnorm(200) compare_utility(y ~ x, d, c(0.5, 2), 1e-6, list(y = c(-15, 15)), n_reps = 3)set.seed(1) d <- data.frame(x = rnorm(200)); d$y <- 1 + d$x + rnorm(200) compare_utility(y ~ x, d, c(0.5, 2), 1e-6, list(y = c(-15, 15)), n_reps = 3)
Convenience wrapper calling dp_confint with default settings.
## S3 method for class 'dp_lm' confint(object, parm, level = 0.95, method = "analytical", B = 200, ...)## S3 method for class 'dp_lm' confint(object, parm, level = 0.95, method = "analytical", B = 200, ...)
object |
A |
parm |
Coefficient indices or names (default all). |
level |
Confidence level (default 0.95). |
method |
One of |
B |
Number of bootstrap replicates for bootstrap methods. |
... |
Additional arguments (ignored). |
Numeric matrix with columns estimate, lower,
upper.
Clips rows of a gradient matrix to a maximum L2 norm; equivalent to
clip_gradients but implemented in C++.
cpp_clip_gradients(grads, max_grad_norm)cpp_clip_gradients(grads, max_grad_norm)
grads |
Numeric matrix of per-sample gradients. |
max_grad_norm |
Positive clipping constant. |
Numeric matrix of clipped gradients.
g <- matrix(rnorm(30), 10, 3) cpp_clip_gradients(g, 1)g <- matrix(rnorm(30), 10, 3) cpp_clip_gradients(g, 1)
Vectorized inverse-CDF Laplace sampler implemented in C++ via Rcpp.
Equivalent to rlaplace but faster for large draws.
cpp_rlaplace(n, scale = 1)cpp_rlaplace(n, scale = 1)
n |
Number of draws. |
scale |
Scale parameter (positive). |
Numeric vector of length n.
cpp_rlaplace(5, 1.0)cpp_rlaplace(5, 1.0)
Privatizes group counts, means and variances (equal budget splits) and computes the one-way ANOVA F statistic from privatized summaries only.
dp_anova(formula, data, epsilon, bounds, delta = NULL)dp_anova(formula, data, epsilon, bounds, delta = NULL)
formula |
Formula of the form |
data |
Data frame. |
epsilon |
Total privacy parameter epsilon (> 0). |
bounds |
Common bounds c(L, U) assumed known for both samples. |
delta |
Optional Gaussian-mechanism delta; if NULL Laplace is used. |
An object of class c("dp_htest", "htest").
dat <- data.frame(y = c(rnorm(60), rnorm(60, 1)), g = factor(rep(c("A", "B"), each = 60))) dp_anova(y ~ g, dat, 2.0, bounds = c(-5, 5))dat <- data.frame(y = c(rnorm(60), rnorm(60, 1)), g = factor(rep(c("A", "B"), each = 60))) dp_anova(y ~ g, dat, 2.0, bounds = c(-5, 5))
Privatizes each cell of a contingency table with Laplace noise (sensitivity 1 per cell), truncates negative counts (post-processing), then computes the standard chi-square test on the privatized table.
dp_chisq_test(table, epsilon)dp_chisq_test(table, epsilon)
table |
A numeric matrix or table of non-negative counts. |
epsilon |
Privacy parameter epsilon (> 0). |
An object of class c("dp_htest", "htest") wrapping a
standard chi-square test on the privatized table.
tab <- matrix(c(30, 20, 10, 40), nrow = 2) dp_chisq_test(tab, 1.0)tab <- matrix(c(30, 20, 10, 40), nrow = 2) dp_chisq_test(tab, 1.0)
Confidence intervals for dp_lm coefficients that account for
both sampling variability and privacy noise.
dp_confint( object, parm, level = 0.95, method = c("analytical", "parametric_bootstrap", "privacy_aware_bootstrap"), B = 200, ... )dp_confint( object, parm, level = 0.95, method = c("analytical", "parametric_bootstrap", "privacy_aware_bootstrap"), B = 200, ... )
object |
A |
parm |
Coefficient indices or names (default all). |
level |
Confidence level (default 0.95). |
method |
One of |
B |
Number of bootstrap replicates for bootstrap methods. |
... |
Additional arguments (ignored). |
Three methods:
"analytical"Normal intervals using
, where privacy noise variance is known exactly
from the mechanism calibration.
"parametric_bootstrap"Simulates
and adds fresh privacy noise; returns percentile intervals.
"privacy_aware_bootstrap"Resamples the data and refits the
DP estimator on each resample with per-resample budget
(advanced composition heuristic); returns
percentile intervals. Most conservative but computationally heavy.
Numeric matrix of class c("dp_confint", "matrix") with
columns estimate, lower, upper.
set.seed(11) d <- data.frame(x = rnorm(300)) d$y <- 1 + 2 * d$x + rnorm(300) fit <- dp_lm(y ~ x, d, epsilon = 2.0, delta = 1e-6, bounds = list(y = c(-20, 20))) dp_confint(fit)set.seed(11) d <- data.frame(x = rnorm(300)) d$y <- 1 + 2 * d$x + rnorm(300) fit <- dp_lm(y ~ x, d, epsilon = 2.0, delta = 1e-6, bounds = list(y = c(-20, 20))) dp_confint(fit)
Fits a GLM with differentially private stochastic gradient descent:
per-sample gradients are clipped to max_grad_norm, averaged, and
Gaussian noise is added at each iteration. Privacy is accounted with the
advanced-composition heuristic over n_iter iterations.
dp_glm( formula, data, family = stats::gaussian(), epsilon, delta, max_grad_norm = 1, n_iter = 1000, lr = 0.01, batch_size = NULL, bounds = NULL )dp_glm( formula, data, family = stats::gaussian(), epsilon, delta, max_grad_norm = 1, n_iter = 1000, lr = 0.01, batch_size = NULL, bounds = NULL )
formula |
Model formula. |
data |
Data frame. |
family |
A GLM family (e.g. |
epsilon |
Privacy parameter epsilon (> 0). |
delta |
Privacy parameter delta in (0, 1); required. |
max_grad_norm |
Per-sample gradient clipping norm C (> 0). |
n_iter |
Number of SGD iterations. |
lr |
Learning rate. |
batch_size |
Mini-batch size (default full batch). |
bounds |
Named list: |
A list of class "dp_glm" with coefficients,
epsilon, delta, and convergence details.
set.seed(12) d <- data.frame(x = rnorm(400)) d$y <- rbinom(400, 1, plogis(0.5 * d$x)) fit <- dp_glm(y ~ x, d, binomial(), epsilon = 2.0, delta = 1e-6, bounds = list(y = c(0, 1))) fit$coefficientsset.seed(12) d <- data.frame(x = rnorm(400)) d$y <- rbinom(400, 1, plogis(0.5 * d$x)) fit <- dp_glm(y ~ x, d, binomial(), epsilon = 2.0, delta = 1e-6, bounds = list(y = c(0, 1))) fit$coefficients
Releases bin counts of a bounded variable using Laplace noise with sensitivity 1 per bin, followed by non-negativity post-processing and optional normalization to probabilities.
dp_histogram(x, epsilon, breaks = NULL, normalize = FALSE)dp_histogram(x, epsilon, breaks = NULL, normalize = FALSE)
x |
Numeric vector (NAs ignored). |
epsilon |
Privacy parameter epsilon (> 0). |
breaks |
Numeric vector of cut points (monotone). Defaults to deciles of the observed data. |
normalize |
Logical; return relative frequencies instead of counts. |
An object of class dp_estimate with element estimate
(named numeric vector of privatized bin counts/proportions).
set.seed(4) fit <- dp_histogram(rnorm(500), 1.0, breaks = seq(-3, 3, by = 1)) fit$estimateset.seed(4) fit <- dp_histogram(rnorm(500), 1.0, breaks = seq(-3, 3, by = 1)) fit$estimate
Releases the ECDFs of two bounded samples on a common grid under DP
(ECDF sensitivity per sample; budget split evenly between the two
samples) and computes the KS statistic from the privatized ECDFs.
dp_ks_test( x, y, epsilon, bounds = c(min(c(x, y), na.rm = TRUE), max(c(x, y), na.rm = TRUE)), n_grid = 100 )dp_ks_test( x, y, epsilon, bounds = c(min(c(x, y), na.rm = TRUE), max(c(x, y), na.rm = TRUE)), n_grid = 100 )
x, y
|
Numeric vectors (NAs ignored). |
epsilon |
Total privacy parameter epsilon (> 0). |
bounds |
Common bounds c(L, U) assumed known for both samples. |
n_grid |
Number of grid points for ECDF evaluation (default 100). |
An object of class c("dp_htest", "htest").
set.seed(8) dp_ks_test(rnorm(150), rnorm(150, 1), 1.0, bounds = c(-6, 6))set.seed(8) dp_ks_test(rnorm(150), rnorm(150, 1), 1.0, bounds = c(-6, 6))
Fits OLS on clipped data and releases the coefficient vector under the
Gaussian mechanism, calibrated to the L2 sensitivity of
given response bounds
bounds$y (and optionally a bound on row norms of X; see
dp_lm_sensitivity).
dp_lm( formula, data, epsilon, delta, bounds = NULL, assume_public_design = TRUE )dp_lm( formula, data, epsilon, delta, bounds = NULL, assume_public_design = TRUE )
formula |
Model formula. |
data |
Data frame. |
epsilon |
Privacy parameter epsilon (> 0). |
delta |
Privacy parameter delta in (0, 1); required. |
bounds |
Named list: |
assume_public_design |
Logical; see |
An object of class c("dp_lm", "lm") with privatized
coefficients, residuals and fitted values computed from the private fit,
plus elements epsilon, delta, noise_variance
(privacy noise variance per coefficient), and sampling_vcov
(the non-private sampling covariance estimate used by
dp_confint).
set.seed(11) d <- data.frame(x = rnorm(300), z = rnorm(300)) d$y <- 1 + 2 * d$x - d$z + rnorm(300) fit <- dp_lm(y ~ x + z, d, epsilon = 2.0, delta = 1e-6, bounds = list(y = c(-30, 30))) coef(fit)set.seed(11) d <- data.frame(x = rnorm(300), z = rnorm(300)) d$y <- 1 + 2 * d$x - d$z + rnorm(300) fit <- dp_lm(y ~ x + z, d, epsilon = 2.0, delta = 1e-6, bounds = list(y = c(-30, 30))) coef(fit)
Under the assumption that the design matrix is public/fixed and the
response is bounded (), neighbouring datasets differ
in one response value only. With and
,
dp_lm_sensitivity( X, y_bounds, x_norm_bound = NULL, assume_public_design = TRUE )dp_lm_sensitivity( X, y_bounds, x_norm_bound = NULL, assume_public_design = TRUE )
X |
Numeric model matrix (n x p). |
y_bounds |
Bounds c(L_y, U_y) on the response. |
x_norm_bound |
Optional bound |
assume_public_design |
Logical; if TRUE (default) the sensitivity accounts only for changes in y. |
If the design matrix itself is bounded per-row by x_norm_bound and
treated as private, a conservative multiplicative factor of 3 is applied to
account for simultaneous changes in and (documented
heuristic; see vignette "regression-guide" for discussion).
Scalar L2 sensitivity of the OLS coefficient vector.
X <- cbind(1, 1:10) dp_lm_sensitivity(X, y_bounds = c(0, 10))X <- cbind(1, 1:10) dp_lm_sensitivity(X, y_bounds = c(0, 10))
Differentially private sample mean of a bounded variable. Data are clipped
to bounds before the statistic is computed.
dp_mean( x, epsilon, bounds = range(x, na.rm = TRUE), mechanism = c("laplace", "gaussian", "analytic_gaussian"), delta = NULL )dp_mean( x, epsilon, bounds = range(x, na.rm = TRUE), mechanism = c("laplace", "gaussian", "analytic_gaussian"), delta = NULL )
x |
Numeric vector (NAs ignored). |
epsilon |
Privacy parameter epsilon (> 0). |
bounds |
Bounds c(L, U) assumed known; data are clipped to them. Defaults to the observed range (use a priori bounds in practice). |
mechanism |
One of |
delta |
Required (in (0,1)) when a Gaussian mechanism is used. |
An object of class dp_estimate.
set.seed(1) dp_mean(rnorm(500), 1.0, bounds = c(-5, 5))$estimateset.seed(1) dp_mean(rnorm(500), 1.0, bounds = c(-5, 5))$estimate
Releases a differentially private estimate of the median using the
exponential mechanism over a grid of candidate values, with score
which has L1
sensitivity 1.
dp_median(x, epsilon, bounds, n_bins = 1000)dp_median(x, epsilon, bounds, n_bins = 1000)
x |
Numeric vector (may contain NAs, ignored). |
epsilon |
Privacy parameter epsilon (> 0). |
bounds |
Bounds c(L, U) assumed known for the data. |
n_bins |
Number of candidate grid points (default 1000). |
A list of class dp_estimate with element estimate.
set.seed(42) fit <- dp_median(rnorm(200), 1.0, bounds = c(-5, 5)) fit$estimateset.seed(42) fit <- dp_median(rnorm(200), 1.0, bounds = c(-5, 5)) fit$estimate
Releases arbitrary quantiles probs of a bounded variable using the
exponential mechanism with score based on distance from the target order
statistic. Each quantile receives budget .
dp_quantile(x, epsilon, bounds, probs = 0.5, n_bins = 1000)dp_quantile(x, epsilon, bounds, probs = 0.5, n_bins = 1000)
x |
Numeric vector (may contain NAs, ignored). |
epsilon |
Privacy parameter epsilon (> 0). |
bounds |
Bounds c(L, U) assumed known for the data. |
probs |
Numeric vector of probabilities in (0, 1). |
n_bins |
Number of candidate grid points (default 1000). |
An object of class dp_estimate; estimate is a named
vector with one entry per probability in probs.
set.seed(3) dp_quantile(rnorm(200), 1.0, bounds = c(-5, 5), probs = c(.25, .5, .75))set.seed(3) dp_quantile(rnorm(200), 1.0, bounds = c(-5, 5), probs = c(.25, .5, .75))
Differentially private two-sample t-test. The privacy budget is split evenly across the DP group counts, means and variances; the test statistic is computed entirely by post-processing of privatized quantities.
dp_t_test( x, y, epsilon, bounds = c(min(c(x, y), na.rm = TRUE), max(c(x, y), na.rm = TRUE)), alternative = c("two.sided", "less", "greater"), delta = NULL )dp_t_test( x, y, epsilon, bounds = c(min(c(x, y), na.rm = TRUE), max(c(x, y), na.rm = TRUE)), alternative = c("two.sided", "less", "greater"), delta = NULL )
x, y
|
Numeric vectors (NAs ignored). |
epsilon |
Total privacy parameter epsilon (> 0). |
bounds |
Common bounds c(L, U) assumed known for both samples. |
alternative |
Character, |
delta |
Optional Gaussian-mechanism delta; if NULL Laplace is used. |
An object of class c("dp_htest", "htest") with elements
statistic, estimate, std.error, p.value,
alternative, and epsilon.
set.seed(7) dp_t_test(rnorm(200), rnorm(200, 0.5), 1.0, bounds = c(-5, 5))set.seed(7) dp_t_test(rnorm(200), rnorm(200, 0.5), 1.0, bounds = c(-5, 5))
Computes bias, mean squared error, RMSE, and (optionally) CI coverage of a DP estimator relative to its non-private counterpart or a known truth, across Monte Carlo replicates.
dp_utility_diagnostics(estimates, truth, ci_matrix = NULL)dp_utility_diagnostics(estimates, truth, ci_matrix = NULL)
estimates |
Numeric vector of estimates from repeated runs. |
truth |
Scalar true value (or the non-private estimate). |
ci_matrix |
Optional B x 2 matrix of interval endpoints
( |
List with bias, mse, rmse and optional
coverage.
ests <- replicate(100, dp_mean(rnorm(200), 1.0, c(-5, 5))$estimate) dp_utility_diagnostics(ests, 0)ests <- replicate(100, dp_mean(rnorm(200), 1.0, c(-5, 5))$estimate) dp_utility_diagnostics(ests, 0)
Differentially private variance of a bounded variable, released as the DP
second moment minus the square of the DP mean under a split budget
( each), with post-processing to ensure non-negativity.
dp_variance( x, epsilon, bounds = range(x, na.rm = TRUE), mechanism = c("laplace", "gaussian"), delta = NULL )dp_variance( x, epsilon, bounds = range(x, na.rm = TRUE), mechanism = c("laplace", "gaussian"), delta = NULL )
x |
Numeric vector (NAs ignored). |
epsilon |
Privacy parameter epsilon (> 0). |
bounds |
Bounds c(L, U) assumed known; data are clipped to them. Defaults to the observed range (use a priori bounds in practice). |
mechanism |
One of |
delta |
Privacy parameter for Gaussian mechanisms (if chosen). |
An object of class dp_estimate.
set.seed(2) dp_variance(runif(300), 1.0, bounds = c(0, 1))$estimateset.seed(2) dp_variance(runif(300), 1.0, bounds = c(0, 1))$estimate
Estimates the delta needed so that given a
sample of simulated losses.
empirical_delta_from_losses(losses, epsilon)empirical_delta_from_losses(losses, epsilon)
losses |
Numeric vector of simulated privacy losses. |
epsilon |
Privacy parameter threshold. |
Empirical tail probability.
empirical_delta_from_losses(simulate_gaussian_losses(1000, 5), 2.0)empirical_delta_from_losses(simulate_gaussian_losses(1000, 5), 2.0)
An (epsilon, 0)-DP mechanism satisfies (epsilon^2 / 2)-zCDP (Bun & Steinke, 2016).
epsilon_to_rdp(epsilon)epsilon_to_rdp(epsilon)
epsilon |
Pure-DP epsilon. |
zCDP rho parameter.
A synthetic census-style microdata set of 2000 individuals generated for use in examples and vignettes. Income is a linear function of education, age, hours worked, region, plus noise; it is not real personal data.
example_microdataexample_microdata
A data frame with 2000 rows and 5 variables:
Years of education (integer, 0-20).
Age in years (integer, 18-80).
Weekly working hours (numeric, 0-80).
Factor with levels North, South, East, West.
Annual income (numeric, non-negative).
Simulated; see data-raw/generate_data.R.
Samples a candidate from candidates with probability proportional to
, where is the
sensitivity of the score function.
exponential_mechanism(candidates, scores, epsilon, score_sensitivity = 1)exponential_mechanism(candidates, scores, epsilon, score_sensitivity = 1)
candidates |
Numeric vector of candidate outputs. |
scores |
Numeric vector of scores (higher is better). |
epsilon |
Privacy parameter epsilon (> 0). |
score_sensitivity |
L1 sensitivity of the score function. |
One sampled candidate.
exponential_mechanism(1:10, -(1:10 - 5)^2, 1.0, 1)exponential_mechanism(1:10, -(1:10 - 5)^2, 1.0, 1)
Releases f(x) plus Gaussian noise using the classic
calibration for (epsilon, delta)-DP.
gaussian_mechanism(f, x, epsilon, delta, l2_sensitivity)gaussian_mechanism(f, x, epsilon, delta, l2_sensitivity)
f |
Function computing the target statistic on data |
x |
The private dataset. |
epsilon |
Privacy parameter epsilon (> 0). |
delta |
Privacy parameter delta in (0, 1). |
l2_sensitivity |
L2 sensitivity of |
The privatized value of f(x) (scalar or vector).
gaussian_mechanism(sum, c(1:100), 1.0, 1e-6, l2_sensitivity = 100)gaussian_mechanism(sum, c(1:100), 1.0, 1e-6, l2_sensitivity = 100)
Computes the standard deviation used by the classic Gaussian mechanism.
gaussian_sigma(epsilon, delta, l2_sensitivity)gaussian_sigma(epsilon, delta, l2_sensitivity)
epsilon |
Privacy parameter epsilon (> 0). |
delta |
Privacy parameter delta in (0, 1). |
l2_sensitivity |
L2 sensitivity of |
Scalar noise standard deviation.
Releases f(x) plus Laplace noise calibrated to the L1 sensitivity,
providing pure epsilon-DP.
laplace_mechanism(f, x, epsilon, l1_sensitivity)laplace_mechanism(f, x, epsilon, l1_sensitivity)
f |
Function computing the target statistic on data |
x |
The private dataset. |
epsilon |
Privacy parameter epsilon (> 0). |
l1_sensitivity |
L1 sensitivity of |
The privatized value of f(x) (scalar or vector).
laplace_mechanism(mean, c(1:100), 1.0, l1_sensitivity = 100 / 99)laplace_mechanism(mean, c(1:100), 1.0, l1_sensitivity = 100 / 99)
The privacy loss of one Laplace release with scale is
with , i.e.
when .
These helpers expose its tail probabilities and quantiles for PLRV-based
accounting and simulation studies.
laplace_plr_tail(t, epsilon) laplace_plr_quantile(p, epsilon)laplace_plr_tail(t, epsilon) laplace_plr_quantile(p, epsilon)
t |
Numeric vector of thresholds on the privacy loss. |
epsilon |
Privacy parameter (> 0). |
p |
Numeric vector of probabilities in [0, 1]. |
Tail probabilities (laplace_plr_tail) or
quantiles at probabilities p (laplace_plr_quantile).
laplace_plr_tail(3, 1.0) # exp(-3)laplace_plr_tail(3, 1.0) # exp(-3)
An S3 object tracking cumulative privacy loss across multiple DP releases under basic, advanced, or Renyi DP (RDP) composition.
new_privacy_budget( epsilon, delta = 1e-06, composition = c("basic", "advanced", "rdp") )new_privacy_budget( epsilon, delta = 1e-06, composition = c("basic", "advanced", "rdp") )
epsilon |
Total epsilon available (> 0). |
delta |
Total delta available (default 1e-6; must be in (0,1)). |
composition |
One of |
An object of class "privacy_budget".
b <- new_privacy_budget(3.0, 1e-6) b <- spend(b, 1.0, description = "DP mean") b$epsilon_spentb <- new_privacy_budget(3.0, 1e-6) b <- spend(b, 1.0, description = "DP mean") b$epsilon_spent
Print method for dp_confint objects
## S3 method for class 'dp_confint' print(x, digits = 4, ...)## S3 method for class 'dp_confint' print(x, digits = 4, ...)
x |
A |
digits |
Number of digits to print. |
... |
Additional arguments (ignored). |
The object x, invisibly.
Print method for dp_estimate objects
## S3 method for class 'dp_estimate' print(x, digits = 4, ...)## S3 method for class 'dp_estimate' print(x, digits = 4, ...)
x |
A |
digits |
Number of digits to print. |
... |
Additional arguments (ignored). |
The object x, invisibly.
Print method for dp_glm objects
## S3 method for class 'dp_glm' print(x, ...)## S3 method for class 'dp_glm' print(x, ...)
x |
A |
... |
Additional arguments (ignored). |
The object x, invisibly.
Print method for dp_htest objects
## S3 method for class 'dp_htest' print(x, digits = 4, ...)## S3 method for class 'dp_htest' print(x, digits = 4, ...)
x |
A |
digits |
Number of digits to print. |
... |
Additional arguments (ignored). |
The object x, invisibly.
Print method for dp_lm objects
## S3 method for class 'dp_lm' print(x, ...)## S3 method for class 'dp_lm' print(x, ...)
x |
A |
... |
Additional arguments (ignored). |
The object x, invisibly.
Print method for privacy_budget objects
## S3 method for class 'privacy_budget' print(x, ...)## S3 method for class 'privacy_budget' print(x, ...)
x |
A |
... |
Additional arguments (ignored). |
The object x, invisibly.
Print method for summary.dp_lm objects
## S3 method for class 'summary.dp_lm' print(x, digits = 4, ...)## S3 method for class 'summary.dp_lm' print(x, digits = 4, ...)
x |
A |
digits |
Number of digits to print. |
... |
Additional arguments (ignored). |
The object x, invisibly.
Composes a sequence of pure-DP releases via the zCDP/RDP accumulator and converts the result back to (epsilon, delta)-DP. Typically much tighter than basic composition for many small releases.
rdp_composition(epsilons, delta = 1e-06)rdp_composition(epsilons, delta = 1e-06)
epsilons |
Numeric vector of per-release epsilons. |
delta |
Target delta in (0, 1). |
List with elements rho and epsilon.
rdp_composition(rep(0.1, 20), 1e-6)$epsilon basic_composition(rep(0.1, 20))$epsilonrdp_composition(rep(0.1, 20), 1e-6)$epsilon basic_composition(rep(0.1, 20))$epsilon
Uses .
rdp_to_epsilon(rho, delta)rdp_to_epsilon(rho, delta)
rho |
zCDP parameter (>= 0). |
delta |
Target delta in (0, 1). |
Equivalent epsilon.
Draws n independent Laplace(0, scale) deviates using inverse-CDF
sampling with base R's uniform generator.
rlaplace(n, location = 0, scale = 1)rlaplace(n, location = 0, scale = 1)
n |
Number of draws. |
location |
Location parameter (default 0). |
scale |
Scale parameter; must be positive. |
Numeric vector of length n.
rlaplace(5, 0, 1)rlaplace(5, 0, 1)
Generates a census-like dataset with correlated predictors used in examples and vignettes: income as a function of education, age, and hours worked.
simulate_data(n = 1000)simulate_data(n = 1000)
n |
Sample size (default 1000). |
A data frame with columns education (years, 0-20),
age (18-80), hours (weekly hours, 0-80),
income (annual income, >= 0), and region (factor).
head(simulate_data(5))head(simulate_data(5))
Draws the realized privacy loss approximated
by for two neighbouring datasets under
the Gaussian mechanism, useful for empirical delta estimation.
simulate_gaussian_losses(n, sigma)simulate_gaussian_losses(n, sigma)
n |
Number of draws. |
sigma |
Noise standard deviation. |
Numeric vector of simulated losses.
l <- simulate_gaussian_losses(1000, 5); mean(l)l <- simulate_gaussian_losses(1000, 5); mean(l)
Records one DP release against the budget and updates the cumulative privacy loss under the configured composition rule.
spend(budget, epsilon, delta = 0, description = "")spend(budget, epsilon, delta = 0, description = "")
budget |
A |
epsilon |
Epsilon spent by this release (> 0). |
delta |
Delta spent by this release (default 0). |
description |
Optional text label. |
The updated "privacy_budget" object (invisibly also sets
remaining_epsilon).
Computes the L1/L2 sensitivities under bounded-DP (neighbouring datasets
differ in one record, same size n) for a record .
stat_sensitivities(bounds, n)stat_sensitivities(bounds, n)
bounds |
Numeric vector c(L, U) giving the data bounds. |
n |
Sample size (positive integer). |
A list with elements l1 and l2, each a named numeric
vector of sensitivities for "mean", "sum", "count",
"variance" and "ecdf".
sens <- stat_sensitivities(c(0, 500000), 1000) sens$l1["mean"]sens <- stat_sensitivities(c(0, 500000), 1000) sens$l1["mean"]
Prints privatized coefficients with analytical privacy-aware standard errors and confidence intervals.
## S3 method for class 'dp_lm' summary(object, level = 0.95, ...)## S3 method for class 'dp_lm' summary(object, level = 0.95, ...)
object |
A |
level |
Confidence level (default 0.95). |
... |
Additional arguments (ignored). |
Invisibly, the list with coefficients table.
Repeatedly simulates data, fits dp_lm, builds privacy-aware
CIs with dp_confint, and reports empirical coverage and mean
interval width against the true generating coefficients.
validate_coverage( formula, true_betas, sigma = 1, data_gen = NULL, n = 500, n_sims = 200, epsilon = 1, delta = 1e-06, y_bounds = c(-50, 50), confint_method = "analytical", level = 0.95 )validate_coverage( formula, true_betas, sigma = 1, data_gen = NULL, n = 500, n_sims = 200, epsilon = 1, delta = 1e-06, y_bounds = c(-50, 50), confint_method = "analytical", level = 0.95 )
formula |
Model formula matching |
true_betas |
Named numeric vector of true coefficients. |
sigma |
Error standard deviation of the simulated model. |
data_gen |
Function returning a data frame with columns needed by
|
n |
Sample size per replicate (used by the default generator). |
n_sims |
Number of Monte Carlo replicates. |
epsilon |
Privacy parameter epsilon. |
delta |
Privacy parameter delta. |
y_bounds |
Response bounds passed to |
confint_method |
Method passed to |
level |
Confidence level. |
List with coverage_rate, mean_ci_width,
target_coverage, n_sims.
set.seed(1) validate_coverage(y ~ x, c(`(Intercept)` = 1, x = 2), sigma = 1, n = 200, n_sims = 30, epsilon = 3, delta = 1e-6, y_bounds = c(-15, 15))set.seed(1) validate_coverage(y ~ x, c(`(Intercept)` = 1, x = 2), sigma = 1, n = 200, n_sims = 30, epsilon = 3, delta = 1e-6, y_bounds = c(-15, 15))