def __init__(self,cdts,Rcut,hyp,Dist,centre_init): """ Constructor of the logposteriorModule """ rad,thet = Deg2pc(cdts,centre_init,Dist) c,r,t,self.Rmax = TruncSort(cdts,rad,thet,Rcut) self.pro = c[:,2] self.cdts = c[:,:2] self.Dist = Dist #------------- poisson ---------------- self.quadrants = [0,np.pi/2.0,np.pi,3.0*np.pi/2.0,2.0*np.pi] self.poisson = st.poisson(len(r)/4.0) #-------------- priors ---------------- self.Prior_0 = st.norm(loc=centre_init[0],scale=hyp[0]) self.Prior_1 = st.norm(loc=centre_init[1],scale=hyp[1]) self.Prior_2 = st.halfcauchy(loc=0.01,scale=hyp[2]) self.Prior_3 = st.halfcauchy(loc=0.01,scale=hyp[3]) print "Module Initialized"
def LogLike(self,params,ndim,nparams): ctr= params[:2] rc = params[2] rt = params[3] a = params[4] b = params[5] #----- Checks if parameters' values are in the ranges if not Support(rc,rt,a,b) : return -1e50 #------- Obtains radii and angles --------- radii,theta = Deg2pc(self.cdts,ctr,self.Dist) ############### Radial likelihood ################### # Computes likelihood lf = (1.0-self.pro)*LikeField(radii,self.Rmax) lk = radii*(self.pro)*Kernel(radii,rc,rt,a,b) # In king's profile no objects is larger than tidal radius idBad = np.where(radii > rt)[0] lk[idBad] = 0.0 # Normalisation constant if rt < self.Rmax: up = rt else: up = self.Rmax cte = NormCte(np.array([rc,rt,a,b,up])) k = 1.0/cte llike_r = np.sum(np.log((k*lk + lf))) ##################### POISSON ################################### quarter = cut(theta,bins=self.quadrants,include_lowest=True) counts = value_counts(quarter) llike_t = self.poisson.logpmf(counts).sum() ################################################################## llike = llike_t + llike_r # print(llike) return llike
def LogLike(self, params, ndim, nparams): ctr = params[:2] rc = params[2] rt = params[3] #----- Checks if parameters' values are in the ranges if not Support(rc, rt): return -1e50 #------- Obtains radii and angles --------- radii, theta = Deg2pc(self.cdts, ctr, self.Dist) ############### Radial likelihood ################### # Computes likelihood lf = (1.0 - self.pro) * LikeField(radii, self.Rmax) lk = radii * (self.pro) * Kernel(radii, rc, rt) # In king's profile no objects is larger than tidal radius idBad = np.where(radii > rt)[0] lk[idBad] = 0.0 # Normalisation constant # w = rc**2 + r**2 y = rc**2 + self.Rmax**2 z = rc**2 + rt**2 a = (self.Rmax**2) / z + 4 * (rc - np.sqrt(y)) / np.sqrt(z) + np.log( y) - 2 * np.log(rc) # Truncated at Rm # a = (rt**2)/z - 4.0 + 4.0*(rc/np.sqrt(z)) + np.log(z) - 2*np.log(rc) # Truncated at Rt (i.e. No truncated) k = 2.0 / (a * (rc**2.0)) llike_r = np.sum(np.log((k * lk + lf))) ##################### POISSON ################################### quarter = cut(theta, bins=self.quadrants, include_lowest=True) counts = value_counts(quarter) llike_t = self.poisson.logpmf(counts).sum() ################################################################## llike = llike_t + llike_r # print(llike) return llike
def __init__(self, cdts, Rcut, hyp, Dist, centre_init): """ Constructor of the logposteriorModule """ rad, thet = Deg2pc(cdts, centre_init, Dist) c, r, t, self.Rmax = TruncSort(cdts, rad, thet, Rcut) self.pro = c[:, 2] self.cdts = c[:, :2] self.Dist = Dist #-------------- Finds the mode of the band ------- band_all = c[:, 3] idv = np.where(np.isfinite(c[:, 3]))[0] band = c[idv, 3] kde = st.gaussian_kde(band) x = np.linspace(np.min(band), np.max(band), num=1000) self.mode = x[kde(x).argmax()] print "Mode of band at ", self.mode #---- repleace NANs by mode ----- idnv = np.setdiff1d(np.arange(len(band_all)), idv) band_all[idnv] = self.mode self.delta_band = band_all - self.mode #------------- poisson ---------------- self.quadrants = [ 0, np.pi / 2.0, np.pi, 3.0 * np.pi / 2.0, 2.0 * np.pi ] self.poisson = st.poisson(len(r) / 4.0) #-------------- priors ---------------- self.Prior_0 = st.norm(loc=centre_init[0], scale=hyp[0]) self.Prior_1 = st.norm(loc=centre_init[1], scale=hyp[1]) self.Prior_2 = st.uniform(loc=-0.5 * np.pi, scale=np.pi) self.Prior_3 = st.halfcauchy(loc=0.01, scale=hyp[2]) self.Prior_4 = st.halfcauchy(loc=0.01, scale=hyp[3]) self.Prior_5 = st.halfcauchy(loc=0.01, scale=hyp[2]) self.Prior_6 = st.halfcauchy(loc=0.01, scale=hyp[3]) self.Prior_7 = st.truncexpon(b=hyp[4], loc=0.01, scale=hyp[5]) self.Prior_8 = st.truncexpon(b=hyp[4], loc=0.01, scale=hyp[5]) self.Prior_9 = st.norm(loc=hyp[6], scale=hyp[7]) print("Module Initialized")
def LogLike(self,params,ndim,nparams): ctr= params[:2] rc = params[0] a = params[1] g = params[2] #----- Checks if parameters' values are in the ranges if not Support(rc,a,g) : return -1e50 #------- Obtains radii and angles --------- radii,theta = Deg2pc(self.cdts,ctr,self.Dist) ############### Radial likelihood ################### # Computes likelihood lf = (1.0-self.pro)*LikeField(radii,self.Rmax) lk = radii*(self.pro)*Kernel(radii,rc,a,g) # Normalisation constant x = -1.0*((rc/self.Rmax)**(-1.0/a)) u = -a*(g-2.0) v = 1.0+ a*g z = ((-1.0+0j)**(a*(g-2.0)))*a*(rc**2) betainc = (((x+0j)**u)/u)*hyp2f1(u,1.0-v,1.0+u,x) k = 1.0/np.abs(z*betainc) llike_r = np.sum(np.log((k*lk + lf))) ##################### POISSON ################################### quarter = cut(theta,bins=self.quadrants,include_lowest=True) counts = value_counts(quarter) llike_t = self.poisson.logpmf(counts).sum() ################################################################## llike = llike_t + llike_r # print(llike) return llike
# extracts coordinates cdtsT = np.c_[tycho['RAJ2000'], tycho['DEJ2000'], tycho['Jmag']] cdtsD = np.c_[dance['RA'], dance['Dec'], dance["J"]] cdts = np.vstack([cdtsT, cdtsD]) #---- removes duplicateds -------- sumc = np.sum(cdts[:, :2], axis=1) idx = np.unique(sumc, return_index=True)[1] cdts = cdts[idx] sumc = np.sum(cdts[:, :2], axis=1) if len(sumc) != len(list(set(sumc))): sys.exit("Duplicated entries in Coordinates!") #------- Real distances and position angles radii, theta = Deg2pc(cdts, centre, Dist) id_17 = np.where(cdts[:, 2] <= 17)[0] id_18 = np.where(cdts[:, 2] <= 18)[0] id_19 = np.where(cdts[:, 2] <= 19)[0] id_20 = np.where(cdts[:, 2] <= 20)[0] id_21 = np.where(cdts[:, 2] <= 21)[0] rad_17, the_17 = Deg2pc(cdts[id_17, :], centre, Dist) rad_18, the_18 = Deg2pc(cdts[id_18, :], centre, Dist) rad_19, the_19 = Deg2pc(cdts[id_19, :], centre, Dist) rad_20, the_20 = Deg2pc(cdts[id_20, :], centre, Dist) rad_21, the_21 = Deg2pc(cdts[id_21, :], centre, Dist) pdf = PdfPages(fout) plt.figure()
# ----- select only 100 parameters to plot samp = samples[np.random.choice(np.arange(len(samples)),size=100,replace=False)] ############### Stores the covariance matrix ############## print("Finding covariance matrix around MAP ...") covar = fCovar(samples,MAP) np.savetxt(dir_out+"/"+model+"_covariance.txt",covar, fmt=str('%2.3f'),delimiter=" & ",newline=str("\\\ \n")) # ------ establish new centre (if needed) ------ if not exte == "None": centre = MAP[:2] # -------- prepare data for plots --------- rad,thet = Deg2pc(cdts,centre,Dist) cdts,radii,theta,Rmax = TruncSort(cdts,rad,thet,Rcut) if exte == "Ell": radii,theta = RotRadii(cdts,centre,Dist,MAP[3]) Nr,bins,dens = DenNum(radii,Rmax) x = np.linspace(0.01,Rmax,50) pdf = PdfPages(dir_out+"/"+model+'_fit.pdf') plt.figure() for s,par in enumerate(samp): plt.plot(x,mod.Number(x,par,Rmax,np.max(Nr)),lw=1,color="orange",alpha=0.2,zorder=1) plt.fill_between(radii, Nr+np.sqrt(Nr), Nr-np.sqrt(Nr), facecolor='grey', alpha=0.5,zorder=2) plt.plot(radii,Nr,lw=1,color="black",zorder=3)
mag_labs = ["Y [mag]", "i-K [mag]", "J [mag]", "H [mag]", "K [mag]"] # extracts coordinates cdts = np.c_[dance['RA'], dance['Dec'], dance['Y'], dance['i'], dance['J'], dance['H'], dance['K']] cdts[:, 3] = cdts[:, 3] - cdts[:, 6] #---- removes duplicateds -------- sumc = np.sum(cdts[:, :2], axis=1) idx = np.unique(sumc, return_index=True)[1] cdts = cdts[idx] sumc = np.sum(cdts[:, :2], axis=1) if len(sumc) != len(list(set(sumc))): sys.exit("Duplicated entries in Coordinates!") radii, theta = Deg2pc(cdts, centre, Dist) #----- plot distributions ------ pdf = PdfPages(fout) plt.figure() for i in range(5): # compute 2D density PM X = np.c_[radii.copy(), cdts[:, 2 + i].copy()] X = X[np.where(np.sum(np.isfinite(X), axis=1) == 2)[0]] Y, x_bins, y_bins = np.histogram2d(X[:, 0], X[:, 1], (nbins, nbins), normed=True) plt.imshow(Y.T,