Differential Privacy for Power and Energy Systems¶
Workshop Lecture 2 — Differentially Private Projected Gradient Descent
Open with a Julia kernel and run all cells. The code uses Plots.jl.
Notation¶
N: number of EVs.a= $\mathbf a$: charging limits, with $a_i=0$ if EV $i$ is away.x= $\mathbf x$: charging rates.r: reference power.η: step size.K: number of updates.p[k]= $p^k$: noiseless signal.p_hat[k]= $\hat p^k$: DP public signal.w[k]= $w^k$: noise.α: adjacency bound.ε: privacy budget.b: Laplace scale.
In [1]:
using Printf, Plots, Random, Distributions
arrival = [1, 3, 10, 5, 2]
departure = [20, 21, 18, 23, 15]
rated = [10.0, 10.0, 15.0, 10.0, 20.0]
# This is a helper function that outputs charging limits given a certain time
function charging_limits(t)
limits = zeros(length(rated))
for i in eachindex(rated)
if arrival[i] <= t < departure[i]
limits[i] = rated[i]
end
end
return limits
end
hours = [6, 12]
K = 10
r = 40.0
η = 0.2
0.2
1. Projected Gradient Descent¶
In [ ]:
# a: EV charging limits at hour t; r: target power; η: step size; K: number of updates.
function pgd(a, r, η, K)
N = length(a) # Number of EVs
x = copy(a) # x^0 = a since we assume each EV is initalized at maximum charging rate
X = zeros(N, K + 1) # X[:, k+1] = x^k.
X[:, 1] = x
aggregate = zeros(K + 1)
aggregate[1] = ones(N)' * x
p = zeros(K)
for k in 1:K
p[k] = ones(N)' * x - r
x = clamp.(x .- η * p[k], 0.0, a)
X[:, k + 1] = x
aggregate[k + 1] = ones(N)' * x
end
return (
X = X,
aggregate = aggregate,
p = p,
)
end
pgd (generic function with 1 method)
In [4]:
pgd_results = [pgd(charging_limits(t), r, η, K) for t in hours]
for (t, result) in zip(hours, pgd_results)
# print the final tracking error
@printf("Hour %d final error: %.6g kW\n", t, abs(result.aggregate[end] - r))
end
Hour 6 final error: 1.024e-06 kW Hour 12 final error: 0 kW
In [ ]:
# hours: plotted hours; results: PGD runs; r: target power.
function plot_pgd_trajectories(hours, results, r)
power_plot = plot(xlabel="Iteration k", ylabel="Aggregate (kW)")
broadcast_plot = plot(xlabel="Broadcast k", ylabel="Broadcast (kW)")
for (t, result) in zip(hours, results)
K = length(result.p)
plot!(power_plot, 0:K, result.aggregate, label="t = $t", marker=:circle)
plot!(broadcast_plot, 1:K, result.p, label="t = $t", marker=:circle)
end
hline!(power_plot, [r], label="Target", linestyle=:dash)
return plot(power_plot, broadcast_plot, layout=(1, 2), size=(900, 360))
end
plot_pgd_trajectories(hours, pgd_results, r)
2. Attack model¶
In [ ]:
p = [5.0, -10.0, 7.5] # Broadcast signals; w^k = 0 in this attack.
r = 60.0
η = 0.5 # Step size in the attack model.
a_known = rated # Charging rate limits for the five known EVs at t = 12.
x_known = [10.0, 10.0, 10.0, 10.0, 20.0] # Known entries of x^0 from Table 2.
x6 = Float64[] # x_6^(k-1) before broadcast k.
for p_k in p
# TODO 2(a): recover x_6^(k-1) from p_k, then update the known EVs
error("TODO 2(a)")
end
proposed_x6 = error("TODO 2(a): EV 6 proposal in update 2")
a6 = error("TODO 2(a): EV 6 charging limit")
println("EV 6 rates: ", x6, " kW")
println("Update 2 proposal: ", proposed_x6, " kW")
println("EV 6 limit a_6: ", a6, " kW")
3. Privacy Definition¶
In [ ]:
α = error("TODO 3(a): adjacency bound α")
r = 40.0
η = 0.2
# ε: total privacy budget; K: number of updates; α: adjacency
function laplace_scale(ε, K, α)
return error("TODO 3(d): Laplace scale")
end
@printf("Laplace scale at ε = 10: %.1f kW\n", laplace_scale(10, K, α))
4. Differentially Private Projected Gradient Descent¶
(a) For $\varepsilon=10$, both hours use the same noise draws. The plots show total power and public $\hat p^k$.
In [ ]:
# a: EV limits; r: target; η: step size; K: updates.
# ε: privacy budget; α: adjacency bound.
function dp_pgd(a, r, η, K, ε, α)
N = length(a) # Number of Evs
b = laplace_scale(ε, K, α)
w = error("TODO 3(d): draw K noise samples") # Use the global random seed.
x = copy(a) # x^0 = a.
X = zeros(N, K + 1) # X[:, k+1] = x^k. history record of the charging rate setpoints
X[:, 1] = x
aggregate = zeros(K + 1)
aggregate[1] = ones(N)' * x
p_hat = zeros(K)
for k in 1:K
p_hat[k] = error("TODO 4(a): noisy broadcast p_hat^k")
x = clamp.(x .- η * p_hat[k], 0.0, a)
X[:, k + 1] = x
aggregate[k + 1] = ones(N)' * x
end
return (
X = X,
aggregate = aggregate,
p_hat = p_hat,
w = w,
)
end
In [ ]:
# Reset the global seed so both hours use the same 10 noise draws.
ε = 10.0
dp_pgd_results = []
for t in hours
Random.seed!(10)
push!(dp_pgd_results, dp_pgd(charging_limits(t), r, η, K, ε, α))
end
for (t, result) in zip(hours, dp_pgd_results)
@printf("Hour %d final error: %.6g kW\n", t, abs(result.aggregate[end] - r))
end
In [ ]:
# hours: plotted hours; results: DP-PGD runs; r: target power.
function plot_dp_pgd_trajectories(hours, results, r)
power_plot = plot(xlabel="Iteration k", ylabel="Aggregate (kW)")
broadcast_plot = plot(xlabel="Broadcast k", ylabel="Noisy broadcast (kW)")
for (t, result) in zip(hours, results)
K = length(result.p_hat)
plot!(power_plot, 0:K, result.aggregate, label="t = $t", marker=:circle)
plot!(broadcast_plot, 1:K, result.p_hat, label="t = $t", marker=:circle)
end
hline!(power_plot, [r], label="Target", linestyle=:dash)
return plot(power_plot, broadcast_plot, layout=(1, 2), size=(900, 360))
end
plot_dp_pgd_trajectories(hours, dp_pgd_results, r)
(b–c) Use 100 trials per $\varepsilon$ at hour 6 and 12.
In [ ]:
epsilons = [0.5, 1.0, 5.0, 10.0, 20.0, 50.0]
trials = 100
Random.seed!(2026)
mean_error = zeros(length(epsilons), length(hours))
for j in eachindex(epsilons), h in eachindex(hours)
a = charging_limits(hours[h])
total_error = 0.0
for trial in 1:trials
# TODO 4(b): run DP-PGD and add its final tracking error
error("TODO 4(b)")
end
mean_error[j, h] = total_error / trials
end
println(" ε Scale (kW) Hour 6 MAE Hour 12 MAE")
for j in eachindex(epsilons)
@printf("%5.1f %12.1f %12.2f %13.2f\n", epsilons[j],
laplace_scale(epsilons[j], K, α), mean_error[j, 1], mean_error[j, 2])
end
In [ ]:
tradeoff = plot(xlabel="Total privacy loss ε", ylabel="Mean tracking error (kW)",
xticks=(epsilons, string.(epsilons)), xscale=:log10, size=(780, 450))
for h in eachindex(hours)
plot!(tradeoff, epsilons, mean_error[:, h], label="t = $(hours[h])", marker=:circle)
end
tradeoff
(e) Homework. See the exercise sheet.
In [ ]:
# TODO 4(e): homework