# Likelihood fit of y(x)

**URL:** <https://root-forum.cern.ch/t/likelihood-fit-of-y-x/40518>\
**Category:** Newbie\
**Created:** [July 19, 2020, 11:11pm UTC](https://root-forum.cern.ch/t/likelihood-fit-of-y-x/40518 "2020-07-19T23:11:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [July 19, 2020, 11:11pm UTC](https://root-forum.cern.ch/t/likelihood-fit-of-y-x/40518/1 "2020-07-19T23:11:43Z")

</div>

Hi,  
I am sorry for my question. Is it possible to make a Likelihood fit of the variables y vs x when the y axis does not represent counts?  
Thanks a lot!

---

<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 20, 2020, 2:39pm UTC](https://root-forum.cern.ch/t/likelihood-fit-of-y-x/40518/2 "2020-07-20T14:39:27Z")

</div>

Hi,

If you know the probability distributions of observing a y giving x, P(y|x) then it is possible.  
Like in case of counts we have for each observation y , we assume that y is Poisson( y | f(x,p) ), where  
f(x,p) is a model function given the expected counts for a given x and some parameters p that needs to be estimated by the fit

Lorenzo

---

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [July 20, 2020, 5:44pm UTC](https://root-forum.cern.ch/t/likelihood-fit-of-y-x/40518/3 "2020-07-20T17:44:55Z")

</div>

Hi Lorenzo,  
Many thanks for your reply.

> [@moneta](#):
>
> If you know the probability distributions of observing a y giving x, P(y|x) then it is possible.

Does that implies to perform a 2D likelihood fit, where the fit function is the pdf of x and y ( f(x,y|p) )?

In my problem I have a TGraphErrors of the cross-section “y” vs phi “x” where the fit function is y\_model = f(x).

1. I tried using Binned Fitter::LikelihoodFit but I get the parameter errors to be extremely large, even if using SetErrorDef(0.5).
2. Is it correct to do the likelihood fit by minimizing the Baker-Cousins sum for every x point using TMinuit, something like:

```auto
void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t par[], Int_t iflag){
    Double_t nll = 0.0;
    for (int i=0; i<no_of_points; i++){
      /* some code to get y_model(x[i]) and y[i] for each point and the fitting parameters*/
       nll = nll + y_model(x[i]) - y[i] + y[i]*TMath::Log(y[i]/y_model(x[i]));
    }
 f = 2*nll;
 }

```

Can I do any of this even if I don’t have a PDF? I just have one value of the cross-section for each phi.  
Many thanks for the clarifications,  
Lily

---

<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 21, 2020, 2:25pm UTC](https://root-forum.cern.ch/t/likelihood-fit-of-y-x/40518/4 "2020-07-21T14:25:14Z")

</div>

Hi,

If y represents counts you can use the Baker-Cousins methods to perform a likelihood fit. distribution of observing y is a Poisson distribution.If you have weighted counts you can use a weighted likelihood fit . These two cases are implemented in ROOT if you do TH1::Fit with option `L` or 'LW` (weighted case).  
If you have a TGraph you can early convert in an histogram if needed.

You can also use `Fitter::LikelihoodFit` (it is used anyway by `TH1::Fit`). If the error are very large, maybe it is because you have a weighted fit ?

But if y does not represent counts, you need to know its distribution. In the majority of case you can do a least square (chi-square) fit, which is also a maximum likelihood fit if the distribution of y is assumed to be normal. Your case, a cross-section value for each phi, looks to me a case for a chi-square fit.  
So you can just use `TGraphErrors::Fit`

Lorenzo

---

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [July 21, 2020, 3:49pm UTC](https://root-forum.cern.ch/t/likelihood-fit-of-y-x/40518/5 "2020-07-21T15:49:58Z")

</div>

Hi,

Thanks for your replies, they have been very helpful. Could I just ask if you could point me out an example of a 1d Likelihood fit of Y vs X where Y does not represent counts if you know its distribution? I can just find likelihood fits to PDFs.

Best regards,

Lily

---

<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 21, 2020, 3:59pm UTC](https://root-forum.cern.ch/t/likelihood-fit-of-y-x/40518/6 "2020-07-21T15:59:58Z")

</div>

Hi,  
As I said every chi-square fit, as those in TGraphErrors::Fit with option “EX0” (no error in X) is a likelihood 1d fit where the Pdf in each point is a normal distribution Gauss( y, f(x), sigma(y) ), where y is the observed y value, f(x) is the expected value given by the model function f(x) and sigma(y) is the error in y.

Lorenzo

---

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [July 21, 2020, 4:12pm UTC](https://root-forum.cern.ch/t/likelihood-fit-of-y-x/40518/7 "2020-07-21T16:12:42Z")

</div>

Hi,  
Yes, thanks a lot, but I would like it to assume that the Pdf in each point is a Poisson distribution instead.  
Thanks,  
Lily
