-
Notifications
You must be signed in to change notification settings - Fork 1
/
figure_2.py
executable file
·523 lines (444 loc) · 19.2 KB
/
figure_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#Produces plots seen in Figure 2
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import MaxNLocator
from tqdm import tqdm
import cvxpy as cp
from tqdm import tqdm
np.set_printoptions(formatter={'float': lambda x: "{0:0.2f}".format(x)})
from pathlib import Path
import matplotlib as mpl
mpl.rcdefaults()
plt.rc('axes', labelsize=30) # fontsize of the x and y labels
plt.rc('axes', linewidth=3) #border of figures
plt.rc('xtick', labelsize=30) # fontsize of the tick labels
plt.rc('ytick', labelsize=30) # fontsize of the tick labels
plt.rc('legend', fontsize=30) # legend fontsize
plt.rc('figure', titlesize=30) # fontsize of the figure title
plt.rc('legend', frameon = False)
plt.rc('lines', linewidth = 4)
plt.rc('lines', markersize = 8)
plt.rc('axes', titlesize = 40)
plt.rc('figure', figsize = (12,8))
def RBF_cov(n_pts, len_scale_sq = None, var = 1):
""" returns RBF covariance matrix
inputs:
- n_pts: number of points of GP
- var: \sigma^2 of RBF kernel, defaults to n_pts / 2
returns:
- Sigma: covariance matrix of n_pts X n_pts
"""
t_pts = np.arange(n_pts)[:,None]
if len_scale_sq == None:
len_scale_sq = n_pts / 2
Sigma = var * np.exp(- (t_pts - t_pts.T)**2 / (2*len_scale_sq))
return Sigma
def PER_cov(n_pts, len_scale_sq = None, period = None, var = 1):
""" returns periodic covariance matrix
inputs:
- n_pts: number of points of GP
- var: \sigma^2 of RBF kernel, defaults to n_pts / 2
returns:
- Sigma: covariance matrix of n_pts X n_pts
"""
t_pts = np.arange(n_pts)[:,None]
if len_scale_sq == None:
len_scale_sq = n_pts / 2
if period == None:
period = n_pts / 2
Sigma = var * np.exp(- 2*np.sin( np.pi * np.abs(t_pts - t_pts.T) / period)**2 / (len_scale_sq))
return Sigma
def get_A_and_B_info(Sigma, s_idx):
"""gets the matrix A = $\Sigma_{us}\Sigma_{ss}^{-1}$.
For matrix B = $(\Sigma_{u|s} + \Sigma_{uu}^{(z)})^{-1}$, returns $\Sigma_{u|s}$.
Inputs:
- Sigma: n X n covariance matrix of conditional prior
- s_idx: k-element 1d np array with indices of hypothesized points s
Returns:
- A: |U| X |S| matrix
- Sigma_ugs: |U| X |U| matrix
"""
n_pts = Sigma.shape[0]
u_idx = np.array([True]*n_pts)
u_idx[s_idx] = False
u_idx = np.arange(n_pts)[u_idx]
s_idx = s_idx[:,None]
u_idx = u_idx[:,None]
#get submatrices
Sig_us = Sigma[u_idx, s_idx.T]
Sig_ss = Sigma[s_idx, s_idx.T]
Sig_su = Sigma[s_idx, u_idx.T]
Sig_uu = Sigma[u_idx, u_idx.T]
#inverse of Sig_ss
Sig_ss_inv = np.linalg.pinv(Sig_ss)
#get A matrix
A = Sig_us.dot(Sig_ss_inv)
#get Sigma_ugs
Sig_ugs = Sig_uu - Sig_us.dot(Sig_ss_inv).dot(Sig_su)
return A, Sig_ugs
def get_Sigma_z(Sigma, s_idx, MSE_max, is_print = False):
"""Finds noise covariance matrix optimizing privacy loss for secret s_idx
Inputs:
- Sigma: n X n conditional prior covarience matrix
- s_idx: np array of indices of your secret
- MSE_max: MSE constraint on noise covariance matrix
- is_print: boolean determining whether to print status info
Outputs:
- Sigma_z: u X u block of the noise covariance matrix Sigma_z
- beta_star: minimum eigenvalue found for A'BA
"""
#Construct problem data
n_pts = np.shape(Sigma)[0]
n_s = len(s_idx)
n_u = n_pts - n_s
u_idx = np.delete(np.arange(n_pts), s_idx)
A, Sig_ugs = get_A_and_B_info(Sigma, s_idx)
#augment matrices to n pts
A_tilde = np.concatenate((np.eye(n_s), A), axis = 0)
Sig_ugs_tilde = np.zeros((n_pts, n_pts))
Sig_ugs_tilde[n_s:, n_s:] = Sig_ugs
# #get pseudo inverse
A_tilde_i, res, _, _ = np.linalg.lstsq(A_tilde, np.eye(n_pts), rcond = None)
# Build problem
B_tilde_i = cp.Variable((n_pts,n_pts),symmetric = True)
beta_star = cp.Variable(1)
#Construct constraints
constraints = [beta_star >= 0,
A_tilde_i@B_tilde_i@A_tilde_i.T >> beta_star*np.eye(n_s),
B_tilde_i >> Sig_ugs_tilde,
cp.trace(B_tilde_i) <= np.trace(Sig_ugs_tilde) + MSE_max]
#objective
obj = cp.Maximize(beta_star)
#create SDP
prob = cp.Problem(obj, constraints)
if is_print:
print('\nProblem DCP?:', prob.is_dcp())
#Solve SDP
_ = prob.solve()
if is_print:
print("status:\n", prob.status)
Sigma_z = np.zeros((n_pts, n_pts))
Sigma_z[s_idx, s_idx] = (B_tilde_i.value[np.arange(n_s),np.arange(n_s)]).mean()
Sigma_z[u_idx[:,None],u_idx[:,None].T] = (B_tilde_i.value - Sig_ugs_tilde)[n_s:, n_s:]
return Sigma_z
def plot_sweep_data(l_effs, len_scales_sq, SDP_CIs, ISO_uni_CIs, ISO_conc_CIs, fname, title, misspec = False, ylabel = False):
#Get distribution stats
quartile1, median, quartile3 = np.percentile(l_effs, [25, 50, 75])
#plot interval
if ISO_conc_CIs is not None:
minval = 0.9*np.min(np.concatenate((SDP_CIs, ISO_uni_CIs, ISO_conc_CIs)))
maxval = 1.1*np.max(np.concatenate((SDP_CIs, ISO_uni_CIs, ISO_conc_CIs)))
else:
minval = 0.9*np.min(np.concatenate((SDP_CIs, ISO_uni_CIs)))
maxval = 1.1*np.max(np.concatenate((SDP_CIs, ISO_uni_CIs)))
plt.fill_betweenx(np.linspace(minval, maxval, 10), quartile1, quartile3, color = 'gray', alpha = 0.2)
plt.plot(median * np.ones(10), np.linspace(minval, maxval, 10), '--',color = 'black')
#plot data
if misspec:
plt.plot(np.sqrt(len_scales_sq), np.array(SDP_CIs), '-o', label = 'Good prior')
plt.plot(np.sqrt(len_scales_sq), np.array(ISO_uni_CIs),'-o', label = 'Over Corr. Prior')
plt.plot(np.sqrt(len_scales_sq), np.array(ISO_conc_CIs),'-o', label = 'Under Corr. Prior')
plt.xlabel("Prior Dependence ($l_{eff}$ of Mech.)")
else:
plt.plot(np.sqrt(len_scales_sq), np.array(SDP_CIs), '-o', label = 'SDP')
plt.plot(np.sqrt(len_scales_sq), np.array(ISO_uni_CIs),'-o', label = 'Ind. Unif.')
if ISO_conc_CIs is not None:
plt.plot(np.sqrt(len_scales_sq), np.array(ISO_conc_CIs),'-o', label = 'Ind. Conc.')
plt.xlabel("Prior Dependence ($l_{eff}$)")
plt.legend(loc = 'upper right')
if ylabel:
plt.ylabel("Posterior Uncertainty Interval")
plt.title(title)
plt.savefig('./images/' + fname, bbox_inches = 'tight', pad_inches = 0)
plt.show()
def get_super_sigma_z(Sigma, secrets, MSE_max, is_print = False):
"""Finds an optimal noise covariance matrix for each listed secret
and solves for a minimum trace noise covariance that maintains the privacy of each
Inputs:
-Sigma: model covariance
-secrets: list of numpy array secrets to protect
-MSE_max: the maximum trace of the individual secret covariance matrices
Outputs:
-Sigma_z: the final covariance matrix
-Sigma_zs: the individual secret-optimal covariance matrices
"""
n_pts = np.shape(Sigma)[0]
#list of individual secret matrices
Sigma_zs = []
#get individual Sigma_z's
for s_idx in secrets:
Sigma_zs.append(get_Sigma_z(Sigma, s_idx, MSE_star, is_print=is_print))
#Run program to pick final Sigma_z
# Build problem
Sigma_z = cp.Variable((n_pts,n_pts),symmetric = True)
#Construct constraints
constraints = [Sigma_z >> Sigma_zs[i] for i in range(len(secrets))]
#objective
obj = cp.Minimize(cp.trace(Sigma_z))
#create SDP
prob = cp.Problem(obj, constraints)
if is_print:
print('\nProblem DCP?:', prob.is_dcp())
#Solve SDP
_ = prob.solve()
if is_print:
print("status:\n", prob.status)
Sigma_z = Sigma_z.value
return Sigma_z, Sigma_zs
def get_posterior_cov(Sigma, Sigma_z):
"""
return the normal posterior distribution of X values
Inputs:
- Sigma: covariance matrix of X
- Sigma_z: covariance matrix of mechanism (G)
Returns:
- Sigma_xgz: posterior covariance of X_s
"""
#get inverses (need the 1e-6 for stability)
Sigma_i = np.linalg.pinv(Sigma + 1e-6 * np.mean(Sigma.diagonal()) * np.eye(len(Sigma)))
Sigma_z_i = np.linalg.pinv(Sigma_z + 1e-6 * np.mean(Sigma_z.diagonal()) * np.eye(len(Sigma_z)))
#posterior covariance of all Xs
Sigma_xgz = np.linalg.pinv(Sigma_i + Sigma_z_i)
return Sigma_xgz
#Make figure directory
Path("./images").mkdir(parents=True, exist_ok=True)
#load l_eff data for RBF (location)
l_eff_x = np.load('./saved_data/l_eff_x.npy')
l_eff_y = np.load('./saved_data/l_eff_y.npy')
l_effs = np.concatenate((l_eff_x, l_eff_y), axis = 0)
#load l_eff data for Periodic (home temperature)
l_eff_temp = np.load('./saved_data/l_eff_temp.npy')
#which figures to make
two_a = True
two_b = True
two_c = True
two_d = True
two_e = True
two_f = True
two_g = True
two_h = True
#################
### FIGURE 2A ###
#################
if two_a == True:
print('Making Figure 2A')
n_pts = 50
s_idx = np.array([24])
len_scales_sq = np.linspace(1, 10**2, 20)
stab_factor = 10
MSE_star = stab_factor * n_pts * 0.02
SDP_CIs = []
ISO_uni_CIs = []
ISO_conc_CIs = []
for l in tqdm(len_scales_sq):
Sigma = RBF_cov(n_pts, len_scale_sq = l, var = 1)
Sigma_z = get_Sigma_z(Sigma, s_idx, MSE_star, is_print = False) / stab_factor
Sigma_xgz = get_posterior_cov(Sigma, Sigma_z)
CI = Sigma_xgz[s_idx, s_idx]
SDP_CIs.append(CI)
Sigma_z_iso_uni = (np.trace(Sigma_z)/n_pts) * np.eye(n_pts)
Sigma_xgz_iso_uni = get_posterior_cov(Sigma, Sigma_z_iso_uni)
CI_iso_uni = Sigma_xgz_iso_uni[s_idx, s_idx]
ISO_uni_CIs.append(CI_iso_uni)
Sigma_z_iso_conc = np.zeros((n_pts, n_pts))
Sigma_z_iso_conc[s_idx, s_idx] = np.trace(Sigma_z) / len(s_idx)
Sigma_xgz_iso_conc = get_posterior_cov(Sigma, Sigma_z_iso_conc)
CI_iso_conc = Sigma_xgz_iso_conc[s_idx, s_idx]
ISO_conc_CIs.append(CI_iso_conc)
plot_sweep_data(l_effs, len_scales_sq, 2*np.sqrt(np.array(SDP_CIs)), 2*np.sqrt(np.array(ISO_uni_CIs)), 2*np.sqrt(np.array(ISO_conc_CIs)), 'figure_2a.png', 'RBF Basic Secret', ylabel = True)
#################
### FIGURE 2B ###
#################
if two_b == True:
n_pts = 50
s_idx = np.array([24, 25])
len_scales_sq = np.linspace(1, 10**2, 20)
stab_factor = 1000
MSE_star = stab_factor * n_pts * 0.02
SDP_CIs = []
ISO_uni_CIs = []
ISO_conc_CIs = []
for l in tqdm(len_scales_sq):
Sigma = RBF_cov(n_pts, len_scale_sq = l, var = 1)
Sigma_z = get_Sigma_z(Sigma, s_idx, MSE_star, is_print = False) / stab_factor
Sigma_xgz = get_posterior_cov(Sigma, Sigma_z)[s_idx[:,None], s_idx[None,:]]
CI = np.min(np.abs(np.linalg.eigvals(Sigma_xgz)))
SDP_CIs.append(CI)
Sigma_z_iso_uni = (np.trace(Sigma_z)/n_pts) * np.eye(n_pts)
Sigma_xgz_iso_uni = get_posterior_cov(Sigma, Sigma_z_iso_uni)[s_idx[:,None], s_idx[None,:]]
CI_iso_uni = np.min(np.abs(np.linalg.eigvals(Sigma_xgz_iso_uni)))
ISO_uni_CIs.append(CI_iso_uni)
Sigma_z_iso_conc = np.zeros((n_pts, n_pts))
Sigma_z_iso_conc[s_idx, s_idx] = np.trace(Sigma_z) / len(s_idx)
Sigma_xgz_iso_conc = get_posterior_cov(Sigma, Sigma_z_iso_conc)[s_idx[:,None], s_idx[None,:]]
CI_iso_conc = np.min(np.abs(np.linalg.eigvals(Sigma_xgz_iso_conc)))
ISO_conc_CIs.append(CI_iso_conc)
plot_sweep_data(l_effs, len_scales_sq, 2*np.sqrt(np.array(SDP_CIs)), 2*np.sqrt(np.array(ISO_uni_CIs)), 2*np.sqrt(np.array(ISO_conc_CIs)), 'figure_2b.png', 'RBF Compound Secret')
#################
### FIGURE 2C ###
#################
if two_c == True:
print('Making Figure 2C')
n_pts = 50
len_scales_sq = np.linspace(1, 10**2, 20)
SDP_totals = []
ISO_totals = []
stab_factor = 1e4
secrets = [np.array([i]) for i in range(n_pts)]
MSE_star = stab_factor * n_pts * 0.02 #/ len(secrets) #trace of each single basic secret covariance mat
SDP_CIs = []
ISO_uni_CIs = []
for l in tqdm(len_scales_sq):
Sigma = RBF_cov(n_pts, len_scale_sq = l, var = 1)
Sigma_z, Sigma_zs = get_super_sigma_z(Sigma, secrets, MSE_star)
Sigma_z = Sigma_z / stab_factor #renormalize trace to targeted noise level
Sigma_xgz = get_posterior_cov(Sigma, Sigma_z)
CI = np.mean(Sigma_xgz.diagonal())
SDP_CIs.append(CI)
Sigma_z_iso_uni = (np.trace(Sigma_z)/n_pts) * np.eye(n_pts)
Sigma_xgz_iso_uni = get_posterior_cov(Sigma, Sigma_z_iso_uni)
CI_iso_uni = np.mean(Sigma_xgz_iso_uni.diagonal())
ISO_uni_CIs.append(CI_iso_uni)
plot_sweep_data(l_effs, len_scales_sq, 2*np.sqrt(np.array(SDP_CIs)), 2*np.sqrt(np.array(ISO_uni_CIs)), None, 'figure_2c.png', 'RBF All Basic Secrets')
#################
### FIGURE 2D ###
#################
if two_d == True:
print('Making Figure 2D')
n_pts = 50
s_idx = np.array([24])
len_scales_sq = np.linspace(1, 10**2, 20)
stab_factor = 10
MSE_star = stab_factor * n_pts * 0.02
Proper_CIs = []
lm1_CIs = []
lp1_CIs = []
for l in tqdm(len_scales_sq):
Sigma = RBF_cov(n_pts, len_scale_sq = l, var = 1)
Sigma_z = get_Sigma_z(Sigma, s_idx, MSE_star, is_print = False) / stab_factor
Sigma_xgz = get_posterior_cov(Sigma, Sigma_z)
CI = Sigma_xgz[s_idx, s_idx]
Proper_CIs.append(CI)
#1.5lenscale
Sigma_p1 = RBF_cov(n_pts, len_scale_sq = (1.5*np.sqrt(l))**2, var = 1)
Sigma_xgz_p1 = get_posterior_cov(Sigma_p1, Sigma_z)
CI_p1 = Sigma_xgz_p1[s_idx, s_idx]
lp1_CIs.append(CI_p1)
#0.5lenscale
Sigma_m1 = RBF_cov(n_pts, len_scale_sq = (0.5*np.sqrt(l))**2, var = 1)
Sigma_xgz_m1 = get_posterior_cov(Sigma_m1, Sigma_z)
CI_m1 = Sigma_xgz_m1[s_idx, s_idx]
lm1_CIs.append(CI_m1)
plot_sweep_data(l_effs, len_scales_sq, 2*np.sqrt(np.array(Proper_CIs)), 2*np.sqrt(np.array(lm1_CIs)), 2*np.sqrt(np.array(lp1_CIs)), 'figure_2d.png', 'RBF Misspec. Prior', misspec = True)
#################
### FIGURE 2E ###
#################
if two_e == True:
print('Making Figure 2E')
n_pts = 48
len_scales_sq = np.linspace(0.5**2, 1.5**2, 20)
SDP_CIs = []
ISO_uni_CIs = []
ISO_conc_CIs = []
s_idx = np.array([24])
stab_factor = 10
MSE_star = stab_factor * n_pts * 0.02
for l in tqdm(len_scales_sq):
Sigma = PER_cov(n_pts, period = 24, len_scale_sq = l, var = 1)
Sigma_z = get_Sigma_z(Sigma, s_idx, MSE_star, is_print = False) / stab_factor
Sigma_xgz = get_posterior_cov(Sigma, Sigma_z)
CI = Sigma_xgz[s_idx, s_idx]
SDP_CIs.append(CI)
Sigma_z_iso_uni = (np.trace(Sigma_z)/n_pts) * np.eye(n_pts)
Sigma_xgz_iso_uni = get_posterior_cov(Sigma, Sigma_z_iso_uni)
CI_iso_uni = Sigma_xgz_iso_uni[s_idx, s_idx]
ISO_uni_CIs.append(CI_iso_uni)
Sigma_z_iso_conc = np.zeros((n_pts, n_pts))
Sigma_z_iso_conc[s_idx, s_idx] = np.trace(Sigma_z) / len(s_idx)
Sigma_xgz_iso_conc = get_posterior_cov(Sigma, Sigma_z_iso_conc)
CI_iso_conc = Sigma_xgz_iso_conc[s_idx, s_idx]
ISO_conc_CIs.append(CI_iso_conc)
plot_sweep_data(l_eff_temp, len_scales_sq, 2*np.sqrt(np.array(SDP_CIs)), 2*np.sqrt(np.array(ISO_uni_CIs)), 2*np.sqrt(np.array(ISO_conc_CIs)), 'figure_2e.png', 'PER Basic Secret', ylabel = True)
#################
### FIGURE 3F ###
#################
if two_f == True:
print('Making Figure 2F')
n_pts = 48
len_scales_sq = np.linspace(0.5**2, 1.5**2, 20)
SDP_CIs = []
ISO_uni_CIs = []
ISO_conc_CIs = []
s_idx = np.array([16,32])
stab_factor = 10
MSE_star = stab_factor * n_pts * 0.02
for l in tqdm(len_scales_sq):
Sigma = PER_cov(n_pts, period = 24, len_scale_sq = l, var = 1)
Sigma_z = get_Sigma_z(Sigma, s_idx, MSE_star, is_print = False) / stab_factor
Sigma_xgz = get_posterior_cov(Sigma, Sigma_z)[s_idx[:,None], s_idx[None,:]]
CI = np.min(np.abs(np.linalg.eigvals(Sigma_xgz)))
SDP_CIs.append(CI)
Sigma_z_iso_uni = (np.trace(Sigma_z)/n_pts) * np.eye(n_pts)
Sigma_xgz_iso_uni = get_posterior_cov(Sigma, Sigma_z_iso_uni)[s_idx[:,None], s_idx[None,:]]
CI_iso_uni = np.min(np.abs(np.linalg.eigvals(Sigma_xgz_iso_uni)))
ISO_uni_CIs.append(CI_iso_uni)
Sigma_z_iso_conc = np.zeros((n_pts, n_pts))
Sigma_z_iso_conc[s_idx, s_idx] = np.trace(Sigma_z) / len(s_idx)
Sigma_xgz_iso_conc = get_posterior_cov(Sigma, Sigma_z_iso_conc)[s_idx[:,None], s_idx[None,:]]
CI_iso_conc = np.min(np.abs(np.linalg.eigvals(Sigma_xgz_iso_conc)))
ISO_conc_CIs.append(CI_iso_conc)
plot_sweep_data(l_eff_temp, len_scales_sq, 2*np.sqrt(np.array(SDP_CIs)), 2*np.sqrt(np.array(ISO_uni_CIs)), 2*np.sqrt(np.array(ISO_conc_CIs)), 'figure_2f.png', 'PER Compound Secret')
#################
### FIGURE 3G ###
#################
if two_g == True:
print('Making Figure 2G')
n_pts = 48
len_scales_sq = np.linspace(0.5**2, 1.5**2, 20)
stab_factor = 1e4
secrets = [np.array([i]) for i in range(n_pts)]
MSE_star = stab_factor * n_pts * 0.02 #/ len(secrets) #trace of each single basic secret covariance mat
SDP_CIs = []
ISO_uni_CIs = []
for l in tqdm(len_scales_sq):
Sigma = PER_cov(n_pts, period = 24, len_scale_sq = l, var = 1)
Sigma_z, Sigma_zs = get_super_sigma_z(Sigma, secrets, MSE_star)
Sigma_z = Sigma_z / stab_factor #renormalize
Sigma_xgz = get_posterior_cov(Sigma, Sigma_z)
CI = np.mean(Sigma_xgz.diagonal())
SDP_CIs.append(CI)
Sigma_z_iso_uni = (np.trace(Sigma_z)/n_pts) * np.eye(n_pts)
Sigma_xgz_iso_uni = get_posterior_cov(Sigma, Sigma_z_iso_uni)
CI_iso_uni = np.mean(Sigma_xgz_iso_uni.diagonal())
ISO_uni_CIs.append(CI_iso_uni)
plot_sweep_data(l_eff_temp, len_scales_sq, 2*np.sqrt(np.array(SDP_CIs)), 2*np.sqrt(np.array(ISO_uni_CIs)), None, 'figure_2g.png', 'PER All Basic Secrets')
#################
### FIGURE 3H ###
#################
if two_h == True:
print('Making Figure 2H')
n_pts = 48
s_idx = np.array([24])
len_scales_sq = np.linspace(0.5**2, 1.5**2, 20)
stab_factor = 10
MSE_star = stab_factor * n_pts * 0.02
Proper_CIs = []
lm1_CIs = []
lp1_CIs = []
for l in tqdm(len_scales_sq):
Sigma = PER_cov(n_pts, period = 24, len_scale_sq = l, var = 1)
Sigma_z = get_Sigma_z(Sigma, s_idx, MSE_star, is_print = False) / stab_factor
Sigma_xgz = get_posterior_cov(Sigma, Sigma_z)
CI = Sigma_xgz[s_idx, s_idx]
Proper_CIs.append(CI)
#lenscale + 1
Sigma_p1 = PER_cov(n_pts, len_scale_sq = (1.5*np.sqrt(l))**2, var = 1)
Sigma_xgz_p1 = get_posterior_cov(Sigma_p1, Sigma_z)
CI_p1 = Sigma_xgz_p1[s_idx, s_idx]
lp1_CIs.append(CI_p1)
#lenscale - 1
Sigma_m1 = PER_cov(n_pts, len_scale_sq = (0.5*np.sqrt(l))**2, var = 1)
Sigma_xgz_m1 = get_posterior_cov(Sigma_m1, Sigma_z)
CI_m1 = Sigma_xgz_m1[s_idx, s_idx]
lm1_CIs.append(CI_m1)
plot_sweep_data(l_eff_temp, len_scales_sq, 2*np.sqrt(np.array(Proper_CIs)), 2*np.sqrt(np.array(lm1_CIs)), 2*np.sqrt(np.array(lp1_CIs)), 'figure_2h.png', 'PER Misspec. Prior', misspec = True)