# Constraints on fitting parameter in iminuit?

Hi, I’ve been trying to fit some parameters to a curve but I need to put a constraint on one of the constants and I don’t know how to make my code acknowledge the constraints and fit a value with the correct value. I’ll try to write a simple example code just to show my problem:

``````def chi(paras):
mpi=paras[0:32]
cf=paras
chif=0
for i in range(32):
chif+=((fpi-f(mpi,cf))/error)**2
return chif
m=Minuit.from_array_func(chi,parin,parstep,name=parname,errordef=1)

print(m.values)
``````

I want for example cf<=np.log(mpi**2). I’ve tried for example:

``````if cf<=np.log(mpi**2):
chif+=((fpi-f(mpi,cf))/error)**2

else:
pass
``````

but it hasn’t worked. Is there anyway to put this constraint in the code?

Hi,
Minuit is not a minimiser algorithms supporting directly non trivial constraints such as those you would like to have.
A possible solution is trying to apply a modification to tour objective functions (chi§ ) by adding a penalty term for your constraints, that increases the chi2 values when your parameters are not satisfying the constraint.

Lorenzo

Hi,

Thank you for your reply how would I put in the code if I for example would like 0<cf<100? Because if I put if 0<cf<100 then the code just takes the input value for cf and decides if it’s in the correct range I would like it to do the fit s.t. it keeps cf in the correct range.

HI,
Trivial constraints, like parameter bounds are possible in Minuit.
I am not an expert of iminuit, that is a different python API for Minuit. But looking at the doc it seems to be an option in the `Minuit.from_array_func`.