From c3e7684bb1c22616c93ac5f8f38e897b47851984 Mon Sep 17 00:00:00 2001 From: Juan Fernando Jaramillo Botero <juanf.jaramillo@opdevel.com> Date: Thu, 3 May 2018 10:18:31 -0500 Subject: [PATCH] Translate tStudent.C to Python Add tStudent.py to the list of math tutorials. --- tutorials/CMakeLists.txt | 1 + tutorials/math/tStudent.py | 83 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tutorials/math/tStudent.py diff --git a/tutorials/CMakeLists.txt b/tutorials/CMakeLists.txt index b83afd4a8aa..0e338d4c06e 100644 --- a/tutorials/CMakeLists.txt +++ b/tutorials/CMakeLists.txt @@ -381,6 +381,7 @@ if(ROOT_python_FOUND) if(NOT ROOT_mathmore_FOUND) list(APPEND pyveto math/Legendre.py) list(APPEND pyveto math/Bessel.py) + list(APPEND pyveto math/tStudent.py) endif() list(REMOVE_ITEM pytutorials ${pyveto}) diff --git a/tutorials/math/tStudent.py b/tutorials/math/tStudent.py new file mode 100644 index 00000000000..fe920c843a7 --- /dev/null +++ b/tutorials/math/tStudent.py @@ -0,0 +1,83 @@ +## \file +## \ingroup tutorial_math +## \notebook +## Example macro describing the student t distribution +## +## ~~~{.cpp} +## root[0]: .x tStudent.C +## ~~~ +## +## It draws the pdf, the cdf and then 10 quantiles of the t Student distribution +## +## based on Magdalena Slawinska's tStudent.C +## +## \macro_image +## \macro_code +## +## \author Juan Fernando Jaramillo Botero + +from ROOT import TH1D, TF1, TCanvas, kRed, kBlue +import ROOT + + +# This is the way to force load of MathMore in Cling +ROOT.Math.MathMoreLibrary.Load() + +# Create the pdf and the cumulative distributions +n = 100 +a = -5. +b = 5. +pdf = TF1("pdf", "ROOT::Math::tdistribution_pdf(x,3.0)", a, b) +cum = TF1("cum", "ROOT::Math::tdistribution_cdf(x,3.0)", a, b) + +# Create the histogram and fill it with the quantiles +quant = TH1D("quant", "", 9, 0, 0.9) + +for i in range(1, 10): + quant.Fill((i-0.5)/10.0, ROOT.Math.tdistribution_quantile(0.1 * i, + 3.0)) + +# For each quantile fill with the pdf +xx = [-1.5] + [quant.GetBinContent(i)for i in range(1, 9)] + [1.5] +pdfq = [] +for i in range(9): + nbin = int(n * (xx[i+1] - xx[i]) / 3.0 + 1.0) + name = "pdf%d" % i + pdfq.append(TH1D(name, "", nbin, xx[i], xx[i+1])) + for j in range(1, nbin): + x = j * (xx[i+1] - xx[i]) / nbin + xx[i] + pdfq[i].SetBinContent(j, ROOT.Math.tdistribution_pdf(x, 3)) + +# Create the Canvas and divide in four draws, for every draw set the line width +# the title, and the line color. +Canvas = TCanvas("DistCanvas", "Student Distribution graphs", 10, 10, 800, 700) +pdf.SetTitle("Student t distribution function") +cum.SetTitle("Cumulative for Student t") +quant.SetTitle("10-quantiles for Student t") +Canvas.Divide(2, 2) +Canvas.cd(1) +pdf.SetLineWidth(2) +pdf.DrawCopy() +Canvas.cd(2) +cum.SetLineWidth(2) +cum.SetLineColor(kRed) +cum.Draw() +Canvas.cd(3) +quant.Draw() +quant.SetLineWidth(2) +quant.SetLineColor(kBlue) +quant.SetStats(0) +Canvas.cd(4) +pdfq[0].SetTitle("Student t & its quantiles") +pdf.SetTitle("") +pdf.Draw() +pdfq[0].SetTitle("Student t & its quantiles") + +# Set the colors in every quantile. +i = 1 +for pd in pdfq[1:]: + pd.SetStats(0) + i += 1 + pd.SetFillColor(i) + pd.Draw("same") +Canvas.Modified() -- GitLab