Logo ROOT   6.14/05
Reference Guide
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 
13 from ROOT import TF1, TCanvas, TSystem, TAxis, TLegend
14 from ROOT import kRed, kGreen, kBlue
15 
16 # Create the one dimentional functions for normal distribution.
17 pdfunc = TF1("pdf","ROOT::Math::normal_pdf(x, [0],[1])", -5, 5)
18 cdfunc = TF1("cdf","ROOT::Math::normal_cdf(x, [0],[1])", -5, 5)
19 ccdfunc = TF1("cdf_c","ROOT::Math::normal_cdf_c(x, [0])", -5, 5)
20 qfunc = TF1("quantile","ROOT::Math::normal_quantile(x, [0])", 0, 1)
21 cqfunc = 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.
25 pdfunc.SetParameters(1.0, 0.0)
26 pdfunc.SetTitle("")
27 pdfunc.SetLineColor(kBlue)
28 
29 # Set the configuration for the X and Y axis.
30 Xaxis = pdfunc.GetXaxis()
31 Yaxis = pdfunc.GetYaxis()
32 Xaxis.SetLabelSize(0.06)
33 Xaxis.SetTitle("x")
34 Xaxis.SetTitleSize(0.07)
35 Xaxis.SetTitleOffset(0.55)
36 Yaxis.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.
40 cdfunc.SetParameters(1.0, 0.0)
41 cdfunc.SetTitle("")
42 cdfunc.SetLineColor(kRed)
43 
44 # Set the configuration for the X and Y axis for the cumulative normal
45 # distribution.
46 cdXaxis = cdfunc.GetXaxis()
47 cdYaxis = cdfunc.GetYaxis()
48 cdXaxis.SetLabelSize(0.06)
49 cdXaxis.SetTitle("x")
50 cdXaxis.SetTitleSize(0.07)
51 cdXaxis.SetTitleOffset(0.55)
52 cdYaxis.SetLabelSize(0.06)
53 cdYaxis.SetTitle("p")
54 cdYaxis.SetTitleSize(0.07)
55 cdYaxis.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
59 ccdfunc.SetParameters(1.0, 0.0)
60 ccdfunc.SetTitle("")
61 ccdfunc.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
65 qfunc.SetParameter(0, 1.0)
66 qfunc.SetTitle("")
67 qfunc.SetLineColor(kRed)
68 qfunc.SetNpx(1000)
69 
70 # Set the configuration of X and Y axis
71 qfXaxis = qfunc.GetXaxis()
72 qfYaxis = qfunc.GetYaxis()
73 qfXaxis.SetLabelSize(0.06)
74 qfXaxis.SetTitle("p")
75 qfYaxis.SetLabelSize(0.06)
76 qfXaxis.SetTitleSize(0.07)
77 qfXaxis.SetTitleOffset(0.55)
78 qfYaxis.SetTitle("x")
79 qfYaxis.SetTitleSize(0.07)
80 qfYaxis.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.
84 cqfunc.SetParameter(0, 1.0)
85 cqfunc.SetTitle("")
86 cqfunc.SetLineColor(kGreen)
87 cqfunc.SetNpx(1000)
88 
89 # Create canvas and divide in three parts
90 c1 = TCanvas("c1", "Normal Distributions", 100, 10, 600, 800)
91 c1.Divide(1, 3)
92 c1.cd(1)
93 
94 # Draw the normal distribution
95 pdfunc.Draw()
96 legend1 = TLegend(0.583893, 0.601973, 0.885221, 0.854151)
97 legend1.AddEntry(pdfunc, "normal_pdf", "l")
98 legend1.Draw()
99 
100 # Draw the cumulative normal distribution
101 c1.cd(2)
102 cdfunc.Draw()
103 ccdfunc.Draw("same")
104 legend2 = TLegend(0.585605, 0.462794, 0.886933, 0.710837)
105 legend2.AddEntry(cdfunc, "normal_cdf", "l")
106 legend2.AddEntry(ccdfunc, "normal_cdf_c", "l")
107 legend2.Draw()
108 
109 # Draw the normal quantile of normal distribution
110 c1.cd(3)
111 qfunc.Draw()
112 cqfunc.Draw("same")
113 legend3 = TLegend(0.315094, 0.633668, 0.695179, 0.881711)
114 legend3.AddEntry(qfunc, "normal_quantile", "l")
115 legend3.AddEntry(cqfunc, "normal_quantile_c", "l")
116 legend3.Draw()
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
The Canvas class.
Definition: TCanvas.h:31
1-Dim function class
Definition: TF1.h:211