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[32]
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)
fmin,param=m.migrad(ncall=10000)
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?
To this type of constrains there are always simple parameter transform solution. In this case you may define:
The parameter
s
is allowed to vary from-np.inf
tonp.inf
while the internalcf
can vary from-np.inf
toa = np.log( sum ( mpi**2 ) )
. To getcf
and its error you make standard error propagation.