def lnlike(theta, x, y, yerr, singcomp=False): vs_n, am_n, fw_n, vs_b, am_b, fw_b, h1_col, h1_b, h1_vel, d2h = theta y_model = lyapy.damped_lya_profile(x,vs_n,10**am_n,fw_n,vs_b,10**am_b,fw_b,h1_col, h1_b,h1_vel,d2h=d2h,resolution=resolution, single_component_flux=singcomp)/1e14 return -0.5 * np.sum(np.log(2 * np.pi * yerr**2) + (y - y_model) ** 2 / yerr**2)
def lnlike_single(theta, x, y, yerr): vs_n, am_n, fw_n, h1_col, h1_b, h1_vel = theta y_model = lyapy.damped_lya_profile(x,vs_n,10**am_n,fw_n,h1_col, 0., 0., 0., h1_b,h1_vel,d2h=d2h_true,resolution=resolution, single_component_flux=True)/1e14 return -0.5 * np.sum(np.log(2 * np.pi * yerr**2) + (y - y_model) ** 2 / yerr**2)
h1_b_mcmc, h1_vel_mcmc)) print("Mean acceptance fraction: {0:.3f}" .format(np.mean(sampler.acceptance_fraction))) print("should be between 0.25 and 0.5") print "" ## best fit intrinsic profile lya_intrinsic_profile_mcmc,lya_intrinsic_flux_mcmc = lyapy.lya_intrinsic_profile_func(wave_to_fit, vs_n_mcmc[0],10**am_n_mcmc[0],fw_n_mcmc[0],vs_b_mcmc[0],10**am_b_mcmc[0],fw_b_mcmc[0], return_flux=True) ## best fit attenuated profile model_best_fit = lyapy.damped_lya_profile(wave_to_fit,vs_n_mcmc[0],10.**am_n_mcmc[0],fw_n_mcmc[0], vs_b_mcmc[0],10.**am_b_mcmc[0],fw_b_mcmc[0],h1_col_mcmc[0], h1_b_mcmc[0],h1_vel_mcmc[0],d2h_true,resolution, single_component_flux=single_component_flux)/1.e14 'Here I will calculate 1-sigma errors on the total integrated flux' print "Calculating Total Flux Errors..." print "" flux_errors = [] vs_n_errors = samples[:,0] am_n_errors = samples[:,1] fw_n_errors = samples[:,2] vs_b_errors = samples[:,3] am_b_errors = samples[:,4] fw_b_errors = samples[:,5] for i in range(len(vs_n_errors)):
10**variables['am_n']['best'][0], variables['fw_n']['best'][0], variables['vs_b']['best'][0], 10**variables['am_b']['best'][0], variables['fw_b']['best'][0], return_flux=True, single_component_flux=variables['am_b']['single_comp']) ## best fit attenuated profile model_best_fit = lyapy.damped_lya_profile( wave_to_fit, variables['vs_n']['best'][0], 10**variables['am_n']['best'][0], variables['fw_n']['best'][0], variables['vs_b']['best'][0], 10**variables['am_b']['best'][0], variables['fw_b']['best'][0], variables['h1_col']['best'][0], variables['h1_b']['best'][0], variables['h1_vel']['best'][0], variables['d2h']['best'][0], resolution, single_component_flux=variables['am_b']['single_comp']) / 1.e14 ## Here's the big messy part where I determine the 1-sigma error bars on the ## reconstructed, intrinsic LyA flux. From my paper: "For each of the 9 parameters, ## the best-fit values are taken as the 50th percentile (the median) of the marginalized ## distributions, and 1-σ error bars as the 16th and 84th percentiles (shown as dashed ## vertical lines in Figures 3 and 4). The best-fit reconstructed Lyα fluxes are determined ## from the best-fit amplitude, FWHM, and velocity centroid parameters, and the 1-σ error bars ## of the reconstructed Lyα flux are taken by varying these parameters individually between ## their 1-σ error bars and keeping all others fixed at their best-fit value.
def profile(wave_to_fit, flux_to_fit, error_to_fit, resolution, model_best_fit, lya_intrinsic_profile_mcmc, variables, param_order, samples = None): f = plt.figure() plt.rc('text', usetex=True) plt.rc('font', family='serif', size=14) ax = f.add_subplot(1,1,1) ## plots 1 sigma contours of the Lya profile - Will draws samples from the walkers and plots them ## e.g., from the emcee website example of fitting a straight line - ## for m, b, lnf in samples[np.random.randint(len(samples), size=100)]: ## plt.plot(xl, m*xl+b, color="k", alpha=0.1) ## Should we implement this? Will, share your code on this? if samples is not None: ndim = len(samples[0]) #print ndim model_fits = np.zeros((len(samples),len(wave_to_fit))) #for i, sample in enumerate(samples[np.random.randint(len(samples), size=10)]): for i, sample in enumerate(samples): theta_all = [] j = 0 for p in param_order: if variables[p]['vary']: theta_all.append(sample[j]) j = j+1 else: theta_all.append(variables[p]['value']) vs_n_i, am_n_i, fw_n_i, vs_b_i, am_b_i, fw_b_i, h1_col_i, h1_b_i, \ h1_vel_i, d2h_i = theta_all singcomp = variables['am_b']['single_comp'] model_fit = lyapy.damped_lya_profile(wave_to_fit,vs_n_i,10**am_n_i,fw_n_i, vs_b_i,10**am_b_i,fw_b_i,h1_col_i, h1_b_i,h1_vel_i,d2h_i,resolution, single_component_flux=singcomp)/1e14 model_fits[i,:] = model_fit #plt.plot(wave_to_fit,model_fit,'deeppink',linewidth=1., alpha=0.1) low = np.zeros_like(wave_to_fit) mid = np.zeros_like(wave_to_fit) high = np.zeros_like(wave_to_fit) for i in np.arange(len(wave_to_fit)): low[i], mid[i], high[i] = np.percentile(model_fits[:,i], [16,50,84]) plt.fill_between(wave_to_fit, low, high) # end 1 sigma contours plotting ax.step(wave_to_fit,flux_to_fit,'k') short_wave = np.linspace(wave_to_fit[0],wave_to_fit[-1],25) error_bars_short = np.interp(short_wave,wave_to_fit,error_to_fit) short_flux = np.interp(short_wave,wave_to_fit,flux_to_fit) ax.errorbar(short_wave,short_flux,yerr=error_bars_short, fmt="none",ecolor='limegreen',elinewidth=3,capthick=3) ax.plot(wave_to_fit,model_best_fit,'deeppink',linewidth=1.5) ax.plot(wave_to_fit,lya_intrinsic_profile_mcmc,'b--',linewidth=1.3) # chi2_mcmc = np.sum( ( (flux_to_fit[~mask] - model_best_fit[~mask]) / error_to_fit[~mask] )**2 ) # dof_mcmc = len(flux_to_fit[~mask]) - ndim - 1 ##### # # ax.step(wave_to_fit[mask],flux_to_fit[mask],'lightblue',linewidth=0.8) ## plotting "masked" region ax.set_ylabel(r'Flux ' r'(erg s$^{-1}$ cm$^{-2}$ \AA$^{-1}$)',fontsize=14) ax.set_xlabel(r'Wavelength (\AA)',fontsize=14) # defining max of y axis y_max = np.max( np.array( [np.max(flux_to_fit),np.max(model_best_fit)] ) ) y_min = 0.0 ax.set_ylim([y_min,y_max+0.5e-13]) ax.set_xlim( [np.min(wave_to_fit),np.max(wave_to_fit)] ) plt.ticklabel_format(useOffset=False)