def loglike_func(hyper,local, dat_fixm, dat_varm): sigma_like_R0, sigma_like_R1 = split_group(hyper, local) # fix mass Rob0 = dat_fixm[:,2] Rerr0 = dat_fixm[:,3] fM0 = piece_linear(hyper, M0, 0.5*np.ones_like(M0), n_pop) L0 = 0. - 0.5* np.sum( (Rob0-fM0)**2./(Rerr0**2.+sigma_like_R0**2.) ) \ - 0.5* np.sum( np.log(Rerr0**2.+sigma_like_R0**2.) ) # variable mass Mob1 = dat_varm[:,0] Merr1 = dat_varm[:,1] Rob1 = dat_varm[:,2] Rerr1 = dat_varm[:,3] Mth1 = local + 0. fM1 = piece_linear(hyper, Mth1, 0.5*np.ones_like(Mth1), n_pop) L1 = 0. - 0.5* np.sum( (Mob1-Mth1)**2./Merr1**2. ) \ - 0.5* np.sum( (Rob1-fM1)**2./(Rerr1**2.+sigma_like_R1**2.) ) \ - 0.5* np.sum( np.log(Rerr1**2.+sigma_like_R1**2.) ) L = L0 + L1 return L
def inverse_local(local_prob, hyper): # R(0,i) for fix mass prob_R0 = local_prob[0:n_fixm] R0 = piece_linear(hyper, M0, prob_R0) # M(1,i) for variable mass prob_M1 = local_prob[n_fixm:n_fixm+n_varm] M1 = uniform.ppf(prob_M1, -4., 10.) # R(1,i) for varibable mass prob_R1 = local_prob[n_fixm+n_varm:] R1 = piece_linear(hyper, M1, prob_R1) local = np.hstack((R0, M1, R1)) return local
def Mpost2R(mass, unit='Earth', classify='No'): """ Forecast the Radius distribution given the mass distribution. Parameters --------------- mass: one dimensional array The mass distribution. unit: string (optional) Unit of the mass. Options are 'Earth' and 'Jupiter'. Default is 'Earth'. classify: string (optional) If you want the object to be classifed. Options are 'Yes' and 'No'. Default is 'No'. Result will be printed, not returned. Returns --------------- radius: one dimensional array Predicted radius distribution in the input unit. """ # mass input mass = np.array(mass) assert len(mass.shape) == 1, "Input mass must be 1-D." # unit input if unit == 'Earth': pass elif unit == 'Jupiter': mass = mass * mearth2mjup else: print( "Input unit must be 'Earth' or 'Jupiter'. Using 'Earth' as default." ) # mass range if np.min(mass) < 3e-4 or np.max(mass) > 3e5: print('Mass range out of model expectation. Returning None.') return None ## convert to radius sample_size = len(mass) logm = np.log10(mass) prob = np.random.random(sample_size) logr = np.ones_like(logm) hyper_ind = np.random.randint(low=0, high=np.shape(all_hyper)[0], size=sample_size) hyper = all_hyper[hyper_ind, :] if classify == 'Yes': classification(logm, hyper[:, -3:]) for i in range(sample_size): logr[i] = piece_linear(hyper[i], logm[i], prob[i]) radius_sample = 10.**logr ## convert to right unit if unit == 'Jupiter': radius = radius_sample / rearth2rjup else: radius = radius_sample return radius
# find_best.py with spatial_median.txt and find trans_1up/down trans_1up = trans_best + np.array([0.1245372 , 0.0534476 , 0.04035559]) trans_1down = trans_best - np.array([0.14317994, 0.06079727, 0.03514506 ]) # convert trans best mearth2mjup = 317.828 mearth2msun = 333060.4 m_trans = 10.**trans_best m1 = m_trans[0] m2 = m_trans[1] / mearth2mjup m3 = m_trans[2] / mearth2msun # find the corresponding R at transition points rad_trans = 10.** piece_linear( best_hyper, trans_best, prob_R = 0.5*np.ones(3) ) print 'radius at transition (RE)/(RJ)', rad_trans, rad_trans/11.21 ### plot plt.clf() figscale=1.5 fig = plt.figure(figsize=(4*figscale,3*figscale)) ax = plt.gca() # fit line m_max = np.log10(np.max(star[:,0])) m_min = np.log10(np.min(dwarfplanet[:,0])) m_sample = np.linspace( m_min, m_max, 1000 ) r_sample = piece_linear(best_hyper, m_sample, prob_R = 0.5*np.ones_like(m_sample))
def Mpost2R(mass, unit='Earth', classify='No'): """ Forecast the Radius distribution given the mass distribution. Parameters --------------- mass: one dimensional array The mass distribution. unit: string (optional) Unit of the mass. Options are 'Earth' and 'Jupiter'. Default is 'Earth'. classify: string (optional) If you want the object to be classifed. Options are 'Yes' and 'No'. Default is 'No'. Result will be printed, not returned. Returns --------------- radius: one dimensional array Predicted radius distribution in the input unit. """ # mass input mass = np.array(mass) assert len(mass.shape) == 1, "Input mass must be 1-D." # unit input if unit == 'Earth': pass elif unit == 'Jupiter': mass = mass * mearth2mjup else: print("Input unit must be 'Earth' or 'Jupiter'. " "Using 'Earth' as default.") # mass range if np.min(mass) < 3e-4 or np.max(mass) > 3e5: print('Mass range out of model expectation. Returning None.') return None ## convert to radius sample_size = len(mass) logm = np.log10(mass) prob = np.random.random(sample_size) logr = np.ones_like(logm) hyper_ind = np.random.randint(low=0, high=np.shape(all_hyper)[0], size=sample_size) hyper = all_hyper[hyper_ind,:] if classify == 'Yes': classification(logm, hyper[:,-3:]) for i in range(sample_size): logr[i] = piece_linear(hyper[i], logm[i], prob[i]) radius_sample = 10.** logr ## convert to right unit if unit == 'Jupiter': radius = radius_sample / rearth2rjup else: radius = radius_sample return radius
def plt_linear(infile, outfile, datadir=""): pp = PdfPages(outfile) ### data hyper = np.loadtxt(infile + "hyper.out") loglike = np.loadtxt(infile + "loglike.out") repeat = np.loadtxt(infile + "repeat.out") ### split data c0 = hyper[:, 0] slope = hyper[:, 1:5] sigma = hyper[:, 5:9] trans = hyper[:, 9:12] ### plot plt.clf() row = 2 col = 4 f, ((a00, a01, a02, a03), (a10, a11, a12, a13)) = plt.subplots(row, col, figsize=(col * 5, row * 5)) ax = ((a00, a01, a02, a03), (a10, a11, a12, a13)) # repeat ax[0][0].plot(repeat) ax[0][0].set_yscale("log") ax[0][0].set_xlabel("repeat") # loglike ax[0][1].plot(loglike) ax[0][1].set_xlabel("L") # loglike ax[0][2].plot(range(len(loglike) / 2, len(loglike)), loglike[len(loglike) / 2 :]) ax[0][1].set_xlabel("L") # over plot dat = convert_data(np.loadtxt(datadir + "PlanetGroup.txt")) ax[0][3].errorbar(dat[:, 0], dat[:, 2], xerr=dat[:, 1], yerr=dat[:, 3], fmt=".") best_ind = np.argmax(loglike) hyper_best = hyper[best_ind, :] trans_best = hyper_best[-3:] print 10.0 ** trans_best m_sample = np.linspace(np.min(dat[:, 0]), np.max(dat[:, 0]), 1000) r_sample = piece_linear(hyper_best, m_sample, prob_R=0.5 * np.ones_like(m_sample)) # r_upper = piece_linear(hyper_best, m_sample, prob_R = 0.84 * np.ones_like(m_sample)) # r_lower = piece_linear(hyper_best, m_sample, prob_R = 0.16 * np.ones_like(m_sample)) r_upper = piece_linear_complex(hyper_best, m_sample, prob_R=0.84 * np.ones_like(m_sample)) r_lower = piece_linear_complex(hyper_best, m_sample, prob_R=0.16 * np.ones_like(m_sample)) ax[0][3].plot(m_sample, r_sample, "r-") ax[0][3].fill_between(m_sample, r_lower, r_upper, color="grey", alpha=0.2) r_trans = piece_linear(hyper_best, trans_best, prob_R=0.5 * np.ones_like(trans_best)) ax[0][3].plot(trans_best, r_trans, "rx") ax[0][3].set_xlabel(r"log10(M [M$_\oplus$])") ax[0][3].set_ylabel(r"log10(R [R$_\oplus$])") # C ax[1][0].plot(c0) ax[1][0].set_xlabel("c0") ax[1][0].set_ylim([-1, 1]) # slope for i in range(4): ax[1][1].plot(slope[:, i]) ax[1][1].set_xlabel("slope") # sigma for i in range(4): ax[1][2].plot(sigma[:, i]) ax[1][2].set_yscale("log") ax[1][2].set_xlabel("sigma") ax[1][2].set_ylim([1e-3, 1e0]) # transition for i in range(3): ax[1][3].plot(trans[:, i]) ax[1][3].set_xlabel("transition") ax[1][3].set_ylim([-4, 6]) pp.savefig() pp.close() return None
r2m_m = np.log10(Rpost2M(10.**r2m_r)) r2m_r2 = np.log10(Mpost2R(10.**r2m_m)) ### mass histogram nbin=10 ax1.hist(m2r_m2, fc='r', ec='r', alpha=0.3, bins=nbin) ax1.hist(m2r_m,fc='b', bins=nbin, label=['M -> R -> M']) ax1.hist(r2m_m,fc='g', bins=nbin, label=['R -> M']) ax1.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) ### model from func import piece_linear, piece_linear_complex best_hyper = np.loadtxt('spatial_median.txt', delimiter=',') m_sample = np.linspace( -3.9, 5.5, 1000 ) r_sample = piece_linear(best_hyper, m_sample, prob_R = 0.5*np.ones_like(m_sample)) r_upper = piece_linear_complex(best_hyper, m_sample, prob_R = 0.84 * np.ones_like(m_sample)) r_lower = piece_linear_complex(best_hyper, m_sample, prob_R = 0.16 * np.ones_like(m_sample)) r_2upper = piece_linear_complex(best_hyper, m_sample, prob_R = 0.975 * np.ones_like(m_sample)) r_2lower = piece_linear_complex(best_hyper, m_sample, prob_R = 0.025 * np.ones_like(m_sample)) ax2.plot(m_sample, r_sample, 'r-') ax2.fill_between(m_sample, r_lower, r_upper, color='grey', alpha=0.6) ax2.fill_between(m_sample, r_2lower, r_2upper, color='grey', alpha=0.4) ax2.set_xlabel(r'$\rm log_{10}\ M/M_\oplus$') ax2.set_ylabel(r'$\rm log_{10}\ R/R_\oplus$') ### radius histogram ax3.hist(m2r_r,fc='b', orientation='horizontal', bins=nbin) ax3.hist(r2m_r,fc='g', orientation='horizontal', bins=nbin)