def GetData(): MagRange = (8.25, 13, 0.25) mags = np.arange(*MagRange) brighttimes, darktimes = [], [] pb = progressbarClass(2. * mags.size) counter = 1 for mag in mags: result = get_bright_contrib(mag) brighttimes.append(result) pb.progress(counter) counter += 1 for mag in mags: result = get_dark_contrib(mag) darktimes.append(result) pb.progress(counter) counter += 1 return mags, np.log10(brighttimes), np.log10(darktimes)
def run(self): # The total integral of a 2d Gaussian to infinity in each direction total = self.Integrate((-np.Inf, np.Inf, -np.Inf, np.Inf), (0., 0.))[0] fractions = [] pb = progressbarClass(self.N) counter = 0 # Main loop while counter < self.N: # Pick two random coordinates x = (self.xRange[1] - self.xRange[0]) * np.random.ranf() + self.xRange[0] y = (self.yRange[1] - self.yRange[0]) * np.random.ranf() + self.yRange[0] # Integrate the new offset Gaussian in the centre pixel and take the fraction Fraction = self.Integrate((-0.5, 0.5, -0.5, 0.5), (x, y))[0] / total # Add the result to the list fractions.append(Fraction) pb.progress(counter+1) counter += 1 med_val = np.median(fractions) med_val_err = np.std(fractions) # Log the fractions # Makes the plot easier to read fractions = np.log10(fractions) # Create the histogram vals, edges = np.histogram(fractions, bins=50, range=(-1, 0)) # Counting errors errs = np.sqrt(vals) # Width of a bin binWidth = np.diff(edges)[0] # Normalise the histogram # I realise that the numpy.histogram function inclues a density parameter which # achieves the same thing but I need the errors from the raw values Integral = float(np.sum(vals)) # Divide the values by the integral of the system normalisedVals = vals / Integral normalisedErrs = errs / Integral # Get the bin centres centres = edges[:-1] + binWidth / 2. # Print a line at the most probable value MostProbable = 10 ** centres[normalisedVals==normalisedVals.max()][0] fig = plt.figure() ax = fig.add_subplot(111) # Plot the histogram ax.plot(10 ** centres, normalisedVals, 'k-', drawstyle='steps-mid') ax.set_xlabel(r'Fraction') ax.set_ylabel(r'Probability') ax.set_xscale('log') ticks = [0.1, 0.2, 0.5, 1] ax.set_xticks(ticks) ax.set_xticklabels(ticks) ax.axvline(MostProbable, color='k', ls=':', zorder=-10) ax.axvline(med_val, color='g') ax.axvline(med_val - med_val_err / 2., color='b') ax.axvline(med_val + med_val_err / 2., color='b') with tables.openFile('out.h5', 'w') as outfile: outfile.createArray('/', 'x', 10 ** centres) outfile.createArray('/', 'y', normalisedVals) outfile.root._v_attrs.most_probable = med_val outfile.root._v_attrs.sd = med_val_err plt.show()