-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfde_sc_c2.py
312 lines (258 loc) · 9.38 KB
/
fde_sc_c2.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
import numpy as np
import scipy as sp
from scipy import stats
from matrix_util import *
from random_matrices import *
import matplotlib.pyplot as plt
from timer import Timer
from itertools import chain #for ESD
import time
import logging
E = np.zeros([2,2,2,2])
for i in range(2):
for j in range(2):
E[i][j][i][j] = 1
matrix_units = np.asarray( E, np.complex128)
eye2 = np.eye(2,dtype=np.complex128)
d00eta = matrix_units[1][1]
d01eta = np.zeros([2,2], np.complex128)
d10eta = np.zeros([2,2], np.complex128)
d11eta = matrix_units[0][0]
J_eta = np.asarray([[d00eta, d01eta], [d10eta, d11eta]])
i_TEST_MODE= False
#@jitclass(spec)
class SemiCircular(object):
"""Matrix valued SemiCircular."""
def __init__(self,dim=1,p_dim=-1, scale=1e-1):
super(SemiCircular, self).__init__()
self.diag_A = np.asarray([0])
self.sigma = 0
self.scale= scale
self.test_grads = False
self.dim = dim
### rectangular
### p_dim \times dim
if p_dim > 0:
self.p_dim = p_dim
else:
self.p_dim = dim
self.G= np.eye(2*self.dim)*(1-1j)
self.grads = np.zeros( (self.dim+1, 2*self.dim, 2*self.dim), dtype=np.complex128)
### for subordination
self.des = Descrete(self.diag_A)
self.G2 = np.ones(2)*(-1j)
self.grads2 = np.zeros((self.dim+1, 2,2),dtype = np.complex128)
self.omega = np.ones(2)*1j
self.omega_sc = np.ones(2)*1j
def set_params(self, a,sigma):
assert self.dim == a.shape[0]
assert self.dim == a.size
self.diag_A = a
self.des = Descrete(self.diag_A, p_dim=self.p_dim)
self.sigma = sigma
def update_params(self, a,sigma):
self.diag_A = a
self.des.__init__(a,p_dim=self.p_dim)
self.sigma = sigma
def eta_array(self, in_mat):
M = in_mat.shape[0]
#assert M % 2 == 0 and M == in_mat.shape[1]
half_M = int(M/2)
t2 = np.trace(in_mat[half_M:,half_M:])/half_M
t1 = np.trace(in_mat[:half_M,:])/half_M
#assert t2 + t1 == np.trace(in_mat)/(half_M)
out = np.empty(M, dtype=np.complex128)
for i in range(half_M):
out[i]= t2
for i in range(half_M, M):
out[i]= t1
return out
### G^{-1} = b - \eta(G)
### -jbW + \eta(W)W = 1
### VW + \eta(W)W = 1
#@jit
def fixed_point(self, init_mat, var_mat , max_iter=100, thres=1e-7):
W = init_mat
size = W.shape[0]
sub = thres + 1
#timer = Timer()
#timer.tic()
flag = False
for it in range(max_iter):
sub = np.linalg.inv( self.eta(W)+ var_mat) - W
sub*= 0.5
if it > 1 and np.linalg.norm(sub) < thres*np.linalg.norm(W):
flag = True
W += sub
if flag:
break
#timer.toc()
#logging.info("cauchy time={}/ {}-iter".format(timer.total_time, it))
return W
#@jit
def cauchy(self, init_G, var_mat,sigma):
#assert init_G.shape == var_mat.shape
#assert sigma > 0 or sigma ==0
if abs(sigma) == 0:
print(sigma)
G = np.linalg.inv(var_mat)
else:
init_W = 1j*init_G*sigma
var_mat *= -1j/sigma
W = self.fixed_point(init_W, var_mat)
G = -1j*W/sigma
return G
def ESD(self, num_shot, dim_cauchy_vec=0,COMPLEX=False):
evs_list = []
param_mat = rectangular_diag(self.diag_A, self.p_dim, self.dim)
for n in range(num_shot):
W = info_plus_noise(param_mat, self.sigma, COMPLEX)
evs = np.linalg.eigh(W)[0]
c_noise = sp.stats.cauchy.rvs(loc=0, scale=self.scale, size=dim_cauchy_vec)
if dim_cauchy_vec >0:
for k in range(dim_cauchy_vec):
evs_list.append( (evs - c_noise[k]).tolist())
else:
evs_list.append(evs.tolist())
out = list(chain.from_iterable(evs_list))
return out
def ESD_symm(self, num_shot, dim_cauchy_vec=0,COMPLEX=False):
evs_list = []
param_mat = rectangular_diag(self.diag_A, self.p_dim, self.p_dim)
for n in range(num_shot):
W = info_plus_noise_symm(self.p_dim, self.dim, param_mat, self.sigma, COMPLEX)
evs = np.linalg.eigh(W)[0]
c_noise = sp.stats.cauchy.rvs(loc=0, scale=self.scale, size=dim_cauchy_vec)
if dim_cauchy_vec >0:
for k in range(dim_cauchy_vec):
evs_list.append( (evs - c_noise[k]).tolist())
else:
evs_list.append(evs.tolist())
out = list(chain.from_iterable(evs_list))
return out
##########################
###### Subordinatioin ####
##########################
def cauchy_subordination(self, B, \
init_omega,init_G_sc, max_iter=1000,thres=1e-7, TEST_MODE=i_TEST_MODE):
des = self.des
omega = init_omega
flag = False;
sc_g = init_G_sc
for n in range(max_iter):
assert omega.imag[0] > 0
assert omega.imag[1] > 0
sc_g = self.cauchy_2by2(omega, sc_g)
sc_h = 1/sc_g - omega
omega_transform = des.h_transform(sc_h + B) + B
sub = omega_transform - omega
if np.linalg.norm(sub) < thres:
flag = True
omega += sub
if flag :
break
out = self.cauchy_2by2(omega, sc_g)
omega_sc = 1/out - omega + B
if TEST_MODE:
G1 = out
G2 = des.cauchy_transform(omega_sc)
G3 = 1/(omega + omega_sc - B)
assert ( np.allclose(G1, G2))
assert ( np.allclose(G1, G3))
assert ( np.allclose(G2, G3))
return out, omega, omega_sc
def rho(self, x, G, omega):
z = x+1j*self.scale
L = sp.sqrt(z)*np.ones(2)
G,omega, omgega_sc = self.cauchy_subordination(B=L, init_omega=omega, init_G_sc=G)
self.G2 = G
G_out = G[0]/ sp.sqrt(z)
rho = - G_out.imag/sp.pi
return rho, G, omega
def rho_symm(self, x, G, omega):
z = x+1j*self.scale
L = z*np.ones(2)
G,omega, omgega_sc = self.cauchy_subordination(B=L, init_omega=omega, init_G_sc=G)
rho =- ntrace(G).imag/sp.pi
return rho, G, omega
def density_subordinaiton(self, x_array):
num = len(x_array)
omega = 1j*np.ones(2)
G = -1j*np.ones(2)
rho_list = []
for i in range(num):
rho, G, omega = self.rho(x_array[i], G, omega)
if rho < 0:
print(rho)
#assert rho > 0
rho_list.append(rho)
return np.array(rho_list)
def density_subordinaiton_symm(self, x_array):
num = len(x_array)
omega = 1j*np.eye(2,dtype=np.complex128)
G = -1j*np.eye(2,dtype=np.complex128)
rho_list = []
for i in range(num):
rho, G, omega = self.rho_symm(x_array[i], G, omega)
if rho < 0:
print(rho)
#assert rho > 0
rho_list.append(rho)
return np.array(rho_list)
def cauchy_2by2(self,Z, G_init, max_iter=1000, thres=1e-7):
G = G_init
sigma = self.sigma
flag = False
for d in range(max_iter):
eta = np.copy(G[::-1])
eta[0] *=float(self.p_dim)/self.dim ### for recutangular matrix
sub = 1/(Z - sigma**2*eta) -G
sub *= 0.5
if np.linalg.norm(sub) < thres:
flag = True
G += sub
if flag:
return G
#logging.info("cauchy_2by2: sub = {} @ iter= {}".format(np.linalg.norm(sub),d))
logging.info("cauchy_2by2: reahed max_iter")
return G_init
######## Derivations of SemiCircular
### transpose of tangent
### 2 x 2
### i k
### \part f_k / \part x_i
def eta_2by2(self,G):
eta = np.copy(G[::-1])
eta[0]*=float(self.p_dim)/self.dim ### for recutangular matrix
return eta
class Descrete(object):
"""docstring for Descrete."""
def __init__(self, a, p_dim=-1):
super(Descrete, self).__init__()
self.a = a
self.dim = a.shape[0]
if p_dim > 0:
assert p_dim >= self.dim
self.p_dim = p_dim
else:
self.p_dim = self.dim
self.G = 0
self.f = 0
self.h = 0
def cauchy_transform(self,W):
#assert np.allclose(W.shape, [2,2])
a = self.a
sum_inv_det = np.sum( 1/(W[1]*W[0] - a*a) )
G = [ (1/self.dim)*W[1]*sum_inv_det,\
(1./self.p_dim)*(W[0]*sum_inv_det + (self.p_dim -self.dim)/W[1] ) ]
"""
T = [ [W[1][1]*np.ones(self.dim), a - W[0][1]],\
[a - W[1][0], W[0][0]*np.ones(self.dim)] ] \
/ (W[1][1]*W[0][0] - (W[0][1]-a)*(W[1][0]-a) )
G = np.mean(T, axis=2)
"""
return np.asarray(G)
def f_transfrom(self, W):
return 1/(self.cauchy_transform(W))
def h_transform(self,W):
return self.f_transfrom(W) - W