Example #1
0
def main():
    data, data_ctl = get_data()
    signal, bkg, signal_ctl, bkg_ctl = get_templates()
    edges = list( data.xedges() )
#     print list(data.y())
#     data, be = hist_to_numpy(data)
#     print data
#     data_ctl, _ = hist_to_numpy(data_ctl)
#     signal, _ = hist_to_numpy(signal)
#     signal_ctl, _ = hist_to_numpy(signal_ctl)
#     bkg, _ = hist_to_numpy(bkg)
#     bkg_ctl, _ = hist_to_numpy(bkg_ctl)
    sig_pdf = hist_to_pdf( signal )
    bkg_pdf = hist_to_pdf( bkg )
    epsig = Extended( sig_pdf, extname = 'N1' )
    epbkg = Extended( bkg_pdf, extname = 'N2' )
    pdf = AddPdf( epbkg, epsig )
    fitdata = array( list( data.y() ) )
    blh = BinnedLH( pdf, fitdata, bins = n_bins, bound = ( min_x, max_x ), extended = True )
    m = Minuit( blh, N1 = 2000, N2 = 30000, error_N1 = 44, error_N2 = 200 )
    m.migrad()
    blh.draw( m, parts = True )
Example #2
0
from iminuit import Minuit
from probfit import BinnedLH, Extended, AddPdf, gen_toy
from probfit.pdf import HistogramPdf
from probfit.plotting import draw_pdf
import numpy as np

bound = (0, 10)
np.random.seed(0)
bkg = gen_toy(lambda x: x**2, 100000, bound=bound)  # a parabola background
sig = np.random.randn(50000) + 5  # a Gaussian signal
data = np.concatenate([sig, bkg])
# fill histograms with large statistics
hsig, be = np.histogram(sig, bins=40, range=bound)
hbkg, be = np.histogram(bkg, bins=be, range=bound)
# randomize data
data = np.random.permutation(data)
fitdata = data[:1000]

psig = HistogramPdf(hsig, be)
pbkg = HistogramPdf(hbkg, be)
epsig = Extended(psig, extname='N1')
epbkg = Extended(pbkg, extname='N2')
pdf = AddPdf(epbkg, epsig)

blh = BinnedLH(pdf, fitdata, bins=40, bound=bound, extended=True)
m = Minuit(blh, N1=330, N2=670, error_N1=20, error_N2=30)
#m.migrad()
blh.draw(m, parts=True)
Example #3
0
from iminuit import Minuit
from probfit import BinnedLH, gaussian, Extended
from matplotlib import pyplot as plt
from numpy.random import randn

data = randn(1000) * 2 + 1

#Unextended

blh = BinnedLH(gaussian, data)
#if you wonder what it looks like call describe(blh)
m = Minuit(blh, mean=0., sigma=0.5)

plt.figure(figsize=(8, 6))
plt.subplot(221)
blh.draw(m)
plt.title('Unextended Before')

m.migrad()  # fit

plt.subplot(222)
blh.draw(m)
plt.title('Unextended After')

#Extended

ext_gauss = Extended(gaussian)

blh = BinnedLH(ext_gauss, data, extended=True)
m = Minuit(blh, mean=0., sigma=0.5, N=900.)
Example #4
0
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)
plt.title('After')
Example #5
0
from iminuit import Minuit
from probfit import BinnedLH, Extended, AddPdf, gen_toy
from probfit.pdf import HistogramPdf
import numpy as np

bound = (0, 10)
np.random.seed(0)
bkg = gen_toy(lambda x : x**2, 100000, bound=bound) # a parabola background
sig= np.random.randn(50000)+5  # a Gaussian signal
data= np.concatenate([sig,bkg])
# fill histograms with large statistics
hsig,be= np.histogram(sig, bins=40, range=bound);
hbkg,be= np.histogram(bkg, bins=be, range=bound);
# randomize data 
data= np.random.permutation(data)
fitdata= data[:1000] 

psig= HistogramPdf(hsig,be)
pbkg= HistogramPdf(hbkg,be)
epsig= Extended(psig, extname='N1')
epbkg= Extended(pbkg, extname='N2')
pdf= AddPdf(epbkg,epsig)

blh= BinnedLH(pdf, fitdata, bins=40, bound=bound, extended=True)
m= Minuit(blh, N1=330, N2= 670, error_N1=20, error_N2=30)
#m.migrad()
blh.draw(m, parts=True)
Example #6
0
File: blh.py Project: bks/probfit
from iminuit import Minuit
from probfit import BinnedLH, gaussian, Extended
from matplotlib import pyplot as plt
from numpy.random import randn

data = randn(1000)*2 + 1

#Unextended

blh = BinnedLH(gaussian, data)
#if you wonder what it loos like call desceibe(blh)
m = Minuit(blh, mean=0., sigma=0.5)

plt.figure(figsize=(8, 6))
plt.subplot(221)
blh.draw(m)
plt.title('Unextended Before')

m.migrad() # fit

plt.subplot(222)
blh.draw(m)
plt.title('Unextended After')

#Extended

ext_gauss = Extended(gaussian)

blh = BinnedLH(ext_gauss, data, extended=True)
m = Minuit(blh, mean=0., sigma=0.5, N=900.)
Example #7
0
nominal_lh = BinnedLH(
    nominal_template,
    bin_centers,
    bins=n_bins,
    bound=bounds,
    weights=binned_asimov,
    weighterrors=binned_asimov_errors,
    use_w2=True,
    nint_subdiv=3,
    extended=True,
)
print(f"{Style.RESET_ALL}", end="")
nominal_lh_val = 2 * nominal_lh(signal_templates[(1.0, 1.0)][1],
                                bkg_template[1])
nominal_lh.draw()
nominal_lh = nominal_lh_val
plt.savefig("nominal.pdf")
plt.close()

for (yuk, lam), (pdf, norm) in signal_templates.items():
    bkg_norm = bkg_template[1]
    splusb_template = AddPdf(Extended(pdf, extname="s"),
                             Extended(bkg_template[0], extname="b"))
    splusb = BinnedLH(
        splusb_template,
        bin_centers,
        bins=n_bins,
        bound=bounds,
        weights=binned_asimov,
        weighterrors=binned_asimov_errors,
Example #8
0
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")

m.migrad()  # fit

plt.subplot(122)
ulh.draw(m, parts=True)
plt.title("After")