-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathrandom_matrices.py
377 lines (258 loc) · 10.1 KB
/
random_matrices.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
# coding: utf8
""" Implementation of full and banded matrix models for :math:`\beta`-Ensemble:
- Hermite Ensemble (full + tridiagonal)
- Laguerre Ensemble (full + tridiagonal)
- Jacobi Ensemble (full + tridiagonal)
- Circular Ensemble (full + quindiagonal)
- Ginibre Ensemble (full)
.. seealso:
`Documentation on ReadTheDocs <https://dppy.readthedocs.io/en/latest/continuous_dpps/beta_ensembles.sampling.html>`_
"""
import numpy as np
import scipy.linalg as la
import scipy.sparse as sp
from dppy.utils import check_random_state
###########
# Hermite #
###########
def hermite_sampler_full(N, beta=2,
random_state=None):
rng = check_random_state(random_state)
if beta == 1:
A = rng.randn(N, N)
elif beta == 2:
A = rng.randn(N, N) + 1j * rng.randn(N, N)
elif beta == 4:
X = rng.randn(N, N) + 1j * rng.randn(N, N)
Y = rng.randn(N, N) + 1j * rng.randn(N, N)
A = np.block([[X, Y], [-Y.conj(), X.conj()]])
else:
err_print = ('`beta` parameter must be 1, 2 or 4.',
'Given: {}'.format(beta))
raise ValueError('\n'.join(err_print))
return la.eigvalsh(A + A.conj().T) / np.sqrt(2.0)
def semi_circle_law(x, R=2.0):
"""
.. seealso::
- :cite:`DuEd15` Table 1
- https://en.wikipedia.org/wiki/Wigner_semicircle_distribution
"""
return 2 / (np.pi * R**2) * np.sqrt(np.maximum(R**2 - x**2, 0.0))
def mu_ref_normal_sampler_tridiag(loc=0.0, scale=1.0, beta=2, size=10,
random_state=None):
"""Implementation of the tridiagonal model to sample from
.. math::
\\Delta(x_{1}, \\dots, x_{N})^{\\beta}
\\prod_{n=1}^{N} \\exp(-\\frac{(x_i-\\mu)^2}{2\\sigma^2} ) dx_i
.. seealso::
:cite:`DuEd02` II-C
"""
rng = check_random_state(random_state)
if not (beta > 0):
raise ValueError('`beta` must be positive. Given: {}'.format(beta))
# beta/2*[N-1, N-2, ..., 1]
b_2_Ni = 0.5 * beta * np.arange(size - 1, 0, step=-1)
alpha_coef = rng.normal(loc=loc, scale=scale, size=size)
beta_coef = rng.gamma(shape=b_2_Ni, scale=scale**2)
return la.eigvalsh_tridiagonal(alpha_coef, np.sqrt(beta_coef))
############
# Laguerre #
############
def laguerre_sampler_full(M, N, beta=2,
random_state=None):
rng = check_random_state(random_state)
if beta == 1:
A = rng.randn(N, M)
elif beta == 2:
A = rng.randn(N, M) + 1j * rng.randn(N, M)
elif beta == 4:
X = rng.randn(N, M) + 1j * rng.randn(N, M)
Y = rng.randn(N, M) + 1j * rng.randn(N, M)
A = np.block([[X, Y], [-Y.conj(), X.conj()]])
else:
err_print = ('`beta` parameter must be 1, 2 or 4.',
'Given: {}'.format(beta))
raise ValueError('\n'.join(err_print))
return la.eigvalsh(A.dot(A.conj().T))
def marcenko_pastur_law(x, M, N, sigma=1.0):
""" M >= N
.. seealso::
- :cite:`DuEd15` Table 1
- https://en.wikipedia.org/wiki/Marchenko-Pastur_distribution
"""
c = N / M
Lm, Lp = (sigma * (1 - np.sqrt(c)))**2, (sigma * (1 + np.sqrt(c)))**2
return np.sqrt(np.maximum((Lp-x)*(x-Lm),0)) / (c*x) / (2*np.pi*sigma**2)
def mu_ref_gamma_sampler_tridiag(shape=1.0, scale=1.0, beta=2, size=10,
random_state=None):
"""
.. seealso::
:cite:`DuEd02` III-B
"""
rng = check_random_state(random_state)
if not (beta > 0):
raise ValueError('`beta` must be positive. Given: {}'.format(beta))
# beta/2*[N-1, N-2, ..., 1, 0]
b_2_Ni = 0.5 * beta * np.arange(size - 1, -1, step=-1)
# xi_odd = xi_1, ... , xi_2N-1
xi_odd = rng.gamma(shape=b_2_Ni + shape, scale=scale) # odd
# xi_even = xi_0=0, xi_2, ... ,xi_2N-2
xi_even = np.zeros(size)
xi_even[1:] = rng.gamma(shape=b_2_Ni[:-1], scale=scale) # even
# alpha_i = xi_2i-2 + xi_2i-1, xi_0 = 0
alpha_coef = xi_even + xi_odd
# beta_i+1 = xi_2i-1 * xi_2i
beta_coef = xi_odd[:-1] * xi_even[1:]
return la.eigvalsh_tridiagonal(alpha_coef, np.sqrt(beta_coef))
##########
# Jacobi #
##########
def jacobi_sampler_full(M_1, M_2, N, beta=2,
random_state=None):
rng = check_random_state(random_state)
if beta == 1:
X = rng.randn(N, M_1)
Y = rng.randn(N, M_2)
elif beta == 2:
X = rng.randn(N, M_1) + 1j * rng.randn(N, M_1)
Y = rng.randn(N, M_2) + 1j * rng.randn(N, M_2)
elif beta == 4:
X_1 = rng.randn(N, M_1) + 1j * rng.randn(N, M_1)
X_2 = rng.randn(N, M_1) + 1j * rng.randn(N, M_1)
Y_1 = rng.randn(N, M_2) + 1j * rng.randn(N, M_2)
Y_2 = rng.randn(N, M_2) + 1j * rng.randn(N, M_2)
X = np.block([[X_1, X_2], [-X_2.conj(), X_1.conj()]])
Y = np.block([[Y_1, Y_2], [-Y_2.conj(), Y_1.conj()]])
else:
err_print = ('`beta` parameter must be 1, 2 or 4.',
'Given: {}'.format(beta))
raise ValueError('\n'.join(err_print))
X_tmp = X.dot(X.conj().T)
Y_tmp = Y.dot(Y.conj().T)
return la.eigvals(X_tmp.dot(la.inv(X_tmp + Y_tmp))).real
def wachter_law(x, M_1, M_2, N):
""" M_1, M_2>=N
.. seealso::
:cite:`DuEd15` Table 1
"""
a, b = M_1 / N, M_2 / N
Lm = ((np.sqrt(a * (a + b - 1)) - np.sqrt(b)) / (a + b))**2
Lp = ((np.sqrt(a * (a + b - 1)) + np.sqrt(b)) / (a + b))**2
return (a+b)/(2*np.pi) * 1/(x*(1-x)) * np.sqrt(np.maximum((Lp-x)*(x-Lm),0))
def mu_ref_beta_sampler_tridiag(a, b, beta=2, size=10,
random_state=None):
""" Implementation of the tridiagonal model given by Theorem 2 of :cite:`KiNe04` to sample from
.. math::
\\Delta(x_{1}, \\dots, x_{N})^{\\beta}
\\prod_{n=1}^{N} x^{a-1} (1-x)^{b-1} dx
.. seealso::
:cite:`KiNe04` Theorem 2
"""
rng = check_random_state(random_state)
if not (beta > 0):
raise ValueError('`beta` must be positive. Given: {}'.format(beta))
# beta/2*[N-1, N-2, ..., 1, 0]
b_2_Ni = 0.5 * beta * np.arange(size - 1, -1, step=-1)
# c_odd = c_1, c_3, ..., c_2N-1
c_odd = rng.beta(b_2_Ni + a, b_2_Ni + b)
# c_even = c_0, c_2, c_2N-2
c_even = np.zeros(size)
c_even[1:] = rng.beta(b_2_Ni[:-1], b_2_Ni[1:] + a + b)
# xi_odd = xi_2i-1 = (1-c_2i-2) c_2i-1
xi_odd = (1 - c_even) * c_odd
# xi_even = xi_0=0, xi_2, xi_2N-2
# xi_2i = (1-c_2i-1)*c_2i
xi_even = np.zeros(size)
xi_even[1:] = (1 - c_odd[:-1]) * c_even[1:]
# alpha_i = xi_2i-2 + xi_2i-1
# alpha_1 = xi_0 + xi_1 = xi_1
alpha_coef = xi_even + xi_odd
# beta_i+1 = xi_2i-1 * xi_2i
beta_coef = xi_odd[:-1] * xi_even[1:]
return la.eigvalsh_tridiagonal(alpha_coef, np.sqrt(beta_coef))
#####################
# Circular ensemble #
#####################
def circular_sampler_full(N, beta=2, haar_mode='QR',
random_state=None):
"""
.. seealso::
:cite:`Mez06` Section 5
"""
rng = check_random_state(random_state)
if haar_mode == 'Hermite':
# size_sym_mat = int(N*(N-1)/2)
if beta == 1: # COE
A = rng.randn(N, N)
elif beta == 2: # CUE
A = rng.randn(N, N) + 1j * rng.randn(N, N)
elif beta == 4:
X = rng.randn(N, N) + 1j * rng.randn(N, N)
Y = rng.randn(N, N) + 1j * rng.randn(N, N)
A = np.block([[X, Y], [-Y.conj(), X.conj()]])
else:
err_print = ('For `haar_mode="hermite"`, `beta` = 1, 2 or 4.',
'Given: {}'.format(beta))
raise ValueError('\n'.join(err_print))
_, U = la.eigh(A + A.conj().T)
elif haar_mode == 'QR':
if beta == 1: # COE
A = rng.randn(N, N)
elif beta == 2: # CUE
A = rng.randn(N, N) + 1j * rng.randn(N, N)
# elif beta==4:
else:
err_print = ('With `haar_mode="QR", `beta` = 1 or 2.',
'Given: {}'.format(beta))
raise ValueError('\n'.join(err_print))
# U, _ = la.qr(A)
Q, R = la.qr(A)
d = np.diagonal(R)
U = np.multiply(Q, d / np.abs(d), Q)
else:
err_print = ('Invalid `haar_mode`.',
'Choose from `haar_mode="Hermite" or "QR".',
'Given: {}'.format(haar_mode))
raise ValueError('\n'.join(err_print))
return la.eigvals(U)
def mu_ref_unif_unit_circle_sampler_quindiag(beta=2, size=10,
random_state=None):
"""
.. see also::
:cite:`KiNe04` Theorem 1
"""
rng = check_random_state(random_state)
if not (isinstance(beta, int) and (beta > 0)):
raise ValueError('`beta` must be positive integer.\
Given: {}'.format(beta))
alpha = np.zeros(size, dtype=np.complex_)
# nu = 1 + beta*(N-1, N-2, ..., 0)
for i, nu in enumerate(1 + beta * np.arange(size - 1, -1, step=-1)):
gauss_vec = rng.randn(nu + 1)
alpha[i] = (gauss_vec[0] + 1j * gauss_vec[1]) / la.norm(gauss_vec)
rho = np.sqrt(1 - np.abs(alpha[:-1])**2)
xi = np.zeros((size - 1, 2, 2), dtype=np.complex_) # xi[0,..,N-1]
xi[:, 0, 0], xi[:, 0, 1] = alpha[:-1].conj(), rho
xi[:, 1, 0], xi[:, 1, 1] = rho, -alpha[:-1]
# L = diag(xi_0, xi_2, ...)
# M = diag(1, xi_1, x_3, ...)
# xi[N-1] = alpha[N-1].conj()
if size % 2 == 0: # even
L = sp.block_diag(xi[::2, :, :],
dtype=np.complex_)
M = sp.block_diag([1.0, *xi[1::2, :, :], alpha[-1].conj()],
dtype=np.complex_)
else: # odd
L = sp.block_diag([*xi[::2, :, :], alpha[-1].conj()],
dtype=np.complex_)
M = sp.block_diag([1.0, *xi[1::2, :, :]],
dtype=np.complex_)
return la.eigvals(L.dot(M).toarray())
###########
# Ginibre #
###########
def ginibre_sampler_full(N, random_state=None):
"""Compute the eigenvalues of a random complex standard Gaussian matrix"""
rng = check_random_state(random_state)
A = rng.randn(N, N) + 1j * rng.randn(N, N)
return la.eigvals(A) / np.sqrt(2.0)