Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ConfidenceIntervals.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Illustrates TVirtualFitter::GetConfidenceIntervals This method computes confidence intervals for the fitted function

#include "TGraphErrors.h"
#include "TGraph2DErrors.h"
#include "TCanvas.h"
#include "TF2.h"
#include "TH1.h"
#include "TVirtualFitter.h"
#include "TRandom.h"
void ConfidenceIntervals()
{
TCanvas *myc = new TCanvas("myc",
"Confidence intervals on the fitted function",1000, 500);
myc->Divide(3,1);
//### 1. A graph
//Create and fill a graph
int ngr = 100;
TGraph *gr = new TGraph(ngr);
gr->SetName("GraphNoError");
double x, y;
int 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->Draw("psame");
//### 2. A histogram
myc->cd(2);
//Create, fill and fit a histogram
int 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(false);
hint->SetFillColor(2);
hint->Draw("e3 same");
//### 3. A 2d graph
//Create and fill the graph
int ngr2 = 100;
double 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*/
//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->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();
}
#define f(i)
Definition RSha256.hxx:104
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
@ kRed
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:38
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:45
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:716
1-Dim function class
Definition TF1.h:233
virtual void SetNpx(Int_t npx=100)
Set the number of points used to draw the function.
Definition TF1.cxx:3433
virtual void SetParameters(const Double_t *params)
Definition TF1.h:670
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
Evaluate this function.
Definition TF1.cxx:1441
A 2-Dim function with parameters.
Definition TF2.h:29
virtual void GetRandom2(Double_t &xrandom, Double_t &yrandom, TRandom *rng=nullptr)
Return 2 random numbers following this function shape.
Definition TF2.cxx:547
virtual void SetNpy(Int_t npy=100)
Set the number of points used to draw the function.
Definition TF2.cxx:927
void Draw(Option_t *option="") override
Draw this function with its current attributes.
Definition TF2.cxx:259
Graph 2D class with errors.
void SetPoint(Int_t i, Double_t x, Double_t y, Double_t z) override
Set x, y and z values for point number i.
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition TGraph2D.h:41
Double_t * GetY() const
Definition TGraph2D.h:122
Double_t * GetX() const
Definition TGraph2D.h:121
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="")
Fits this graph with function with name fname Predefined functions such as gaus, expo and poln are au...
Definition TGraph2D.cxx:791
void SetName(const char *name) override
Changes the name of this 2D graph.
void SetNpy(Int_t npx=40)
Sets the number of bins along Y used to draw the function.
void SetTitle(const char *title="") override
Sets the 2D graph title.
virtual void SetPoint(Int_t point, Double_t x, Double_t y, Double_t z)
Sets point number n.
void Draw(Option_t *option="P0") override
Specific drawing options can be used to paint a TGraph2D:
Definition TGraph2D.cxx:738
void SetNpx(Int_t npx=40)
Sets the number of bins along X used to draw the function.
A TGraphErrors is a TGraph with error bars.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition TGraph.cxx:2319
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Axis_t xmin=0, Axis_t xmax=0)
Fit this graph with function with name fname.
Definition TGraph.cxx:1232
Double_t * GetX() const
Definition TGraph.h:138
void SetName(const char *name="") override
Set graph name.
Definition TGraph.cxx:2358
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:809
void SetTitle(const char *title="") override
Change (i.e.
Definition TGraph.cxx:2374
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:664
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3062
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:8954
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1153
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:275
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition TRandom.cxx:682
static TVirtualFitter * GetFitter()
static: return the current Fitter
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
TGraphErrors * gr
Definition legend1.C:25
bool GetConfidenceIntervals(const TH1 *h1, const ROOT::Fit::FitResult &r, TGraphErrors *gr, double cl=0.95)
compute confidence intervals at level cl for a fitted histogram h1 in a TGraphErrors gr
Author
Rene Brun

Definition in file ConfidenceIntervals.C.