def model(fit_range, bin): nrm_bkg_pdf = Normalized(rename(exp, ['x', 'l%d' % bin]), fit_range) ext_bkg_pdf = Extended(nrm_bkg_pdf, extname='Ncomb_%d' % bin) ext_sig_pdf = Extended(rename(gaussian, ['x', 'm%d' % bin, "sigma%d" % bin]), extname='Nsig_%d' % bin) tot_pdf = AddPdf(ext_bkg_pdf, ext_sig_pdf) print('pdf: {}'.format(describe(tot_pdf))) return tot_pdf
def test_rename(): def f(x, y, z): return None assert describe(f) == ["x", "y", "z"] g = rename(f, ["x", "a", "b"]) assert describe(g) == ["x", "a", "b"]
def test_rename(): def f(x, y, z): return None assert describe(f) == ['x', 'y', 'z'] g = rename(f, ['x', 'a', 'b']) assert describe(g) == ['x', 'a', 'b']
def test_rename(): def f(x, y, z): return None assert_equal(describe(f), ["x", "y", "z"]) g = rename(f, ["x", "a", "b"]) assert_equal(describe(g), ["x", "a", "b"])
bound = (1.83,1.91) bgpdf = Normalized(linear,bound) # <codecell> describe(bgpdf) # <codecell> #remember our breit wigner also has m argument which means different thing describe(rtv_breitwigner) # <codecell> #renaming is easy signalpdf = Normalized(rename(rtv_breitwigner,['x','mass','gamma']),(1.83,1.91)) # <codecell> describe(signalpdf) # <codecell> #now we can add them total_pdf = AddPdfNorm(signalpdf,bgpdf) #if you want to just directly add them up with out the factor(eg. adding extended pdf) #use AddPdf describe(total_pdf) # <codecell>
from iminuit import Minuit from probfit import AddPdfNorm, BinnedLH, gaussian, Extended, rename from matplotlib import pyplot as plt from numpy.random import randn import numpy as np peak1 = randn(1000)*0.5 + 1. peak2 = randn(500)*0.5 + 0. #two peaks data with shared width data = np.concatenate([peak1, peak2]) #Share the width #If you use Normalized here. Do not reuse the object. #It will be really slow due to cache miss. Read Normalized doc for more info. pdf1 = rename(gaussian, ('x', 'm_1', 'sigma')) pdf2 = rename(gaussian, ('x', 'm_2', 'sigma')) compdf = AddPdfNorm(pdf1, pdf2) # merge by name (merge sigma) ulh = BinnedLH(compdf, data, extended=False) m = Minuit(ulh, m_1=1.1, m_2=-0.1, sigma=0.48, f_0=0.6, limit_f_0=(0,1)) plt.figure(figsize=(8, 3)) plt.subplot(121) ulh.draw(m, parts=True) plt.title('Before') m.migrad() # fit plt.subplot(122) ulh.draw(m, parts=True)
abund_guess = 0.5 * \ np.array([RSLT[0][DFS_func.nearest_point(i, REF_M)] for i in mass_guess]) # Arbitrary values for resolution and cup width, easy to tweak with # a rough scan after the fit objects are created res_guess = np.array([float(30000)]) sigma_guess = ((min(REF_M) + max(REF_M)) / 2) / \ res_guess / 2.0 / 1.645 / np.sqrt(2.0) cup_guess = np.array([float(0.00015 * REF_M[0] / 18)]) # CONSTRUCTING MODEL # Building one function with arguments for each peak, multiple concurrent fits peak0 = peak1 = peak2 = peak3 = [] for i in range(PEAKS): name = 'peak' + str(i) vars()[name] = probfit.Extended( probfit.rename(DFS_func.peakshape, ['mass', 'center' + str(i), 'sigma', 'cupwidth']), extname='amp' + str(i)) # Sum those with the special iminut functions if PEAKS == 1: MODEL = peak0 if PEAKS == 2: MODEL = probfit.AddPdf(peak0, peak1) if PEAKS == 3: MODEL = probfit.AddPdf(peak0, peak1, peak2) if PEAKS == 4: MODEL = probfit.AddPdf(peak0, peak1, peak2, peak3) # STEP ONE : fit the first row (or only row) print('Fitting the first peak shape') # Setting up a chi2 function
label=['Signal 1', 'Signal 2', 'Background', 'Total'], bins=200, histtype='step', range=data_range) plt.legend(loc='upper left'); # <codecell> # Using a polynomial to fit a distribution is problematic, because the # polynomial can assume negative values, which results in NaN (not a number) # values in the likelihood function. # To avoid this problem we restrict the fit to the range (0, 5) where # the polynomial is clearly positive. fit_range = (0, 5) normalized_poly = probfit.Normalized(probfit.Polynomial(2), fit_range) normalized_poly = probfit.Extended(normalized_poly, extname='NBkg') gauss1 = probfit.Extended(probfit.rename(probfit.gaussian, ['x', 'mu1', 'sigma1']), extname='N1') gauss2 = probfit.Extended(probfit.rename(probfit.gaussian, ['x', 'mu2', 'sigma2']), extname='N2') # Define an extended PDF consisting of three components pdf = probfit.AddPdf(normalized_poly, gauss1, gauss2) print('normalized_poly: {}'.format(probfit.describe(normalized_poly))) print('gauss1: {}'.format(probfit.describe(gauss1))) print('gauss2: {}'.format(probfit.describe(gauss2))) print('pdf: {}'.format(probfit.describe(pdf))) # <codecell> # Define the cost function in the usual way ... binned_likelihood = probfit.BinnedLH(pdf, data_all, bins=200, extended=True, bound=fit_range)
import numpy as np from iminuit import Minuit from matplotlib import pyplot as plt from numpy.random import randn from probfit import AddPdf, BinnedLH, Extended, gaussian, rename peak1 = randn(1000) * 0.5 + 1.0 peak2 = randn(500) * 0.5 + 0.0 # two peaks data with shared width data = np.concatenate([peak1, peak2]) # Share the width # If you use Normalized here. Do not reuse the object. # It will be really slow due to cache miss. Read Normalized doc for more info. pdf1 = rename(gaussian, ("x", "m_1", "sigma")) pdf2 = rename(gaussian, ("x", "m_2", "sigma")) ext_pdf1 = Extended(pdf1, extname="N_1") ext_pdf2 = Extended(pdf2, extname="N_2") compdf = AddPdf(ext_pdf1, ext_pdf2) # merge by name (merge sigma) ulh = BinnedLH(compdf, data, extended=True) m = Minuit(ulh, m_1=0.1, m_2=-0.1, sigma=0.1, N_1=900, N_2=480) plt.figure(figsize=(8, 3)) plt.subplot(121) ulh.draw(m, parts=True) plt.title("Before")
def test_rename(): def f(x, y, z): return None assert_equal(describe(f), ['x', 'y', 'z']) g = rename(f, ['x', 'a', 'b']) assert_equal(describe(g), ['x', 'a', 'b'])
from iminuit import Minuit from probfit import AddPdf, BinnedLH, gaussian, Extended, rename from matplotlib import pyplot as plt from numpy.random import randn import numpy as np peak1 = randn(1000)*0.5 + 1. peak2 = randn(500)*0.5 + 0. #two peaks data with shared width data = np.concatenate([peak1, peak2]) #Share the width #If you use Normalized here. Do not reuse the object. #It will be really slow due to cache miss. Read Normalized doc for more info. pdf1 = rename(gaussian, ('x', 'm_1', 'sigma')) pdf2 = rename(gaussian, ('x', 'm_2', 'sigma')) ext_pdf1 = Extended(pdf1, extname='N_1') ext_pdf2 = Extended(pdf2, extname='N_2') compdf = AddPdf(ext_pdf1, ext_pdf2) # merge by name (merge sigma) ulh = BinnedLH(compdf, data, extended=True) m = Minuit(ulh, m_1=0.1, m_2=-0.1, sigma=0.1, N_1=900, N_2=480) plt.figure(figsize=(8, 3)) plt.subplot(121) ulh.draw(m, parts=True) plt.title('Before') m.migrad() # fit
sigma_guess = ((min(REF_M) + max(REF_M)) / 2) / \ res_guess / 2.0 / 1.645 / np.sqrt(2.0) cup_guess = np.array([float(0.00015 * REF_M[0] / 18)]) # CONSTRUCTING MODEL # Building one function with arguments for each peak, multiple concurrent fits peak0 = peak1 = peak2 = [] # for i in range(PEAKS): # name = 'peak' + str(i) # vars()[name] = probfit.Extended( # probfit.rename(DFS_func.peakshape, # ['mass', 'center13' , 'sigma', 'cupwidth']), # extname='amp' + str(i)) name = 'peak0' vars()[name] = probfit.Extended( probfit.rename(DFS_func.peakshape_13, ['mass', 'center13' , 'sigma', 'cupwidth']), extname='amp' + str(0)) name = 'peak1' vars()[name] = probfit.Extended( probfit.rename(DFS_func.peakshape_D, ['mass', 'center13' , 'sigma', 'cupwidth']), extname='amp' + str(1)) name = 'peak2' vars()[name] = probfit.Extended( probfit.rename(DFS_func.peakshape_adduct, ['mass', 'center13' , 'sigma', 'cupwidth']), extname='amp' + str(2)) # Sum those with the special iminut functions if PEAKS == 1: MODEL = peak0