{
"cells": [
{
"cell_type": "markdown",
"id": "71ef89f6",
"metadata": {},
"source": [
"# fitLinearRobust\n",
"This tutorial shows how the least trimmed squares regression,\n",
"included in the TLinearFitter class, can be used for fitting\n",
"in cases when the data contains outliers.\n",
"Here the fitting is done via the TGraph::Fit function with option \"rob\":\n",
"If you want to use the linear fitter directly for computing\n",
"the robust fitting coefficients, just use the TLinearFitter::EvalRobust\n",
"function instead of TLinearFitter::Eval\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Anna Kreshuk \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 19, 2024 at 07:08 PM."
]
},
{
"cell_type": "markdown",
"id": "a374ee71",
"metadata": {},
"source": [
"First generate a dataset, where 20% of points are spoiled by large\n",
"errors"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "4e09608b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:08:47.129763Z",
"iopub.status.busy": "2024-03-19T19:08:47.129115Z",
"iopub.status.idle": "2024-03-19T19:08:48.192674Z",
"shell.execute_reply": "2024-03-19T19:08:48.190634Z"
}
},
"outputs": [],
"source": [
"int npoints = 250;\n",
"int fraction = int(0.8*npoints);\n",
"double *x = new double[npoints];\n",
"double *y = new double[npoints];\n",
"double *e = new double[npoints];\n",
"TRandom r;\n",
"int i;\n",
"for (i=0; iSetMinimum(-30);\n",
"grr->SetMaximum(80);\n",
"TF1 *ffit1 = new TF1(\"ffit1\", \"pol3\", -5, 5);\n",
"TF1 *ffit2 = new TF1(\"ffit2\", \"pol3\", -5, 5);\n",
"ffit1->SetLineColor(kBlue);\n",
"ffit2->SetLineColor(kRed);\n",
"TCanvas *myc = new TCanvas(\"myc\", \"Linear and robust linear fitting\");\n",
"myc->SetGrid();\n",
"grr->Draw(\"ap\");"
]
},
{
"cell_type": "markdown",
"id": "3b993d65",
"metadata": {},
"source": [
"first, let's try to see the result sof ordinary least-squares fit:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "93a9295f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:08:48.202139Z",
"iopub.status.busy": "2024-03-19T19:08:48.201496Z",
"iopub.status.idle": "2024-03-19T19:08:48.604813Z",
"shell.execute_reply": "2024-03-19T19:08:48.603633Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ordinary least squares:\n",
"****************************************\n",
"Minimizer is Linear / Migrad\n",
"Chi2 = 606758\n",
"NDf = 246\n",
"p0 = 15.724 +/- 0.0887641 \n",
"p1 = -0.835912 +/- 0.14096 \n",
"p2 = -3.40616 +/- 0.0607296 \n",
"p3 = 4.82569 +/- 0.0602628 \n"
]
}
],
"source": [
"printf(\"Ordinary least squares:\\n\");\n",
"grr->Fit(ffit1);"
]
},
{
"cell_type": "markdown",
"id": "9ad54274",
"metadata": {},
"source": [
"the fitted function doesn't really follow the pattern of the data\n",
"and the coefficients are far from the real ones"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5e72d4c6",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:08:48.609952Z",
"iopub.status.busy": "2024-03-19T19:08:48.609521Z",
"iopub.status.idle": "2024-03-19T19:08:48.815403Z",
"shell.execute_reply": "2024-03-19T19:08:48.814267Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Resistant Least trimmed squares fit:\n"
]
}
],
"source": [
"printf(\"Resistant Least trimmed squares fit:\\n\");"
]
},
{
"cell_type": "markdown",
"id": "c8aa5080",
"metadata": {},
"source": [
"Now let's try the resistant regression\n",
"The option \"rob=0.75\" means that we want to use robust fitting and\n",
"we know that at least 75% of data is good points (at least 50% of points\n",
"should be good to use this algorithm). If you don't specify any number\n",
"and just use \"rob\" for the option, default value of (npoints+nparameters+1)/2\n",
"will be taken"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7c666180",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:08:48.824063Z",
"iopub.status.busy": "2024-03-19T19:08:48.823629Z",
"iopub.status.idle": "2024-03-19T19:08:49.217527Z",
"shell.execute_reply": "2024-03-19T19:08:49.201596Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"****************************************\n",
"Minimizer is Linear / Robust (h=0.75)\n",
"Chi2 = 634792\n",
"NDf = 246\n",
"p0 = 1.00953\n",
"p1 = 1.71148\n",
"p2 = 2.97937\n",
"p3 = 4.07752\n"
]
}
],
"source": [
"grr->Fit(ffit2, \"+rob=0.75\");"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5a1298a2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:08:49.224834Z",
"iopub.status.busy": "2024-03-19T19:08:49.224431Z",
"iopub.status.idle": "2024-03-19T19:08:49.439373Z",
"shell.execute_reply": "2024-03-19T19:08:49.438103Z"
}
},
"outputs": [],
"source": [
"TLegend *leg = new TLegend(0.6, 0.8, 0.89, 0.89);\n",
"leg->AddEntry(ffit1, \"Ordinary least squares\", \"l\");\n",
"leg->AddEntry(ffit2, \"LTS regression\", \"l\");\n",
"leg->Draw();\n",
"\n",
"delete [] x;\n",
"delete [] y;\n",
"delete [] e;"
]
},
{
"cell_type": "markdown",
"id": "baf9a226",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c7af00e3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:08:49.444530Z",
"iopub.status.busy": "2024-03-19T19:08:49.444133Z",
"iopub.status.idle": "2024-03-19T19:08:49.961871Z",
"shell.execute_reply": "2024-03-19T19:08:49.960779Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%jsroot on\n",
"gROOT->GetListOfCanvases()->Draw()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}