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
00013
00014
00015
00016 TCanvas *myc = new TCanvas("myc",
00017 "Confidence intervals on the fitted function",1200, 500);
00018 myc->Divide(3,1);
00019
00020
00021
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
00033 TF1 *fpol = new TF1("fpol", "pol1", -1, 1);
00034 fpol->SetLineWidth(2);
00035 gr->Fit(fpol, "Q");
00036
00037
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
00043 (TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint);
00044
00045
00046
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
00055 myc->cd(2);
00056
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
00067 TH1D *hint = new TH1D("hint",
00068 "Fitted gaussian with .95 conf.band", 100, -3, 3);
00069 (TVirtualFitter::GetFitter())->GetConfidenceIntervals(hint);
00070
00071
00072 hint->SetStats(kFALSE);
00073 hint->SetFillColor(2);
00074 hint->Draw("e3 same");
00075
00076
00077
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
00088 rnd = 2*gRandom->Rndm()*e-e;
00089 z = f2->Eval(x,y)*(1+rnd);
00090 gr2->SetPoint(i,x,y,z);
00091 }
00092
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
00098 f2->SetParameters(0.5,1.5);
00099 gr2->Fit(f2, "Q");
00100
00101 (TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint2);
00102
00103
00104
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