Getting the Most Utility Out of Every Bit of Privacy
Differential privacy (DP) gives a mathematically provable guarantee: whether or not any single individual is in a dataset, what an observer sees barely changes. The cost of that guarantee is noise, and the amount of noise is set by the privacy budget $\varepsilon$. A smaller $\varepsilon$ gives stronger privacy and noisier answers.
In practice an analyst rarely asks just one question. A data release usually holds a batch of statistics, and they all draw on one shared, limited budget. That raises the question this post answers:
How should we spend a privacy budget $\varepsilon$ — or how small can we make it — while keeping the released statistics as useful as possible?
We’ll set up a realistic example, derive the optimal answer in closed form, check it numerically, confirm it with Monte Carlo simulation, and visualize the whole privacy–utility trade-off, including in 3D.
The Problem Setting
A hospital holds records for $n = 5000$ patients and wants to publish five aggregate statistics:
| # | Query | L1 sensitivity $\Delta_i$ | Importance $\alpha_i$ |
|---|---|---|---|
| 1 | Number of diabetic patients | $1$ | $1.0$ |
| 2 | Number of rare-disease patients | $1$ | $2.0$ |
| 3 | Sum of ages (clipped to $[0,100]$) | $100$ | $1.0$ |
| 4 | Sum of incomes (clipped to $[0, 200{,}000]$) | $200{,}000$ | $1.5$ |
| 5 | Sum of hospital visits (clipped to $[0,20]$) | $20$ | $0.5$ |
The rare-disease count is small (about 1% of patients), so the same noise hurts it much more in relative terms. The analyst also cares about it most.
The Laplace Mechanism
Each query $f_i$ is answered with the Laplace mechanism:
$$
\tilde{f}i(D) = f_i(D) + \mathrm{Lap}!\left(\frac{\Delta_i}{\varepsilon_i}\right),
\qquad
\Delta_i = \max{D \sim D’} \lvert f_i(D) - f_i(D’) \rvert .
$$
Under sequential composition, releasing all five answers is $\varepsilon_{\text{total}}$-DP with
$$
\varepsilon_{\text{total}} = \sum_{i=1}^{K} \varepsilon_i .
$$
Measuring Utility
The noise variance of the Laplace mechanism is $\mathrm{Var}[\mathrm{Lap}(b)] = 2b^2$. To compare statistics with very different scales (a count of 58 against an income total of 240 million), we use the importance-weighted relative mean squared error:
$$
\mathrm{wMSE}(\boldsymbol{\varepsilon})
= \sum_{i=1}^{K} \alpha_i,\mathbb{E}!\left[\left(\frac{\tilde f_i - f_i}{f_i}\right)^{2}\right]
= \sum_{i=1}^{K} \frac{2\alpha_i \Delta_i^2}{f_i^2},\frac{1}{\varepsilon_i^2}
= \sum_{i=1}^{K} \frac{a_i}{\varepsilon_i^2},
\qquad a_i = \frac{2\alpha_i\Delta_i^2}{f_i^2}.
$$
Three Ways to Pose the Optimization
(P1) Primal — maximize utility for a fixed budget
$$
\min_{\boldsymbol{\varepsilon} > 0} \ \sum_{i=1}^{K} \frac{a_i}{\varepsilon_i^2}
\quad \text{s.t.} \quad \sum_{i=1}^{K} \varepsilon_i = E .
$$
(P2) Dual — minimize the budget for a utility target
$$
\min_{\boldsymbol{\varepsilon} > 0} \ \sum_{i=1}^{K} \varepsilon_i
\quad \text{s.t.} \quad \sum_{i=1}^{K} \frac{a_i}{\varepsilon_i^2} \le U .
$$
(P3) Penalized — trade privacy and utility with a price $\lambda$
$$
\min_{E > 0} \ J(E) = \mathrm{wMSE}^*(E) + \lambda E .
$$
Closed-Form Solution
For (P1), the Lagrangian $\mathcal{L} = \sum_i a_i\varepsilon_i^{-2} + \mu\left(\sum_i \varepsilon_i - E\right)$ gives
$$
\frac{\partial \mathcal{L}}{\partial \varepsilon_i} = -\frac{2a_i}{\varepsilon_i^3} + \mu = 0
;\Longrightarrow;
\varepsilon_i \propto a_i^{1/3}.
$$
Normalizing to the budget:

