Beispiel #1
0
    def generate_binned_kd(self, bandwidth, pdf_seed=None):
        """Wrapper method of `BinnedKernelDensity` constructor for N-dimensional
        kernel PDF with binned interpolation from the sample of points in an
        NTuple with weight.

        Parameters
        ----------
        bandwidth : list floats
            List of kernel widths.
        pdf_seed : KernelDensity | None
            PDF seed for the approximation PDF.

        Returns
        -------
        binned_kernel : BinnedKernelDensity
            BinnedKernelDensity instance.
        """
        if pdf_seed is None:
            pdf_seed = 0

        args = []
        args.extend([
            "BinnedKernelDensity",
            self.space,  # Phase space.
            self.tree  # Input NTuple.
        ])
        args.extend(self.model.vars)  # Variables to use.
        args.append("weight")  # Weights.
        args.extend(self.model.nbins)  # Numbers of bins.
        args.extend(bandwidth)  # Kernel widths.
        args.extend([
            pdf_seed,  # Approximation PDF (0 for flat approximation).
            0
        ])  # Sample size for MC convolution (0 for binned convolution)

        self.binned_kernel = BinnedKernelDensity(*args)

        return self.binned_kernel
Beispiel #2
0
rnd = TRandom3()
for i in range(0, 10000) :
  x = rnd.Rndm()*2.-1.
  w = x*x 
  ntuple.Fill(x, w) 
  uniform_hist.Fill(x, w)
ntuple.Write() 

# Create kernel PDF from the generated sample
# If the number of variables passed to the constructor is larger by 1 than the phase 
# space dimensionality, the last variables is considered as the weight
kde = BinnedKernelDensity("KernelPDF",
                          phsp, 
                          ntuple, # Input ntuple
                          "x",     # Variable to use
                          "w",     # Weight variable
                          1000,    # Number of bins
                          0.1,     # Kernel width
                          0,       # Approximation PDF (0 for flat approximation)
                          100000   # Sample size for MC convolution (0 for binned convolution)
                         ) 

# Write binned PDF into a file
kde.writeToFile("WeightedTuple_bins.root") 

# That's it. Now fill some control histograms and plot them. 
kernel_hist = TH1F("kernel", "Kernel PDF", 200, -1.5, 1.5) 

kde.project(kernel_hist) 

uniform_hist.Write()
kernel_hist.Write()
Beispiel #3
0
# Create an approximation PDF, polynomial of power 3
poly = PolynomialDensity("PolyPDF", 
                         phsp,    # Phase space
                         2,       # Power of the polynomial
                         ntuple,  # Input ntuple
                         "x","y", # Ntuple variables
                         50000    # Sample size for MC integration
                         )

# Create kernel PDF from the generated sample
kde = BinnedKernelDensity("KernelPDF", 
                          phsp,    # Phase space
                          ntuple,  # Input ntuple
                          "x","y", # Variables to use
                          200,200, # Numbers of bins
                          0.4, 0.4,# Kernel widths
                          poly,    # Approximation PDF (0 for flat approximation)
                          50000    # Sample size for MC convolution (0 for binned convolution)
                         )

# Write binned PDF into a file
kde.writeToFile("DalitzPdf_bins.root")

# That's it. Now fill some histograms and show the results. 

true_hist = TH2F("true", "True PDF", 100, phsp.lowerLimit(0), phsp.upperLimit(0), 
                                     100, phsp.lowerLimit(1), phsp.upperLimit(1))
poly_hist = TH2F("poly", "Polynomial PDF", 100, phsp.lowerLimit(0), phsp.upperLimit(0), 
                                     100, phsp.lowerLimit(1), phsp.upperLimit(1))
