# Fit with constraints in TMinuit

**URL:** <https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824>\
**Category:** ROOT\
**Tags:** root\
**Created:** [June 28, 2025, 8:16am UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824 "2025-06-28T08:16:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![OrangeCat](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/orangecat/32/30721_2.png) [@OrangeCat](https://root-forum.cern.ch/u/OrangeCat)\
**Post date:** [June 28, 2025, 8:16am UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/1 "2025-06-28T08:16:27Z")

</div>

Hi rooters,

I am currently performing an unbinned maximum likelihood fit to extract two parameters (a1, a2) from N events. The likelihood function is constructed as:

-lnL=Sum\_{i=0}^{N} ln W(\xi\_i; a1,a2)

I then attempted to incorporate a constraint between a1 and a2 using the Lagrange multiplier method, modifying the likelihood function to:

-lnL=Sum\_{i=0}^{N} ln W(\xi\_i; a1,a2) + lambda Func(a1, a2)

However, when minimizing -lnL using TMinuit, the fit consistently hits the boundary regardless of how I adjust the range of lambda, resulting in failed convergence. I would like to ask:

1. Whether my construction of the likelihood function using the Lagrange multiplier method is correct
2. Whether TMinuit is an appropriate tool for this constrained minimization approach

thanks for your attention,

Yupeng

---

<div class="post-metadata">

**Author:** ![lbreitwi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lbreitwi/32/21330_2.png) [@lbreitwi](https://root-forum.cern.ch/u/lbreitwi)\
**Post date:** [June 30, 2025, 4:34pm UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/2 "2025-06-30T16:34:45Z")

</div>

Hi @OrangeCat,  
Welcome to the ROOT forum.  
Our fitting expert @jonas may be able to help here.

---

<div class="post-metadata">

**Author:** ![atd](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/atd/32/33835_2.png) [@atd](https://root-forum.cern.ch/u/atd)\
**Post date:** [July 3, 2025, 7:36pm UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/3 "2025-07-03T19:36:54Z")

</div>

Hi @OrangeCat

maybe a penalty method is better suited? something like

```bash
-lnL=Sum_{i=0}^{N} ln W(\xi_i; a1,a2) + lambda * pow( Func(a1, a2) , 2 ) 

```

This example shows how to add a Gaussian constraint function

> [@ROOT::FIT, parameter error propagation](https://root-forum.cern.ch/t/root-fit-parameter-error-propagation/23778):
>
> Dear rooters, I’m still new to the ROOT::FIT classes. In some of the fits, several parameters are kept fix; but they have an associated error. In order to propagate these errors (associated with the fix parameters) to the “free” parameters, I have to perform several fits changing the values of the fixed parameters (a Montecarlo way of propagating errors). So, my question is: is there a way using ROOT::FIT to propagate the errors of the fixed parameters? I spent a while looking for it, but I …

Best,  
Alvaro

---

<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:** [July 7, 2025, 7:24pm UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/4 "2025-07-07T19:24:04Z")

</div>

Hi Yupeng,

Your notation is confusing me. Did you have a look at this [discussion](https://root-forum.cern.ch/t/minimization-subject-to-constrains-with-tminuit-is-it-possible-pyroot/48027). best regards, Eddy

---

<div class="post-metadata">

**Author:** ![OrangeCat](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/orangecat/32/30721_2.png) [@OrangeCat](https://root-forum.cern.ch/u/OrangeCat)\
**Post date:** [July 8, 2025, 2:34am UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/5 "2025-07-08T02:34:16Z")

</div>

Hi atd,

Sorry for the late reply as I was away attending a conference.  
I will promptly try the Gaussian constraint approach as you suggested.

Regarding error propagation for fixed parameters, I also implemented it by performing multiple fits while varying the values of the fixed parameters.  
Would it be possible for you to share the relevant code if convenient?

Thank you for your time.

best,  
Yupeng

---

<div class="post-metadata">

**Author:** ![atd](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/atd/32/33835_2.png) [@atd](https://root-forum.cern.ch/u/atd)\
**Post date:** [July 8, 2025, 8:38am UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/6 "2025-07-08T08:38:59Z")

</div>

Hi @OrangeCat

you may find a minimal working code here:

> [@ROOT::FIT, parameter error propagation](https://root-forum.cern.ch/t/root-fit-parameter-error-propagation/23778/4):
>
> Hi, Here is an example where the constraint is added for the parameter 1, slope of the exponential Lorenzo[propagateSystematicErrors.C](https://root-forum.cern.ch/uploads/default/original/2X/4/4855b3953a503109d86f5d9d847577319f01f8ce.C) (2.4 KB)

The key is in line 67,

```auto
auto nll = [&](const double *x) { 
        return likelihood_F(x) - std::log( ROOT::Math::normal_pdf(x[1],sigmaPar, meanPar) ); };

```

where `likelihood_F(x) ` is the negative log likelihood estimator, and `std::log( ... )` is the constraint.

I am not a great expert, but I suspect that the result might be biased when adding constraints, so please try at least with some synthetic data (where you know the parameters) similar to your experimental data, to crosscheck that your method is still robust and non-biased (check the pulls)

Best,  
Alvaro

---

<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:** [July 8, 2025, 10:40am UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/7 "2025-07-08T10:40:52Z")

</div>

Hello,

I would also go for a Gaussian constraint, thanks @atd!  
And just to not discourage you from using the constraint, I would not call the fit “biased”, because if you know (due to external physics knowledge or similar) that there needs to be a relation between the parameters, you are actually giving the minimiser more information, which it couldn’t know otherwise.

---

<div class="post-metadata">

**Author:** ![atd](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/atd/32/33835_2.png) [@atd](https://root-forum.cern.ch/u/atd)\
**Post date:** [July 8, 2025, 11:48am UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/8 "2025-07-08T11:48:07Z")

</div>

Hi @StephanH

Thanks for crosschecking!

I wanted to ask a follow-up from a more mathematical/statistical perspective:

Suppose I generate many synthetic datasets using a known model and known true parameter values, and then fit each dataset by minimizing a likelihood function plus Gaussian constraints. Is it mathematically guaranteed that this estimator will have these properties?

- Unbiased, i.e. the average of the fitted parameters over many pseudo-experiments converges to the true values?
- Consistent, i.e. the variance of the estimator decreases as the number of events increases?

Or, from other perspective: under what conditions can we say that adding such Gaussian constraints (assuming they’re centered on the correct values) still yields asymptotically unbiased and consistent estimates?

Thanks in advance for your thoughts, I’m trying to get a deeper understanding of when these techniques are robust.

Best,  
Alvaro

---

<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:** [July 8, 2025, 2:26pm UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/9 "2025-07-08T14:26:48Z")

</div>

Hi Alvaro,

we would better refer this question to somebody with deeper knowledge in statistics like @moneta.

That being said, if you add a Gaussian constraint on a parameter, it depends to what value you constrain it. You will clearly bias a fit if you generate a dataset with some parameter `a=-10`, but then you constrain it to `a=0±1`. Clearly, an unbiased fit model without the constraint should converge to `-10`, whereas a fit with constraint will be pulled towards `0`.

But in most cases, that’s not what we are doing. We usually generate toy data with some value of `a`, but then we constrain `a` to the same value when fitting. And moreover, we add constraints mostly on parameters that the fit model isn’t very sensitive to. We want to avoid them to assume values that seem somehow random, and therefore we add information from external measurements using those constraint terms. If our fit is already very sensitive to the parameter, there is no real need to constrain it.

In HEP we therefore have the “parameter of interest” and the “nuisance parameters”: We measure the parameter of interest, and we constrain nuisance parameters (the ones we don’t want to measure directly) to values that we found in external measurements.

To come back to your questions:  
If you add a constraint at the correct central value and with the correct variance, you are actually helping the fit model to be unbiased and consistent. When you generate toy datasets, you should actually generate them with the constraint terms active, so you get a “realistic” distribution.

This will of course break down if one of your constrained parameters is not Gaussian in nature. In that case, you can add other constraints, such as Poisson or LogNormal, or (the most accurate way but not always feasible) you simply multiply the likelihood function of your main measurement with the likelihood function of your external or auxiliary measurement, which will allow you to perform a combined measurement of the two, or of many parameters simultaneously.  
Since this is either cumbersome or slow, or both, we often “approximate” the auxiliary likelihood functions with a Gaussian around whatever value the auxiliary measurements produced. So in that sense, the preferred order in terms of accuracy and predictive power is:

1. The “super likelihood” with all measurements, main and the auxiliary ones, multiplying all likelihood functions of all measurements.
2. The second best is to create the main likelihood model and add constraint terms for the auxiliary measurements, getting very close to the real thing if the errors of your auxiliary measurements are Gaussian.
3. And the worst is to have the main likelihood without any constraint terms, completely missing the external information.

---

<div class="post-metadata">

**Author:** ![OrangeCat](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/orangecat/32/30721_2.png) [@OrangeCat](https://root-forum.cern.ch/u/OrangeCat)\
**Post date:** [July 14, 2025, 7:10am UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/10 "2025-07-14T07:10:41Z")

</div>

Hi @atd and all,

Sorry for the late reply. I tried using Gaussian-shaped constraints in the likelihood equation, but ultimately failed. Similarly, during minimization, lambda reached the boundary. My fitting procedure is as follows: [AngularFit.C](https://root-forum.cern.ch/uploads/short-url/zvItgcVr3emGij7z2p5h1lDaVgm.C) (7.7 KB)

The key is in line 205,

```auto
double ConsFunc = (1-alpha_Xim*alpha_Xim)*cos(LYphi_Xim)*cos(LYphi_Xim) - (1-alpha_Xip*alpha_Xip)*cos(LYphi_Xip)*cos(LYphi_Xip);
LogLike -= lambda*pow( ConsFunc, 2 );

```

I’m not an expert in fitting, but I have a conjecture about this issue: Since the lambda multiplier is linear in the likelihood function, if the constraint is not strictly zero during fitting, then minimizing the likelihood will exhibit monotonic behavior with respect to lambda. As a result, the likelihood can only reach its minimum when lambda hits the boundary.

I’m not entirely sure whether this understanding is correct, so I also performed a scan of the likelihood function over the lambda multiplier. Indeed, the results show that the likelihood only reaches its minimum when lambda is at the boundary—its extremum at lambda=0 is not the minimum point.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/c/d/cd2238792906d1f71af02cde716458fbe236d2fb.png)

Best,  
Yupeng

---

<div class="post-metadata">

**Author:** ![OrangeCat](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/orangecat/32/30721_2.png) [@OrangeCat](https://root-forum.cern.ch/u/OrangeCat)\
**Post date:** [July 14, 2025, 7:56am UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/11 "2025-07-14T07:56:35Z")

</div>

Hi rooters,

I attempted an alternative approach using the ​ **Augmented Lagrangian Method**. The likelihood function is constructed as follows:

S=S\_{Nom} + lambda \* g(Omega) + rho \* g(Omega)^2

where S\_{Nom} is the likelihood function without constraint, g is constraint function, Omega is the fitted parameters, lambda is the Lagrange multiplier and rho is the penalty coefficient. In this fitting procedure, ​lambda (λ)​​ and ​rho (ρ)​​ are ​not treated as free parameters​ in the TMinuit minimization, but rather as ​external control parameters . We need to perform a series of iterations, where in each iteration:

1. Fix lambda\_{k} and rho\_{k}, solve the unconstrained problem by TMinuit:  
Parameter Omega\_{k} = min S\_{Nom}
2. Update the multiplier lambda\_{k}:  
lambda\_{k+1} = lambda\_{k} + rho\_{k} \* g(Omega\_{k+1})
3. Update the penalty coefficient rho\_{k}:  
rho\_{k+1} = 2 \* rho\_{k}
4. Termination criterion for iterations: |g(Omega)| \< epsilon, where epsilon is the predefined tolerance, set to 1e-6.

Through this approach, the expected results were successfully obtained after 16 iterations. I would like to inquire whether this method can be widely applied to constrained optimization problems in general.​

Best,  
Yupeng

Reference:  
[1] Hestenes, M.R. (1969) Multiplier and Gradient Methods. Journal of Optimization  
Theory and Applications, 4, 303-320.  
[2] Powell, M.J.D. (1969) A Method for Nonlinear Constraints in Minimization Prob  
lems. In: Fletcher, R., Ed., Optimization, Academic Press, New York, NY, 283-298.  
[3] Bertsekas, D.P. (1996) Constrained Optimization and Lagrange Multiplier Methods.  
Athena Scientific (First Published 1982).

---

<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:** [July 28, 2025, 7:56am UTC](https://root-forum.cern.ch/t/fit-with-constraints-in-tminuit/63824/12 "2025-07-28T07:56:36Z")

</div>

Closed on July 28, 2025, 7:56am UTC

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