Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
ConfidenceIntervals.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_fit
3 /// \notebook
4 /// Illustrates TVirtualFitter::GetConfidenceIntervals
5 /// This method computes confidence intervals for the fitted function
6 ///
7 /// \macro_image
8 /// \macro_output
9 /// \macro_code
10 ///
11 /// \author Rene Brun
12 
13 #include "TGraphErrors.h"
14 #include "TGraph2DErrors.h"
15 #include "TCanvas.h"
16 #include "TF2.h"
17 #include "TH1.h"
18 #include "TVirtualFitter.h"
19 #include "TRandom.h"
20 
21 void ConfidenceIntervals()
22 {
23  TCanvas *myc = new TCanvas("myc",
24  "Confidence intervals on the fitted function",1200, 500);
25  myc->Divide(3,1);
26 
27 //### 1. A graph
28  //Create and fill a graph
29  Int_t ngr = 100;
30  TGraph *gr = new TGraph(ngr);
31  gr->SetName("GraphNoError");
32  Double_t x, y;
33  Int_t i;
34  for (i=0; i<ngr; i++){
35  x = gRandom->Uniform(-1, 1);
36  y = -1 + 2*x + gRandom->Gaus(0, 1);
37  gr->SetPoint(i, x, y);
38  }
39  //Create the fitting function
40  TF1 *fpol = new TF1("fpol", "pol1", -1, 1);
41  fpol->SetLineWidth(2);
42  gr->Fit(fpol, "Q");
43 
44  /*Create a TGraphErrors to hold the confidence intervals*/
45  TGraphErrors *grint = new TGraphErrors(ngr);
46  grint->SetTitle("Fitted line with .95 conf. band");
47  for (i=0; i<ngr; i++)
48  grint->SetPoint(i, gr->GetX()[i], 0);
49  /*Compute the confidence intervals at the x points of the created graph*/
50  (TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint);
51  //Now the "grint" graph contains function values as its y-coordinates
52  //and confidence intervals as the errors on these coordinates
53  //Draw the graph, the function and the confidence intervals
54  myc->cd(1);
55  grint->SetLineColor(kRed);
56  grint->Draw("ap");
57  gr->SetMarkerStyle(5);
58  gr->SetMarkerSize(0.7);
59  gr->Draw("psame");
60 
61 //### 2. A histogram
62  myc->cd(2);
63  //Create, fill and fit a histogram
64  Int_t nh=5000;
65  TH1D *h = new TH1D("h",
66  "Fitted gaussian with .95 conf.band", 100, -3, 3);
67  h->FillRandom("gaus", nh);
68  TF1 *f = new TF1("fgaus", "gaus", -3, 3);
69  f->SetLineWidth(2);
70  h->Fit(f, "Q");
71  h->Draw();
72 
73  /*Create a histogram to hold the confidence intervals*/
74  TH1D *hint = new TH1D("hint",
75  "Fitted gaussian with .95 conf.band", 100, -3, 3);
76  (TVirtualFitter::GetFitter())->GetConfidenceIntervals(hint);
77  //Now the "hint" histogram has the fitted function values as the
78  //bin contents and the confidence intervals as bin errors
79  hint->SetStats(kFALSE);
80  hint->SetFillColor(2);
81  hint->Draw("e3 same");
82 
83 //### 3. A 2d graph
84  //Create and fill the graph
85  Int_t ngr2 = 100;
86  Double_t z, rnd, e=0.3;
87  TGraph2D *gr2 = new TGraph2D(ngr2);
88  gr2->SetName("Graph2DNoError");
89  TF2 *f2 = new TF2("f2",
90  "1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+250",-6,6,-6,6);
91  f2->SetParameters(1,1);
92  for (i=0; i<ngr2; i++){
93  f2->GetRandom2(x,y);
94  // Generate a random number in [-e,e]
95  rnd = 2*gRandom->Rndm()*e-e;
96  z = f2->Eval(x,y)*(1+rnd);
97  gr2->SetPoint(i,x,y,z);
98  }
99  //Create a graph with errors to store the intervals
100  TGraph2DErrors *grint2 = new TGraph2DErrors(ngr2);
101  for (i=0; i<ngr2; i++)
102  grint2->SetPoint(i, gr2->GetX()[i], gr2->GetY()[i], 0);
103 
104  //Fit the graph
105  f2->SetParameters(0.5,1.5);
106  gr2->Fit(f2, "Q");
107  /*Compute the confidence intervals*/
108  (TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint2);
109  //Now the "grint2" graph contains function values as z-coordinates
110  //and confidence intervals as their errors
111  //draw
112  myc->cd(3);
113  f2->SetNpx(30);
114  f2->SetNpy(30);
115  f2->SetFillColor(kBlue);
116  f2->Draw("surf4");
117  grint2->SetNpx(20);
118  grint2->SetNpy(20);
119  grint2->SetMarkerStyle(24);
120  grint2->SetMarkerSize(0.7);
121  grint2->SetMarkerColor(kRed);
122  grint2->SetLineColor(kRed);
123  grint2->Draw("E0 same");
124  grint2->SetTitle("Fitted 2d function with .95 error bars");
125 
126  myc->cd();
127 
128 }
129 
130 
131 
132