Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
normalDist.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_math
3## \notebook
4## Tutorial illustrating the new statistical distributions functions (pdf, cdf and quantile)
5##
6## based on Anna Kreshuk's normalDist.C
7##
8## \macro_image
9## \macro_code
10##
11## \author Juan Fernando Jaramillo Botero
12
13from ROOT import TF1, TCanvas, TSystem, TAxis, TLegend
14from ROOT import kRed, kGreen, kBlue
15
16# Create the one dimentional functions for normal distribution.
17pdfunc = TF1("pdf","ROOT::Math::normal_pdf(x, [0],[1])", -5, 5)
18cdfunc = TF1("cdf","ROOT::Math::normal_cdf(x, [0],[1])", -5, 5)
19ccdfunc = TF1("cdf_c","ROOT::Math::normal_cdf_c(x, [0])", -5, 5)
20qfunc = TF1("quantile","ROOT::Math::normal_quantile(x, [0])", 0, 1)
21cqfunc = TF1("quantile_c","ROOT::Math::normal_quantile_c(x, [0])", 0, 1)
22
23# Set the parameters for the normal distribution with sigma to 1 and mean to
24# zero. And set the color to blue and title to none.
25pdfunc.SetParameters(1.0, 0.0)
26pdfunc.SetTitle("")
27pdfunc.SetLineColor(kBlue)
28
29# Set the configuration for the X and Y axis.
30Xaxis = pdfunc.GetXaxis()
31Yaxis = pdfunc.GetYaxis()
32Xaxis.SetLabelSize(0.06)
33Xaxis.SetTitle("x")
34Xaxis.SetTitleSize(0.07)
35Xaxis.SetTitleOffset(0.55)
36Yaxis.SetLabelSize(0.06)
37
38# Set sigma to 1 and mean to zero, for the cumulative normal distribution, and
39# set the color to red and title to none.
40cdfunc.SetParameters(1.0, 0.0)
41cdfunc.SetTitle("")
42cdfunc.SetLineColor(kRed)
43
44# Set the configuration for the X and Y axis for the cumulative normal
45# distribution.
46cdXaxis = cdfunc.GetXaxis()
47cdYaxis = cdfunc.GetYaxis()
48cdXaxis.SetLabelSize(0.06)
49cdXaxis.SetTitle("x")
50cdXaxis.SetTitleSize(0.07)
51cdXaxis.SetTitleOffset(0.55)
52cdYaxis.SetLabelSize(0.06)
53cdYaxis.SetTitle("p")
54cdYaxis.SetTitleSize(0.07)
55cdYaxis.SetTitleOffset(0.55)
56
57# Set sigma to 1 and mean to zero, for the survival function of normal
58# distribution, and set the color to green and title to none
59ccdfunc.SetParameters(1.0, 0.0)
60ccdfunc.SetTitle("")
61ccdfunc.SetLineColor(kGreen)
62
63# Set sigma to 1 and mean to zero, for the quantile of normal distribution
64# To get more precision for p close to 0 or 1, set Npx to 1000
65qfunc.SetParameter(0, 1.0)
66qfunc.SetTitle("")
67qfunc.SetLineColor(kRed)
68qfunc.SetNpx(1000)
69
70# Set the configuration of X and Y axis
71qfXaxis = qfunc.GetXaxis()
72qfYaxis = qfunc.GetYaxis()
73qfXaxis.SetLabelSize(0.06)
74qfXaxis.SetTitle("p")
75qfYaxis.SetLabelSize(0.06)
76qfXaxis.SetTitleSize(0.07)
77qfXaxis.SetTitleOffset(0.55)
78qfYaxis.SetTitle("x")
79qfYaxis.SetTitleSize(0.07)
80qfYaxis.SetTitleOffset(0.55)
81
82# Set sigma to 1 and mean to zero of survival function of quantile of normal
83# distribution, and set color to green and title to none.
84cqfunc.SetParameter(0, 1.0)
85cqfunc.SetTitle("")
86cqfunc.SetLineColor(kGreen)
87cqfunc.SetNpx(1000)
88
89# Create canvas and divide in three parts
90c1 = TCanvas("c1", "Normal Distributions", 100, 10, 600, 800)
91c1.Divide(1, 3)
92c1.cd(1)
93
94# Draw the normal distribution
95pdfunc.Draw()
96legend1 = TLegend(0.583893, 0.601973, 0.885221, 0.854151)
97legend1.AddEntry(pdfunc, "normal_pdf", "l")
98legend1.Draw()
99
100# Draw the cumulative normal distribution
101c1.cd(2)
102cdfunc.Draw()
103ccdfunc.Draw("same")
104legend2 = TLegend(0.585605, 0.462794, 0.886933, 0.710837)
105legend2.AddEntry(cdfunc, "normal_cdf", "l")
106legend2.AddEntry(ccdfunc, "normal_cdf_c", "l")
107legend2.Draw()
108
109# Draw the normal quantile of normal distribution
110c1.cd(3)
111qfunc.Draw()
112cqfunc.Draw("same")
113legend3 = TLegend(0.315094, 0.633668, 0.695179, 0.881711)
114legend3.AddEntry(qfunc, "normal_quantile", "l")
115legend3.AddEntry(cqfunc, "normal_quantile_c", "l")
116legend3.Draw()
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:213
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23