LoginSignup
0
0

More than 3 years have passed since last update.

scipy.optimize.leastsqで何を最小化するか what should be minimized in the optimization of scipy.optimize.leastsq.

Posted at

※初投稿で勝手がわからなかったので、後々告知なく修正するかもしれません。

scipy.optimize.leastsqは所謂最小二乗法で二乗残差を最小化することで、ある $y$ の $x$ 依存性 $y(x)$ をある関数 $f(x)$で再現する際のパラメーターを最適化する場合にも用いられる。

scipy.optimize.leastsq is sometimes used to optimize the parameters of a function $f(x)$ that reproduces the $x$-dependency of $y$.

例えば、ある半導体の低効率の温度依存性 $\rho(T)$ をArrheniusの式 $a\exp(b/T)$ でフィッティングしたい場合($a,b$ はフィッティングパラメータ。物理的な意味はググってください。)は

For example, the temperature depencencies of resistivity $\rho(T)$ is sometimes fitted by the Arrhenius formula as follows,

import scipy.optimize as opt

def arrhenius(param,x): #Arreniusの式
    a,b = param[0],param[1]
    return a*np.exp(b/x)
def residual(param,x,y,func): #残差計算
    return y - func(param,x)

result = opt.leastsq(residual,param,args=(X,Y0,arrhenius))

のような形でフィッティングできる。
個人的にはフィッティング結果をプロットする場合などを考えるとフィッティング用の関数と残差計算用の関数は別に用意しておいたほうが楽だと思っている。

ここで疑問なのが、残差 y - func(param,x) を最小化することが適切なのかどうか。

Here, it is quastionable whether the minimizing y - func(x) is valid or not.

今回の場合、$y$が低温ほど(つまり$x$が小さいほど)大きくなる。抵抗測定では測定範囲の関係で一般的に真の値が大きいほど誤差も大きくなるので、上のような単純な残差を最小化する場合、低温側のデータの重みが大きくなることになる(逆に抵抗率の逆数である伝導率を用いた場合は高温側の重みが大きくなる)。そこでこのような残差計算を考えてみる。

In this case, the error of resistivity tend to be proprtional to the magnitude of the real value due to the range of the measurable volrage, thus the data at lower temperature become the hevior aginst the fitting. To modify such a weighted tendencies, I suggest one option of residual function like this:

def residual(param,x,y,func): #残差計算
    return func(param,x)/y - 1

こうすると各データ点とフィッティング関数の差分を比として計算しているため、真の値に依存するような重みづけは無くなる。yで割っているのはfの関数形によってはfが0になることがあるから。

that evaluates the not the difference but the ratio that is not relevant to the absolute value of signs.

個人的な経験としては測定値の誤差自体が十分小さくフィッティングに用いるデータ数が十分な場合やフィッティングパラメータの数が少ない場合は大差はないが、そうでない場合は残差計算の方法の違いでそれなりに結果に違いが生じてくるので個人的には注意したいと思っている部分である。

In my experience, such a fifference of residual function make no difference if the error itself is enough small or the number of data for fitting is enough or the number of fitting paremeter is enough small, there is no significant diference, Otherwise, there are possibilities to make differencies depending on the choice of residual function. Thus we wolud like to be careful about such a differencies.

正直質問として投稿する内容かとも思ったけれども、フィッティングの関数や範囲などの条件でどうとでも状況が変わる話題だと思うのでこちらで投稿しました。ご指摘ありましたら歓迎いたします。

Although I'm not sure whether it should be submitted by article or question, I did because the situation will change depending on the range and the condition of fitting. I welcome the any suggestions ans corrections.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0