def ln_posterior_population(x): """ Calculate the natural log of the posterior probability Parameters ---------- x : M1, M2, A, ecc, v_k, theta, phi, ra_b, dec_b, t_b 10 model parameters args : M2_d, P_orb_obs, ecc_obs, ra, dec System observations Returns ------- lp : float Natural log of the posterior probability """ M1, M2, A, ecc, v_k, theta, phi, ra_b, dec_b, t_b = x # Call priors lp = ln_priors_population(x) if np.isinf(lp): return -np.inf ll = 0 M1_b, M2_b, A_b = binary_evolve.func_MT_forward(M1, M2, A, ecc) A_c, v_sys, ecc_out = binary_evolve.func_SN_forward(M1_b, M2_b, A_b, v_k, theta, phi) M2_d_out, L_x_out, M_dot_out, A_d = binary_evolve.func_Lx_forward(M1, M2, M2_b, A_c, ecc_out, t_b) P_orb_d = A_to_P(c.M_NS, M2_d_out, A_d) # If system disrupted or no X-ray luminosity, return -infty if ecc_out < 0.0 or ecc_out > 1.0 or np.isnan(ecc) or L_x_out==0.0: return -np.inf if np.isnan(ll): return -np.inf return ll + lp
def full_forward(M1, M2, A, ecc, v_k, theta, phi, t_obs): """ Evolve a binary forward from its initial conditions Parameters ---------- M1 : float Initial primary mass (Msun) M2 : float Initial secondary mass (Msun) A : float Initial orbital separation (Rsun) ecc : float Initial orbital eccentricity (unitless) v_k : float SN kick velocity theta : float SN kick polar angle phi : float SN kick azimuthal angle t_obs : float observation time Returns ------- M_NS : float or ndarray Array of final primary masses (Currently set to the NS mass, c.M_NS) M_2 : float or ndarray Array of final secondary masses (Msun) L_x : float or ndarray X-ray luminosity (erg/s) v_sys : float or ndarray Systemic velocity (km/s) M2_dot : float or ndarray Mass accretion rate (Msun/yr) A : float or ndarray Orbital separation (Rsun) ecc : float or ndarray Orbital eccentricity (unitless) theta : float or ndarray Projected angular distance traveled from birth location (radians) k_type : int k-type of HMXB donor """ if load_sse.func_sse_mass is None: load_sse.load_sse() if isinstance(M1, np.ndarray): dtypes = [('M_NS','<f8'), \ ('M_2','<f8'), \ ('L_x','<f8'), \ ('v_sys','<f8'), \ ('M2_dot','<f8'), \ ('A','<f8'), \ ('ecc','<f8'), \ ('theta','<f8'), \ ('k_type','<i8')] HMXB = np.recarray(len(M1), dtype=dtypes) for i in np.arange(len(M1)): if isinstance(t_obs, np.ndarray): if t_obs[i] < load_sse.func_sse_ms_time(M1[i]): HMXB["M_NS"][i] = M1[i] HMXB["M_2"][i] = M2[i] HMXB["A"][i] = A[i] continue else: if t_obs < load_sse.func_sse_ms_time(M1[i]): HMXB["M_NS"][i] = M1[i] HMXB["M_2"][i] = M2[i] HMXB["A"][i] = A[i] continue # First MT phase M_1_b, M_2_b, A_b = binary_evolve.func_MT_forward(M1[i], M2[i], A[i], ecc[i]) if isinstance(t_obs, np.ndarray): if t_obs[i] < load_sse.func_sse_tmax(M1[i]): HMXB["M_NS"][i] = M_1_b HMXB["M_2"][i] = M_2_b HMXB["A"][i] = A_b continue else: if t_obs < load_sse.func_sse_tmax(M1[i]): HMXB["M_NS"][i] = M_1_b HMXB["M_2"][i] = M_2_b HMXB["A"][i] = A_b continue # SN A_tmp, v_sys_tmp, e_tmp = binary_evolve.func_SN_forward(M_1_b, M_2_b, A_b, v_k[i], theta[i], phi[i]) # XRB if isinstance(t_obs, np.ndarray): M_2_tmp, L_x_tmp, M2_dot_out, A_out = binary_evolve.func_Lx_forward(M1[i], M2[i], M_2_b, A_tmp, e_tmp, t_obs[i]) theta_out = (t_obs[i] - load_sse.func_sse_tmax(M1[i])) * v_sys_tmp / c.dist_SMC * c.yr_to_sec * 1.0e6 * np.sin(get_theta(1)) tobs_eff = binary_evolve.func_get_time(M1[i], M2[i], t_obs[i]) else: M_2_tmp, L_x_tmp, M2_dot_out, A_out = binary_evolve.func_Lx_forward(M1[i], M2[i], M_2_b, A_tmp, e_tmp, t_obs) theta_out = (t_obs - load_sse.func_sse_tmax(M1[i])) * v_sys_tmp / c.dist_SMC * c.yr_to_sec * 1.0e6 * np.sin(get_theta(1)) tobs_eff = binary_evolve.func_get_time(M1[i], M2[i], t_obs) # To get k-type of HMXB donor if M_2_b > c.max_mass: k_type = -999 else: M_tmp, M_dot_tmp, R_tmp, k_type = load_sse.func_get_sse_star(M_2_b, tobs_eff) HMXB["M_NS"][i] = c.M_NS HMXB["M_2"][i] = M_2_tmp HMXB["L_x"][i] = L_x_tmp HMXB["v_sys"][i] = v_sys_tmp HMXB["M2_dot"][i] = M2_dot_out HMXB["A"][i] = A_out HMXB["ecc"][i] = e_tmp HMXB["theta"][i] = theta_out HMXB["k_type"][i] = int(k_type) return HMXB["M_NS"], HMXB["M_2"], HMXB["L_x"], HMXB["v_sys"], HMXB["M2_dot"], HMXB["A"], HMXB["ecc"], HMXB["theta"], HMXB["k_type"] else: # Star does not make it to MT phase if t_obs < load_sse.func_sse_ms_time(M1): return M1, M2, 0.0, 0.0, 0.0, A, ecc, 0.0 # MT phase M_1_b, M_2_b, A_b = binary_evolve.func_MT_forward(M1, M2, A, ecc) # Star does not make it to SN if t_obs < load_sse.func_sse_tmax(M1): return M_1_b, M_2_b, 0.0, 0.0, 0.0, A_b, ecc, 0.0 # SN A_tmp, v_sys_tmp, e_tmp = binary_evolve.func_SN_forward(M_1_b, M_2_b, A_b, v_k, theta, phi) # XRB M_2_tmp, L_x_tmp, M2_dot_out, A_out = binary_evolve.func_Lx_forward(M1, M2, M_2_b, A_tmp, e_tmp, t_obs) theta_out = (t_obs - load_sse.func_sse_tmax(M1)) * v_sys_tmp / c.dist_SMC * c.yr_to_sec * 1.0e6 * np.sin(get_theta(1)) # To get k-type of HMXB donor tobs_eff = binary_evolve.func_get_time(M1, M2, t_obs) M_tmp, M_dot_tmp, R_tmp, k_type = load_sse.func_get_sse_star(M_2_b, tobs_eff) return c.M_NS, M_2_tmp, L_x_tmp, v_sys_tmp, M2_dot_out, A_out, e_tmp, theta_out, int(k_type)
def ln_posterior(x, args): """ Calculate the natural log of the posterior probability Parameters ---------- x : M1, M2, A, ecc, v_k, theta, phi, ra_b, dec_b, t_b 10 model parameters args : M2_d, P_orb_obs, ecc_obs, ra, dec System observations Returns ------- lp : float Natural log of the posterior probability """ M1, M2, A, ecc, v_k, theta, phi, ra_b, dec_b, t_b = x M2_d, M2_d_err, P_orb_obs, P_orb_obs_err, ecc_obs, ecc_obs_err, ra, dec = args y = ra, dec, M1, M2, A, ecc, v_k, theta, phi, ra_b, dec_b, t_b # Call priors lp = ln_priors(y) if np.isinf(lp): return -np.inf ll = 0 M1_b, M2_b, A_b = binary_evolve.func_MT_forward(M1, M2, A, ecc) A_c, v_sys, ecc_out = binary_evolve.func_SN_forward(M1_b, M2_b, A_b, v_k, theta, phi) M2_d_out, L_x_out, M_dot_out, A_d = binary_evolve.func_Lx_forward(M1, M2, M2_b, A_c, ecc_out, t_b) P_orb_d = A_to_P(c.M_NS, M2_d_out, A_d) # If system disrupted or no X-ray luminosity, return -infty if ecc_out < 0.0 or ecc_out > 1.0 or np.isnan(ecc) or L_x_out==0.0: return -np.inf # Observed secondary mass delta_M_err = M2_d_err # uncertainty is user input coeff_M = -0.5 * np.log( 2. * np.pi * delta_M_err**2 ) argument_M = -( M2_d - M2_d_out ) * ( M2_d - M2_d_out ) / ( 2. * delta_M_err**2 ) ll += coeff_M + argument_M # Observed X-ray luminosity # delta_ln_L_x_err = 0.2 # coeff_ln_L_x = -0.5 * np.log( 2. * np.pi * delta_ln_L_x_err**2 ) # argument_ln_L_x = -( np.log(L_x) - np.log(L_x_out) ) * ( np.log(L_x) - np.log(L_x_out) ) / ( 2. * delta_ln_L_x_err**2 ) # ll += coeff_ln_L_x + argument_ln_L_x # Observed orbital period delta_P_orb_err = P_orb_obs_err # uncertainty is user input coeff_P_orb = -0.5 * np.log( 2. * np.pi * delta_P_orb_err**2) argument_P_orb = -( P_orb_obs - P_orb_d )**2 / ( 2. * delta_P_orb_err**2 ) ll += coeff_P_orb + argument_P_orb # Observed eccentricity delta_ecc_err = ecc_obs_err # uncertainty is user input coeff_ecc = -0.5 * np.log( 2. * np.pi * delta_ecc_err**2) argument_ecc = -( ecc_obs - ecc_out )**2 / ( 2. * delta_ecc_err**2 ) ll += coeff_ecc + argument_ecc ######## Under Construction ####### theta_proj = get_theta_proj(c.deg_to_rad*ra, c.deg_to_rad*dec, c.deg_to_rad*ra_b, c.deg_to_rad*dec_b) # Projected travel distance t_sn = (t_b - load_sse.func_sse_tmax(M1)) * 1.0e6 * c.yr_to_sec # The time since the primary's core collapse tmp = (v_sys * t_sn) / c.dist_SMC # Unitless conds = [theta_proj>tmp, theta_proj<=tmp] # Define conditional funcs = [lambda theta_proj: -np.inf, lambda theta_proj: np.log(np.tan(np.arcsin(theta_proj/tmp))/tmp)] J_coor = np.abs(get_J_coor(c.deg_to_rad*ra, c.deg_to_rad*dec, c.deg_to_rad*ra_b, c.deg_to_rad*dec_b)) # Jacobian for coordinate change P_omega = 1.0 / (2.0 * np.pi) ll += np.piecewise(theta_proj, conds, funcs) + np.log(P_omega) + np.log(J_coor) # print rad_to_dec(theta_proj)*3600.0, tmp, t_sn, v_sys, v_sys*t_sn, \ # np.arcsin(theta_proj/tmp), np.tan(np.arcsin(theta_proj/tmp)), np.piecewise(theta_proj, conds, funcs), \ # np.log(J_coor * P_omega) # Observed distance from the birth cluster # t_travel = (t_b - func_sse_tmax(M1)) * 1.0e6 * yr_to_sec # sin_theta = theta_proj * dist_LMC / (v_sys * t_travel) # if sin_theta < 0.0 or sin_theta > 1.0: return -np.inf # sine must be bounded # cos_theta = np.sqrt(1.0 - sin_theta*sin_theta) # prob = sin_theta / cos_theta * v_sys * t_travel / dist_LMC # ll += np.log(prob) if np.isnan(ll): return -np.inf return ll + lp