# Feedback on χ²/ndof Calculation Discrepancy

**URL:** <https://root-forum.cern.ch/t/feedback-on-ndof-calculation-discrepancy/64348>\
**Category:** Roofit and RooStats\
**Tags:** root\
**Created:** [October 22, 2025, 11:38am UTC](https://root-forum.cern.ch/t/feedback-on-ndof-calculation-discrepancy/64348 "2025-10-22T11:38:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![abaoud](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f05b48/32.png) [@abaoud](https://root-forum.cern.ch/u/abaoud)\
**Post date:** [October 22, 2025, 11:38am UTC](https://root-forum.cern.ch/t/feedback-on-ndof-calculation-discrepancy/64348/1 "2025-10-22T11:38:59Z")

</div>

Dear experts,

I’m reaching out to seek feedback regarding an issue with the χ²/ndof calculation. I performed a fit to my data, and the fit looks good both visually and in the model/data ratio plot. However, the χ²/ndof value is always quite large. I verified that the number of degrees of freedom (ndof) is correct and stable, so I started investigating the χ² itself. Initially, I computed it using: `frame.chiSquare(“model”, “data”, npar)` Then I wrote a custom function [1] to compute the χ² manually, and this time I obtained a more reasonable value. However, I still don’t fully understand why the two results differ.  
For reference, I’m using the `chi2FitTo` method for the fitting.  
Please let me know if you need any additional details.  
[1]

```auto
`
def calculate_chi2_manual(data_hist, model, var, n_params):`

    chi2 = 0.0

    n_bins = data_hist.numEntries()

    x_min = var.getMin()

    x_max = var.getMax()

    total_range = x_max - x_min

`for i in range(n_bins):`

        data_val = data_hist.weight(i)

        data_point = data_hist.get(i)

        x_val = data_point.getRealValue("t_Time")

        var.setVal(x_val)

        pdf_val = model.getVal(ROOT.RooArgSet(var))

        bin_width = total_range / n_bins

        expected_val = pdf_val \* bin_width \* data_hist.sumEntries()

        error = data_val\*\*0.5 if data_val > 0 else 1.0

`if error > 0:`

            chi2 += ((data_val - expected_val) / error)\*\*2

    ndf = n_bins - n_params

    chi2_per_ndf = chi2 / ndf if ndf > 0 else float('inf')

`return chi2, ndf, chi2_per_ndf`

```

Model:

```auto
modeldata = ROOT.RooAddPdf("combined_model", "Prompt + NonPrompt in Data",
ROOT.RooArgList(modelp, t_SigNonPromptModel),ROOT.RooArgList(yieldP,yieldNP))

```

```auto
result_data = modeldata.chi2FitTo(dataHist,ROOT.RooFit.Save(True),
ROOT.RooFit.Extended(True),ROOT.RooFit.Strategy(2))

```

---

<div class="post-metadata">

**Author:** ![mczurylo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mczurylo/32/33269_2.png) [@mczurylo](https://root-forum.cern.ch/u/mczurylo)\
**Post date:** [October 22, 2025, 1:05pm UTC](https://root-forum.cern.ch/t/feedback-on-ndof-calculation-discrepancy/64348/2 "2025-10-22T13:05:35Z")

</div>

Hi @abaoud,

thank you for your question, I will tag our RooFit expert @jonas to give you some more details here.

Cheers,

Marta

---

<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:** [November 5, 2025, 1:06pm UTC](https://root-forum.cern.ch/t/feedback-on-ndof-calculation-discrepancy/64348/3 "2025-11-05T13:06:32Z")

</div>

Closed on November 5, 2025, 1:06pm UTC

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