solve an n-dimensional optimisation probl using iminuit
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](http://nbviewer.ipython.org/urls/raw.github.com/iminuit/iminuit/master/tutorial/tutorial.ipynb))

Something similar is achieved "hacking" pyminuit2, like described
[here](http://code.google.com/p/pyminuit/issues/detail?id=6)

ps: I wish I could have added the tag [iminuit], but I have no enough
reputation to create a new tag... ps2: stack overflow is somewhat crazy... I
cannot use the word "problem" in the title!


Answer: For your example I recommend you using
[iminuit](https://github.com/iminuit/iminuit) and
[probfit](https://github.com/iminuit/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](http://nbviewer.ipython.org/urls/raw.github.com/iminuit/probfit/master/tutorial/tutorial.ipynb).
Also see [the
documentation](http://iminuit.github.io/probfit/api.html#probfit.costfunc.Chi2Regression)

    
    
    
    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}
    
    

