Question

I woul like to solve an n-dimensional optimisation problem using iminuit.

So my approach is the following. I am trying to figure out how to extend this:

def f(x,y,z):
    return (x-1.)**2 + (y-2*x)**2 + (z-3.*x)**2 -1.

to a variable "x" that is a numpy.array.

I would like to do something like this:

x = [1,2,3,4,5]
y = [2,4,6,8,10]# y=2x
class StraightLineChi2:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def __call__(self,m,c): #lets try to find slope and intercept
        chi2 = sum((y - m*x+c)**2 for x,y in zip(self.x,self.y))
        return chi2

but in my case x is my unknown, and it is an array. Like in many optimization/minimization problems, the function is a f=f(x1,...,xn) where n can be big. x1,...,xn are the unknowns of the problem.

(These examples are taken from here)

Something similar is achieved "hacking" pyminuit2, like described here

Was it helpful?

Solution

For your example I recommend you using iminuit and probfit. Having an argument as a list of parameter is not exactly what you want to do since you will get confused which parameter is what very soon.

Here is an example taken straight from probfit tutorial. Also see the documentation


import iminuit
import probfit
x = np.linspace(0, 10, 20) 
y = 3 * x + 15 + np.random.randn(len(x))
err = np.ones(len(x))
def line(x, m, c): # define it to be parabolic or whatever you like
    return m * x + c
chi2 = probfit.Chi2Regression(line, x, y, err)
minuit = iminuit.Minuit(chi2)
minuit.migrad();
print(minuit.values) #{'c': 16.137947520534624, 'm': 2.8862774144823855}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top