# Chi Squared calculation

Hi,

I’m a bit ashamed to ask, but I really don’t understand how ROOT determines its Chi Square.

I did a VERY simple test and I have trouble understanding the results.

Let’s say I have 3 points: A(X=0, Y=0); B(X=1, Y=1) et C(X=2, Y=2).
There are perfectly aligned because they lie on the Y=X line and then the Chi Square = 0 (like it should be)

Now, Imagine I move slightly C to (X=2, Y=3).

1. When I compute the Chi Square, with ROOT, I now get 4.16667

2. When I move C to (X=2, Y=4), I get 16.6667

3. When I move C to (X=2, Y=4), I get 37.5

I’m very bad with statistics, but I thought I should get 2 for the first case, 8 in the second case and 18 for the third one.

What am I doing wrong ??

Thank you and sorry again for the question

I guess a little macro showing which ROOT commands you are using might be useful.

Sorry.
Here the piece of code I’m using:

``````    TH2F *h_dummy = new TH2F("dummy", "dummy", 500, -50, 50, 500, -50, 50);
h_dummy->Fill(double(0),double(0));
h_dummy->Fill(double(1),double(1));
h_dummy->Fill(double(2),double(5));  // <--- this where I change the coordinates of point C

h_dummy->Fit("pol1","QM");
Dummy_ChiS = h_dummy->GetFunction("pol1")->GetChisquare();

cout << Dummy_ChiS << endl;``````

Hi,

The chi-squared for the histogram is weighted by the error in each bin, which when is not set, is assumed to be the square root of the bin content.
You are also doing something bizzarre, because you are creating a 2D histogram and fit with 1D function. In this case the error (weight in the chi-square) is more complicated, it is given by the bin width/sqrt(N) where N is the bin content in the bin.

If you want to reproduce what you expect you should use a TGraph for storing your points.

Best Regards

Lorenzo

That helped a lot !

Thank you very much.

I have a another question: why can’t I fit a vertical distribution ?
If I align 10 points (using a TGraph) on a vertical line (let’s say x=5), why can’t ROOT fit it ?
Is there a way around that ?

Currently, I rotate my distribution 90 degrees, fit and rotate back, but I was wondering if there’s a more elegant way to achieve the same result.

Hi,

The way we (and most of high energy physics) define “fitting” means that we find function parameters that minimize the “difference” (that can be of any form) between said function and a set of points. That assumes that we can evaluate the function (i.e. getting y = f(x)) and that y is meaningful for the “difference”.

In your example (with a vertical line), evaluating that function doesn’t really give anything helpful for tweaking f. The f you’re after is not a normal function.

For what you want to do, your approach makes sense.

Cheers, Axel.

Hi Axel,