From $ROOTSYS/tutorials/fit/ConfidenceIntervals.C


#include "TGraphErrors.h"
#include "TGraph2DErrors.h"
#include "TCanvas.h"
#include "TF2.h"
#include "TH1.h"
#include "TVirtualFitter.h"
#include "TRandom.h"

void ConfidenceIntervals()
{
//Illustrates TVirtualFitter::GetConfidenceIntervals
//This method computes confidence intervals for the fitted function
//Author: Anna Kreshuk

   TCanvas *myc = new TCanvas("myc",
      "Confidence intervals on the fitted function",1200, 500);
   myc->Divide(3,1);

/////1. A graph
   //Create and fill a graph
   Int_t ngr = 100;
   TGraph *gr = new TGraph(ngr);
   gr->SetName("GraphNoError");
   Double_t x, y;
   Int_t i;
   for (i=0; i<ngr; i++){
      x = gRandom->Uniform(-1, 1);
      y = -1 + 2*x + gRandom->Gaus(0, 1);
      gr->SetPoint(i, x, y);
   }
   //Create the fitting function
   TF1 *fpol = new TF1("fpol", "pol1", -1, 1);
   fpol->SetLineWidth(2);
   gr->Fit(fpol, "Q");

   //Create a TGraphErrors to hold the confidence intervals
   TGraphErrors *grint = new TGraphErrors(ngr);
   grint->SetTitle("Fitted line with .95 conf. band");
   for (i=0; i<ngr; i++)
      grint->SetPoint(i, gr->GetX()[i], 0);
   //Compute the confidence intervals at the x points of the created graph
   (TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint);
   //Now the "grint" graph contains function values as its y-coordinates
   //and confidence intervals as the errors on these coordinates
   //Draw the graph, the function and the confidence intervals
   myc->cd(1);
   grint->SetLineColor(kRed);
   grint->Draw("ap");
   gr->SetMarkerStyle(5);
   gr->SetMarkerSize(0.7);
   gr->Draw("psame");

/////2. A histogram
   myc->cd(2);
   //Create, fill and fit a histogram
   Int_t nh=5000;
   TH1D *h = new TH1D("h",
      "Fitted gaussian with .95 conf.band", 100, -3, 3);
   h->FillRandom("gaus", nh);
   TF1 *f = new TF1("fgaus", "gaus", -3, 3);
   f->SetLineWidth(2);
   h->Fit(f, "Q");
   h->Draw();

   //Create a histogram to hold the confidence intervals
   TH1D *hint = new TH1D("hint",
      "Fitted gaussian with .95 conf.band", 100, -3, 3);
   (TVirtualFitter::GetFitter())->GetConfidenceIntervals(hint);
   //Now the "hint" histogram has the fitted function values as the
   //bin contents and the confidence intervals as bin errors
   hint->SetStats(kFALSE);
   hint->SetFillColor(2);
   hint->Draw("e3 same");

/////3. A 2d graph
   //Create and fill the graph
   Int_t ngr2 = 100;
   Double_t z, rnd, e=0.3;
   TGraph2D *gr2 = new TGraph2D(ngr2);
   gr2->SetName("Graph2DNoError");
   TF2  *f2 = new TF2("f2",
      "1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+250",-6,6,-6,6);
   f2->SetParameters(1,1);
   for (i=0; i<ngr2; i++){
      f2->GetRandom2(x,y);
      // Generate a random number in [-e,e]
      rnd = 2*gRandom->Rndm()*e-e;
      z = f2->Eval(x,y)*(1+rnd);
      gr2->SetPoint(i,x,y,z);
   }
   //Create a graph with errors to store the intervals
   TGraph2DErrors *grint2 = new TGraph2DErrors(ngr2);
   for (i=0; i<ngr2; i++)
      grint2->SetPoint(i, gr2->GetX()[i], gr2->GetY()[i], 0);

   //Fit the graph
   f2->SetParameters(0.5,1.5);
   gr2->Fit(f2, "Q");
   //Compute the confidence intervals
   (TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint2);
   //Now the "grint2" graph contains function values as z-coordinates
   //and confidence intervals as their errors
   //draw
   myc->cd(3);
   f2->SetNpx(30);
   f2->SetNpy(30);
   f2->SetFillColor(kBlue);
   f2->Draw("surf4");
   grint2->SetNpx(20);
   grint2->SetNpy(20);
   grint2->SetMarkerStyle(24);
   grint2->SetMarkerSize(0.7);
   grint2->SetMarkerColor(kRed);
   grint2->SetLineColor(kRed);
   grint2->Draw("E0 same");
   grint2->SetTitle("Fitted 2d function with .95 error bars");

   myc->cd();

}




 ConfidenceIntervals.C:1
 ConfidenceIntervals.C:2
 ConfidenceIntervals.C:3
 ConfidenceIntervals.C:4
 ConfidenceIntervals.C:5
 ConfidenceIntervals.C:6
 ConfidenceIntervals.C:7
 ConfidenceIntervals.C:8
 ConfidenceIntervals.C:9
 ConfidenceIntervals.C:10
 ConfidenceIntervals.C:11
 ConfidenceIntervals.C:12
 ConfidenceIntervals.C:13
 ConfidenceIntervals.C:14
 ConfidenceIntervals.C:15
 ConfidenceIntervals.C:16
 ConfidenceIntervals.C:17
 ConfidenceIntervals.C:18
 ConfidenceIntervals.C:19
 ConfidenceIntervals.C:20
 ConfidenceIntervals.C:21
 ConfidenceIntervals.C:22
 ConfidenceIntervals.C:23
 ConfidenceIntervals.C:24
 ConfidenceIntervals.C:25
 ConfidenceIntervals.C:26
 ConfidenceIntervals.C:27
 ConfidenceIntervals.C:28
 ConfidenceIntervals.C:29
 ConfidenceIntervals.C:30
 ConfidenceIntervals.C:31
 ConfidenceIntervals.C:32
 ConfidenceIntervals.C:33
 ConfidenceIntervals.C:34
 ConfidenceIntervals.C:35
 ConfidenceIntervals.C:36
 ConfidenceIntervals.C:37
 ConfidenceIntervals.C:38
 ConfidenceIntervals.C:39
 ConfidenceIntervals.C:40
 ConfidenceIntervals.C:41
 ConfidenceIntervals.C:42
 ConfidenceIntervals.C:43
 ConfidenceIntervals.C:44
 ConfidenceIntervals.C:45
 ConfidenceIntervals.C:46
 ConfidenceIntervals.C:47
 ConfidenceIntervals.C:48
 ConfidenceIntervals.C:49
 ConfidenceIntervals.C:50
 ConfidenceIntervals.C:51
 ConfidenceIntervals.C:52
 ConfidenceIntervals.C:53
 ConfidenceIntervals.C:54
 ConfidenceIntervals.C:55
 ConfidenceIntervals.C:56
 ConfidenceIntervals.C:57
 ConfidenceIntervals.C:58
 ConfidenceIntervals.C:59
 ConfidenceIntervals.C:60
 ConfidenceIntervals.C:61
 ConfidenceIntervals.C:62
 ConfidenceIntervals.C:63
 ConfidenceIntervals.C:64
 ConfidenceIntervals.C:65
 ConfidenceIntervals.C:66
 ConfidenceIntervals.C:67
 ConfidenceIntervals.C:68
 ConfidenceIntervals.C:69
 ConfidenceIntervals.C:70
 ConfidenceIntervals.C:71
 ConfidenceIntervals.C:72
 ConfidenceIntervals.C:73
 ConfidenceIntervals.C:74
 ConfidenceIntervals.C:75
 ConfidenceIntervals.C:76
 ConfidenceIntervals.C:77
 ConfidenceIntervals.C:78
 ConfidenceIntervals.C:79
 ConfidenceIntervals.C:80
 ConfidenceIntervals.C:81
 ConfidenceIntervals.C:82
 ConfidenceIntervals.C:83
 ConfidenceIntervals.C:84
 ConfidenceIntervals.C:85
 ConfidenceIntervals.C:86
 ConfidenceIntervals.C:87
 ConfidenceIntervals.C:88
 ConfidenceIntervals.C:89
 ConfidenceIntervals.C:90
 ConfidenceIntervals.C:91
 ConfidenceIntervals.C:92
 ConfidenceIntervals.C:93
 ConfidenceIntervals.C:94
 ConfidenceIntervals.C:95
 ConfidenceIntervals.C:96
 ConfidenceIntervals.C:97
 ConfidenceIntervals.C:98
 ConfidenceIntervals.C:99
 ConfidenceIntervals.C:100
 ConfidenceIntervals.C:101
 ConfidenceIntervals.C:102
 ConfidenceIntervals.C:103
 ConfidenceIntervals.C:104
 ConfidenceIntervals.C:105
 ConfidenceIntervals.C:106
 ConfidenceIntervals.C:107
 ConfidenceIntervals.C:108
 ConfidenceIntervals.C:109
 ConfidenceIntervals.C:110
 ConfidenceIntervals.C:111
 ConfidenceIntervals.C:112
 ConfidenceIntervals.C:113
 ConfidenceIntervals.C:114
 ConfidenceIntervals.C:115
 ConfidenceIntervals.C:116
 ConfidenceIntervals.C:117
 ConfidenceIntervals.C:118
 ConfidenceIntervals.C:119
 ConfidenceIntervals.C:120
 ConfidenceIntervals.C:121
 ConfidenceIntervals.C:122
 ConfidenceIntervals.C:123
 ConfidenceIntervals.C:124
 ConfidenceIntervals.C:125
 ConfidenceIntervals.C:126