def bootstrap_N(n,BaryonMass,dBM,W,costheta,s_fit_range,ds_dcos,ds_dcos_ran,ds_dcos_sys): s = np.power(W,2) cosThetaMatrix = [] for i in range(len(s)): cosThetaMatrix.append(0) cosThetaMatrix[i] = costheta cosThetaMatrix = np.array(cosThetaMatrix) mt,dfc,dfc_err = D.Theta_to_t(s,BaryonMass,dBM,cosThetaMatrix,ds_dcos,ds_dcos_ran,ds_dcos_sys) N_list = [] A_list = [] j = 0 while s[j] < s_fit_range: j += 1 for i in range(n): dfc_err_b = resample(dfc_err) mt_fit = mt[j:] s_fit = s[j:] dfc_fit = dfc[j:] dfc_err_fit = dfc_err_b[j:] print(i) def f(s,A,N): return(A*s**(-N)) popt, pcov = curve_fit(f, s_fit, dfc_fit, sigma = dfc_err_fit, maxfev=10000) A,N = popt N_list.append(N) A_list.append(A) B_err = np.sqrt(pcov[1][1]) print("N = %s" % N) print("N_err = %s" % B_err ) bins = np.linspace(min(N_list),max(N_list), 150) binscenters = np.array([0.5 * (bins[i] + bins[i+1]) for i in range(len(bins)-1)]) #fig1 = plt.figure() hist = plt.hist(N_list, bins = bins) #binscenters = np.array([0.5 * (hist[1][k] + hist[1][k+1]) for k in range(len(hist[1])-1)]) def gaussian(x,a,b,c): return(a*np.exp(-((x-b)**2)/(2*c**2)) ) param, param_cov = curve_fit(gaussian,binscenters,hist[0], maxfev=10000) print("Height = %s , location = %s , standard deviation = %s " % (param[0],param[1],param[2])) smooth = np.linspace(min(N_list),max(N_list),10000) loc_ord = sorted(N_list) #print("The expected 95% confidence interval is [%f,%f]" % (loc_ord[249],loc_ord[9749])) print(loc_ord[249]) print(loc_ord[9749]) plt.plot(smooth, gaussian(smooth,*param), label = 'mean = %s, SD = %s, 95 percent CI = [%s,%s]' % ( round(param[1],3),round(param[2],3),round(loc_ord[249],3),round(loc_ord[9749],3) )) #plt.plot(location_mean,gaussian(location_mean,*param)) plt.xlabel(r'$N$') plt.ylabel(r'Frequency') plt.legend() plt.title(r'N Histogram $[ As^{-N} ]$ for 10,000 samples fitted from %s $GeV^2$ ' % s_fit_range) '''
def bootstrap_N3(n,BaryonMass,dBM,W,costheta,s_fit_range,ds_dcos,ds_dcos_ran,ds_dcos_sys): s = np.power(W,2) cosThetaMatrix = [] for i in range(len(s)): cosThetaMatrix.append(0) cosThetaMatrix[i] = costheta cosThetaMatrix = np.array(cosThetaMatrix) mt,dfc,dfc_err = D.Theta_to_t(s,BaryonMass,dBM,cosThetaMatrix,ds_dcos,ds_dcos_ran,ds_dcos_sys) N_list = [] j = 0 while s[j] < s_fit_range: j += 1 mt_fit = mt[j:] s_fit = s[j:] dfc_fit = dfc[j:] dfc_err_fit = dfc_err[j:] def f(s,A,N): return(A*s**(-N)) popt, pcov = curve_fit(f, s_fit, dfc_fit, sigma = dfc_err_fit) A_est,N_est = popt res = dfc_fit - f(s_fit,*popt) mean_res = np.mean(res) adj_res = res - mean_res for i in range(n): adj_res_b = resample(adj_res) popt, pcov = curve_fit(f, s_fit, dfc_fit, sigma = adj_res_b, maxfev = 1000000) A,N = popt N_list.append(N) print(i) hist = plt.hist(N_list, bins = 'auto') binscenters = np.array([0.5 * (hist[1][k] + hist[1][k+1]) for k in range(len(hist[1])-1)]) def pdf(x,a,b,c): # gaussian pdf return(a*np.exp(-((x-b)**2)/(2*c**2)) ) param, param_cov = curve_fit(pdf,binscenters,hist[0], maxfev=1000000) a,b,c = param smooth = np.linspace(min(N_list),max(N_list),10000) ordered_list = sorted(N_list) BEG,END = D.BCa('sp',n,0.16,N_est,s_fit,dfc_fit,adj_res,ordered_list) plt.plot(smooth,pdf(smooth,*param), label = "mean = %s, SD = %s" "\n" r"68 percent CI: [%s,%s]" % (round(b,3),round(c,3),round(BEG,3),round(END,3) )) plt.xlabel(r'N') plt.ylabel(r'Frequency') plt.legend() plt.show() print("The mean is: %s, and the standard deviation is: %s" % (b,c))
def interp(Baryon_name, BaryonMass, dBM, costheta, W, ds_dcos, ds_dcos_ran_err, ds_dcos_sys_err): s = np.power(W, 2) cosThetaMatrix = [] for i in range(len(s)): cosThetaMatrix.append(0) cosThetaMatrix[i] = costheta cosThetaMatrix = np.array(cosThetaMatrix) mt1, dfc1, dfc_err1 = D.Theta_to_t(s, BaryonMass, dBM, cosThetaMatrix, ds_dcos, ds_dcos_ran_err, ds_dcos_sys_err) # interpolating the cross section values spl1 = splrep(s, dfc1, w=1 / dfc_err1, k=3) #, full_output = 1) #, task = -1, t = s[1:-1]) s1 = np.linspace(min(s), max(s), 100) y1 = splev(s1, spl1) #interpolating the cross section errors spl2 = splrep(s, dfc_err1, k=3) s2 = s1 y2 = splev(s2, spl2) # normal distribution from which to choose cross section errors #err = np.random.normal(y1,y2) err = y2 #fig1 = plt.figure() #####plt.errorbar(s, dfc1, yerr = dfc_err1, fmt = 'go', ecolor = 'r', label = r'Experimental values')###### #plt.plot(s1,y1,'o') ######plt.errorbar(s1,y1, yerr = err, fmt = 'b.', ecolor = 'black', label = r'Interpolated values')###### #plt.plot(s,dfc_err1, 'bo') #plt.show() #CCR fit s_beg = s[2] i = 0 while s1[i] < s_beg: i += 1 s_fit = s1[i:] dfc_fit = y1[i:] dfc_err_fit = err[i:] #fig2 = plt.figure() fit(s_fit, dfc_fit, dfc_err_fit) ######plt.legend()###### #plt.show() return (s_fit, dfc_fit, dfc_err_fit)
def Best_s_range(Baryon_name,BaryonMass,dBM,costheta,W,ds_dcos,ds_dcos_ran_err,ds_dcos_sys_err): # BaryonMass is to indicate the particle mass # s_fit_range indicates that you only want to consider s_fit_range <= s <= max {s} for the fit # mt = momentum_transfer(t), dfc = differential cross section (dsig/dt), dfc_err is the error in dfc s = np.power(W,2) cosThetaMatrix = [] for i in range(len(s)): cosThetaMatrix.append(0) cosThetaMatrix[i] = costheta cosThetaMatrix = np.array(cosThetaMatrix) mt,dfc,dfc_err = D.Theta_to_t(s,BaryonMass,dBM,cosThetaMatrix,ds_dcos,ds_dcos_ran_err,ds_dcos_sys_err) red_chisq = [] for i in range(len(s)-1): # the last 4 data points are giving trouble for some reason def f(s,A,N): return(A*s**(-N)) popt, pcov = curve_fit(f, s[i:], dfc[i:], sigma = dfc_err[i:], maxfev = 1000000) A,N = popt res = dfc[i:] - f(s[i:],A,N) #return(res) chisq = sum((res/dfc_err[i:])**2) red_chisq.append(chisq/(len(dfc[i:])-len(popt))) ideal_chisq = [] for i in range(len(red_chisq)): ideal_chisq.append(1) plt.plot(s[:-1], red_chisq, 'go')#,label = r'Optimal $s \approx %s$' % round(5.1,2) + ' \n ' + r'$\frac{d\sigma}{dt} = A*s^{2-n}$' ) plt.plot(s[:-1],ideal_chisq, 'b-')#,label = '') plt.xlabel(r'$\ s (GeV^2)$') plt.ylabel(r'$\chi^2/\nu $') #plt.legend() #plt.title(r'$\gamma + P \rightarrow K^+ + \%s$' % Baryon_name + " Statistical Analysis " + r'for $\cos \theta _{c.m.} = %s $' % costheta ) plt.show() # shifted the graph down by one to find roots, i.e. points that would normally be at red_chisq = 1 red_chisq2 = np.array(red_chisq) - 1 # Method (Bisection) delta = 0.1 a = 0 b = len(red_chisq2) q = [] q.append((a+b)/2) COUNTER = 0 while abs(red_chisq2[int(q[COUNTER])]) > delta: if red_chisq2[int(q[COUNTER])]*red_chisq2[int(a)] > 0: a = q[COUNTER] else: b = q[COUNTER] q.append((a+b)/2) #print(COUNTER) COUNTER += 1 if COUNTER > 100: break if s[int(q[-2])] > s[int(q[-1])]: s1 = s[int(q[-1])] s2 = s[int(q[-2])] else: s1 = s[int(q[-2])] s2 = s[int(q[-1])] s_bi = s[int(q[-1])] #print(len(s)) #print(len(red_chisq)) plt.plot(s[:-1], red_chisq, 'go' , label = 'green = Lambda')#,label = r'Optimal $s \approx %s$' % round(s_bi,2) + ' \n ' + r'$\frac{d\sigma}{dt} = A*s^{2-n}$' ) plt.plot(s[:-1],ideal_chisq, 'b-')#,label = '') plt.xlabel(r'$\ s (GeV^2)$') plt.ylabel(r'$\chi^2/\nu $') plt.legend() #plt.title(r'$\gamma + P \rightarrow K^+ + \%s$' % Baryon_name + " Statistical Analysis " + r'for $\cos \theta _{c.m.} = %s $' % costheta ) ''' plt.annotate("", xy=(4.3, 1.17), xytext=(5.29,14.48), arrowprops=dict(arrowstyle="->")) a = plt.axes([0.45, 0.6, .2, .2]) plt.plot(s[8:],red_chisq[:], 'go') plt.plot(s[8:],ideal_chisq[:],'b-') #plt.title('Impulse response') plt.xlim(3.2, 5.7) plt.ylim(0.5,1.5) plt.xticks([]) plt.yticks([]) ''' plt.show() print("Bisection: Best range %s < s < %s " % (s1,s2)) print("Bisection: Reduced Chi Square = %s" % (red_chisq2[int(q[-1])]+1)) return(s_bi)
def bootstrap_unc(n,num_bins,BaryonMass,dBM,W,costheta,ds_dcos,ds_dcos_ran,ds_dcos_sys): # n stands for number of bootstrap samples location_mean = [] arithmetic_mean = [] median = [] p0 = [1,1,1] s = np.power(W,2) cosThetaMatrix = [] for i in range(len(s)): cosThetaMatrix.append(0) cosThetaMatrix[i] = costheta cosThetaMatrix = np.array(cosThetaMatrix) mt,dfc,dfc_err = D.Theta_to_t(s,BaryonMass,dBM,cosThetaMatrix,ds_dcos,ds_dcos_ran,ds_dcos_sys) def phi(x,A,B,C): return((1/A)*np.exp(-(x-B)/A) + C) for i in range(n): dfc_err_b = resample(dfc_err) bins = np.linspace(min(dfc_err_b),max(dfc_err_b),num_bins) binscenters = np.array([0.5 * (bins[i] + bins[i+1]) for i in range(len(bins)-1)]) hist = np.histogram(dfc_err_b, bins= bins) popt, pcov = curve_fit(phi,binscenters,hist[0], p0 = p0, maxfev=10000) p0 = [popt[0],popt[1],popt[2]] location_mean.append(popt[0]) arithmetic_mean.append(np.mean(dfc_err_b)) median.append(np.median(dfc_err_b)) print(i) print("A = %f, B = %f, C = %f " % (popt[0],popt[1],popt[2]) ) smooth_x = np.linspace(min(dfc_err_b),max(dfc_err_b),10000) plt.subplot(2,1,2) plt.hist(dfc_err_b, bins = bins) plt.plot(smooth_x,phi(smooth_x,*popt) , label = ("A = %f, B = %f, C = %f " % (popt[0],popt[1],popt[2])) ) plt.xlabel(r'$\sigma_{d\sigma/dt}$') plt.ylabel(r'Frequency') plt.legend() plt.show() location_mean = arithmetic_mean binss = np.linspace(min(location_mean),max(location_mean), 60) bin_cent = np.array([0.5 * (binss[place_holder] + binss[place_holder+1]) for place_holder in range(len(binss)-1)]) histo_info = np.histogram(location_mean,bins= binss) #return("The length of the bin center array is %s and the length of the frequency array is %s " % (len(histo_info[1]), len(histo_info[0]))) def gaussian(x,a,b,c): return( a*np.exp(-((x-b)**2)/(2*c**2) ) ) param, param_cov = curve_fit(gaussian,bin_cent,histo_info[0], p0 = [500,0.0565,0.0482], maxfev=10000) print("Height = %s , location = %s , standard deviation = %s " % (param[0],param[1],param[2])) print("The length of the location_mean array is %s " % len(location_mean)) smooth = np.linspace(min(location_mean),max(location_mean),10000) loc_ord = sorted(location_mean) #print("The expected 95% confidence interval is [%f,%f]" % (loc_ord[249],loc_ord[9749])) print(loc_ord[249]) print(loc_ord[9749]) plt.subplot(2,1,1) plt.hist(location_mean,bins= binss) plt.plot(smooth, gaussian(smooth,*param), label = "Mean = %s, SD = %s, 95 percent CI = [%s,%s] " % (round(param[1],3),round(param[2],3),round(loc_ord[249],3) ,round(loc_ord[9749],3))) plt.xlabel(r'Mean $\sigma_{d\sigma/dt}$') plt.ylabel(r'Frequency') plt.legend() plt.title(r'$\sigma_{d\sigma/dt}$ Histograms fitted to $ae^{-({x-b})^2/{2c^2}}$ and $(1/A)e^{-({x-B})/{A}} + C$ for 10,000 samples') plt.show()
def bootstrap_N2(n,BaryonMass,dBM,W,costheta,s_fit_range,ds_dcos,ds_dcos_ran,ds_dcos_sys): s = np.power(W,2) cosThetaMatrix = [] for i in range(len(s)): cosThetaMatrix.append(0) cosThetaMatrix[i] = costheta cosThetaMatrix = np.array(cosThetaMatrix) mt,dfc,dfc_err = D.Theta_to_t(s,BaryonMass,dBM,cosThetaMatrix,ds_dcos,ds_dcos_ran,ds_dcos_sys) s = np.delete(s,0) mt = np.delete(mt,0) dfc = np.delete(dfc,0) dfc_err = np.delete(dfc_err,0) neg_dfc_err = -dfc_err tot_dfc_err = np.append(dfc_err, neg_dfc_err) #neg_dfc_err = -dfc_err #tot_dfc_err = np.append(dfc_err, neg_dfc_err) N_list = [] A_list = [] #bins = np.linspace(min(tot_dfc_err),max(tot_dfc_err),30) #binscenters = np.array([0.5 * (bins[i] + bins[i+1]) for i in range(len(bins)-1)]) fig1 = plt.figure() hist = plt.hist(tot_dfc_err, bins = 'auto') binscenters = np.array([0.5 * (hist[1][k] + hist[1][k+1]) for k in range(len(hist[1])-1)]) #print(len(binscenters)) def gaussian(x,a,b,c): return(a*np.exp(-((x-b)**2)/(2*c**2)) ) param, param_cov = curve_fit(gaussian,binscenters,hist[0], maxfev=10000) mean = round(param[1],5) #SD = 3*round(abs(param[2]),5) SD = 1000 print("Height = %s , location = %s , standard deviation = %s " % (param[0],param[1],param[2])) smooth = np.linspace(min(tot_dfc_err),max(tot_dfc_err),10000) plt.plot(smooth,gaussian(smooth,*param)) plt.show() j = 0 while s[j] < s_fit_range: j += 1 for i in range(n): normal_boot_err = np.random.normal(mean,SD,len(dfc_err)) s_fit = s[j:] dfc_fit = dfc[j:] dfc_err_fit = normal_boot_err[j:] def f(s,A,N): return(A*s**(-N)) popt, pcov = curve_fit(f, s_fit, dfc_fit, sigma = dfc_err_fit, maxfev=1000000) A,N = popt N_list.append(N) A_list.append(A) print(i) print(mean) print(SD) fig2 = plt.figure() hist = plt.hist(N_list, bins = 'auto') binscenters = np.array([0.5 * (hist[1][k] + hist[1][k+1]) for k in range(len(hist[1])-1)]) print("Hello") param, param_cov = curve_fit(gaussian,binscenters,hist[0], maxfev=10000) print("Height = %s , location = %s , standard deviation = %s " % (param[0],param[1],param[2])) smooth = np.linspace(min(N_list),max(N_list),10000) loc_ord = sorted(N_list) #print("The expected 95% confidence interval is [%f,%f]" % (loc_ord[249],loc_ord[9749])) print(loc_ord[249]) print(loc_ord[9749]) plt.plot(smooth, gaussian(smooth,*param), label = ' ') #label = 'mean = %s, SD = %s, 95 percent CI = [%s,%s]' % ( round(param[1],3),round(param[2],3),round(loc_ord[249],3),round(loc_ord[9749],3) )) plt.xlabel(r'$N$') plt.ylabel(r'Frequency') plt.legend() plt.title(r'N Histogram $[ As^{-N} ]$ for 10,000 samples fitted from %s $GeV^2$ ' % s_fit_range) plt.show()
def s_range(Baryon_name, BaryonMass, dBM, costheta, W, ds_dcos, ds_dcos_ran_err, ds_dcos_sys_err): # BaryonMass is to indicate the particle mass # s_fit_range indicates that you only want to consider s_fit_range <= s <= max {s} for the fit # mt = momentum_transfer(t), dfc = differential cross section (dsig/dt), dfc_err is the error in dfc s = np.power(W, 2) cosThetaMatrix = [] for i in range(len(s)): cosThetaMatrix.append(0) cosThetaMatrix[i] = costheta cosThetaMatrix = np.array(cosThetaMatrix) mt1, dfc1, dfc_err1 = D.Theta_to_t(s, BaryonMass, dBM, cosThetaMatrix, ds_dcos, ds_dcos_ran_err, ds_dcos_sys_err) mt2, dfc2, dfc_err2 = D.Theta_to_t(s, BaryonMass, dBM, cosThetaMatrix, D.ds_dcos_1405NPN, D.ds_dcos_ran_1405NPN, D.ds_dcos_sys_1405NPN) mt3, dfc3, dfc_err3 = D.Theta_to_t(s, BaryonMass, dBM, cosThetaMatrix, D.ds_dcos_1405P00, D.ds_dcos_ran_1405P00, D.ds_dcos_sys_1405P00) mt4, dfc4, dfc_err4 = D.Theta_to_t(s, BaryonMass, dBM, cosThetaMatrix, D.ds_dcos_1405N00, D.ds_dcos_ran_1405N00, D.ds_dcos_sys_1405N00) mt5, dfc5, dfc_err5 = D.Theta_to_t(s, BaryonMass, dBM, cosThetaMatrix, D.ds_dcos_1405PNP, D.ds_dcos_ran_1405PNP, D.ds_dcos_sys_1405PNP) mt6, dfc6, dfc_err6 = D.Theta_to_t(s, BaryonMass, dBM, cosThetaMatrix, D.ds_dcos_1405NNP, D.ds_dcos_ran_1405NNP, D.ds_dcos_sys_1405NNP) print(dfc5 / dfc6) print plt.subplot(2, 3, 1) fit(s, dfc1, dfc_err1) plt.title(r'$cos \theta = 0.05$, $\Lambda(1405) < \Sigma^+ \pi^-$ ') plt.ylabel(r'$\frac{d\sigma}{dt}$') plt.subplot(2, 3, 2) fit(s, dfc3, dfc_err3) plt.title(r'$cos \theta = 0.05$, $\Lambda(1405) < \Sigma^0 \pi^0$ ') plt.subplot(2, 3, 3) fit(s, dfc5, dfc_err5) plt.title(r'$cos \theta = 0.05$, $\Lambda(1405) < \Sigma^- \pi^+$ ') plt.subplot(2, 3, 4) fit(s, dfc2, dfc_err2) plt.title(r'$cos \theta = -0.05$') #, $\Lambda(1405) < \Sigma^+ \pi^-$ ') plt.ylabel(r'$\frac{d\sigma}{dt}$') plt.xlabel(r'$s$') plt.subplot(2, 3, 5) fit(s, dfc4, dfc_err4) plt.title(r'$cos \theta = -0.05$') #, $\Lambda(1405) < \Sigma^0 \pi^0$ ') plt.xlabel(r'$s$') plt.subplot(2, 3, 6) fit(s, dfc6, dfc_err6) plt.title(r'$cos \theta = -0.05$') # , $\Lambda(1405) < \Sigma^- \pi^+$ ') plt.xlabel(r'$s$') plt.show()
plt.subplot(2, 3, 6) SP_boot(n, D.Lambda1405PN, D.Lambda1405Mass, D.d1405, 0, D.W_1405PPN, D.ds_dcos_1405NNP, D.ds_dcos_ran_1405NNP, D.ds_dcos_sys_1405NNP) plt.title(r'$cos \theta = -0.05$') # , $\Lambda(1405) < \Sigma^- \pi^+$ ') plt.xlabel(r'$N$') plt.show() ##################################################### FINAL RESULTS ##################################################### # cos(theta) expansion and 2d fit _s = np.power(D.W_1405PPN, 2) costheta = np.array([-0.15, -0.05, 0.05, 0.15]) # for SIGMA+ PI- mt1, dfc1, dfc_err1 = D.Theta_to_t(_s, D.Lambda1405Mass, D.d1405, costheta[3], D.Pds_dcos_1405PPN, D.Pds_dcos_ran_1405PPN, D.Pds_dcos_sys_1405PPN) mt2, dfc2, dfc_err2 = D.Theta_to_t(_s, D.Lambda1405Mass, D.d1405, costheta[2], D.ds_dcos_1405PPN, D.ds_dcos_ran_1405PPN, D.ds_dcos_sys_1405PPN) mt3, dfc3, dfc_err3 = D.Theta_to_t(_s, D.Lambda1405Mass, D.d1405, costheta[1], D.ds_dcos_1405NPN, D.ds_dcos_ran_1405NPN, D.ds_dcos_sys_1405NPN) mt4, dfc4, dfc_err4 = D.Theta_to_t(_s, D.Lambda1405Mass, D.d1405, costheta[0], D.Nds_dcos_1405NPN, D.Nds_dcos_ran_1405NPN, D.Nds_dcos_sys_1405NPN) # for SIGMA0 PI0 mt5, dfc5, dfc_err5 = D.Theta_to_t(_s, D.Lambda1405Mass, D.d1405, costheta[3], D.Pds_dcos_1405P00, D.Pds_dcos_ran_1405P00, D.Pds_dcos_sys_1405P00)