# How to fit GraphErrors with uncertainty only along X

Hi,

Please do me a favor to this problem.

I would like to fit a graph with uncertainty only along x, I do it in the following way:

TGraphErrors *gerr = new TGraphErrors(n, x,y,ex,ey);

The fit will fail if ey is initialized as an array full of 0, but it will work if I set it as a small number, like 1e-7.

Is this the right way to do this fit?

The resulted errors on parameters are smaller than the fit result to TGraph(n,x,y), which I don’t understand why.

Best Reagrs,

`` Hua``

Can you provide a running script reproducing the problem ?
Thanks

Hi, couet,

following is my code, in the TGraphErrors, I only have errors along x, but not along y.

``````
void test3(){
Double_t x, y,xe,ye;
for (int i=0; i<100; i++){
x[i] = gRandom->Uniform(-1, 1);
y[i] = -1 + 2*x[i] + gRandom->Gaus(0, 0.2);
xe[i]=gRandom->Gaus(0, 0.1);   //errors along x
ye[i]=0;    //errors along y.
}

TF1 *fpol = new TF1("fpol", "+*x", -1, 1);

TCanvas *can = new TCanvas("can","can");
TGraphErrors *gre = new TGraphErrors(100, x,y,xe,ye);
gre->Draw("ap");
gre->Fit("fpol","F");

}
``````

The out put is

``````Warning in <Fit>: Abnormal termination of minimization.
FCN=0 FROM MIGRAD    STATUS=FAILED        100 CALLS         101 TOTAL
EDM=346193    STRATEGY= 1  ERROR MATRIX UNCERTAINTY 100.0 per cent
EXT PARAMETER                APPROXIMATE        STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  p0           0.00000e+00   1.41421e+00  -0.00000e+00   0.00000e+00
2  p1           0.00000e+00   6.69298e-31   0.00000e+00  -8.79103e+32``````

If I initialize ye[i]=1e-7, the fit seems working, I’m not sure.

`````` FCN=2.31192e+14 FROM MIGRAD    STATUS=CONVERGED      47 CALLS          48 TOTAL
EDM=2.58566e-10    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                   STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  p0          -1.03559e+00   1.39271e-08   1.02957e-04  -1.51763e+03
2  p1           1.99617e+00   2.33300e-08   1.72468e-04   4.52983e+02``````

I guess you meet something like this:
[root.cern.ch/phpBB3/viewtopic.p … ors#p65423](Reading Histogram, Histogram drawing Empty

Try with (though, I do not know if your x-errros are properly considered in this case):
gre->Fit(“fpol”,“W”);

The error in “X” are automatically wondered in TGraphErrors::Fit, if they are available. If you want to exclude them use the fit option “EX0”

Lorenzo

The “W” fit option means: “sets all weights to 1; ignore error bars”.
So, it’s not really clear to me if x-errors are PROPERLY taken into account in this case.

[quote=“moneta”]The error in “X” are automatically wondered in TGraphErrors::Fit, if they are available. If you want to exclude them use the fit option “EX0”

Lorenzo[/quote]

Hi, Lorenzo,

I have provided error in X, but no error in Y, in this case, I don’t know how can I do the fit.

Hua.

Hi,

I think it should work if you have errors only in X. Did you try ?

Lorenzo

[quote=“Wile E. Coyote”]I guess you meet something like this:
[root.cern.ch/phpBB3/viewtopic.p … ors#p65423](Reading Histogram, Histogram drawing Empty

Try with (though, I do not know if your x-errros are properly considered in this case):
gre->Fit(“fpol”,“W”);[/quote]

Hi, Wile E. Coyote

I don’t know how ROOT deal with the error when fitting TGraph, it seems if I use the X error I provided, and the Y error which is the same to the TGraph fit, my problem should be solved.

I tried to set ye[i]=-1 in my code, it does not work. (if I set both xe and ye to -1, TGraphErrors fit is the same as TGraph fit) .

Hua

I would say that, for the moment, the safest approach is to set all “y-errors” to a very small positive value (e.g. +1e-7) and do not use the “W” fit option -> then also your “x-errors” should PROPERLY be taken into account.

P.S. Lorenzo, please note that (s)he DID try it -> this is exactly the problem that (s)he faces.

[quote=“Wile E. Coyote”]I would say that, for the moment, the safest approach is to set all “y-errors” to a very small positive value (e.g. +1e-7) and do not use the “W” fit option -> then also your “x-errors” should PROPERLY be taken into account.

P.S. Lorenzo, please note that (s)he DID try it -> this is exactly the problem that (s)he faces.[/quote]

Hi, Wile E. Coyote,

As I showed above, I set the “y-error” to 1e-7, the fit seems working, but the fitted errors of parameters are so small ~1e-8, if I use TGraph for fitting, errors of paras would be ~1e-2. This makes me uncomfortable since my first intuition is the TGraphErrors would give bigger error.

The problem is like this. If I set both x-error and y-error to 1e-7, in principle, fit to TGraphError should give the same result as TGraph, but not (shown below). I know there would be some corrections in TGraph fit, but I don’t know much detail. All these things make me feel unsatisfied to just use the y-error of 1e-7.

Hua

[code]****************************************

void test3(){
Double_t x, y,xe,ye;
for (int i=0; i<100; i++){
x[i] = gRandom->Uniform(-1, 1);
y[i] = -1 + 2*x[i] + gRandom->Gaus(0, 0.2);
xe[i]=1e-7;//gRandom->Gaus(0, 0.2);
ye[i]=1e-7;
}

TF1 *fpol = new TF1(“fpol”, “+*x”, -1, 1);

TGraph *gr = new TGraph(100, x,y);
gr->Fit(“fpol”,“F”);

TGraphErrors *gre = new TGraphErrors(100, x,y,xe,ye);
gre->Fit(“fpol”,“F”);
}

// Fit result of TGraph

Chi2 = 4.25313
NDf = 98
Edm = 2.34623e-09
NCalls = 29
p0 = -1.01514 +/- 0.0208327
p1 = 1.96667 +/- 0.0350903

// Fit result of TGraphErrors

Warning in : Abnormal termination of minimization.
FCN=8.56765e+13 FROM MIGRAD STATUS=FAILED 73 CALLS 74 TOTAL
EDM=11070.6 STRATEGY= 1 ERR MATRIX APPROXIMATE
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 -1.01526e+00 2.25011e-08 1.02067e-04 -1.86870e+09
2 p1 2.01567e+00 3.75321e-08 6.92710e-05 -3.80736e+09
[/code]

Note that the first fit is “successful” and gives chi^2 = 4.3, while the second fit returns a “failure” and gives chi^2 = FCN = 8.6e13 (and so the returned errors of parameters are meaningless, I think).
Well, I don’t really know how to interpret errors of parameters if the returned chi^2 (=FCN) is much bigger than 1, even if the fit “converges”.