kernel_hist = TH2F("kernel", "Kernel PDF", 100, phsp.lowerLimit(0), phsp.upperLimit(0), 
Beispiel #4
0
# Create a uniform PDF over this phase space
uniform = UniformDensity("UniformPDF", phsp)

outfile = TFile("OneDimPdf.root", "RECREATE")
ntuple = TNtuple("ntuple", "1D NTuple", "x")

# Generate 10000 toys according to the uniform PDF
uniform.generate(ntuple, 10000)

# Create kernel PDF from the generate sample
kde = BinnedKernelDensity("KernelPDF", 
                          phsp, 
                          ntuple, # Input ntuple
                          "x",    # Variable to use
                          1000,   # Number of bins
                          0.2,    # Kernel width
                          0,      # Approximation PDF (0 for flat approximation)
                          100000  # Sample size for MC convolution (0 for binned convolution)
                         )

# Write binned PDF into a file
kde.writeToFile("OneDimPdfBins.root")

uniform_hist = TH1F("unform", "PDF", 200, -1.5, 1.5)
kernel_hist = TH1F("kernel", "Kernel PDF", 200, -1.5, 1.5)

uniform.project(uniform_hist) 
kde.project(kernel_hist)

gStyle.SetOptStat(0)
Beispiel #5
0
    def __init__(self, args):

        self.kde_norm = 1.0
        self.kde = None
        self.args = args

        self.approx_pdf = 0
        if args.pdf_seed:
            self.approx_pdf = args.pdf_seed

        for i in range(args.ndim):
            self.kde_norm *= args.variables[i].range[1] - args.variables[
                i].range[0]

        self.kde_norm = self.kde_norm**(-1)

        # fixed bw kde
        if not args.adaptive:
            if args.ndim == 1:
                print ""
                print "... constructing 1 dim. KDE (fixed bandwith)"
                print ""
                kde = BinnedKernelDensity(
                    "KernelPDF",
                    args.space,  # Phase space
                    args.tree,  # Input ntuple
                    args.var_names[0],  # Variables to use
                    "weight",  # weights
                    args.nbins[0],  # Numbers of bins
                    args.bws[0],  # Kernel widths
                    self.
                    approx_pdf,  # Approximation PDF (0 for flat approximation)
                    args.mc_conv
                )  # Sample size for MC convolution (0 for binned convolution)

                self.kde = kde

            elif args.ndim == 2:
                print ""
                print "... constructing 2 dim. KDE (fixed bandwith)"
                print ""

                kde = BinnedKernelDensity(
                    "KernelPDF",
                    args.space,  # Phase space
                    args.tree,  # Input ntuple
                    args.var_names[0],
                    args.var_names[1],  # Variables to use
                    "weight",  # weights
                    args.nbins[0],
                    args.nbins[1],  # Numbers of bins
                    args.bws[0],
                    args.bws[1],  # Kernel widths
                    self.
                    approx_pdf,  # Approximation PDF (0 for flat approximation)
                    args.mc_conv
                )  # Sample size for MC convolution (0 for binned convolution)

                self.kde = kde

            elif args.ndim == 3:
                print ""
                print "... constructing 3 dim. KDE (fixed bandwith)"
                print ""

                kde = BinnedKernelDensity(
                    "KernelPDF",
                    args.space,  # Phase space
                    args.tree,  # Input ntuple
                    args.var_names[0],
                    args.var_names[1],
                    args.var_names[2],  # Variables to use
                    "weight",  # weights
                    args.nbins[0],
                    args.nbins[1],
                    args.nbins[2],  # Numbers of bins
                    args.bws[0],
                    args.bws[1],
                    args.bws[2],  # Kernel widths
                    self.
                    approx_pdf,  # Approximation PDF (0 for flat approximation)
                    args.mc_conv
                )  # Sample size for MC convolution (0 for binned convolution)

                self.kde = kde

            elif args.ndim == 4:
                print ""
                print "... constructing 4 dim. KDE (fixed bandwith)"
                print ""

                kde = BinnedKernelDensity(
                    "KernelPDF",
                    args.space,  # Phase space
                    args.tree,  # Input ntuple
                    args.var_names[0],
                    args.var_names[1],
                    args.var_names[2],
                    args.var_names[3],  # Variables to use
                    "weight",  # weights
                    args.nbins[0],
                    args.nbins[1],
                    args.nbins[2],
                    args.nbins[3],  # Numbers of bins
                    args.bws[0],
                    args.bws[1],
                    args.bws[2],
                    args.bws[3],  # Kernel widths
                    self.
                    approx_pdf,  # Approximation PDF (0 for flat approximation)
                    args.mc_conv
                )  # Sample size for MC convolution (0 for binned convolution)

                self.kde = kde

        # adaptive bw kde
        else:

            if not self.approx_pdf:
                print "please provide a seed for the KDE. exiting..."
                sys.exit(1)

            if args.ndim == 1:
                print ""
                print "... constructing 1 dim. KDE (adaptive bandwith)"
                print ""

                kde_adapt = AdaptiveKernelDensity(
                    "KernelPDF",
                    args.space,  # Phase space
                    args.tree,  # Input ntuple
                    args.var_names[0],  # Variables to use
                    "w",  # weights
                    args.nbins[0],  # Numbers of bins
                    args.bws[0],  # Kernel widths
                    self.approx_pdf,  # PDF for width scaling
                    0,  # Approximation PDF (0 for flat approximation)
                    args.mc_conv
                )  # Sample size for MC convolution (0 for binned convolution)

                self.kde = kde_adapt

            elif args.ndim == 2:
                print ""
                print "... constructing 2 dim. KDE (adaptive bandwith)"
                print ""

                kde_adapt = AdaptiveKernelDensity(
                    "KernelPDF",
                    args.space,  # Phase space
                    args.tree,
                    args.var_names[0],
                    args.var_names[1],  # Variables to use
                    "weight",  # weights
                    args.nbins[0],
                    args.nbins[1],  # Numbers of bins
                    args.bws[0],
                    args.bws[1],  # Kernel widths
                    self.approx_pdf,  # PDF for width scaling
                    0,  # Approximation PDF (0 for flat approximation)
                    args.mc_conv
                )  # Sample size for MC convolution (0 for binned convolution)

                self.kde = kde_adapt

            elif args.ndim == 3:
                print ""
                print "... constructing 3 dim. KDE (adaptive bandwith)"
                print ""

                kde_adapt = AdaptiveKernelDensity(
                    "KernelPDF",
                    args.space,  # Phase space
                    args.tree,  # Input ntuple
                    args.var_names[0],
                    args.var_names[1],
                    args.var_names[2],  # Variables to use
                    "weight",  # weights
                    args.nbins[0],
                    args.nbins[1],
                    args.nbins[2],  # Numbers of bins
                    args.bws[0],
                    args.bws[1],
                    args.bws[2],  # Kernel widths
                    self.approx_pdf,  # PDF for width scaling
                    0,  # Approximation PDF (0 for flat approximation)
                    args.mc_conv
                )  # Sample size for MC convolution (0 for binned convolution)
                self.kde = kde_adapt

            elif args.ndim == 4:
                print ""
                print "... constructing 4 dim. KDE (adaptive bandwith)"
                print ""

                kde_adapt = AdaptiveKernelDensity(
                    "KernelPDF",
                    args.space,  # Phase space
                    args.tree,  # Input ntuple
                    args.var_names[0],
                    args.var_names[1],
                    args.var_names[2],
                    args.var_names[3],  # Variables to use
                    "weight",  # weights
                    args.nbins[0],
                    args.nbins[1],
                    args.nbins[2],
                    args.nbins[3],  # Numbers of bins
                    args.bws[0],
                    args.bws[1],
                    args.bws[2],
                    args.bws[3],  # Kernel widths 
                    self.approx_pdf,  # PDF for width scaling
                    0,  # Approximation PDF (0 for flat approximation)
                    args.mc_conv
                )  # Sample size for MC convolution (0 for binned convolution)

                self.kde = kde_adapt
# Create a "true" PDF over this phase space in a parametrised way
formula = FormulaDensity("FormulaPDF", phsp, "(1. + 4.*exp(-x*x/2/0.1/0.1))*( abs(x)<1 )")

outfile = TFile("OneDimAdaptiveKernel.root", "RECREATE")
ntuple = TNtuple("ntuple", "1D NTuple", "x")

# Generate toys according to the "true" PDF
formula.generate(ntuple, 10000)
  
# Create kernel PDF from the generate sample
kde = BinnedKernelDensity("KernelPDF", 
                        phsp,   # Phase space
                        ntuple, # Input ntuple
                        "x",     # Variable to use
                        1000,    # Number of bins
                        0.1,     # Kernel width
                        0,       # Approximation PDF (0 for flat approximation)
                        100000   # Sample size for MC convolution (0 for binned convolution)
                       )

# Create adaptive kernel PDF with the kernel width depending on the binned PDF 
kde_adaptive = AdaptiveKernelDensity("AdaptivePDF", 
                          phsp,        # Phase space
                          ntuple,      # Input ntuple
                          "x",         # Variable to use
                          1000,        # Number of bins
                          0.1,         # Initial kernel width
                          kde,         # PDF for width scaling
                          0,           # Approximation PDF (0 for flat approximation)
                          100000       # Sample size for MC convolution (0 for binned convolution)
Beispiel #7
0
# Just a polymonial shape. It will mostly be used to approximate the PDF 
# at the edges of the phase space.
approxpdf = FormulaDensity("TruePDF", phsp, "1.-0.8*x^2-0.8*y^2") 

outfile = TFile("ParamPhsp.root", "RECREATE")
ntuple = TNtuple("ntuple", "2D NTuple", "x:y") 

# Generate 50000 toys according to the "true" PDF
truepdf.generate(ntuple, 50000) 

# Create kernel PDF from the generated sample. Use polynomial shape as an approximation PDF 
kde = BinnedKernelDensity("KernelPDF", 
                          phsp,      # Phase space
                          ntuple,    # Input ntuple
                          "x","y",   # Variables to use
                          200,200,   # Numbers of bins
                          0.2, 0.2,  # Kernel widths
                          approxpdf, # Approximation PDF (0 for flat approximation)
                          100000     # Sample size for MC convolution (0 for binned convolution)
                         ) 

# Write binned PDF into a file
kde.writeToFile("ParamPhsp_bins.root") 

# That's it. Now fill some histograms and show the results. 

true_hist = TH2F("true", "True PDF", 100, phsp.lowerLimit(0), phsp.upperLimit(0), 
                                     100, phsp.lowerLimit(1), phsp.upperLimit(1)) 
kernel_hist = TH2F("kernel", "Kernel PDF", 100, phsp.lowerLimit(0), phsp.upperLimit(0), 
                                           100, phsp.lowerLimit(1), phsp.upperLimit(1))