The objective is strictly convex on the positive orthant, so this stationary point is the unique global optimum. Splitting the budget evenly, $\varepsilon_i = E/K$, gives
$$
\mathrm{wMSE}^{\text{unif}}(E) = \frac{K^2 \sum_j a_j}{E^2}.
$$
Solving (P2) by inverting the same relation gives
$$
E_{\min}^{*} = \sqrt{\frac{S^3}{U}},
\qquad
E_{\min}^{\text{unif}} = K\sqrt{\frac{\sum_j a_j}{U}},
$$
so the relative budget saving $1 - \dfrac{S^{3/2}}{K\sqrt{\sum_j a_j}}$ does not depend on the target $U$.
For (P3), setting $J’(E) = -2S^3/E^3 + \lambda = 0$ gives
$$
E^{*}(\lambda) = \left(\frac{2S^3}{\lambda}\right)^{1/3}.
$$
We’ll also use a tail-probability version of utility. Since $P(\lvert \mathrm{Lap}(b)\rvert > t) = e^{-t/b}$, requiring each query to be within a relative tolerance $\tau$ with probability at least $1-\beta$ gives
$$
P!\left(\lvert \tilde f_i - f_i \rvert > \tau f_i\right) \le \beta
;\Longleftrightarrow;
\varepsilon_i \ge \frac{\Delta_i \ln(1/\beta)}{\tau f_i}.
$$
Source Code
1 | import time |
Code Walkthrough
1. Synthetic patient database
We draw 5,000 synthetic patients from a seeded generator (default_rng(42)), so every run gives the same results. Each attribute is clipped to a fixed range. Clipping is what makes the sensitivities finite and known: adding or removing one patient can change the age sum by at most 100, the income sum by at most 200,000, and the visit sum by at most 20. A count changes by at most 1.
Diabetes is more likely above age 60. The rare disease affects about 1.2% of patients, so its count is only 58. That count is the hard case of the problem.
The vector a holds the coefficients $a_i = 2\alpha_i\Delta_i^2/f_i^2$ from the derivation, so the whole problem reduces to $\sum_i a_i/\varepsilon_i^2$.
2. Closed-form optimal allocation
optimal_alloc(E) implements $\varepsilon_i^* = E,a_i^{1/3}/S$ directly. It’s a single vectorized NumPy expression that takes about 2 microseconds. wmse_opt and wmse_unif are the analytic error formulas $S^3/E^2$ and $K^2\sum a_j/E^2$. With them we can draw whole frontier curves without running any optimizer.
3. Numerical cross-check with SLSQP
To confirm the derivation, we solve the same problem with SciPy’s SLSQP. Two choices keep the solver fast and stable:
- Optimizing over budget shares $x_i = \varepsilon_i/E$ on the simplex $\sum_i x_i = 1$. This makes the problem independent of scale.
- Supplying the analytic gradient $-2a_i/x_i^3$ and dividing the objective by $S^3$, so its value is of order 1 at the optimum.
The solver agrees with the closed form to about $10^{-9}$. It takes milliseconds, against microseconds for the formula. This is where the closed form pays off: if you need allocations for thousands of budgets, as in a frontier plot or a dashboard, the formula is effectively free.
4. Vectorized Monte Carlo verification
Theory says the optimal allocation should give a smaller expected error. To check, we simulate 20,000 independent releases under each allocation. All the Laplace noise is drawn in one rng.laplace(..., size=(T, K)) call. NumPy broadcasts the per-query scale $\Delta_i/\varepsilon_i$ across the columns, so there is no Python loop, and 40,000 five-query releases take a few milliseconds. The empirical weighted MSE should match $S^3/E^2$ and $K^2\sum a_j/E^2$.
5. Dual problem: minimum ε for a utility target
Here we flip the question. We set a target average relative RMSE $r$, so $U = \left(\sum_i \alpha_i\right) r^2$, and compute the smallest budget that reaches it: $E^*_{\min} = \sqrt{S^3/U}$. We compare this with the budget an even split would need.
The second block applies the stricter tail-probability guarantee. Each query must be within 5% of its true value with 95% probability. This sets a per-query floor $\varepsilon_i \ge \Delta_i\ln(1/\beta)/(\tau f_i)$, and the floors add up to the minimum total budget.
6. Penalized formulation
If an organization can put a “price” $\lambda$ on each unit of privacy loss, the best total budget is $E^*(\lambda) = (2S^3/\lambda)^{1/3}$. The code prints this for several values of $\lambda$ and also sweeps $\lambda$ for the 3D plot.
7. Visualization
All six panels share one 2 × 3 figure. Two of them are 3D surfaces: the objective landscape over the budget simplex, and the penalized objective over $(\varepsilon, \lambda)$.
Execution Results

