【问题标题】:solve an n-dimensional optimisation problem using iminuit使用 iminuit 解决 n 维优化问题
【发布时间】:2013-07-19 14:27:38
【问题描述】:

我想用 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

【问题讨论】:

  • 您想返回一个变量的函数(使用mc 参数化),我说得对吗?
  • 不,也许这个例子本身并不是很容易解释。 f=f(x1,...,xn) 其中 x1,...,xn 是一个 numpy 数组。 x1,...,xn 是我的未知数,我有一些初始值。所以我想使用 minuit,因为你可以使用 scipy.optimize 或 ipopt 或 nlopt。
  • “数组减1”是什么意思?平方是什么意思?
  • 我是iminuit作者。问题是 n 是固定的吗?如果不是那么,这不是一个真正的参数问题,iminuit 无法帮助您。但如果它是固定的。你可以看看硬核教程。特别是在通用的可重用成本函数中。您可以展平将所有展平参数放在一个数组中,然后在 __call__(self, *arg) 中再次调用您的函数。 nbviewer.ipython.org/urls/raw.github.com/iminuit/iminuit/master/…
  • 如果你需要,我也可以写一个简单的例子(假设 n 是固定的)。

标签: python numpy iminuit pyminuit


【解决方案1】:

对于您的示例,我建议您使用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}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2021-09-25
    • 2021-09-09
    相关资源
    最近更新 更多