#include "TRandom.h"
#include "TGraphErrors.h"
#include "TF1.h"
#include "TCanvas.h"
#include "TLegend.h"
void fitLinearRobust()
{
Int_t npoints = 250;
Int_t fraction = Int_t(0.8*npoints);
Double_t *x = new Double_t[npoints];
Double_t *y = new Double_t[npoints];
Double_t *e = new Double_t[npoints];
TRandom r;
Int_t i;
for (i=0; i<fraction; i++){
x[i]=r.Uniform(-2, 2);
e[i]=1;
y[i]=1 + 2*x[i] + 3*x[i]*x[i] + 4*x[i]*x[i]*x[i] + e[i]*r.Gaus();
}
for (i=fraction; i<npoints; i++){
x[i]=r.Uniform(-1, 1);
e[i]=1;
y[i] = 1 + 2*x[i] + 3*x[i]*x[i] + 4*x[i]*x[i]*x[i] + r.Landau(10, 5);
}
TGraphErrors *grr = new TGraphErrors(npoints, x, y, 0, e);
grr->SetMinimum(-30);
grr->SetMaximum(80);
TF1 *ffit1 = new TF1("ffit1", "pol3", -5, 5);
TF1 *ffit2 = new TF1("ffit2", "pol3", -5, 5);
ffit1->SetLineColor(kBlue);
ffit2->SetLineColor(kRed);
TCanvas *myc = new TCanvas("myc", "Linear and robust linear fitting");
myc->SetFillColor(42);
myc->SetGrid();
grr->Draw("ap");
printf("Ordinary least squares:\n");
grr->Fit(ffit1);
printf("Resistant Least trimmed squares fit:\n");
grr->Fit(ffit2, "+rob=0.75");
TLegend *leg = new TLegend(0.6, 0.8, 0.89, 0.89);
leg->AddEntry(ffit1, "Ordinary least squares", "l");
leg->AddEntry(ffit2, "LTS regression", "l");
leg->SetFillColor(42);
leg->Draw();
delete [] x;
delete [] y;
delete [] e;
}
|
|