ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fitLinearRobust.C
Go to the documentation of this file.
1 #include "TRandom.h"
2 #include "TGraphErrors.h"
3 #include "TF1.h"
4 #include "TCanvas.h"
5 #include "TLegend.h"
6 
8 {
9 //This tutorial shows how the least trimmed squares regression,
10 //included in the TLinearFitter class, can be used for fitting
11 //in cases when the data contains outliers.
12 //Here the fitting is done via the TGraph::Fit function with option "rob":
13 //If you want to use the linear fitter directly for computing
14 //the robust fitting coefficients, just use the TLinearFitter::EvalRobust
15 //function instead of TLinearFitter::Eval
16 //Author: Anna Kreshuk
17 
18  //First generate a dataset, where 20% of points are spoiled by large
19  //errors
20  Int_t npoints = 250;
21  Int_t fraction = Int_t(0.8*npoints);
22  Double_t *x = new Double_t[npoints];
23  Double_t *y = new Double_t[npoints];
24  Double_t *e = new Double_t[npoints];
25  TRandom r;
26  Int_t i;
27  for (i=0; i<fraction; i++){
28  //the good part of the sample
29  x[i]=r.Uniform(-2, 2);
30  e[i]=1;
31  y[i]=1 + 2*x[i] + 3*x[i]*x[i] + 4*x[i]*x[i]*x[i] + e[i]*r.Gaus();
32  }
33  for (i=fraction; i<npoints; i++){
34  //the bad part of the sample
35  x[i]=r.Uniform(-1, 1);
36  e[i]=1;
37  y[i] = 1 + 2*x[i] + 3*x[i]*x[i] + 4*x[i]*x[i]*x[i] + r.Landau(10, 5);
38  }
39 
40  TGraphErrors *grr = new TGraphErrors(npoints, x, y, 0, e);
41  grr->SetMinimum(-30);
42  grr->SetMaximum(80);
43  TF1 *ffit1 = new TF1("ffit1", "pol3", -5, 5);
44  TF1 *ffit2 = new TF1("ffit2", "pol3", -5, 5);
45  ffit1->SetLineColor(kBlue);
46  ffit2->SetLineColor(kRed);
47  TCanvas *myc = new TCanvas("myc", "Linear and robust linear fitting");
48  myc->SetFillColor(42);
49  myc->SetGrid();
50  grr->Draw("ap");
51  //first, let's try to see the result sof ordinary least-squares fit:
52  printf("Ordinary least squares:\n");
53  grr->Fit(ffit1);
54  //the fitted function doesn't really follow the pattern of the data
55  //and the coefficients are far from the real ones
56 
57  printf("Resistant Least trimmed squares fit:\n");
58  //Now let's try the resistant regression
59  //The option "rob=0.75" means that we want to use robust fitting and
60  //we know that at least 75% of data is good points (at least 50% of points
61  //should be good to use this algorithm). If you don't specify any number
62  //and just use "rob" for the option, default value of (npoints+nparameters+1)/2
63  //will be taken
64  grr->Fit(ffit2, "+rob=0.75");
65  //
66  TLegend *leg = new TLegend(0.6, 0.8, 0.89, 0.89);
67  leg->AddEntry(ffit1, "Ordinary least squares", "l");
68  leg->AddEntry(ffit2, "LTS regression", "l");
69  leg->SetFillColor(42);
70  leg->Draw();
71 
72  delete [] x;
73  delete [] y;
74  delete [] e;
75 
76 }
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:1024
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
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:235
Definition: Rtypes.h:61
virtual void SetMinimum(Double_t minimum=-1111)
Set the minimum of the graph.
Definition: TGraph.cxx:2118
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:740
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:326
virtual void SetMaximum(Double_t maximum=-1111)
Set the maximum of the graph.
Definition: TGraph.cxx:2109
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
leg
Definition: legend1.C:34
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Double_t y[n]
Definition: legend1.C:17
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
void fitLinearRobust()
1-Dim function class
Definition: TF1.h:149
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:28
Definition: Rtypes.h:61
virtual Double_t Landau(Double_t mean=0, Double_t sigma=1)
Generate a random number following a Landau distribution with location parameter mu and scale paramet...
Definition: TRandom.cxx:340