def test_minimize(self): print('Finding maximum likelihood') sampler_args = { "method":'Powell', "tol":None, "callback":None, "options":{'xtol':1E-4,'ftol':1E-4,'maxiter':None,'maxfev':None,'direc':None} } rdict=clean_pixels(self.maplike,run_minimize,**sampler_args) print("Results: ",) if rdict['ML_success'] : print(" Successful maximization") else : print(" Unsuccessful maximization") print(" Param truth: ",self.true_params) print(" Param ML: ",rdict['params_ML']) print("\n")
def test_emcee(self): # Calculate the p value and reduced chi squred for the true parameter values # in the 4 pixels above. sampler_args = { "nwalkers": 20, "nsamps": 500, "nburn": 100, "verbose": True } samples=clean_pixels(self.maplike,run_emcee,**sampler_args)['chains'] ranges = lambda v: (v[1], v[2]-v[1], v[1]-v[0]) params_mcmc = list(map(ranges, zip(*np.percentile(samples, [16, 50, 84], axis=0)))) print("Results:", ) print("Param medians: ", [p[0] for p in params_mcmc]) print("Param spread, 14th to 84th percentile: ", [p[1] + p[2] for p in params_mcmc]) labels = [r"$\beta_s$", r"$\beta_d$", r"$T_d$", r"$\beta_c$"] fig = corner.corner(samples, labels=labels, truths=self.true_params) plt.show() print("\n")
def test_fisher(self): print('Fisher sampler') sampler_args = { "ml_first": True, "ml_method":'Powell', "ml_options":{'xtol':1E-4,'ftol':1E-4,'maxiter':None,'maxfev':None,'direc':None} } rdict=clean_pixels(self.maplike,run_fisher,**sampler_args) covar=np.linalg.inv(rdict['fisher_m']) pbias=np.dot(covar,rdict['fisher_v']) print("Results: ",) if rdict['ML_success'] : print(" Successful maximization") else : print(" Unsuccessful maximization") print(" Param truth: ",self.true_params) print(" Param ML: ",rdict['params_cent']) print(" Param sigma: ",np.sqrt(np.diag(covar))) print(" Paramb bias: ",pbias) print("\n")
'var_prior_mean': var_prior_mean, 'var_prior_width': var_prior_width, 'var_prior_type': ['tophat' for b in var_pars] }, sky_true, instrument) if W_FWHM: ml_rw = mpl.MapLike( { 'data': mps_d_rw[isim][:, ips, :], 'noisevar': mps_nv[:, ips, :]**2, 'fixed_pars': fixed_pars, 'var_pars': var_pars, 'var_prior_mean': var_prior_mean, 'var_prior_width': var_prior_width, 'var_prior_type': ['tophat' for b in var_pars] }, sky_true, instrument) rdict = clean_pixels(ml, run_minimize, **sampler_args) if VERBOSE > 1: print(" - %d function evaluations" % (rdict['ML_nev'])) if W_FWHM: amps = ml_rw.get_amplitude_mean(rdict['params_ML']).reshape( [2, npix_here, 3]) cova = np.linalg.inv( ml_rw.get_amplitude_covariance(rdict['params_ML'])).reshape( [2, npix_here, 3, 3]) else: amps = ml.get_amplitude_mean(rdict['params_ML']).reshape( [2, npix_here, 3]) cova = np.linalg.inv( ml.get_amplitude_covariance(rdict['params_ML'])).reshape( [2, npix_here, 3, 3]) spec_o[isim, 0][ips] = rdict['params_ML'][0]
def clean_maps(k, fn, sdir): testmap = hp.read_map(f'{fn}maps_sky_signal.fits', field=np.arange(12), verbose=False) Qs = testmap[::2, sat_mask>0] Us = testmap[1::2, sat_mask>0] skymaps = np.array([np.transpose(Qs), np.transpose(Us)]) nu_ref_sync_p=23. beta_sync_fid=-3. curv_sync_fid=0. nu_ref_dust_p=353. beta_dust_fid=1.5 temp_dust_fid=19.6 spec_i=np.zeros([2, npix]); spec_o=np.zeros([2, npix]); amps_o=np.zeros([3, 2, npix]); cova_o=np.zeros([6, 2, npix]); bs=beta_sync_fid; bd=beta_dust_fid; td=temp_dust_fid; cs=curv_sync_fid; sbs=3.0; sbd=3.0; spec_i[0]=bs; spec_i[1]=bd fixed_pars={'nu_ref_d':nu_ref_dust_p,'nu_ref_s':nu_ref_sync_p,'T_d':td} var_pars=['beta_s','beta_d'] var_prior_mean=[bs,bd] var_prior_width=[sbs,sbd] sky_true=sky.SkyModel(['syncpl', 'dustmbb', 'unit_response']) nus = [27., 39., 93., 145., 225., 280.] bps=np.array([{'nu':np.array([n-0.5,n+0.5]),'bps':np.array([1])} for n in nus]) instrument=ins.InstrumentModel(bps) ml=mpl.MapLike({'data': skymaps, 'noisevar':np.ones_like(skymaps), 'fixed_pars':fixed_pars, 'var_pars':var_pars, 'var_prior_mean':var_prior_mean, 'var_prior_width':var_prior_width, 'var_prior_type':['tophat' for b in var_pars]}, sky_true, instrument) sampler_args = { "method" : 'Powell', "tol" : None, "callback" : None, "options" : {'xtol':1E-4,'ftol':1E-4,'maxiter':None,'maxfev':None,'direc':None} } rdict = clean_pixels(ml, run_minimize, **sampler_args) Sbar = ml.f_matrix(rdict['params_ML']).T Sninv = np.linalg.inv(np.dot(Sbar.T, Sbar)) P = np.diag([1., 1., 0.]) Q = np.identity(6) - Sbar.dot(P).dot(Sninv).dot(Sbar.T) reducedmaps = np.einsum('ab, cdb', Q, skymaps).reshape((12, -1)) filled_maps = np.zeros((12, hp.nside2npix(nside))) filled_maps[:, sat_mask>0] = reducedmaps np.savez(f'{sdir}{sname}_hybrid_params_{k}', params=rdict['params_ML'], Sbar=Sbar, Q=Q) hp.write_map(f'{sdir}{sname}_residualmaps_{k}.fits', filled_maps, overwrite=True) return