つれづれなる備忘録

日々の発見をあるがままに綴る

gnuplotによるグラフ作成13~フィッティングの詳細

 前回はgnuplotによるデータのフィッティングについて紹介したが、今回はフィッティングの設定、実行時に表示される情報およびフィッティングに用いるデータの選択について紹介する。

atatat.hatenablog.com

1. フィッティングの設定

 gnuplot非線形(線形)フィッティングはMarquardt-Levenberg 法を用いている。(詳しくは数理最適化の勉強メモ − Levenberg-Marquardt法 - かみのメモ) 通常はデフォルトのままで、初期値を工夫すればよいがMarquardt-Levenberg 法のパラメータや収束条件も指定することができる。現状の設定を見るにはshow fitとして デフォルトでは以下のようになる。

show fit
        fit will output brief results to console and log-file.
        fit can handle up to 12 independent variables
        fit will prescale parameters by their initial values
        fit will place parameter errors in variables
        fit will not place covariances in variables
        fit will scale parameter errors with the reduced chi square
        log-file for fits is unchanged from the environment default of
                'fit.log'
        fits will be considered to have converged if  delta chisq < chisq * 1e-05
        fit has no limit in the number of iterations
        fit will default to `unitweights` if no `error`keyword is given on the command line.
        fit can run the following command when interrupted:
                'replot'
        last fit command was: fit g(x) 'exp_dat.txt' via c,d

設定を変更するにはset fitを用いて変更する。例えば繰り返し回数を設定するには

set fit maxiter 100

no limit in the number of iterationsからstop after a maximum of 100 iterationsへ変更されていることが確認できる

show fit
        fit will output brief results to console and log-file.
        fit can handle up to 12 independent variables
        fit will prescale parameters by their initial values
        fit will place parameter errors in variables
        fit will not place covariances in variables
        fit will scale parameter errors with the reduced chi square
        log-file for fits is unchanged from the environment default of
                'fit.log'
        fits will be considered to have converged if  delta chisq < chisq * 1e-05
        fit will stop after a maximum of 100 iterations
        fit will default to `unitweights` if no `error`keyword is given on the command line.
        fit can run the following command when interrupted:
                'replot'
        last fit command was: fit g(x) 'exp_dat.txt' via c,d

各項目を設定するには次のサイトを参照。 88.25 非線形関数回帰 (fit)

2. フィッティング結果の情報

 フィッティング実行時に表示される情報についていくつか補足する。

iter      chisq       delta/lim  lambda   c             d            
   0 7.9349681954e+03   0.00e+00  2.81e+00    1.000000e+00   1.000000e-01
   * 1.0947233099e+10   1.00e+05  2.81e+01    2.009572e+00   7.149508e-01
   1 1.0855216784e+03  -6.31e+05  2.81e+00    1.803721e+00   2.041256e-01
   2 1.3163872840e+02  -7.25e+05  2.81e-01    2.105784e+00   2.277526e-01
   3 6.7779979889e+01  -9.42e+04  2.81e-02    2.225940e+00   2.179931e-01
   4 6.7749260463e+01  -4.53e+01  2.81e-03    2.212564e+00   2.183251e-01
   5 6.7749246439e+01  -2.07e-02  2.81e-04    2.213473e+00   2.182958e-01
iter      chisq       delta/lim  lambda   c             d            

After 5 iterations the fit converged.
final sum of squares of residuals : 67.7492
rel. change during last iteration : -2.06994e-07

degrees of freedom    (FIT_NDF)                        : 14
rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 2.19983
variance of residuals (reduced chisquare) = WSSR/ndf   : 4.83923

Final set of parameters            Asymptotic Standard Error
=======================            ==========================
c               = 2.21347          +/- 0.2982       (13.47%)
d               = 0.218296         +/- 0.01006      (4.607%)

correlation matrix of the fit parameters:
                c      d      
c               1.000 
d              -0.986  1.000 

まずchisqは残差を標準偏差で割った値で

 \displaystyle \chi^{2}=\sum_{i=1}^{m} \left( \frac{y-\hat{y}}{\sigma} \right)^{2}

フィッティングした曲線に対してどの程度近似できているかを示す。最後の値はfinal sum of squares of residualsとして使われている。

Asymptotic Standard Errorは推定したパラメータ値の68%信頼区間を表している。(括弧内はパラメータ値に対する信頼区間の比率)

correlation matrixの非対角項はパラメータの相関を表し独立であれば0に近い値となる。また対角項については1となる。

3. フィッティングデータの選択

 フィッティングを実行する際、すべてのデータではなくある程度範囲を狭めてフィッティングを行うこともできる。前回の指数関数のフィッティングでxが0~15ではなく0~10でフィッティングする場合fit [0:10][] g(x) 'exp_dat.txt' via c,dとする。

fit [0:10][] g(x) 'exp_dat.txt' via c,d 
iter      chisq       delta/lim  lambda   c             d            
   0 1.2520115146e+01   0.00e+00  1.48e+01    2.213473e+00   2.182958e-01
   1 3.7000131504e+00  -2.38e+05  1.48e+00    2.077580e+00   2.159255e-01
   2 1.7742743011e+00  -1.09e+05  1.48e-01    1.656853e+00   2.386640e-01
   3 1.3186377653e+00  -3.46e+04  1.48e-02    1.665400e+00   2.407553e-01
   4 1.3185738402e+00  -4.85e+00  1.48e-03    1.666037e+00   2.406825e-01
   5 1.3185738207e+00  -1.48e-03  1.48e-04    1.666020e+00   2.406837e-01
iter      chisq       delta/lim  lambda   c             d            

After 5 iterations the fit converged.
final sum of squares of residuals : 1.31857
rel. change during last iteration : -1.48032e-08

degrees of freedom    (FIT_NDF)                        : 9
rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 0.382764
variance of residuals (reduced chisquare) = WSSR/ndf   : 0.146508

Final set of parameters            Asymptotic Standard Error
=======================            ==========================
c               = 1.66602          +/- 0.0971       (5.828%)
d               = 0.240684         +/- 0.006731     (2.796%)

すべてのデータでフィッティングするとc= 2.21347, d=0.218296だったが、xを0~10に選択した場合c=1.66602, d=0.240684と真値(c=1.5,d=0.25)にだいぶ近づいた。フィッティングした曲線とデータをプロットすると、xが10以下のところでデータと曲線がよく一致していることがわかる。

選択して非線形フィッティング

"全体でフィッティング"
全体でフィッティング

4. まとめ

 今回はgnuplotによる非線形フィッティングに関して、フィッティング時に表示される情報およびフィッティングに用いるデータの選択について紹介した。