# Getting negative chi sq value, but the fit looks good?

**URL:** <https://root-forum.cern.ch/t/getting-negative-chi-sq-value-but-the-fit-looks-good/54281>\
**Category:** Roofit and RooStats\
**Created:** [March 29, 2023, 12:31pm UTC](https://root-forum.cern.ch/t/getting-negative-chi-sq-value-but-the-fit-looks-good/54281 "2023-03-29T12:31:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Subhu18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@Subhu18](https://root-forum.cern.ch/u/Subhu18)\
**Post date:** [March 29, 2023, 12:31pm UTC](https://root-forum.cern.ch/t/getting-negative-chi-sq-value-but-the-fit-looks-good/54281/1 "2023-03-29T12:31:13Z")

</div>

I am getting this fit when I fit the combined model to the data.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/1/811785b0cc4127681eafd11f07d76ddafe7c613a.png)  
this fit looks good to me but when I print the chi square value it always prints -1.  
can you please correct my code?  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/4/5/4547cd2162a3ae16733e4d928a7705a48f7044a5.png)

[roomodel.cpp](https://root-forum.cern.ch/uploads/short-url/lKUbFIpzgQGzyqXiOQC9gFbry56.cpp) (1.7 KB)

---

<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 29, 2023, 3:36pm UTC](https://root-forum.cern.ch/t/getting-negative-chi-sq-value-but-the-fit-looks-good/54281/2 "2023-03-29T15:36:55Z")

</div>

Hello,  
How did you compute the chisquare ? Using `RooPlot::chiSquare` ?  
See also [How exactly does RooPlot::chisquare() compute the result? - #4 by Xuli](https://root-forum.cern.ch/t/how-exactly-does-rooplot-chisquare-compute-the-result/36388/4)

Lorenzo

---

<div class="post-metadata">

**Author:** ![Subhu18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@Subhu18](https://root-forum.cern.ch/u/Subhu18)\
**Post date:** [March 29, 2023, 7:20pm UTC](https://root-forum.cern.ch/t/getting-negative-chi-sq-value-but-the-fit-looks-good/54281/3 "2023-03-29T19:20:11Z")

</div>

Thanks for your suggestion, I just corrected my code a bit, and it gives Chi Sq = 142.046.  
I did not use `RooPlot::chiSquare` I just used  
double chiSquare = frame-\>chiSquare()  
One more thing, that confuses me is that when I write  
double chiSquare = frame-\>chiSquare(any integer)  
it gives me little different ChiSq value.  
What is the significance of this argument??

---

<div class="post-metadata">

**Author:** ![Subhu18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@Subhu18](https://root-forum.cern.ch/u/Subhu18)\
**Post date:** [March 29, 2023, 7:28pm UTC](https://root-forum.cern.ch/t/getting-negative-chi-sq-value-but-the-fit-looks-good/54281/4 "2023-03-29T19:28:01Z")

</div>

@moneta also If i increase the no. of bins to 10000 .  
I get ChiSq = 23.18 , but my fit looks like this-

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/4/b/4bf08015896255f4f2b02b6283a10c40169b4287.png)  
is there anything wrong in this?  
Thanks.

---

<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 30, 2023, 8:25am UTC](https://root-forum.cern.ch/t/getting-negative-chi-sq-value-but-the-fit-looks-good/54281/5 "2023-03-30T08:25:31Z")

</div>

Hi,

yes, you are using `RooPlot::chiSquare` (see the [ROOT: RooPlot Class Reference](https://root.cern.ch/doc/v606/classRooPlot.html#aec683552a6154d4d8dd00e58e05e8b48)), by calling `frame->chiSquare, since `frame` is a RooPlot object instance.

The function returns the reduced chiSquare (i.e. chisquare divided by the number of bins). If it is after a fit you should provide to the function the number of first parameters.  
In addition, when you are not passing a curve name and a data name, `RooPlot::chiSquare` will compute the chi-square for the last curve you have plotted, in your case I think is the `sig3`. You should then provide the name of the data hist and of the curve. For doing this just ad this code to find these names:

```auto
for (int i = 0; i < frame->numItems(); i++) {
      std::cout << "component " << frame->nameOf(i) << " is a " << frame->getObject(i)->IsA()->GetName() << std::endl;
   }

```

and then call the correct one in `frame->chiSquare` , probably it should be: the data are the first (index 0) and the full pdf is second (with index 1) since this is the order how you have plotted them.

```auto
frame->chiSquare((frame->nameOf(1),frame->nameOf(0), numberOfFitParamters)

```

The number of fit parameters seems to be 6, looking at your code.

If you increase the number of bins, since the function is returning the reduce chi-square it should still give a results around 1 if the fit is good.

Best,

Lorenzo

---

<div class="post-metadata">

**Author:** ![Subhu18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@Subhu18](https://root-forum.cern.ch/u/Subhu18)\
**Post date:** [March 31, 2023, 7:38pm UTC](https://root-forum.cern.ch/t/getting-negative-chi-sq-value-but-the-fit-looks-good/54281/6 "2023-03-31T19:38:54Z")

</div>

Dear @moneta

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/0/1/01e0a445fe7b6239d2a010372c17d0cc4cf9af0a.png)  
this is what it prints if I add your code and when I write

> `frame->chiSquare((frame->model_Norm[X],frame->h_dh, numberOfFitParamters)`

it says these items are not in the RooPlot.  
I am confused What exactly I should add to my code in order to get the chiSq value of **model** (not sig1 sig 2 sig3) fitted to the histogram.  
I am sorry for the inconvenience 🙏  
Im new to all these.  
[roomodel.cpp](https://root-forum.cern.ch/uploads/short-url/1Ziu7vRH9A0mYC9kqCQOLdwnO6Q.cpp) (2.1 KB)

---

<div class="post-metadata">

**Author:** ![Eddy\_Offermann](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eddy_offermann/32/25079_2.png) [@Eddy\_Offermann](https://root-forum.cern.ch/u/Eddy_Offermann)\
**Post date:** [March 31, 2023, 8:15pm UTC](https://root-forum.cern.ch/t/getting-negative-chi-sq-value-but-the-fit-looks-good/54281/7 "2023-03-31T20:15:02Z")

</div>

Dear @Subhu18,

Let me start with a disclaimer: I am clueless about RooFit 😉 but …

Fitting two Gaussians at the same position but different widths is a very difficult task for any fitter; and it shows in your correlation matrix with off-diagonal elements close to 1 and even one negative diagonal element.

The chi-square of exactly -1 means probably that the underlying optimization algorithm bailed out because some matrix was not positive definite.

How would one distinguish between 2 Gaussians (allowing a fatter tail) and a Hyper-Gaussian shape ([see for description](https://root-forum.cern.ch/t/tspectrum-and-something-else/48946/7) )

-Eddy

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [April 14, 2023, 8:15pm UTC](https://root-forum.cern.ch/t/getting-negative-chi-sq-value-but-the-fit-looks-good/54281/8 "2023-04-14T20:15:30Z")

</div>

Closed on April 14, 2023, 8:15pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
