def fitbias(ph, spectra, binit=[1, 0, 0, 0, 1], k=None, kmax=None): if k is not None and kmax is not None: ik = np.where(k > kmax)[0][0] else: ik = len(ph) tomin = lambda b: sum( (ph - tools.getmodel(spectra, [1] + list(b[:-1])) - b[-1]) [:ik]**2 ) #added shot noise rep = minimize(tomin, binit, method='Nelder-Mead', options={'maxfev':10000}) #rep = minimize(tomin, binit, options={'maxfev':10000}) return rep
spectra = np.loadtxt( './output/%s/spectra-%04d-%04d-%04d-R%d.txt' % (subf, aa * 10000, bs, nc, Rsm)).T k, spectra = spectra[0], spectra[1:] header = header.split(',') iv = len(header) fig, axar = plt.subplots(1, 2, figsize=(8, 4)) for ik, kmax in enumerate([0.1, 0.3, 0.5, 0.8, 1.0]): binit = [1., 0, 0, 0, sn] rep = fitbias(ph, spectra, k=k, kmax=kmax, binit=binit) print(rep) bvec = [1] + list(rep.x[:-1]) model = tools.getmodel(spectra, bvec) + rep.x[-1] axis = axar[0] if ik == 0: axis.plot(k, ph, 'k', label='Halo', lw=2) axis.plot(k, model, 'C%d--' % ik, lw=2, alpha=0.7) axis.set_xlabel('k (h/Mpc)', fontsize=12) axis.set_ylabel('$P$', fontsize=12) axis.legend(fontsize=12) axis.loglog() axis.grid() axis = axar[1] axis.plot(k, model / ph, 'C%d-' % ik, label='k=%0.2f' % kmax,
header = '1, b1, b2, bg, bk' if zadisp: np.savetxt('./output/%s/spectraza-%04d-%04d-%04d-R%d.txt' % (subf, aa * 10000, bs, nc, Rsm), np.vstack([k, spectra]).T.real, header='k / ' + header, fmt='%0.4e') else: np.savetxt('./output/%s/spectra-%04d-%04d-%04d-R%d.txt' % (subf, aa * 10000, bs, nc, Rsm), np.vstack([k, spectra]).T.real, header='k / ' + header, fmt='%0.4e') header = header.split(',') bvec = [1, 1, 1, 1, 1] model = tools.getmodel(spectra, bvec) iv = len(header) fig, ax = plt.subplots(1, iv, figsize=(15, 4), sharex=True) counter = 0 for i in range(iv): for j in range(i, iv): ax[i].plot(k, spectra[counter], '-C%d' % j, label=header[j]) ax[i].plot(k, -spectra[counter], '--C%d' % j) counter += 1 ax[i].set_title(header[i])