优化的拟合系数,以实现更好的拟合 [英] Optimized fitting coefficients for better fitting

查看:77
本文介绍了优化的拟合系数,以实现更好的拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用minpack.lm包运行非线性最小二乘法.

但是,对于数据中的每个组,我想优化(最小化)拟合参数,类似于Python的

ps.我把数字和拟合线补齐了.

解决方案

不确定r,但是具有共享参数的最小二乘法通常很容易实现.

一个简单的python示例如下:

  import matplotlibmatplotlib.use('Qt4Agg')从matplotlib导入pyplot作为plt从随机导入随机从scipy导入优化将numpy导入为np#只适合我的正态分布错误def boxmuller(x0,sigma):u1 = random()u2 = random()ll = np.sqrt(-2 * np.log(u1))z0 = ll * np.cos(2 * np.pi * u2)z1 = ll * np.cos(2 * np.pi * u2)返回sigma * z0 + x0,sigma * z1 + x0#一些非线性函数定义f0(x,a,b,c,s = 0.05):返回a * np.sqrt(x ** 2 + b ** 2)-np.log(c ** 2 + x)+ boxmuller(0,s)[0]#最小二乘的残差函数需要两个数据集.#长度不一定相同#三个参数中的两个是通用的def残差(parameters,l1,l2,dataPoints):a,b,c1,c2 =参数set1 = dataPoints [:l1]set2 = dataPoints [-l2:]distance1 = [(set1中x,y的((a * np.sqrt(x ** 2 + b ** 2)-np.log(c1 ** 2 + x))-y]]distance2 = [(set2中x,y的((a * np.sqrt(x ** 2 + b ** 2)-np.log(c2 ** 2 + x))-y]]res =距离1 +距离2返回资源xList0 = np.linspace(0,8,50)#一些xy数据xList1 = np.linspace(0,7,25)data1 = np.array([xList1中x的f0(x,1.2,2.3,.33)]#more xy数据使用不同的第三个参数xList2 = np.linspace(0.1,7.5,28)data2 = np.array([xList2中x的f0(x,1.2,2.3,.77)]alldata = np.array(zip(xList1,data1)+ zip(xList2,data2))#粗略估算估计= [1、1、1,.1]#配件;提供第二个长度实际上是多余的bestFitValues,ier = optimize.leastsq(残差,估计值,args =(len(data1),len(data2),alldata))打印bestFitValues无花果= plt.figure()斧= fig.add_subplot(111)ax.scatter(xList1,data1)ax.scatter(xList2,data2)ax.plot(xList0,[f0(x,bestFitValues [0],bestFitValues [1],bestFitValues [2],s = 0)对于xList0中的x))ax.plot(xList0,[f0(x,bestFitValues [0],bestFitValues [1],bestFitValues [3],s = 0)对于xList0中的x))plt.show()#输出>>[1.19841984 2.31591587 0.34936418 0.7998094] 

如果需要,您甚至可以自己进行最小化.如果您的参数空间表现良好,即近似于抛物线形最小值,则简单的 Nelder米德方法很好.

I'm running a nonlinear least squares using the minpack.lm package.

However, for each group in the data I would like optimize (minimize) fitting parameters like similar to Python's minimize function.

The minimize() function is a wrapper around Minimizer for running an optimization problem. It takes an objective function (the function that calculates the array to be minimized), a Parameters object, and several optional arguments.

The reason why I need this is that I want to optimize fitting function based on the obtained fitting parameters to find global fitting parameters that can fit both of the groups in the data.

Here is my current approach for fitting in groups,

df <- data.frame(y=c(replicate(2,c(rnorm(10,0.18,0.01), rnorm(10,0.17,0.01))), 
                                c(replicate(2,c(rnorm(10,0.27,0.01), rnorm(10,0.26,0.01))))),
                         DVD=c(replicate(4,c(rnorm(10,60,2),rnorm(10,80,2)))),
                         gr = rep(seq(1,2),each=40),logic=rep(c(1,0),each=40))

the fitting equation of these groups is

fitt <- function(data) {
  fit <- nlsLM(y~pi*label2*(DVD/2+U1)^2,
               data=data,start=c(label2=1,U1=4),trace=T,control = nls.lm.control(maxiter=130))
}

library(minpack.lm)
library(plyr)  # will help to fit in groups

fit <- dlply(df, c('gr'), .fun = fitt)  #,"Die" only grouped by Waferr

> fit
$`1`
Nonlinear regression model
  model: y ~ pi * label2 * (DVD/2 + U1)^2
   data: data
   label2        U1 
2.005e-05 1.630e+03 
$`2`
label2      U1 
 2.654 -35.104   

I need to know are there any function that optimizes the sum-of-squares to get best fitting for both of the groups. We may say that you already have the best fitting parameters as the residual sum-of-squares but I know that minimizer can do this but I haven't find any similar example we can do this in R.

ps. I made it up the numbers and fitting lines.

解决方案

Not sure about r, but having least squares with shared parameters is usually simple to implement.

A simple python example looks like:

import matplotlib
matplotlib.use('Qt4Agg')
from matplotlib import pyplot as plt

from random import random
from scipy import optimize
import numpy as np

#just for my normal distributed errord
def boxmuller(x0,sigma):
    u1=random()
    u2=random()
    ll=np.sqrt(-2*np.log(u1))
    z0=ll*np.cos(2*np.pi*u2)
    z1=ll*np.cos(2*np.pi*u2)
    return sigma*z0+x0, sigma*z1+x0

#some non-linear function
def f0(x,a,b,c,s=0.05):
    return a*np.sqrt(x**2+b**2)-np.log(c**2+x)+boxmuller(0,s)[0]

# residual function for least squares takes two data sets. 
# not necessarily same length
# two of three parameters are common
def residuals(parameters,l1,l2,dataPoints):
    a,b,c1,c2 = parameters
    set1=dataPoints[:l1]
    set2=dataPoints[-l2:]
    distance1 = [(a*np.sqrt(x**2+b**2)-np.log(c1**2+x))-y for x,y in set1]
    distance2 = [(a*np.sqrt(x**2+b**2)-np.log(c2**2+x))-y for x,y in set2]
    res = distance1+distance2
    return res

xList0=np.linspace(0,8,50)
#some xy data
xList1=np.linspace(0,7,25)
data1=np.array([f0(x,1.2,2.3,.33) for x in xList1])
#more xy data using different third parameter
xList2=np.linspace(0.1,7.5,28)
data2=np.array([f0(x,1.2,2.3,.77) for x in xList2])
alldata=np.array(zip(xList1,data1)+zip(xList2,data2))

# rough estimates
estimate = [1, 1, 1, .1]
#fitting; providing second length is actually redundant
bestFitValues, ier= optimize.leastsq(residuals, estimate,args=(len(data1),len(data2),alldata))
print bestFitValues


fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xList1, data1)
ax.scatter(xList2, data2)
ax.plot(xList0,[f0(x,bestFitValues[0],bestFitValues[1],bestFitValues[2] ,s=0) for x in xList0])
ax.plot(xList0,[f0(x,bestFitValues[0],bestFitValues[1],bestFitValues[3] ,s=0) for x in xList0])


plt.show()

#output
>> [ 1.19841984  2.31591587  0.34936418  0.7998094 ]

If required you can even make your minimization yourself. If your parameter space is sort of well behaved, i.e. approximately parabolic minimum, a simple Nelder Mead method is quite OK.

这篇关于优化的拟合系数,以实现更好的拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