-
Notifications
You must be signed in to change notification settings - Fork 1
/
runtime_experiment_final.py
146 lines (107 loc) · 4.21 KB
/
runtime_experiment_final.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
"""
Timing experiment
"""
import numpy as np
import random
from data.data import *
from retraining import *
from timeit import timeit
import csv
from scipy.stats import sem
np.random.seed(0)
d_vals = [1000, 1500, 2000, 2500, 3000]
k_vals = [1, 2, 3, 4, 5, 10, 25, 50, 75, 100, 125, 150]
noise = 1
R = 100
timeit_number = 1
runtimes = {
'exact': np.zeros((len(d_vals), len(k_vals), R)),
'res': np.zeros((len(d_vals), len(k_vals), R)),
'inf': np.zeros((len(d_vals), len(k_vals), R))
}
for i in range(len(d_vals)):
d = d_vals[i]
n = 10 * d
theta = np.random.randn(d)
X = np.random.multivariate_normal(np.zeros(d), datasets.make_spd_matrix(d), n)
Y = lin_data(X, noise, theta)
theta_full = lin_exact(X, Y)
invhess = np.linalg.inv(np.matmul(X.T, X))
H = np.matmul(X, np.matmul(invhess, X.T))
for j in range(len(k_vals)):
k = k_vals[j]
for r in range(R):
ind = range(k)
ind_comp = range(k + 1, n)
def test():
return lin_newton(X, Y, theta_full, ind, invhess)
T = timeit("test()", setup="from __main__ import test", number=timeit_number) / timeit_number
runtimes['exact'][i, j, r] = T
def test():
return lin_res(X, Y, theta_full, ind, H)
t = timeit("test()", setup="from __main__ import test", number=timeit_number) / timeit_number
runtimes['res'][i, j, r] = t / T
def test():
return lin_inf(X, Y, theta_full, ind, invhess)
t = timeit("test()", setup="from __main__ import test", number=timeit_number) / timeit_number
runtimes['inf'][i, j, r] = t / T
means = {
'exact': np.mean(runtimes['exact'], axis=2),
'res': np.mean(runtimes['res'], axis=2),
'inf': np.mean(runtimes['inf'], axis=2)
}
stderr = {
'res': sem(runtimes['res'], axis=2),
'inf': sem(runtimes['inf'], axis=2)
}
Q1 = {
'res': np.quantile(runtimes['res'], 0.25, axis=2),
'inf': np.quantile(runtimes['inf'], 0.25, axis=2)
}
Q2 = {
'exact': np.quantile(runtimes['exact'], 0.5, axis=2),
'res': np.quantile(runtimes['res'], 0.5, axis=2),
'inf': np.quantile(runtimes['inf'], 0.5, axis=2)
}
Q3 = {
'res': np.quantile(runtimes['res'], 0.75, axis=2),
'inf': np.quantile(runtimes['inf'], 0.75, axis=2)
}
res = [[None for j in range(len(k_vals))] for i in range(len(d_vals))]
inf = [[None for j in range(len(k_vals))] for i in range(len(d_vals))]
res_quantiles = [[None for j in range(len(k_vals))] for i in range(len(d_vals))]
inf_quantiles = [[None for j in range(len(k_vals))] for i in range(len(d_vals))]
for i in range(len(d_vals)):
for j in range(len(k_vals)):
res_mean = round(means['res'][i, j], 6)
inf_mean = round(means['inf'][i, j], 6)
res_dev = round(stderr['res'][i, j], 6)
inf_dev = round(stderr['inf'][i, j], 6)
res[i][j] = f'{res_mean} \pm {res_dev}'
inf[i][j] = f'{inf_mean} \pm {inf_dev}'
res_Q1 = round(Q1['res'][i, j], 6)
res_Q2 = round(Q2['res'][i, j], 6)
res_Q3 = round(Q3['res'][i, j], 6)
inf_Q1 = round(Q1['inf'][i, j], 6)
inf_Q2 = round(Q2['inf'][i, j], 6)
inf_Q3 = round(Q3['inf'][i, j], 6)
res_quantiles[i][j] = f'{res_Q2} ({res_Q1} - {res_Q3})'
inf_quantiles[i][j] = f'{inf_Q2} ({inf_Q1} - {inf_Q3})'
with open('res_runtime.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(res)
with open('inf_runtime.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(inf)
with open('res_runtime_quant.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(res_quantiles)
with open('inf_runtime_quant.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(inf_quantiles)
with open('exact_means_runtime.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(means['exact'])
with open('exact_meds_runtime.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(Q2['exact'])