# Mean uncertainity of a fit function

**URL:** <https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293>\
**Category:** Newbie\
**Created:** [February 26, 2024, 7:52am UTC](https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293 "2024-02-26T07:52:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [February 26, 2024, 7:52am UTC](https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293/1 "2024-02-26T07:52:54Z")

</div>

Hi everyone,

I am fitting a TGraphErrors() using a TF1() function.

```auto
auto g = new TGraphErrors(n,x,y,nullptr, err_y);
TFitResultPtr r = g->Fit(func, "WRS");

```

I am wondering how to obtain the mean uncertainty in the fitted function. I am unsure what the exact term for this in statistics is. It represents the average of two sigma bounds for each f(x). f = fit function, and x is the x-axis value.  
To clarify a bit more the two red dashed lines around the fit  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/d/7/d7cf1c8e5cb21dacbbdae6101b407f4cb7814ca7.png)

Is there any pre-defined functionality in ROOT for this?

Please share for any additional information from my side.  
Thanks a lot.

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [February 26, 2024, 10:10am UTC](https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293/2 "2024-02-26T10:10:14Z")

</div>

Dear @utkarsh-2000 ,

Thanks for posting on the forum! Yes ROOT provides facilities to get the confidence interval of your fit. From the `TFitResultPtr` object you can call [`r->GetConfidenceIntervals()`](https://root.cern.ch/doc/master/classROOT_1_1Fit_1_1FitResult.html#afddc3ab76b51414d5a9aecd8f19deb7c). See [this tutorial](https://root.cern/doc/master/ConfidenceIntervals_8C.html) for an example usage with plots.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [February 28, 2024, 2:50pm UTC](https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293/3 "2024-02-28T14:50:12Z")

</div>

Hi @vpadulan,  
Thanks for your answer.  
Correct me if am wrong, the GetConfidenceIntervals() only returns the confidence intervals at the points used for fitting, right ?

I was trying the other signature

```auto
double ci[2];
        double points = {345.};
        int stride1 =1, stride2 = 1;
      GetConfidenceIntervals(1, stride1, stride2, points, c1, 0.683, true) ; 
        std::cout << "The confidence level is " << ci[0] << std::endl;

```

However, I am getting.

```auto
error: no matching member function for call to 'GetConfidenceIntervals'
      r->GetConfidenceIntervals(1, stride1, stride2, points, c1, 0.683, true) ; 

```

Can you please help on how to get confidence intervals of unknown points n fitting range?  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [March 1, 2024, 11:27am UTC](https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293/4 "2024-03-01T11:27:34Z")

</div>

Dear @utkarsh-2000 ,

> [@utkarsh-2000](#):
>
> ```auto
> GetConfidenceIntervals(1, stride1, stride2, points, c1, 0.683, true) ; 
> 
> ```

You are not calling any existing signature like this. I guess what you are trying to do is calling this signature [ROOT: ROOT::Fit::FitResult Class Reference](https://root.cern.ch/doc/master/classROOT_1_1Fit_1_1FitResult.html#a65a77daab276d605373d508479c3b6a7) where `x` and `ci` are arrays but in your case they are not arrays so you should correct that.

> Can you please help on how to get confidence intervals of unknown points n fitting range?

About this I am not sure, probably @moneta or @couet know. But in general, the tutorial shows you how to draw the line for the confidence intervals over the entire fit, if you only need to draw and not get the actual values.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 1, 2024, 11:45am UTC](https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293/5 "2024-03-01T11:45:28Z")

</div>

> [@vpadulan](#):
>
> > Can you please help on how to get confidence intervals of unknown points n fitting range?
> 
> About this I am not sure, probably @moneta or @couet know.

I guess @moneta might know better.

---

<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 1, 2024, 12:06pm UTC](https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293/6 "2024-03-01T12:06:52Z")

</div>

Hi,

Yes, the function you need for this is `FitResult::GetConfidenceIntervals`. You can compute the fitted function uncertainty (band) at the fitted points or on whatever points you provide. Afterwards you can create a TGraph from these points and using interpolation display as in the above figure. You can choose teh desired confidence value (e.g 95% as above) and also to re-normalize the uncertainty using the obtained fit chi-square. This is needed when you don’t have uncertainty on data points as above.

Lorenzo

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [March 1, 2024, 12:12pm UTC](https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293/7 "2024-03-01T12:12:15Z")

</div>

Thanks a lot @vpadulan for the help.

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [March 1, 2024, 12:15pm UTC](https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293/8 "2024-03-01T12:15:38Z")

</div>

Thanks @moneta for the detailed explanation on obtaining the confidence bands

> [@moneta](#):
>
> re-normalize the uncertainty using the obtained fit chi-square. This is needed when you don’t have uncertainty on data points as above.

Just to clarify when fitting without the “W” we need this re-normalization and it can be done by the true flag in GetCoincidenceIntervals(), right?

---

<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 4, 2024, 9:23pm UTC](https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293/9 "2024-03-04T21:23:41Z")

</div>

Hi,

The normalization is automatically done when using option “W”, so it is not needed in this case. When fitting a TGraphErrors, and the errors are correctly estimated you might want not to apply this re-normalization.

Lorenzo

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [March 5, 2024, 4:40am UTC](https://root-forum.cern.ch/t/mean-uncertainity-of-a-fit-function/58293/10 "2024-03-05T04:40:10Z")

</div>

Thanks a lot @moneta for the clarification.
