Gnuplot 如何使用包含f(x)和#x2B;的一组数据对a进行加权拟合-df(x)?
我有一个函数Gnuplot 如何使用包含f(x)和#x2B;的一组数据对a进行加权拟合-df(x)?,gnuplot,uncertainty,Gnuplot,Uncertainty,我有一个函数f(x)=a/x,我有一组数据,其中包含f(x)+-df(x)和x+-dx的值。我如何告诉gnuplot用它对a进行加权拟合 我知道fit接受使用术语的,这适用于df(x),但不适用于dx。似乎gnuplot将我对x的错误视为我函数的整个RHS的错误(a/x+-dx) 如何进行加权拟合以拟合f(x)+-df(x)=a/(x+-dx)以找到最佳a?您拟合的方程如下: z = a/(x +- dx) 这可以等效地写成: z = a/x +- dz 对于一个合适的dz 我认为(如果
f(x)=a/x
,我有一组数据,其中包含f(x)+-df(x)
和x+-dx
的值。我如何告诉gnuplot用它对a
进行加权拟合
我知道fit
接受使用术语的,这适用于df(x)
,但不适用于dx
。似乎gnuplot将我对x
的错误视为我函数的整个RHS的错误(a/x+-dx
)
如何进行加权拟合以拟合f(x)+-df(x)=a/(x+-dx)
以找到最佳a
?您拟合的方程如下:
z = a/(x +- dx)
这可以等效地写成:
z = a/x +- dz
对于一个合适的dz
我认为(如果我的演算和统计正确的话),你可以通过以下公式从x和dx计算dz:
dz = partial_z/partial_x*dx
假设dx很小
对于这种情况,这将产生:
dz = -a/x**2*dx
现在你有了一个由两个变量组成的函数(z=a/x-(a/x**2)*dx
),你想要适合一个常数。当然,我可能错了。。。我已经有一段时间没有玩过这种东西了。这里有一个简单的例子足以证明gnuplot正在做你想做的事情:
使用以下玩具模型数据构建平面文本文件data.dat:
#f df x dx
1 0.1 1 0.1
2 0.1 2 0.1
3 0.1 3 0.1
4 0.1 4 0.1
10 1.0 5 0.1
仅通过观察数据,我们期望一个好的模型是直接成比例的f=x,在x=5时有一个明显的异常值,f=10。我们可以告诉gnuplot用两种非常不同的方式来拟合这些数据。以下脚本weightedFit.gp演示了这一区别:
# This file is called weightedFit.gp
#
# Gnuplot script file for demonstrating the difference between a
# weighted least-squares fit and an unweighted fit, using mock data in "data.dat"
#
# columns in the .dat are
# 1:f, 2:d_f, 3: x, 4: d_x
# x is the independent variable and f is the dependent variable
# you have to play with the terminal settings based on your system
# set term wxt
#set term xterm
set autoscale # scale axes automatically
unset log # remove any log-scaling
unset label # remove any previous labels
set xtic auto # set xtics automatically
set ytic auto # set ytics automatically
set key top left
# change plot labels!
set title "Weighted and Un-weighted fits"
set xlabel "x"
set ylabel "f(x)"
#set key 0.01,100
# start with these commented for auto-ranges, then zoom where you want!
set xr [-0.5:5.5]
#set yr [-50:550]
#this allows you to access ASE values of var using var_err
set fit errorvariables
## fit syntax is x:y:Delta_y column numbers from data.dat
#Fit data as linear, allowing intercept to float
f(x)=m*x+b
fW(x)=mW*x+bW
# Here's the important difference. First fit with no uncertainty weights:
fit f(x) 'data.dat' using 3:1 via m, b
chi = sprintf("chiSq = %.3f", FIT_WSSR/FIT_NDF)
t = sprintf("f = %.5f x + %.5f", m, b)
errors = sprintf("Delta_m = %.5f, Delta_b = %.5f", m_err, b_err)
# Now, weighted fit by properly accounting for uncertainty on each data point:
fit fW(x) 'data.dat' using 3:1:2 via mW, bW
chiW = sprintf("chiSqW = %.3f", FIT_WSSR/FIT_NDF)
tW = sprintf("fW = %.5f x + %.5f", mW, bW)
errorsW = sprintf("Delta_mW = %.5f, Delta_bW = %.5f", mW_err, bW_err)
# Pretty up the plot
set label 1 errors at 0,8
set label 2 chi at 0,7
set label 3 errorsW at 0,5
set label 4 chiW at 0,4
# Save fit results to disk
save var 'fit_params'
## plot using x:y:Delta_x:Delta_y column numbers from data.dat
plot "data.dat" using 3:1:4:2 with xyerrorbars title 'Measured f vs. x', \
f(x) title t, \
fW(x) title tW
set term jpeg
set output 'weightedFit.jpg'
replot
set term wxt
生成的plot weightedFit.jpg讲述了一个故事:绿色拟合没有考虑数据点的不确定性,对于数据来说是一个糟糕的模型。蓝色拟合解释了异常值中的较大不确定性,并适当拟合了比例模型,得到斜率1.02+/-0.13和截距-0.05+/-0.35
由于我今天刚刚加入,我缺乏发布图片所需的“10个声誉”,因此您只需亲自运行脚本即可看到适合的内容。在工作目录中包含脚本和数据文件后,请执行以下操作:
gnuplot>load'weightedFit.gp'
您的fit.log应该如下所示:
*******************************************************************************
Thu Aug 20 14:09:57 2015
FIT: data read from 'data.dat' using 3:1
format = x:z
x range restricted to [-0.500000 : 5.50000]
#datapoints = 5
residuals are weighted equally (unit weight)
function used for fitting: f(x)
f(x)=m*x+b
fitted parameters initialized with current variable values
iter chisq delta/lim lambda m b
0 1.0000000000e+01 0.00e+00 4.90e+00 2.000000e+00 -2.000000e+00
1 1.0000000000e+01 0.00e+00 4.90e+02 2.000000e+00 -2.000000e+00
After 1 iterations the fit converged.
final sum of squares of residuals : 10
rel. change during last iteration : 0
degrees of freedom (FIT_NDF) : 3
rms of residuals (FIT_STDFIT) = sqrt(WSSR/ndf) : 1.82574
variance of residuals (reduced chisquare) = WSSR/ndf : 3.33333
Final set of parameters Asymptotic Standard Error
======================= ==========================
m = 2 +/- 0.5774 (28.87%)
b = -2 +/- 1.915 (95.74%)
correlation matrix of the fit parameters:
m b
m 1.000
b -0.905 1.000
*******************************************************************************
Thu Aug 20 14:09:57 2015
FIT: data read from 'data.dat' using 3:1:2
format = x:z:s
x range restricted to [-0.500000 : 5.50000]
#datapoints = 5
function used for fitting: fW(x)
fW(x)=mW*x+bW
fitted parameters initialized with current variable values
iter chisq delta/lim lambda mW bW
0 2.4630541872e+01 0.00e+00 1.78e+01 1.024631e+00 -4.926108e-02
1 2.4630541872e+01 0.00e+00 1.78e+02 1.024631e+00 -4.926108e-02
After 1 iterations the fit converged.
final sum of squares of residuals : 24.6305
rel. change during last iteration : 0
degrees of freedom (FIT_NDF) : 3
rms of residuals (FIT_STDFIT) = sqrt(WSSR/ndf) : 2.86534
variance of residuals (reduced chisquare) = WSSR/ndf : 8.21018
p-value of the Chisq distribution (FIT_P) : 1.84454e-005
Final set of parameters Asymptotic Standard Error
======================= ==========================
mW = 1.02463 +/- 0.1274 (12.43%)
bW = -0.0492611 +/- 0.3498 (710%)
correlation matrix of the fit parameters:
mW bW
mW 1.000
bW -0.912 1.000
有关文档,请参阅。干杯 自5.0版以来,gnuplot对自变量的不确定性有明确规定
fit f(x) datafile using 1:2:3:4 xyerror
采用“欧莱雅有效方差法”
(上面的命令要求数据的格式为xydxdy
)那么dz在哪里?这是一个物理问题的一部分:我测量了z和它的dz,我测量了x和它的dx。现在我想要一个。如果我还不知道的话,你的方法会给我错误。@FooBar——你测量了z和dx。我用你们测量的东西(x和dx)计算了dz,我知道dz是什么,我不需要计算它。让我们做一个简单的例子:E=h*f。我测量E,从我的设备我知道dE。用另一个设备我测量f,知道df。从这里,我现在想通过使用gnuplot拟合函数来找到h和dh。如果我事先不知道dz,但已经知道a,那么你的方法是有效的。我不确定这是否回答了我的问题。这适用于线性方程组。但我的问题是关于类似f(x)=a/x
(或其他非线性)的东西,其中x
和f(x)
上存在(不同)错误。您的回答也忽略了using
子句中的第4列(dx)。我正要参考gnuplot文档的第73页:“注意,与大多数NLLS实现一样,fit最小化了平方距离的加权和(y-f(x))**2。它不提供任何方法来解释x值中的“错误”,仅在y中。还有,any”异常值”(模型正态分布以外的数据点)将对解决方案产生夸大的影响。”但似乎下面提到的更改尚未写入文档中。5.0版的变更日志证实了Karl的观点:“*新的fit命令考虑了自变量的错误”我尝试了Karl在下面建议的语法,并确认它是有效的。我的学生会喜欢的,卡尔-谢谢!我在变更日志里没看到。此外,在我的示例中,没有任何特定于线性拟合的内容。NLLS将处理a/x或任何其他性能良好的拟合函数。