# Fit on a random generated dataset

**URL:** <https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [July 22, 2020, 10:14am UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565 "2020-07-22T10:14:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorenzo\_Sostero](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lorenzo_sostero/32/21981_2.png) [@Lorenzo\_Sostero](https://root-forum.cern.ch/u/Lorenzo_Sostero)\
**Post date:** [July 22, 2020, 10:14am UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/1 "2020-07-22T10:14:03Z")

</div>

Hello,

I am trying to fit an histogram I have previously random generated using a TF1 function (data-\>Fill(function-\>GetRandom()). To run the fit, I use as model the same type of function.

For example, suppose I generated an histogram (“data” in the following code) in the range [1.50;10] using a Gaussian distribution defined by \mu=2 and \sigma=1 (in the range [1.50;10]). Then the model for the fit would be a TF1 Gaussian with parameter [0] = \mu and [1] = \sigma to fit in the range [1.50;10].

Soon after the definition of the “model” (a TF1 function with parameters), I set the parameter limits. For example, for the parameter [0] that I want to keep in the range [1;3]:

```auto
model -> SetParLimits(0, 1, 3);

```

Then, I set an initial value for the parameter ([0]=1.5):

```auto
model -> SetParameter(0, 1.5);

```

and finally, I can proceed in the fit using:

```auto
data -> Fit ("model", "R");

```

where “R” is added in order to use the range specified in the function.

Is this the right way to proceed? How can I improve performance of the fit still using Migrad?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 22, 2020, 12:24pm UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/2 "2020-07-22T12:24:26Z")

</div>

Hi Lorenzo,

why do you do both

`model -> SetParLimits(0, 1, 3);`  
and  
`model -> SetParameter(0, 1.5);`

? I don’t think this makes sense, you should try only of these.

---

<div class="post-metadata">

**Author:** ![Lorenzo\_Sostero](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lorenzo_sostero/32/21981_2.png) [@Lorenzo\_Sostero](https://root-forum.cern.ch/u/Lorenzo_Sostero)\
**Post date:** [July 22, 2020, 12:39pm UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/3 "2020-07-22T12:39:20Z")

</div>

Thanks for answering.

I use (for example):

```auto
model -> SetParLimits(0,1,3)

```

to set the domain of research where the algorithm can search solutions, while I use:

```auto
model -> SetParameter(0,1.5)

```

to define the starting value of the parameter [0].

Why do you think this has no sense?

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 22, 2020, 1:16pm UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/4 "2020-07-22T13:16:34Z")

</div>

I’m a bit confused actually. Your example is probably too “synthetic” for me 🙂

If your initial histogram has `mu=2`, why do you set  
`model -> SetParameter(0,1.5)`  
and not  
`model -> SetParameter(0,2.)`  
?  
You do use the fact that `mu=2` in the  
`model -> SetParLimits(0,1,3);`  
by limiting the search to `mu +/- 1`, right?

My point is that setting the initial value of `mu` with  
`model -> SetParameter(0, 1.5);`  
will not do much in your example. I just tried with and without (see my code below) and I don’t see much of a difference. Because we already know the best `mu` is at `2`, so then what’s the point of doing this? The fitting procedure will test many values inside the limiting range (1-3) anyway…

```auto
        TF1 *function = new TF1("myFunction", "TMath::Gaus(x, 2, 1)", -10., 10.); // mu = 2, sigma = 1

        TH1F *data = new TH1F("data", ";X;Y", 200, -10., 10.);
        for (unsigned int i=0; i<1e6; ++i)
                data->Fill(function->GetRandom());
        data->Draw("PE");

        TF1 *model = new TF1("myModel", "[0]*TMath::Exp(-0.5*((x-[1])/[2])**2)", 1.5, 10.);
        model->SetParLimits(1, 1, 3);
        model->SetParLimits(2, 0.5, 1.5);
// model->SetParameter(1, 1.5); // is this really neeeded?
        model->SetLineColor(kRed);
        data->Fit(model, "R");

```

---

<div class="post-metadata">

**Author:** ![Lorenzo\_Sostero](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lorenzo_sostero/32/21981_2.png) [@Lorenzo\_Sostero](https://root-forum.cern.ch/u/Lorenzo_Sostero)\
**Post date:** [July 22, 2020, 1:37pm UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/5 "2020-07-22T13:37:45Z")

</div>

I’m sorry, I didn’t explain very well.

When I set:

```auto
model ->SetParameter(0, 1.5)

```

I’m trying to make a guess of a good value for the parameter from which the fit can start the research. In my example, I already know that \mu=2 is the solution, but I don’t want to provide to the algorithm the right value.

Setting the limits plays a similar role. If I know, from mathematical considerations or from particular information on the problem, that \mu can vary only in a predefined domain, I use that range as domain of research. The range [1;4] would have worked fine too.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 22, 2020, 1:41pm UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/6 "2020-07-22T13:41:52Z")

</div>

Ah, okay. So in a way you want to know if the fit can find the “best” value given you already know that value, but at the same time you don’t want to give the fitting procedure too many clues on what that value might be.  
In that case, I think everything is fine with the way you are doing things.

---

<div class="post-metadata">

**Author:** ![Lorenzo\_Sostero](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lorenzo_sostero/32/21981_2.png) [@Lorenzo\_Sostero](https://root-forum.cern.ch/u/Lorenzo_Sostero)\
**Post date:** [July 22, 2020, 1:52pm UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/7 "2020-07-22T13:52:14Z")

</div>

Thanks.

Can you help me to understand how to improve the performance of the fit? On trickier fit I have found not so good performance and I was wondering if there were some other implementations that could help the fit. I have tried:

```auto
data -> Fit ("model", "R M")  

```

but I would like to try also other methods.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 22, 2020, 1:56pm UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/8 "2020-07-22T13:56:52Z")

</div>

Not off the top of my head. It’d be great if you could show the code reproducing your issue. A lot of things could go wrong with a fit: wrong function, bad ranges etc., so it’s difficult to give any advice without seeing the code.

---

<div class="post-metadata">

**Author:** ![Lorenzo\_Sostero](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lorenzo_sostero/32/21981_2.png) [@Lorenzo\_Sostero](https://root-forum.cern.ch/u/Lorenzo_Sostero)\
**Post date:** [July 22, 2020, 2:02pm UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/9 "2020-07-22T14:02:11Z")

</div>

Other tests have been done exactly in the same way as described before. I only changed the generating function of the histogram and adapted the limits and initial values of the parameters.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 22, 2020, 2:04pm UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/10 "2020-07-22T14:04:12Z")

</div>

> [@Lorenzo\_Sostero](#):
>
> I only changed the generating function of the histogram

You mean it’s not a gaussian anymore? Did you change the fitting function accordingly then?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 22, 2020, 2:32pm UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/11 "2020-07-22T14:32:08Z")

</div>

Hi,

`Migrad` , the default algorithm used in the fitting, is probably the best one, according to my experience. You can try using other minimiser, but they do not work better than Migrad.  
You can also trying using `Migrad` from Minuit2 that in some cases might work slightly better, but in majority of the case is the same. Option `M` is not really useful.

The best , if you have issues with your fit is trying to input initial parameters as closed as possible to the solution.

Lorenzo

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 22, 2020, 2:47pm UTC](https://root-forum.cern.ch/t/fit-on-a-random-generated-dataset/40565/12 "2020-07-22T14:47:02Z")

</div>

Another common mistake is to not take into account the errors (or take into account wrong errors) of the distribution people are trying to fit. I.e. if you have had complex manipulations with your histogram (e.g., if you have normalized your distribution before fitting it or maybe filled it with weights != 1), extra care should be taken when fitting.
