hp.gnomview(output_map, rot=[0,90], reso=0.3, xsize=600,title='Output Map') # get theta iprings=np.arange(12*nside**2) vecs=hp.pix2vec(int(nside),iprings[~mask]) vec0=hp.pix2vec(int(nside),0) angles=np.arccos(np.dot(np.transpose(vec0),vecs)) themap=output_map[~mask] clf() plot(angles*180/np.pi,themap,'b.') xlim(0,10) yscale('log') from Homogeneity import fitting def radgauss(x,pars): return(pars[0]/(2*np.pi*pars[1]**2)*np.exp(-0.5*(x/pars[1])**2)) angmax=1.6 ok=angles*180/np.pi < angmax clf() plot(angles*180/np.pi,themap,'b,') #plot(angles[ok]*180/np.pi,themap[ok],'r,') yscale('log') xlabel('Angle w.r.t. point source position (deg.)') ylabel('Map value') bla=fitting.dothefit(angles[ok]*180/np.pi,themap[ok],themap[ok]*0+0.01,np.array([0.004,0.275]),functname=radgauss,method='mpfit') title('Fit value : 37.1 arcmin FWHM') xx=linspace(0,angmax,1000) plot(xx,radgauss(xx,bla[1]),'r',lw=1)
# study covariance matrix # 1/ it is extremely diagonal => the map making does not correlate pixels significantly... weird from Homogeneity import fitting def ffunc(x,pars): return(pars[0]*x**pars[1]) npix=diag(covmc).size clf() plot(signoise**2/coverage[~mask],diag(covmc),'r.') bla=fitting.dothefit(signoise**2/coverage[~mask],diag(covmc),ones(npix),[7500.,1.],functname=ffunc,method='minuit') xlabel('naive diag covariance matrix') ylabel('MC diag covariance matrix') xx=linspace(0,0.001,10000) plot(xx,xx,'k--') plot(xx,ffunc(xx,bla[1]),lw=3) clf() plot(bla[1][0]*(signoise**2/coverage[~mask])**bla[1][1],diag(covmc),'r.') xlabel('Model Diag Covariance Matrix from Coverage') ylabel('MC Diag Covariance Matrix') xx=linspace(0,0.06,10000) plot(xx,xx,'g--',lw=3)
drh_m = err_on_funct / deriv_spl(rh) clf() xv = linspace(min(thex), max(thex), 1000) xlabel('$r [h^{-1}.\mathrm{Mpc}]$') ylabel('$d_2(r)$') plot(arange(200), zeros(200) + 2.97, '--') errorbar(thex, they, yerr=theyerr, fmt='ko') plot(xv, spl(xv), label='Spline Fit (with covariance)', color='b') errorbar(rh_m, 2.97, xerr=drh_m, label='$R_H$ = ' + str('%.1f' % rh_m) + ' $\pm$ ' + str('%.1f' % drh_m) + ' $h^{-1}.\mathrm{Mpc}$', fmt='ro') legend(loc='lower right') ##### test from Homogeneity import fitting import numpy as np import numpy.random as npr npr.seed(0) x = linspace(0, 10, 20) y = 3 * x**2 + 15 + randn(20) err = np.array([1] * 20) errorbar(x, y, err, fmt='.') toto = fitting.dothefit(x, y, err, [0, 0, 0], functname=fitting.thepolynomial) plot(x, fitting.thepolynomial(x, toto[1]))
rh_m=rh drh_m=err_on_funct/deriv_spl(rh) clf() xv=linspace(min(thex),max(thex),1000) xlabel('$r [h^{-1}.\mathrm{Mpc}]$') ylabel('$d_2(r)$') plot(arange(200),zeros(200)+2.97,'--') errorbar(thex,they,yerr=theyerr,fmt='ko') plot(xv,spl(xv),label='Spline Fit (with covariance)',color='b') errorbar(rh_m,2.97,xerr=drh_m,label='$R_H$ = '+str('%.1f'%rh_m)+' $\pm$ '+str('%.1f'%drh_m)+' $h^{-1}.\mathrm{Mpc}$',fmt='ro') legend(loc='lower right') ##### test from Homogeneity import fitting import numpy as np import numpy.random as npr npr.seed(0) x = linspace(0,10,20) y = 3*x**2+15+ randn(20) err = np.array([1]*20) errorbar(x,y,err,fmt='.'); toto=fitting.dothefit(x,y,err,[0,0,0],functname=fitting.thepolynomial) plot(x,fitting.thepolynomial(x,toto[1]))
cho = np.linalg.cholesky(cov) corr_data = np.dot(cho.T,data) ##### see if cholesky is correct: print 'Accuracy of Cholesky Decomposition' print np.dot(cho,cho.T.conj()) == cov def model(x,pars): f=np.poly1d(pars) return(f(x)) guess1 = [np.mean(data),np.mean(data)] guess2 = [np.mean(corr_data),np.mean(corr_data)] res = fitting.dothefit(x,data,cov,guess1,functname=model,method='minuit') corr_res = fitting.dothefit(x,corr_data,cov,guess2,functname=model,method='minuit') plt.figure() plt.errorbar(x,data,yerr=np.diag(cov),color='b',fmt='o-',label='data' ) plt.errorbar(x,corr_data,yerr=np.diag(cov),color='g',fmt='o-',label='corr data') plt.plot(x,model(x,res[1]),'r-') plt.plot(x,model(x,corr_res[1]),'r--') plt.legend(loc=2,numpoints=1) plt.show()
dphi=np.random.randn(npoints)*sigptg[j]/3600/np.sin(true_pointings[:,0]*np.pi/180) new_pointings[:,0]=true_pointings[:,0]+dtheta new_pointings[:,1]=true_pointings[:,1]+dphi new_pointings[:,2]=true_pointings[:,2]+0 # maps maptrueptg,masktrue,cctrue=TOD2map(tod,true_pointings,nside,disp=False,covmin=100) map,mask,cc=TOD2map(tod,new_pointings,nside,disp=False,covmin=100) mm=mask+masktrue mask[mm]=np.nan masktrue[mm]=np.nan allressig[i]=np.std(map[~mm]-maptrueptg[~mm]) cltrueptg=hp.anafast(nan_to_num(maptrueptg)) cl=hp.anafast(nan_to_num(map)) allratios[i,j,:]=cl/cltrueptg l=np.arange(3*nside) fit=fitting.dothefit(l,cl/cltrueptg,np.ones(3*nside),[np.radians(sigptg[j]/3600),0.,0.],functname=fct,method='mpfit') clf() plot(l,cl/cltrueptg) plot(l,fct(l,fit[1])) draw() sigrec[n,i,j]=fit[1][0] sigma[n,j]=np.mean(allressig) dsigma[n,j]=np.std(allressig) dictio[nside]=allratios clf() xlim(np.min(sigptg)/2,np.max(sigptg)*2) xscale('log')
res = hist(xvalues, bins=100, range = [-10,15]) xcenter = (res[1][:-1]+res[1][1:])/2 yvals = res[0] errors = np.sqrt(yvals) def mygaussian(x,pars): return(pars[0]*np.exp(-(x-pars[1])**2/(2*pars[2]**2))) avguess = np.sum(xcenter*yvals)/np.sum(yvals) av2guess = np.sum(xcenter**2*yvals)/np.sum(yvals) varguess = av2guess-avguess**2 guess = [np.max(yvals), avguess, np.sqrt(varguess)] ok = errors != 0 result_minuit = fitting.dothefit(xcenter[ok],yvals[ok],errors[ok],guess,functname=mygaussian,method='minuit') result_mpfit = fitting.dothefit(xcenter[ok],yvals[ok],errors[ok],guess,functname=mygaussian,method='mpfit') result_mcmc = fitting.dothefit(xcenter[ok],yvals[ok],errors[ok],guess,functname=mygaussian,method='mcmc') clf() errorbar(xcenter,yvals,yerr=errors,fmt='ro') xxx = np.linspace(-10,15,1000) plot(xxx, mygaussian(xxx, result_minuit[1])) plot(xxx, mygaussian(xxx, result_mpfit[1])) plot(xxx, mygaussian(xxx, result_mcmc[1]))
def toto(x, pars): return (newtt * pars[0] + pars[2]) * np.exp((x / pars[1])) guess = np.array([1.0 / 10000000, 100, 5.5e-4]) clf() ii = 0 errorbar(ellbins, clbb[ii], yerr=errclbb[ii]) thesig = sigmas[ii] plot(ellbins, toto(ellbins, guess)) bla = fitting.dothefit(ellbins, clbb[ii], errclbb[ii], guess, functname=toto) clf() errorbar(ellbins, clbb[ii], yerr=errclbb[ii]) plot(ellbins, toto(ellbins, bla[1])) clf() ellscale = np.zeros(len(sigmas)) normalisation = np.zeros(len(sigmas)) err_ellscale = np.zeros(len(sigmas)) err_normalisation = np.zeros(len(sigmas)) for i in xrange(len(sigmas)): subplot(3, 3, i + 1) thesig = sigmas[i] bla = fitting.dothefit(ellbins, clbb[i], errclbb[i], guess, functname=toto)
plot(lll,clBBl,'g',alpha=0.5) plot(ll,clBB*2,'b',alpha=0.5) plot(ll,clBBl+clBB*2,'r') #### adding noise residuals and try fitting def functsum(x,pars): bla = pars[0] * np.interp(x, ll, clBB) + pars[1] * np.interp(x, ll, clBBl) + pars[2]**2 * x * (x + 1) / 2 / np.pi return bla def functsum2(x,pars): bla = pars[0] * np.interp(x, ll, clBB) + pars[1] * np.interp(x, ll, clBBl) return bla guess = [2., 1., np.sqrt(1e6)] fit = fitting.dothefit(lcenter, BB, dBB, guess, functname = functsum, method = 'minuit') res = fit[1] guess2 = [2., 1.] fit2 = fitting.dothefit(lcenter, BB, dBB, guess2, functname = functsum2, method = 'minuit') res2 = fit2[1] #### adding noise clnoise = res[2]**2 pownoise = clnoise*ll*(ll+1)/2/np.pi clf() xlim(0,500) ylim(-0.01,0.055) errorbar(lcenter,BB,fmt='ro',xlolims=lmin,xuplims=lmax,yerr=dBB) plot(lll,clBBl * res[1],'g',alpha=0.5)
errors = np.sqrt(yvals) def mygaussian(x, pars): return (pars[0] * np.exp(-(x - pars[1])**2 / (2 * pars[2]**2))) avguess = np.sum(xcenter * yvals) / np.sum(yvals) av2guess = np.sum(xcenter**2 * yvals) / np.sum(yvals) varguess = av2guess - avguess**2 guess = [np.max(yvals), avguess, np.sqrt(varguess)] ok = errors != 0 result_minuit = fitting.dothefit(xcenter[ok], yvals[ok], errors[ok], guess, functname=mygaussian, method='minuit') result_mpfit = fitting.dothefit(xcenter[ok], yvals[ok], errors[ok], guess, functname=mygaussian, method='mpfit') result_mcmc = fitting.dothefit(xcenter[ok], yvals[ok], errors[ok], guess, functname=mygaussian, method='mcmc')
#### simulated data nbpts = 25 signoise = 3. err = np.linspace(0.7, 1.3, nbpts)*signoise #signoise = 1. #err = np.zeros(nbpts)+signoise x = np.linspace(0,10,nbpts) y = thefct(x, partrue) + randn(nbpts)*signoise clf() errorbar(x,y,err,fmt='.'); xlim(-0.2,10.2) ############### Fitting initpar = np.array([10.,1.]) #### Minuit toto_minuit=fitting.dothefit(x,y,err,initpar,functname=thefct) #### MPFit (Fast Levenberg-Markardt minimizer) toto_mpfit=fitting.dothefit(x,y,err,initpar,functname=thefct, method='mpfit') #### MCMC [slow !!] toto_mcmc=fitting.dothefit(x,y,err,initpar,functname=thefct, method='mcmc') clf() xx = np.linspace(0,10,1000) plot(xx,thefct(xx,toto_mcmc[1]), label='MCMC') chains = toto_mcmc[0] for i in xrange(len(chains)/100): plot(xx,thefct(xx,chains[i,:]), color='b', alpha=0.01,lw=2) errorbar(x,y,err,fmt='.'); plot(xx,thefct(xx,toto_minuit[1]), label='Minuit', lw=2) plot(xx,thefct(xx,toto_mpfit[1]), label='MPFit', lw=2) legend()
def thegaussian(x,pars): return(pars[0]*np.exp(-(x-pars[1])**2/(2*pars[2]**2))) clf() uu=0 allx0 = np.zeros(nsub) for i in np.arange(nsub)+1: uu += 1 subplot(3,3,uu) mm = np.abs(freq-i*xm) < 0.3 plot(freq[mm]/i, smoothedps[mm]) ampguess = np.max(smoothedps[mm]) x0guess = np.sum(freq[mm]/i*smoothedps[mm])/np.sum(smoothedps[mm]) x02guess = np.sum((freq[mm]/i)**2*smoothedps[mm])/np.sum(smoothedps[mm]) sigguess = np.sqrt(x02guess-x0guess**2) res = fitting.dothefit(freq[mm]/i, smoothedps[mm],freq[mm]*0+1,[ampguess,x0guess,sigguess], functname=thegaussian) plot([res[1][1],res[1][1]],[0, np.max(smoothedps[mm])],'r:') plot(freq[mm]/i, thegaussian(freq[mm]/i, res[1])) allx0[i-1]=res[1][1] xm = np.mean(allx0) noire = 1/xm ixm = np.int(xm/framerate*len(signal)) inoire = np.int(noire * framerate) ########## trace tempo stability size = inoire*16
cosmo=cosmolopy.fidcosmo.copy() if min(pars) <= 0: return(np.zeros(x.size)-1) cosmo['h']=pars[0] cosmo['omega_M_0']=pars[1] cosmo['omega_lambda_0']=pars[2] dlum=cosmolopy.distance.luminosity_distance(x,**cosmo)*1e6 if min(dlum) > 0: return(5*np.log10(dlum)-5) else: return(np.zeros(x.size)-1) from Homogeneity import fitting ### ne marche pas a=fitting.dothefit(zsn,musn,dmusn,np.array([0.7,0.3,0.7]),themusn1a,method='mpfit') ### OK a=fitting.dothefit(zsn,musn,dmusn,np.array([0.7,0.3,0.7]),themusn1a,method='minuit') ### ne marche pas... a=fitting.dothefit(zsn,musn,dmusn,np.array([0.7,0.3,0.7]),themusn1a,method='mcmc') chain=a[0] clf() ylim(0,2) xlim(0,1) plot(chain[:,1],chain[:,2],',') #### essai avec pymc