python - 如何为任意函数定义 chi2 值函数?

标签 python scipy

我正在使用 pyminuit Python 绑定(bind)为 minuit 最小化代码 (http://code.google.com/p/pyminuit/) 进行一些数据拟合。最小化器接受一个函数并使用内省(introspection)来提取要最小化的参数。通常,我想在给定特定函数来描述数据集的情况下最小化数据集的卡方值。

我的问题:有没有办法定义一个卡方函数,给定一个具有不同数量参数的任意函数,返回一个函数,该函数给出该函数的卡方值仅包含函数参数规范中要最小化的参数?

例子:

from scipy import *
import minuit
# Generate some data to fit
data_x = arange(50)
noise = 0.3
data_y = data_x**3 + normal(0.0, noise)
# Fit function, e.g. a cubic
fit_func = lambda x, a1, a2, a3, a4: a1 + a2*x + a3*x**2 + a4*x**3

# Minimisation function e.g. chi squared
# Note this has only the parameters to be minimised in the definition (eg not data_x)
min_func = lambda a1, a2, a3, a4: sum( (fit_func(data_x, a1, a2, a3, a4) - data_y)**2 / noise**2 )

这是我想写类似 min_func = make_chi2(fit_func) 的地方。我不知道该怎么做,因为 data_xdata_y 仅在函数外部定义。为了完整起见,最小化例程的其余部分如下所示:

# Initialise minimiser object with initial values
m = minuit.Minuit(min_func, {'a1': 1.0, 'a2': 1.0, 'a3': 1.0, 'a4': 1.0})
# Run minimiser
m.migrad()
# Print minimised values - example output
print m.values
>>> {'a1': 0.000, 'a2': 0.000, 'a3': 0.000, 'a4': 1.000}

提前感谢您的帮助!

最佳答案

既然 PyMinuit 使用内省(introspection),你也必须使用内省(introspection)。 make_chi_squared() 可以这样实现:

import inspect

chi_squared_template = """
def chi_squared(%(params)s):
    return (((f(data_x, %(params)s) - data_y) / errors) ** 2).sum()
"""

def make_chi_squared(f, data_x, data_y, errors):
    params = ", ".join(inspect.getargspec(f).args[1:])
    exec chi_squared_template % {"params": params}
    return chi_squared

示例用法:

import numpy

def f(x, a1, a2, a3, a4):
    return a1 + a2*x + a3*x**2 + a4*x**3

data_x = numpy.arange(50)
errors = numpy.random.randn(50) * 0.3
data_y = data_x**3 + errors

chi_squared = make_chi_squared(f, data_x, data_y, errors)
print inspect.getargspec(chi_squared).args

打印

['a1', 'a2', 'a3', 'a4']

关于python - 如何为任意函数定义 chi2 值函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7927670/

相关文章:

python - 网站抓取和截图

python - 使用部分关键字搜索 python dict 的最快方法

python - 评估 K 意味着使用 python 进行聚类

python - OSX 上的 Scipy、Numpy、Matplotlib 问题

python - 使用 'fixed points' 在 Python 中插入数据

python - 应用随机森林后提取重要特征进行训练和测试

python - Python 3.3.2 中的重载函数用法

python - 解释一下Django中的代码片段

python - 如何在 python 中部分加载用 numpy save 保存的数组

python - 使用 Scipy 的 stats.kstest 模块进行拟合优度测试