# Conditions on parameters in TF1 fit

**URL:** <https://root-forum.cern.ch/t/conditions-on-parameters-in-tf1-fit/38098>\
**Category:** Newbie\
**Created:** [February 22, 2020, 8:56pm UTC](https://root-forum.cern.ch/t/conditions-on-parameters-in-tf1-fit/38098 "2020-02-22T20:56:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [February 22, 2020, 8:56pm UTC](https://root-forum.cern.ch/t/conditions-on-parameters-in-tf1-fit/38098/1 "2020-02-22T20:56:13Z")

</div>

So I would like to fit a normalized histogram corresponding to a superposition of two probability distributions, let’s call them f\_1 and f\_2. As a TF1 object, let’s write this as

```auto
TF1 probFit("probFit", "[0] * f_1([1] + [2] * x) + [3] * f_2([3] + [4] * x)")

```

When using `TH1::Fit`, is there a way to impose the conditions

```auto
[0] >= 0,
[3] >= 0,
[0] + [3] = 1

```

?  
If there is, should this conditions be imposed when declaring `probFit`, or when using `TH1::Fit`?

Also, is there a notation short cut to write `[1] + [2] * x` and `[3] + [4] * x` in terms of `pol 1`? Perhaps something like `pol(1) 1` and `pol(3) 1`, respectively?

---

<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:** [February 24, 2020, 10:35am UTC](https://root-forum.cern.ch/t/conditions-on-parameters-in-tf1-fit/38098/2 "2020-02-24T10:35:10Z")

</div>

Hi,

First of all when doing a standard TH1::Fit it is assumed the normalization is not fixed but also floating. The distribution in each bin is assumed to be Poisson when doing a likelihood fit (“option L”)

For doing a multinomial fit (i.e. fixed normalization) you can use the fit option `MULTI`.

For applying the conditions you the redefine the parameter [3] as 1. - [0] and then you set for the parameter [0] a constraints to be 0 \<= [0] \<= 1, by doing  
`probFit->SetParLimits(0,0., 1.)`

You should also make sure if doing a multinomial fit that the f\_1 and f\_2 functions are normalized (i.e. their integral is equal =1 for every values of their parameters)

You can apply the short notation and for example re-write your function as

`TF1 probFit("probFit", "[0] * f_1(pol1(1)) + [3] * f_2(pol1(4))")`

where f\_1 and f\_2 are functions defined before

Lorenzo

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [February 24, 2020, 8:00pm UTC](https://root-forum.cern.ch/t/conditions-on-parameters-in-tf1-fit/38098/3 "2020-02-24T20:00:27Z")

</div>

Thank you for clarifying. I hadn’t thought of using `SetParLimits()` in the way that you described.

So then if I wanted to do an unbinned multinomial fit, I would set as options `"WL MULTI"?` If not, under what argument do I set `"MULTI"`?

I hadn’t considered `pol1(1)` and `pol1(4)` in the short notation because in my experience without setting variable indices I found `pol 1` works but not `pol1`? Is that a bug?

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [February 24, 2020, 8:38pm UTC](https://root-forum.cern.ch/t/conditions-on-parameters-in-tf1-fit/38098/4 "2020-02-24T20:38:47Z")

</div>

So as a test to recreate the problem between using `pol1` vs. `pol 1`, I tried both

```auto
TF1 logistic("logistic", "1 / (1 + exp(pol1))");
TF1 logistic1("logistic1", "1 / (1 + exp(pol 1))");

```

Both seemed to work this time when in printed them. So maybe the problem I was having with having to use `pol N` instead of `polN` has been resolved?

---

<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:** [February 25, 2020, 9:02am UTC](https://root-forum.cern.ch/t/conditions-on-parameters-in-tf1-fit/38098/5 "2020-02-25T09:02:45Z")

</div>

Hi,  
the option “MULTI” will do a multinomial binned likelihood fit. From an histogram you cannot do un unbinned likelihood fit. You would need to do from a TTree or directly using the data using the Fitter class.

Using `pol1` or `pol 1` should be equivalent because whitespaces are removed when parsing the formula expressions. However I would use `pol1` for better clarity

Lorenzo

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [February 26, 2020, 1:37am UTC](https://root-forum.cern.ch/t/conditions-on-parameters-in-tf1-fit/38098/6 "2020-02-26T01:37:28Z")

</div>

If possible, could I please get some further clarification on usage of the “MULTI”. Do you mean it would be entered as the `option` or `goption` argument in `TTree::Fit()`?

It looks like when I said “unbinned fit”, I meant to say “fit with non-integer bins”. Sorry about that. Because I have been able to use `TH1D::Fit()` with `option = "WL"`.

So with `TH1D::Fit()`, could I have `option = "WL MULTI"`? Looking under the class documentation for TTree and TH1, I’m having difficulty finding a description of the `MULTI` option and how it is used.

---

<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:** [February 27, 2020, 8:59am UTC](https://root-forum.cern.ch/t/conditions-on-parameters-in-tf1-fit/38098/7 "2020-02-27T08:59:01Z")

</div>

Hi,

The option `MULTI` works only for histograms (binned data). It should work also in case of weighted data (adding “WL” option).  
It is true, I realised now it is not in the documentation of TH1::Fit. .I will add then add it

Lorenzo
