ConfidenceIntervals.C

Go to the documentation of this file.
00001 
00002 #include "TGraphErrors.h"
00003 #include "TGraph2DErrors.h"
00004 #include "TCanvas.h"
00005 #include "TF2.h"
00006 #include "TH1.h"
00007 #include "TVirtualFitter.h"
00008 #include "TRandom.h"
00009 
00010 void ConfidenceIntervals()
00011 {
00012 //Illustrates TVirtualFitter::GetConfidenceIntervals
00013 //This method computes confidence intervals for the fitted function
00014 //Author: Anna Kreshuk
00015    
00016    TCanvas *myc = new TCanvas("myc", 
00017       "Confidence intervals on the fitted function",1200, 500);
00018    myc->Divide(3,1);
00019 
00020 /////1. A graph
00021    //Create and fill a graph
00022    Int_t ngr = 100;
00023    TGraph *gr = new TGraph(ngr);
00024    gr->SetName("GraphNoError");
00025    Double_t x, y;
00026    Int_t i;
00027    for (i=0; i<ngr; i++){
00028       x = gRandom->Uniform(-1, 1);
00029       y = -1 + 2*x + gRandom->Gaus(0, 1);
00030       gr->SetPoint(i, x, y);
00031    }
00032    //Create the fitting function
00033    TF1 *fpol = new TF1("fpol", "pol1", -1, 1);
00034    fpol->SetLineWidth(2);
00035    gr->Fit(fpol, "Q");
00036 
00037    //Create a TGraphErrors to hold the confidence intervals
00038    TGraphErrors *grint = new TGraphErrors(ngr);
00039    grint->SetTitle("Fitted line with .95 conf. band");
00040    for (i=0; i<ngr; i++)
00041       grint->SetPoint(i, gr->GetX()[i], 0);
00042    //Compute the confidence intervals at the x points of the created graph
00043    (TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint);
00044    //Now the "grint" graph contains function values as its y-coordinates
00045    //and confidence intervals as the errors on these coordinates
00046    //Draw the graph, the function and the confidence intervals
00047    myc->cd(1);
00048    grint->SetLineColor(kRed);
00049    grint->Draw("ap");
00050    gr->SetMarkerStyle(5);
00051    gr->SetMarkerSize(0.7);
00052    gr->Draw("psame");
00053 
00054 /////2. A histogram
00055    myc->cd(2);
00056    //Create, fill and fit a histogram
00057    Int_t nh=5000;
00058    TH1D *h = new TH1D("h", 
00059       "Fitted gaussian with .95 conf.band", 100, -3, 3);
00060    h->FillRandom("gaus", nh);
00061    TF1 *f = new TF1("fgaus", "gaus", -3, 3);
00062    f->SetLineWidth(2);
00063    h->Fit(f, "Q");
00064    h->Draw();
00065    
00066    //Create a histogram to hold the confidence intervals
00067    TH1D *hint = new TH1D("hint", 
00068       "Fitted gaussian with .95 conf.band", 100, -3, 3);
00069    (TVirtualFitter::GetFitter())->GetConfidenceIntervals(hint);
00070    //Now the "hint" histogram has the fitted function values as the 
00071    //bin contents and the confidence intervals as bin errors
00072    hint->SetStats(kFALSE);
00073    hint->SetFillColor(2);
00074    hint->Draw("e3 same");
00075 
00076 /////3. A 2d graph
00077    //Create and fill the graph
00078    Int_t ngr2 = 100;
00079    Double_t z, rnd, e=0.3;
00080    TGraph2D *gr2 = new TGraph2D(ngr2);
00081    gr2->SetName("Graph2DNoError");
00082    TF2  *f2 = new TF2("f2",
00083       "1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+250",-6,6,-6,6);
00084    f2->SetParameters(1,1);
00085    for (i=0; i<ngr2; i++){
00086       f2->GetRandom2(x,y);
00087       // Generate a random number in [-e,e]
00088       rnd = 2*gRandom->Rndm()*e-e; 
00089       z = f2->Eval(x,y)*(1+rnd);
00090       gr2->SetPoint(i,x,y,z);
00091    }
00092    //Create a graph with errors to store the intervals
00093    TGraph2DErrors *grint2 = new TGraph2DErrors(ngr2);
00094    for (i=0; i<ngr2; i++)
00095       grint2->SetPoint(i, gr2->GetX()[i], gr2->GetY()[i], 0);
00096 
00097    //Fit the graph
00098    f2->SetParameters(0.5,1.5);
00099    gr2->Fit(f2, "Q");
00100    //Compute the confidence intervals
00101    (TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint2);   
00102    //Now the "grint2" graph contains function values as z-coordinates
00103    //and confidence intervals as their errors
00104    //draw
00105    myc->cd(3);
00106    f2->SetNpx(30);
00107    f2->SetNpy(30);
00108    f2->SetFillColor(kBlue);
00109    f2->Draw("surf4");
00110    grint2->SetNpx(20);
00111    grint2->SetNpy(20);
00112    grint2->SetMarkerStyle(24);
00113    grint2->SetMarkerSize(0.7);
00114    grint2->SetMarkerColor(kRed);
00115    grint2->SetLineColor(kRed);
00116    grint2->Draw("E0 same");
00117    grint2->SetTitle("Fitted 2d function with .95 error bars");
00118 
00119    myc->cd();
00120 
00121 }
00122       
00123 
00124 
00125 

Generated on Tue Jul 5 15:44:06 2011 for ROOT_528-00b_version by  doxygen 1.5.1