Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
tStudent.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_math
3 /// \notebook
4 /// Example macro describing the student t distribution
5 ///
6 /// ~~~{.cpp}
7 /// root[0]: .x tStudent.C
8 /// ~~~
9 ///
10 /// It draws the pdf, the cdf and then 10 quantiles of the t Student distribution
11 ///
12 /// \macro_image
13 /// \macro_code
14 ///
15 /// \author Magdalena Slawinska
16 
17 #include "TH1.h"
18 #include "TF1.h"
19 #include "TCanvas.h"
20 #include "TSystem.h"
21 #include "TLegend.h"
22 #include "TLegendEntry.h"
23 #include "Math/DistFunc.h"
24 
25 
26 void tStudent()
27 {
28 
29  /* gSystem->Load("libMathMore");*/
30 
31  // this is the way to force load of MathMore in Cling
32  ROOT::Math::MathMoreLibrary::Load();
33 
34  int n=100;
35  double a=-5.;
36  double b=5.;
37  //double r = 3;
38  TF1* pdf = new TF1("pdf", "ROOT::Math::tdistribution_pdf(x,3.0)", a,b);
39  TF1* cum = new TF1("cum", "ROOT::Math::tdistribution_cdf(x,3.0)", a,b);
40 
41  TH1D* quant = new TH1D("quant", "", 9, 0, 0.9);
42 
43  for(int i=1; i < 10; i++)
44  quant->Fill((i-0.5)/10.0, ROOT::Math::tdistribution_quantile((1.0*i)/10, 3.0 ) );
45 
46  double xx[10];
47  xx[0] = -1.5;
48  for(int i=1; i<9; i++)
49  xx[i]= quant->GetBinContent(i);
50  xx[9] = 1.5;
51  TH1D* pdfq[10];
52  //int nbin = n/10.0;
53  for(int i=0; i < 9; i++) {
54  int nbin = n * (xx[i+1]-xx[i])/3.0 + 1.0;
55  TString name = "pdf";
56  name += i;
57  pdfq[i]= new TH1D(name, "", nbin,xx[i],xx[i+1] );
58  for(int j=1; j<nbin; j++) {
59  double x= j*(xx[i+1]-xx[i])/nbin + xx[i];
60  pdfq[i]->SetBinContent(j, ROOT::Math::tdistribution_pdf(x,3));
61  }
62  }
63 
64  TCanvas *Canvas = new TCanvas("DistCanvas", "Student Distribution graphs", 10, 10, 800, 700);
65  pdf->SetTitle("Student t distribution function");
66  cum->SetTitle("Cumulative for Student t");
67  quant->SetTitle("10-quantiles for Student t");
68  Canvas->Divide(2, 2);
69  Canvas->cd(1);
70  pdf->SetLineWidth(2);
71  pdf->DrawCopy();
72  Canvas->cd(2);
73  cum->SetLineWidth(2);
74  cum->SetLineColor(kRed);
75  cum->Draw();
76  Canvas->cd(3);
77  quant->Draw();
78  quant->SetLineWidth(2);
79  quant->SetLineColor(kBlue);
80  quant->SetStats(0);
81  Canvas->cd(4);
82  pdfq[0]->SetTitle("Student t & its quantiles");
83  pdf->SetTitle("");
84  pdf->Draw();
85  //pdfq[0]->SetAxisRange(-1.5, 0, 1.5,1.0);
86  pdfq[0]->SetTitle("Student t & its quantiles");
87  for(int i=0; i < 9; i++) {
88  pdfq[i]->SetStats(0);
89  pdfq[i]->SetFillColor(i+1);
90  pdfq[i]->Draw("same");
91  }
92  Canvas->Modified();
93  Canvas->cd();
94 }