# Sigmoid fit to efficiency plot

**URL:** <https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512>\
**Category:** Newbie\
**Created:** [March 21, 2020, 10:59am UTC](https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512 "2020-03-21T10:59:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 21, 2020, 10:59am UTC](https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512/1 "2020-03-21T10:59:40Z")

</div>

_ROOT Version:_ 6.18/04  
_Platform:_ Linux

Hi all! I have plotted efficiency curves, and now I want to fit sigmoid functions to them. I tried to fit a sigmoid function to the first efficiency curve (the red one), which is called eff[0].

 ![c1](https://root-forum.cern.ch/uploads/default/original/3X/9/8/9877e6b9f871e6edb47921bde41ffe59ae13a705.png)

I tried:

```auto
TF1 *sigm = new TF1("sigm", "(1/(1+ TMath::Exp(-[0]*(x-[1]))))", 0, 230);
sigm->SetParLimits(0, 0.1, 5);
sigm->SetParLimits(1, 20, 60);
eff[0]->Fit(sigm);

```

The problem is that I get a different fit every time I run it (even though, my efficiency curve is the same each run). And shouldn’t I also be able to change my parameter limits slightly without it affecting my fit, because what I do with the parameter limits is that I am basically guessing what value I think the parameter will have, right? And so my parameter output changes each time, but surely, it should just find the best fit each time, which should be the same? Also, my terminal says: STATUS = FAILED and ERROR MATRIX UNCERTAINTY = 100%. What am I doing wrong? Thanks!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 21, 2020, 11:10am UTC](https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512/2 "2020-03-21T11:10:47Z")

</div>

Try (before fitting): `sigm->SetParameters(0.5, 50.);`

In ROOT, before you try to fit your graph or histogram, you MUST set “reasonable” initial values for ALL parameters of your function (except for some “built-in” formulas, like “gaus”, for which the standard fit procedure can automatically “guess” them), otherwise the fitting procedure may easily misbehave.

Search for “Setting initial conditions” in the [TGraph::Fit](https://root.cern.ch/doc/master/classTGraph.html#aa978c8ee0162e661eae795f6f3a35589) and [TH1::Fit](https://root.cern.ch/doc/master/classTH1.html#a7e7d34c91d5ebab4fc9bba3ca47dabdd) methods descriptions.

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 21, 2020, 2:11pm UTC](https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512/3 "2020-03-21T14:11:25Z")

</div>

I tried adding that line, and my fit looks better, but it still says STATUS: FAILED, which means that it couldn’t find a proper fit I guess? And the fit still changes quite a lot depending on which initial values I choose… It’s almost as if, it just draws the sigmoid function using the chosen parameters, but it doesn’t actually fit it to my data.

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [March 23, 2020, 10:10am UTC](https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512/4 "2020-03-23T10:10:14Z")

</div>

@moneta can you help with this one?

---

<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:** [March 23, 2020, 10:20am UTC](https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512/5 "2020-03-23T10:20:54Z")

</div>

Already @Wile_E_Coyote answers. It is essential to set parameters as close as possible to the solution. In addition you might need to set parameter limits to avoid regions where the function is ill defined or does not make sense for your data, for example wrong slopes for your sigmoids.

However I see that some of your data contains outliers (see greed data). This could also make the fit more difficult and might require stronger constraints in your parameter

Lorenzo

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 23, 2020, 10:52am UTC](https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512/6 "2020-03-23T10:52:57Z")

</div>

The fit works if I add “W” or “EX0” to the Fit function!

---

<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:** [March 23, 2020, 11:00am UTC](https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512/7 "2020-03-23T11:00:11Z")

</div>

Hi,

I think you need to add `EX0`. It looks to me your plot comes from an histogram. There is then no reason to have an error in ‘X’.

Lorenzo

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 23, 2020, 11:02am UTC](https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512/8 "2020-03-23T11:02:34Z")

</div>

Hi, yes, my plot comes from a histogram. I tried both “W” and “EX0”, and “W” seems to give a nicer fit in general, I am just not sure whether it is justified to just ignore all my errors…

---

<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:** [March 23, 2020, 11:07am UTC](https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512/9 "2020-03-23T11:07:56Z")

</div>

But what is the type of eff[0]. If your data are efficiencies, for a correct error display and fitting you should use the [TEfficiency](https://root.cern.ch/doc/master/classTEfficiency.html) class and [TEfficiency::Fit](https://root.cern.ch/doc/master/classTEfficiency.html#afd9653954fe487243b145f31853e65c0)

Lorenzo

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 23, 2020, 12:02pm UTC](https://root-forum.cern.ch/t/sigmoid-fit-to-efficiency-plot/38512/10 "2020-03-23T12:02:34Z")

</div>

> [@Fit function uses initial parameter values](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516):
>
> ROOT Version: 6.18/04 Platform: Linux I defined a sigmoid function with 2 parameters (and set the initial parameter values). I am now trying to fit this function to a plot (called fun[1]), however my ‘fit’ always just takes my initial parameter values, even though that is definitely not the best fit. Most of the time I also get the message saying that STATUS = FAILED, a few times, however, I got STATUS = CONVERGED, but that result clearly wasn’t the best fit. I tried: TF1 \*sigm = new TF1("si…
