# Problem in Chi square fit value

**URL:** <https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963>\
**Category:** Newbie\
**Created:** [April 18, 2020, 8:45am UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963 "2020-04-18T08:45:49Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [April 18, 2020, 8:45am UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/1 "2020-04-18T08:45:50Z")

</div>

I have drawn a histogram and try to fit it using exponential decay function but the value of chi square is of the order of 10^7.  
can you give me any idea about the chi square values?

_ROOT Version:_ Not Provided  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [April 19, 2020, 8:53pm UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/2 "2020-04-19T20:53:30Z")

</div>

I’m not sure I understand the question. Could you elaborate and/or post your macro?

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [April 20, 2020, 8:44am UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/3 "2020-04-20T08:44:20Z")

</div>

@jblomer  
I am asking about how should i judge my fit. Should I use only chi square value or chi square/ndf value to judge a good or a bad fit?

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [April 20, 2020, 10:03am UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/4 "2020-04-20T10:03:34Z")

</div>

Perhaps @moneta or @StephanH can provide a hint.

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [April 20, 2020, 10:37am UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/5 "2020-04-20T10:37:30Z")

</div>

You definitely have to normalise the chi2 value. chi2/ndf is a good start (it should be a bit larger than 1), but you can do it more accurately by integrating the chi2 pdf. This will give you the probability to obtain a certain result or a result worse than that. Check this out:  
[https://root.cern/doc/v620/group\_\_ProbFunc.html#gae07ba00b57c524cfbbc9923cdff3f9b0](https://root.cern/doc/v620/group__ProbFunc.html#gae07ba00b57c524cfbbc9923cdff3f9b0)

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [April 20, 2020, 10:56am UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/6 "2020-04-20T10:56:01Z")

</div>

Thanks @StephanH  
What if chi square has a value of 0.0025 and chi square/ndf = 0.0025/4?  
What if chi square is slightly lesser than 1?

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [April 20, 2020, 11:06am UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/7 "2020-04-20T11:06:51Z")

</div>

Well, compute the probabilities with the function I gave you, or have a look at the distribution in e.g.

> **[Pearson's chi-squared test | Calculating the test-statistic](https://en.wikipedia.org/wiki/Pearson's_chi-squared_test#Calculating_the_test-statistic)**
>
> The value of the test-statistic is

chi2/ndf = 0.0025/4 is way too good to come from random fluctuations. That looks like you have a fit function that is way to good at following all the points (e.g. a 5th-degree polynomial for 6 data points - that can obviously fit the points almost perfectly).

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [April 24, 2020, 7:03am UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/8 "2020-04-24T07:03:36Z")

</div>

Thanks @StephanH  
Could you check whether this curve is good fit or not?

 ![effvsEnergy](https://root-forum.cern.ch/uploads/default/original/3X/2/4/242271394ddaa34a85ea1ef13c3c0b7f2395137e.png)

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [April 24, 2020, 7:14am UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/9 "2020-04-24T07:14:01Z")

</div>

Well, if you used the function I mentioned above, you would find that the probability to find such result or a fit that’s worse is 1.E-95. So no, it’s a bad fit.  
However, visually, it looks somehow ok. That is an indication that the errors of your points are either wrong or non-existent.

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [May 8, 2020, 3:00pm UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/10 "2020-05-08T15:00:56Z")

</div>

Hi @StephanH

 ![ef](https://root-forum.cern.ch/uploads/default/original/3X/c/a/caf8505aec4a119825cb6933c2e5743af6df8677.png)  
The above shown graph(drawn with error bars) shows the value of chi square/ndf = 4.358/4.  
But if I remove the error bars the value of chi square/ndf changes drastically to 334.3/4. ![ef1](https://root-forum.cern.ch/uploads/default/original/3X/4/f/4f3bb1a1f052866fe8346935ddc905791df4baf5.png)  
Could you please elaborate on it?

---

<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:** [May 8, 2020, 3:41pm UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/11 "2020-05-08T15:41:48Z")

</div>

Try to fit the graph with error bars using the [“W” fit option](https://root.cern/doc/master/classTGraph.html#aa978c8ee0162e661eae795f6f3a35589).

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [May 8, 2020, 4:17pm UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/12 "2020-05-08T16:17:25Z")

</div>

Thanks @Wile_E_Coyote.  
It is showing higher values of chi square as it ignores the error bars for every point.  
Can I assume it as a bad fit?

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [May 11, 2020, 7:14am UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/13 "2020-05-11T07:14:53Z")

</div>

Before you fit, you need to get errors that you can trust in. _Just looking_ at the upper graph that you showed, they errors and also the fit look somehow reasonable. Much better, anyway, than anything that you had before. But you really _first_ have to get reasonable errors, and then you can fit and run statistical tests.

By the way:  
The chi^2 probability for the upper fit is  
`TMath::Prob(4.358, 4) = 0.36`, so not that bad.

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [May 11, 2020, 3:50pm UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/14 "2020-05-11T15:50:21Z")

</div>

@StephanH  
Can you please provide me code for finding the chi square probability so that each time I can find on my own.

---

<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:** [May 11, 2020, 4:23pm UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/15 "2020-05-11T16:23:58Z")

</div>

[TF1::GetChisquare](https://root.cern/doc/master/classTF1.html#a2f38b1f9f01326158459b1bfd8fe4285)  
[TF1::GetNDF](https://root.cern/doc/master/classTF1.html#a0ec4878f851604ca6130832b561e2d81)

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [May 11, 2020, 4:39pm UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/16 "2020-05-11T16:39:57Z")

</div>

> [@robertfroast](#):
>
> Can you please provide me code for finding the chi square probability so that each time I can find on my own.

It’s here:

> [@StephanH](#):
>
> This will give you the probability to obtain a certain result or a result worse than that. Check this out:  
> [https://root.cern/doc/v620/group\_\_ProbFunc.html#gae07ba00b57c524cfbbc9923cdff3f9b0](https://root.cern/doc/v620/group__ProbFunc.html#gae07ba00b57c524cfbbc9923cdff3f9b0)

Or here:

> [@StephanH](#):
>
> The chi^2 probability for the upper fit is  
> `TMath::Prob(4.358, 4) = 0.36`

And the values that you have to insert you get from here:

> [@Wile\_E\_Coyote](#):
>
> [TF1::GetChisquare](https://root.cern/doc/master/classTF1.html#a2f38b1f9f01326158459b1bfd8fe4285)  
> [TF1::GetNDF](https://root.cern/doc/master/classTF1.html#a0ec4878f851604ca6130832b561e2d81)

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [May 11, 2020, 5:29pm UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/17 "2020-05-11T17:29:52Z")

</div>

@StephanH

[https://mathworld.wolfram.com/Chi-SquaredDistribution.html](https://mathworld.wolfram.com/Chi-SquaredDistribution.html)  
Actually it is given as P(r/2, chi^2/2) but you have written opposite values of arguments.

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [May 11, 2020, 5:34pm UTC](https://root-forum.cern.ch/t/problem-in-chi-square-fit-value/38963/18 "2020-05-11T17:34:01Z")

</div>

[TMath::Prob](https://root.cern.ch/doc/master/namespaceTMath.html#a3aba6abaf2bc605680c7be8fc5fd98aa)