======================================================================== Total privacy budget epsilon = 1.0 ======================================================================== Query True value Delta eps_unif eps_opt eps_SLSQP Diabetic count 436.0 1 0.2000 0.1372 0.1372 Rare-disease count 58.0 1 0.2000 0.6633 0.6633 Sum of age 238273.1 100 0.2000 0.0442 0.0442 Sum of income 240183538.8 200000 0.2000 0.0799 0.0799 Sum of visits 15164.0 20 0.2000 0.0753 0.0753 ------------------------------------------------------------------------ Weighted MSE (uniform) : 3.009384e-02 Weighted MSE (optimal) : 4.073663e-03 Weighted MSE (SLSQP) : 4.073663e-03 Error reduction : 86.46 % Max |closed - SLSQP| : 5.71e-09 Time closed form : 6.42 us Time SLSQP : 11.07 ms ======================================================================== Monte Carlo (20000 releases, 20.6 ms) Empirical weighted MSE (uniform) : 2.986664e-02 Empirical weighted MSE (optimal) : 4.085240e-03 Query median rel.err unif median rel.err opt Diabetic count 0.789% 1.185% Rare-disease count 6.005% 1.811% Sum of age 0.143% 0.671% Sum of income 0.289% 0.716% Sum of visits 0.457% 1.210% ======================================================================== Dual problem: minimum total epsilon to reach a weighted-RMSE target target 2.0% : eps_opt = 1.3028 eps_unif = 3.5411 saving = 63.21 % target 5.0% : eps_opt = 0.5211 eps_unif = 1.4164 saving = 63.21 % target 10.0% : eps_opt = 0.2606 eps_unif = 0.7082 saving = 63.21 % ------------------------------------------------------------------------ Per-query guarantee P(|error| > 5% of truth) <= 0.05 Diabetic count eps_i >= 0.13742 Rare-disease count eps_i >= 1.03301 Sum of age eps_i >= 0.02515 Sum of income eps_i >= 0.04989 Sum of visits eps_i >= 0.07902 Minimum total epsilon: 1.32449 ======================================================================== lambda = 1e-05 -> optimal total epsilon E* = 9.3398 lambda = 1e-04 -> optimal total epsilon E* = 4.3352 lambda = 1e-03 -> optimal total epsilon E* = 2.0122 lambda = 1e-02 -> optimal total epsilon E* = 0.9340 ========================================================================
Reading the Results
Top-left: Budget allocation at $\varepsilon_{\text{total}} = 1$
The even split gives every query $0.2$. The optimal split is very uneven:
- The rare-disease count gets about $0.66$, two-thirds of the whole budget. Its true value is tiny (58), so Laplace noise of scale $1/\varepsilon$ is large relative to it, and the analyst gave it twice the importance.
- The age sum gets only about $0.044$. Its sensitivity of 100 sounds large, but next to a total of about 238,000 it is negligible, so it needs very little budget.
- The closed-form bars (blue) and the SLSQP bars (orange) match exactly, which confirms the $a_i^{1/3}$ rule.
The rule $\varepsilon_i \propto \left(\alpha_i \Delta_i^2 / f_i^2\right)^{1/3}$ amounts to this: give budget to queries whose relative sensitivity $\Delta_i/f_i$ is large, and temper it with the cube root, so no single query takes everything.
Top-center: Privacy–utility frontier
Both curves are straight lines with slope $-2$ on log–log axes, since error scales as $1/\varepsilon^2$. The optimal line sits a constant factor below the even split at every budget. Here the weighted MSE drops from about $3.0\times10^{-2}$ to $4.1\times10^{-3}$, an 86% reduction, at no extra privacy cost. The orange SLSQP points land on the analytic curve at all eight budgets checked.
Top-right (3D): Objective landscape
We fix the total budget and vary the shares going to the rare-disease count and to the income sum. The other three queries share what’s left, split optimally among themselves. The surface is a convex bowl. It shoots up toward the edges, where some query’s budget goes to zero and its error blows up. The red dot at the bottom of the bowl is the closed-form optimum. Convexity is why the Lagrangian solution is global and why any gradient-based solver converges to it.
Bottom-left: Monte Carlo relative errors
This panel shows how the gains are distributed across queries. With an even split, the rare-disease count has a median relative error of about 6% and a long upper tail. The optimal allocation cuts that to about 1.8%. The other queries get slightly worse, from fractions of a percent to roughly 1%. That is the intended trade: budget moves from queries that were over-served to the one that was failing. The empirical weighted MSE from the simulation matches the theoretical values closely.
Bottom-center: Minimum privacy cost for a utility target
This panel answers the dual question. For any accuracy target, the optimal allocation reaches it with a much smaller $\varepsilon$. The shaded band is the budget saved. As the closed-form analysis predicts, the saving is a constant 63.2% regardless of the target. For example, a 5% average relative RMSE needs $\varepsilon \approx 0.52$ with optimal allocation, against $\approx 1.42$ with an even split. In DP terms, that is a much stronger privacy guarantee for exactly the same accuracy.
The console also reports the stricter per-query tail guarantee: each answer within 5% of the truth with 95% probability. That needs $\varepsilon_{\text{total}} \approx 1.32$, and over 1.03 of it goes to the rare-disease count alone. Small counts are the bottleneck of almost every DP release, and this per-query floor quantifies that exactly.
Bottom-right (3D): Penalized objective
The surface shows $J = \mathrm{wMSE}^*(\varepsilon) + \lambda\varepsilon$ over $\log\varepsilon$ and $\log\lambda$. For each $\lambda$, the cross-section is a valley: too little budget and the error term dominates, too much and the privacy penalty dominates. The cyan curve runs along the valley floor, following $\varepsilon^*(\lambda) = (2S^3/\lambda)^{1/3}$. As privacy gets ten times more expensive, the best budget shrinks by a factor of $10^{1/3} \approx 2.15$ (from 9.34 to 4.34 to 2.01 to 0.93 in the console output). This gives a principled way to choose $\varepsilon$ itself, not just how to split it.
Takeaways
- With Laplace noise and a squared-error utility, the best way to split a fixed budget is $\varepsilon_i^* \propto \left(\alpha_i\Delta_i^2/f_i^2\right)^{1/3}$, and it comes in closed form.
- Splitting the budget evenly can be very wasteful. In this example the optimal split cut the error by 86% at the same $\varepsilon$, or reached the same accuracy with 63% less privacy budget.
- The same machinery answers three practical questions: how to split a fixed budget (primal), how small the budget can be for a required accuracy (dual), and what total budget to choose given a price on privacy (penalized).
- Small counts dominate the privacy cost. Optimization automatically shifts budget toward them, and the per-query tail bound shows exactly how much they need.
- One caution: we plugged the true values $f_i$ into the weights. In a real deployment those weights should come from public information, prior estimates, or a small, separately accounted slice of the budget, so the allocation itself leaks nothing.















