def fitSkewedGaussianAndComputeMaxLLH(self, n, bins, nbins, fileName, number, latex=False, string=' ', plotFigures=False): # Read data from file data = np.loadtxt(fileName) bestFits = data[:, 1] lowLimit = data[:, 2] highLimit = data[:, 3] # Compute estimates best = bestFits[number] lower = bestFits[number] - lowLimit[number] upper = highLimit[number] - bestFits[number] # Initialise maximum likelihood parameters llhood = Stats() llhood.best = -1000 llhood.minus = 0 llhood.plus = 0 # Fit skewed Gaussian distribution to the result # Note that the number of bins is one larger than the number of results, so we need to remove the last bin # Find approximate position of the maximum and the width of the curve to easy the fitting process curveInfo = (np.max(bins), np.argmax(n[1:-10]), 0, 0, 1) skewFits, skewCovar = opt.curve_fit(self.skewGaussian, bins[1:-10], n[1:-10], p0=curveInfo) ySkew = self.skewGaussian(bins, skewFits[0], skewFits[1], skewFits[2], skewFits[3], skewFits[4]) # Determine maximum likelihood and its uncertainties based on skewed Gaussian fit llhood.likeLimits1D(ySkew, bins, 1) ## OUTPUT # Plot if wanted if (plotFigures == True): plt.plot(bins, ySkew, 'k--', linewidth=2, label="Skewed Gaussian fit") plt.legend(loc="center right", bbox_to_anchor=(1.018, 1.07), ncol=2) # End if-statement # Print distribution properties to screen if (latex==False): # Print in readable format print "~" * 60, "\n" print "\tstackResults" print "\tMaximum likelihood: tau0 =", llhood.best print "\tDifference to lower limit:", llhood.minus print "\tDifference to upper limit:", llhood.plus if (number != None): print "\tJavelin best fit value:", best print "\tDifference to Javelin lower limit:", lower print "\tDifference to Javelin upper limit:", upper # End if-statement print "\n", "~" * 60 else: # Print in LaTeX format print(' ', string, '& $',round(llhood.best,1),'_{-', round(llhood.minus,1),'}^{+',round(llhood.plus,1),'} $ & $', round(best,1),'_{-',round(lower,1),'}^{+', round(upper,1),'} $ \\') # End if-statement # Decide what output to give from function if (number != None): return np.array([llhood.best, llhood.minus, llhood.plus, best, lower, upper]) else: return np.array([llhood.best, llhood.minus, llhood.plus, -1000, 0, 0 ])
def testingTFimpact(): # These are the data we'll be using folder = '/Users/uqnsomme/Documents/Obelix/FakeAGN/TFs/' names = ['0Phi', '001Phi', '010Phi', '025Phi', '01TH', 'Gamma', 'Gauss', 'Sphere'] nn = len(names) maxLag = 1000.0 # Initialisation bins = np.arange(0.0, maxLag, 10) # Grid/x-axis for histograms binssmall = np.arange(0.0, maxLag) # Grid/x-axis for accurate median calculation est = np.zeros((nn, 1000)) # Array to hold all the time lag estimates from Javelin n = np.zeros((nn, 99)) # y-axis for hisograms nsmall = np.zeros((nn, 999)) # y-axis for accurate median calculation # We'll be making the same kind of plot for all of the TFs for i in range(nn): print 'About to start reading ' + names[i] # Import data lagMatrName = folder + 'TFs' + names[i] + '.dat' lagMatr = np.loadtxt(lagMatrName) # Load in results file true = lagMatr[:, 0] # Time lag, as created by me est[i,:] = lagMatr[:, 1] # Estimates found by Javelin # Depending on what physical scenario we're considering, choose appropriate axes and labels for figure if (i==0): maxN = 4e-2 ymal = 1e-2 ymil = 5e-3 else: maxN = 1e-2 ymal = 2e-3 ymil = 1e-3 # End if-statement # Calculate histograms to do some maths n[i,:], _ = np.histogram(est, bins=bins, normed=True) nsmall[i,:], _ = np.histogram(est, bins=binssmall, normed=True) # Fit skewed Gaussian distribution to the result to later calculate median skewFits, skewCovar = opt.curve_fit(Stacking.skewGaussian, bins[0:-1], n[i,:], p0=(1./np.std(n[i,:]), np.argmax(n[i,:]), 0, 0, 1)) ySkew = Stacking.skewGaussian(binssmall, skewFits[0], skewFits[1], skewFits[2], skewFits[3], skewFits[4]) # Determine maximum likelihood and its uncertainties llhood = Stats() llhood.best = -1000 llhood.minus = 0 llhood.plus = 0 llhood.likeLimits1D(ySkew, binssmall, 1) # Determine median and error estimates median, minus, plus = Stats.getMedianEstimates(binssmall, nsmall[i,:]) # Print distribution properties to screen print "=" * 60, "\n" print "\tMaximum likelihood: tau0 =", llhood.best print "\tDifference to lower limit:", llhood.minus print "\tDifference to upper limit:", llhood.plus print "\tMedian:", median print "\tMedian lower:", minus print "\tMedian upper:", plus print "\n", "=" * 60 # Create figure showing the distribution of estimates for a particular TF fig = plt.figure(figsize=(6,10)) plt.hist(est, bins=bins, normed=True, label='JAVELIN estimates') plt.plot(binssmall, ySkew, linewidth=2, label="Best skewed Gaussian fit") plt.plot([true[i], true[i]], [0, maxN], linewidth=2, label='True lag') # Specify figure properties plt.axis([0, maxLag, 0, maxN]) plt.xlabel('Time lag [days]', fontsize='large') plt.ylabel('Number of instances', fontsize='large') plt.legend() ax = plt.axes() ax.xaxis.set_major_locator(tck.MultipleLocator(200)) ax.xaxis.set_minor_locator(tck.MultipleLocator(100)) ax.yaxis.set_major_locator(tck.MultipleLocator(ymal)) ax.yaxis.set_minor_locator(tck.MultipleLocator(ymil)) ax.set_aspect(maxLag/maxN) plt.savefig(folder+names[i]+'.png', dpi=500, bbox_inches='tight') # End for-loop # Create plot showing the different estimate distributions for all of the TFs fig = plt.figure(figsize=(6,10)) plt.hist(est[4,:], bins=bins, normed=True, histtype='step', color='darkblue', label=r'$\mathrm{Face-on\ ring}$') plt.hist(est[7,:], bins=bins, normed=True, histtype='step', color='paleturquoise', label=r'$\mathrm{Sphere}$') plt.hist(est[1,:], bins=bins, normed=True, histtype='step', color='mediumorchid', label=r'$\phi=\pi/100$') plt.hist(est[2,:], bins=bins, normed=True, histtype='step', color='darkgreen', label=r'$\phi=\pi/10$') plt.hist(est[3,:], bins=bins, normed=True, histtype='step', color='black', label=r'$\phi=\pi/4$') plt.hist(est[5,:], bins=bins, normed=True, histtype='step', color='olivedrab', label=r'$\mathrm{Gamma}$') plt.hist(est[4,:], bins=bins, normed=True, histtype='step', color='pink', label=r'$\mathrm{Gauss}$') plt.plot([true[i], true[i]], [0, maxN], 'r', label=r'$\mathrm{True\ lag}$') # Specify figure properties plt.axis([0, maxLag, 0, 0.009]) plt.xlabel(r'$\mathrm{Time\ lag\ [days]}$', fontsize='large') plt.ylabel(r'$N_{norm}$', fontsize='large') plt.legend() ax = plt.axes() ax.xaxis.set_major_locator(tck.MultipleLocator(200)) ax.xaxis.set_minor_locator(tck.MultipleLocator(100)) ax.yaxis.set_major_locator(tck.MultipleLocator(0.001)) ax.yaxis.set_minor_locator(tck.MultipleLocator(0.0005)) ax.set_aspect(maxLag/maxN) plt.savefig('/Users/uqnsomme/Documents/ManyHists.png', dpi=500, bbox_inches='tight')