Constraints on fitting parameters in iminuit?

528 Views Asked by At

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?

1

There are 1 best solutions below

1
On

To this type of constrains there are always simple parameter transform solution. In this case you may define:

def chi( paras ):
    mpi = paras[ 0 : 32 ]
    s = paras[ 32 ]
    a = np.log( np.sum( np.array( mpi )**2 ) )
    cf = a - np.exp( -s )
    chif = 0
    for i in range( 32 ):
        chif += ( ( fpi - f( mpi, cf ) ) / error )**2
    return chif

The parameter s is allowed to vary from-np.inf to np.inf while the internal cf can vary from-np.inf to a = np.log( sum ( mpi**2 ) ). To get cf and its error you make standard error propagation.