from numpy import sqrt import timeit # created by Yu Hu ([email protected]), Aug 2020 plt.rcParams.update({'font.size': 18}) # compare simulation and iid Gaussian theory g_ls = [0.5] N_ls = [100, 400, 800] ifig = 0 for g in g_ls: for N in N_ls: ifig += 1 J = g * randn(N, N) / sqrt(N) C = J2C(J) eig_C = eigvalsh(C) x, px = pdf_g(g, nx=1000) fig = plt.figure(figsize=(8, 6)) plt.plot(x, px, linewidth=1.5, label='theory') plt.hist(eig_C, 40, density=True, label='N=' + str(N)) plt.plot([x[0], x[-1]], [0, 0], '.', markersize=10) plt.xlabel('cov eigenvalues') plt.ylabel('probabilty') plt.legend() plt.title('g=' + str(g)) plt.tight_layout() fig.savefig('./figure/figure1a_' + str(ifig) + '.png', dpi=600) # different g
print('0.995 quantile r:', r_quantile) J = randn(N,N)/sqrt(N)*g # kdivh = 0.25 # squared root of the kappa notations in text kdivh = 0.45 # squared root of the kappa notations in text kdiv = sqrt(kdivh**2/(1-kdivh**2)*(g**2/N)) # g is after removing div motifs print('b entry var b/sqrt(N): ', kdiv*sqrt(N)) print('xuv, x=', N*kdiv) b = randn(N) * kdiv # squared root of the kappa notations in text print('check', np.linalg.norm(b)*np.linalg.norm(ones(N))) print('outlier theory:', J_uv_outlier(g,N*kdiv)) print('outlier theory:', J_uv_outlier(g,np.linalg.norm(b)*np.linalg.norm(ones(N)))) Jm = J + np.outer(ones(N),b) x,px = pdf_g(g) C = J2C(J) Cm = J2C(Jm) eig_C = eigvalsh(C) eig_Cm = eigvalsh(Cm) fig = plt.figure(figsize=(8,4)) plt.hist(eig_C, 40, density=True, label='N='+str(N)); plt.plot(x,px, linewidth=1.5, label='g='+str(g)) x_lim = plt.xlim() plt.xlabel('cov eigenvalues') plt.ylabel('probability') plt.legend() plt.title(r'Cov spectrum with $J$') plt.tight_layout() fig.savefig('./figure/figure3a_1_cov_J.png', dpi=600) eig_J = eigvals(J)
plt.legend() plt.xlabel('cov eigenvalues') plt.ylabel('probabilty') plt.title('Random antisymmetric connectivity') plt.tight_layout() yline = linspace(0, 9, 400) plt.plot(x[-1] * ones(400), yline, color='grey', linestyle='--', linewidth='3') plt.ylim(0, 9) fig.savefig('./figure/figure5a.png', dpi=600) # example bimodal g = 1.1 kre = -0.99 N = 400 J = J_g_kre(N, g, kre) C = J2C(J) eig_C = eigvalsh(C) x, px = pdf_g_kre(g, kre, nx=10000) fig = plt.figure(figsize=(8, 6)) plt.hist(eig_C, 40, density=True, color='orange', label='N=' + str(N)) t0 = timeit.default_timer() ipeaks, _ = find_peaks(px) t1 = timeit.default_timer() print('finding peaks:', t1 - t0) line = plt.plot(x, px, 'b', linewidth=2, label='$g=' + str(g) + ',\;\hat{\kappa}_{re}=' + str(kre) + '$') plt.plot([x[0], x[-1]], [0, 0], '.', color=line[0].get_color(), markersize=10)
# wi = 1.5*p*w0 # x1 = -wi*n + p*n*w0 n_ls = np.array([400, 800, 1200, 1600, 2000]) nn = len(n_ls) ntrial = 400 x1p_ls = zeros((ntrial, nn)) t0 = timeit.default_timer() for i, n in enumerate(n_ls): p = 4 * x1**2 / (4 * x1**2 + n * g**2) w0 = g / sqrt(n * p * (1 - p)) wi = 1.5 * p * w0 for t in range(ntrial): J = np.random.binomial(1, p, (n, n)) J = J * w0 J = J - wi * ones((n, n)) C = J2C(J) eig_C = np.sort(eigvalsh(C)) x1p_ls[t, i] = eig_C[0] t1 = timeit.default_timer() t01 = t1 - t0 print('simulation time: ', t01) with open('./data/figure_outlier_ER_subI_MSE_' + str(plot_id) + '.npz', 'wb') as file1: np.savez(file1, g, x1, n_ls, ntrial, x1p_ls, t01) else: with np.load('./data/figure_outlier_ER_subI_MSE_' + str(plot_id) + '.npz') as file1: g = file1['arr_0'] x1 = file1['arr_1'] n_ls = file1['arr_2'] ntrial = file1['arr_3']