rmtkernel.py
42 lines
| 1.2 KiB
| text/x-python
|
PythonLexer
Brian E Granger
|
r1337 | #------------------------------------------------------------------------------- | ||
# Core routines for computing properties of symmetric random matrices. | ||||
#------------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r4591 | import numpy as np | ||
ra = np.random | ||||
la = np.linalg | ||||
Brian E Granger
|
r1337 | |||
def GOE(N): | ||||
"""Creates an NxN element of the Gaussian Orthogonal Ensemble""" | ||||
m = ra.standard_normal((N,N)) | ||||
m += m.T | ||||
Brian E. Granger
|
r4591 | return m/2 | ||
Brian E Granger
|
r1337 | |||
Brian E. Granger
|
r4591 | def center_eigenvalue_diff(mat): | ||
Brian E Granger
|
r1337 | """Compute the eigvals of mat and then find the center eigval difference.""" | ||
N = len(mat) | ||||
Brian E. Granger
|
r4591 | evals = np.sort(la.eigvals(mat)) | ||
diff = np.abs(evals[N/2] - evals[N/2-1]) | ||||
return diff | ||||
Brian E Granger
|
r1337 | |||
Brian E. Granger
|
r4591 | def ensemble_diffs(num, N): | ||
"""Return num eigenvalue diffs for the NxN GOE ensemble.""" | ||||
diffs = np.empty(num) | ||||
Brian E Granger
|
r1337 | for i in xrange(num): | ||
mat = GOE(N) | ||||
Brian E. Granger
|
r4591 | diffs[i] = center_eigenvalue_diff(mat) | ||
Brian E Granger
|
r1337 | return diffs | ||
Brian E. Granger
|
r4591 | def normalize_diffs(diffs): | ||
Brian E Granger
|
r1337 | """Normalize an array of eigenvalue diffs.""" | ||
return diffs/diffs.mean() | ||||
Brian E. Granger
|
r4591 | def normalized_ensemble_diffs(num, N): | ||
"""Return num *normalized* eigenvalue diffs for the NxN GOE ensemble.""" | ||||
diffs = ensemble_diffs(num, N) | ||||
return normalize_diffs(diffs) | ||||
Brian E Granger
|
r1337 | |||