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

编程入门 行业动态 更新时间:2024-10-27 16:36:29
本文介绍了如何为任意函数定义chi2值函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 pyminuit Python 绑定对 minuit 最小化代码 (code.google/p/pyminuit/) 进行一些数据拟合.最小化器接受一个函数并使用自省来提取要最小化的参数.一般来说,我希望在给定特定函数来描述数据集的情况下最小化数据集的卡方值.

I am doing some data fitting using the pyminuit Python bindings for the minuit minimisation code (code.google/p/pyminuit/). The minimiser accepts a function and uses introspection to extract the parameters to be minimised. In general, I want to minimise the chi squared value for a dataset given a particular function to describe the dataset.

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

My question: Is there a way to define a chi squared function which, given an arbitrary function with varying numbers of parameters, returns a function which gives the chi squared value for that function and only contains the parameters to be minimised in the function argument specification?

示例:

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_x 和 data_y 仅在函数外部定义.为完整起见,最小化例程的其余部分如下所示:

THIS is where I'd like to write something like min_func = make_chi2(fit_func). I don't know what to do as data_x and data_y are only defined outside of the function. The rest of the minimisation routine, for completeness, looks like:

# 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 使用自省,你也必须使用自省.make_chi_squared() 可以这样实现:

Since PyMinuit uses introspection, you have to use introspection, too. make_chi_squared() could be implemented like this:

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']

更多推荐

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

本文发布于:2023-07-26 09:26:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   何为   定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!