使用iminuit解决n维优化问题

时间:2013-07-19 14:27:39

标签: python numpy iminuit pyminuit

我想用iminuit来解决n维优化问题。

所以我的方法如下。 我想弄清楚如何扩展这个:

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

到变量“x”,这是一个numpy.array。

我想做这样的事情:

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

但在我的情况下,x是我未知的,它是一个数组。与许多优化/最小化问题一样,函数是f = f(x1,...,xn),其中n可以很大。 x1,...,xn是问题的未知数。

(这些例子来自here

类似于“hacking”pyminuit2,就像描述here

一样

1 个答案:

答案 0 :(得分:4)

对于您的示例,我建议您使用iminuitprobfit。将参数作为参数列表并不完全是你想要做的事情,因为你很快就会混淆哪个参数。

以下是直接来自probfit tutorial的示例。另请参阅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}