def LogLike(self, params, ndim, nparams): ctr = params[:2] dlt = params[2] rca = params[3] rcb = params[4] a = params[5] b = params[6] g = params[7] slp = params[8] #----- Checks if parameters' values are in the ranges if not Support(rca, rcb, a, b, g): return -1e50 #------- Obtains radii and angles --------- radii, theta = RotRadii(self.cdts, ctr, self.Dist, dlt) rc = (rca * rcb) / np.sqrt((rcb * np.cos(theta))**2 + (rca * np.sin(theta))**2) # Include mass segregation with linear relation rcs = rc + (slp * self.delta_band) #----- Checks if parameters' values are in the ranges if np.min(rcs) <= 0: return -1e50 ############### Radial likelihood ################### # Computes likelihood lf = (1.0 - self.pro) * LikeField(radii, self.Rmax) lk = radii * (self.pro) * Kernel(radii, rcs, a, b, g) # Normalisation constant x = -1.0 * ((rcs / self.Rmax)**(-1.0 / a)) u = -a * (g - 2.0) v = 1.0 + a * (g - b) w = (((x + 0j)**u) / u) z = ((-1.0 + 0j)**(a * (g - 2.0))) * a * (rcs**2) h = hyp2f1(u, 1.0 - v, u + 1.0, x) cte = np.abs(z * h * w) 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 # if not np.isfinite(llike): # ids = np.where(np.isnan(k))[0] # print h[ids],u,v,a,b,g # sys.exit() # return -1e50 # print(llike) return llike
def LogLike(self, params, ndim, nparams): ctr = params[:2] dlt = params[2] rca = params[3] rta = params[4] rcb = params[5] rtb = params[6] slp = params[7] #----- Checks if parameters' values are in the ranges if not Support(rca, rta, rcb, rtb): return -1e50 #------- Obtains radii and angles --------- radii, theta = RotRadii(self.cdts, ctr, self.Dist, dlt) rc = (rca * rcb) / np.sqrt((rcb * np.cos(theta))**2 + (rca * np.sin(theta))**2) rts = (rta * rtb) / np.sqrt((rtb * np.cos(theta))**2 + (rta * np.sin(theta))**2) # Include mass segregation with linear relation rcs = rc + (slp * self.delta_band) #----- Checks if parameters' values are in the ranges if np.min(rcs) <= 0 or np.greater(rcs, rts).any(): return -1e50 ############### Radial likelihood ################### # Computes likelihood lf = (1.0 - self.pro) * LikeField(radii, self.Rmax) lk = radii * (self.pro) * Kernel(radii, rcs, rts) # In king's profile no objects is larger than tidal radius idBad = np.where(radii > rts)[0] lk[idBad] = 0.0 # Normalisation constant ups = self.Rmax * np.ones_like(rts) ids = np.where(rts < self.Rmax)[0] ups[ids] = rts[ids] cte = np.array(map(NormCte, np.c_[rcs, rts, ups])) k = 2.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] dlt = params[2] rca = params[3] rcb = params[4] a = params[5] b = params[6] slp = params[7] #----- Checks if parameters' values are in the ranges if not Support(rca, rcb, a, b): return -1e50 #------- Obtains radii and angles --------- radii, theta = RotRadii(self.cdts, ctr, self.Dist, dlt) rc = (rca * rcb) / np.sqrt((rcb * np.cos(theta))**2 + (rca * np.sin(theta))**2) # Include mass segregation with linear relation rcs = rc + (slp * self.delta_band) if np.min(rcs) <= 0: return -1e50 ############### Radial likelihood ################### # Computes likelihood lf = (1.0 - self.pro) * LikeField(radii, self.Rmax) lk = radii * (self.pro) * Kernel(radii, rcs, a, b) # Normalisation constant x = -((rcs / self.Rmax)**(-1.0 / a)) c = (self.Rmax**2) * hyp2f1(2.0 * a, a * b, 1 + 2.0 * a, x).real k = 2.0 / c if np.isnan(k).any(): return -1e50 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 return llike
def LogLike(self, params, ndim, nparams): ctr = params[:2] dlt = params[2] rca = params[3] rcb = params[4] g = params[5] slp = params[6] #----- Checks if parameters' values are in the ranges if not Support(rca, rcb): return -1e50 #------- Obtains radii and angles --------- radii, theta = RotRadii(self.cdts, ctr, self.Dist, dlt) rc = (rca * rcb) / np.sqrt((rcb * np.cos(theta))**2 + (rca * np.sin(theta))**2) # Include mass segregation with linear relation rcs = rc + (slp * self.delta_band) #----- Checks if parameters' values are in the ranges if np.min(rcs) <= 0: return -1e50 ############### Radial likelihood ################### lf = (1.0 - self.pro) * LikeField(radii, self.Rmax) lk = radii * (self.pro) * Kernel(radii, rcs, g) # Normalisation constant y = rcs**2 + self.Rmax**2 a = (1.0 / (g - 2.0)) * (1.0 - (rcs**(g - 2.0)) * (y**(1.0 - 0.5 * g))) # Truncated at Rm # a = 1.0/(g-2.0) # No truncated k = 1.0 / (a * (rcs**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_r + llike_t # print(llike) return llike
############### 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) plt.plot(x,mod.Number(x,MAP,Rmax,np.max(Nr)), lw=1,color="red",zorder=4) plt.ylim((0,1.1*max(Nr))) plt.xlim((0,1.1*Rmax)) plt.ylabel("Number of stars")