11"""
22This corresponds to the Application 1 of [1].
33"""
4+ import time
45from functools import partial
56
6- import jax .experimental .host_callback as hcb
77import jax .numpy as jnp
88import jax .random
99import matplotlib .pyplot as plt
1010import numpy as np
11- import tikzplotlib
1211import tqdm .auto as tqdm
1312from jax .scipy import linalg
1413
1918save_path = "out/optimal.npz"
2019
2120jax_key = jax .random .PRNGKey (42 )
22- D = 10
21+ D = 15
2322P = jnp .diag (jnp .arange (1 , D + 1 ))
2423
2524chol_P = jnp .linalg .cholesky (P )
2625m = mu = jnp .zeros ((D ,))
2726
2827B = 200 # number of covariances
2928M = 500 # number of RS experiments
30- NS = [1 , 8 , 64 , 512 ]
29+ NS = [4 ** k for k in range ( 6 ) ]
3130
3231
3332def rejection_experiment ():
@@ -39,15 +38,15 @@ def get_chol_covs(k):
3938 orth , _ = linalg .qr (gauss )
4039 Sigma = orth @ P @ orth .T
4140 chol_Sigma = jnp .linalg .cholesky (Sigma )
42- chol_Q = hcb .call (lambda args : get_optimal_covariance (* args ), (m , chol_P , mu , chol_Sigma ),
43- result_shape = chol_P )
41+ chol_Q = get_optimal_covariance (chol_P , chol_Sigma )
4442 return chol_Sigma , chol_Q
4543
4644 res_shape = B , len (NS ), 2
4745 res_mean_coupled = np .zeros (res_shape )
4846 res_var_coupled = np .zeros (res_shape )
4947 res_mean_trials = np .zeros (res_shape )
5048 res_var_trials = np .zeros (res_shape )
49+ res_runtime = np .zeros (res_shape )
5150
5251 res_coupling_bounds = np .zeros ((B , 2 ))
5352 res_trials_bounds = np .zeros (res_shape )
@@ -79,24 +78,31 @@ def test_fun(op_key, chol_Sigma, chol_Q, N):
7978 res_trials_bounds [b , i , 0 ] = (1 + (n - 1 ) / trials_opt ) / (n / trials_opt )
8079 res_trials_bounds [b , i , 1 ] = (1 + (n - 1 ) / trials_max ) / (n / trials_max )
8180
81+ tic = time .time ()
8282 (res_mean_coupled [b , i , 0 ], res_var_coupled [b , i , 0 ], res_mean_trials [b , i , 0 ],
8383 res_var_trials [b , i , 0 ]) = test_fun (rs_key , chol_Sigma , chol_Q , n )
84+ res_runtime [b , i , 0 ] = time .time () - tic
8485
86+ tic = time .time ()
8587 (res_mean_coupled [b , i , 1 ], res_var_coupled [b , i , 1 ], res_mean_trials [b , i , 1 ],
8688 res_var_trials [b , i , 1 ]) = test_fun (rs_key , chol_Sigma , chol_Q_max , n )
87-
88- return res_mean_coupled , res_var_coupled , res_mean_trials , res_var_trials , res_coupling_bounds , res_trials_bounds
89+ res_runtime [ b , i , 1 ] = time . time () - tic
90+ return res_mean_coupled , res_var_coupled , res_mean_trials , res_var_trials , res_coupling_bounds , res_trials_bounds , res_runtime
8991
9092
9193if RUN :
92- (coupled_mean , coupled_var , n_trials_mean , n_trials_var , coupling_bounds , n_trials_bounds ) = rejection_experiment ()
94+ (coupled_mean , coupled_var , n_trials_mean , n_trials_var , coupling_bounds , n_trials_bounds ,
95+ runtime ) = rejection_experiment ()
9396 np .savez (save_path , coupled_mean = coupled_mean , coupled_var = coupled_var ,
9497 n_trials_mean = n_trials_mean , n_trials_var = n_trials_var , coupling_bounds = coupling_bounds ,
95- n_trials_bounds = n_trials_bounds )
98+ n_trials_bounds = n_trials_bounds , runtime = runtime )
9699
97100if PLOT :
98101 data = np .load (save_path )
99- fig , axes = plt .subplots (nrows = 2 , ncols = 2 , figsize = (12 , 6 ), sharex = True , sharey = True )
102+ fig , axes = plt .subplots (nrows = 2 , ncols = 3 , figsize = (15 , 7 ), sharex = True , sharey = True )
103+
104+ print (np .mean (data ["runtime" ][1 :], 0 ))
105+ print (np .std (data ["runtime" ][1 :], 0 ))
100106
101107 for i , n in enumerate (NS ):
102108 ax = axes .flatten ()[i ]
@@ -108,16 +114,16 @@ def test_fun(op_key, chol_Sigma, chol_Q, N):
108114 ax .scatter (range (B ), data ["coupled_mean" ][arg_sort , i , 1 ], label = f"Empirical MAX" ,
109115 color = "tab:orange" ,
110116 alpha = 0.75 )
111-
112- ax . plot (range (B ), data ["coupling_bounds" ][arg_sort , 0 ], label = "Optimised bound" ,
113- color = "tab:blue" )
114- ax . plot (range (B ), data ["coupling_bounds" ][arg_sort , 1 ], label = "MAX bound" ,
115- color = "tab:orange" )
117+ twinx = ax . twinx ()
118+ twinx . semilogy (range (B ), data ["coupling_bounds" ][arg_sort , 0 ], label = "Optimised bound" ,
119+ color = "tab:blue" )
120+ twinx . semilogy (range (B ), data ["coupling_bounds" ][arg_sort , 1 ], label = "MAX bound" ,
121+ color = "tab:orange" )
116122 axes [0 , 0 ].legend (loc = "upper left" )
117- # plt.show()
118- tikzplotlib .save ("out/gaussian_opt_coupling.tikz" )
123+ plt .show ()
124+ # tikzplotlib.save("out/gaussian_opt_coupling.tikz")
119125
120- fig , axes = plt .subplots (nrows = 2 , ncols = 2 , figsize = (12 , 6 ), sharex = True , sharey = True )
126+ fig , axes = plt .subplots (nrows = 2 , ncols = 3 , figsize = (15 , 7 ), sharex = True , sharey = True )
121127 for i , n in enumerate (NS ):
122128 ax = axes .flatten ()[i ]
123129 ax .set_title (f"$N={ n } $" )
@@ -131,6 +137,6 @@ def test_fun(op_key, chol_Sigma, chol_Q, N):
131137 alpha = 0.75 )
132138 ax .plot (range (B ), 1 / data ["n_trials_bounds" ][arg_sort , i , 1 ], label = "MAX bound" , color = "tab:orange" )
133139 axes [0 , 0 ].legend (loc = "upper left" )
134- # plt.show()
140+ plt .show ()
135141
136- tikzplotlib .save ("out/gaussian_opt_acceptance.tikz" )
142+ # tikzplotlib.save("out/gaussian_opt_acceptance.tikz")
0 commit comments