2.2.5 误差与拟合
误差与拟合的概念
误差(error)是统计样本中某一元素的观测值与其“真值”(未必可直接观测得到)之间的离差的度量。
拟合(fit)是一种构建一个函数曲线,使之最佳地吻合现有数据点的过程。
ROOT 中既可以使用TGraph
描述误差并拟合,也可以使用TH
类。
注意:拟合操作应在最后进行,如果宏中的marker属性设置在拟合之后,可能会导致拟合失效
误差棒
误差棒(error bar)在统计图表中起到了传达数据不确定性和可靠性的作用,帮助读者更好地理解数据并做出准确的解释和推断。任何测量都会有误差,使用TGraphAsymmErrors()
函数绘制误差棒,你甚至可以绘制倾斜的误差棒。
TGraphAsymmErrors *gr = new TGraphAsymmErrors(n,x,y,exl,exh,eyl,eyh)
TGraphAsymmErrors(number of var,x,y,low x,x high,y low,y high)
TGraphAsymmErrors(number of var,x,y,low x,x high,y low, 0) // 不绘制上误差
auto g = new TGraph2DErrors(np, rx, ry, rz, ex, ey, ez); // 二维误差棒
设置误差棒的填充颜色:SetFillColor()
设置误差棒的填充风格:SetFillStyle()
设置误差棒的颜色:SetLineColor()
误差棒的样式:

误差带
误差带(error band)是在统计图表中用来表示数据的不确定性或误差范围的一种方式,与误差棒类似,但是误差带通常是用来表示连续数据或函数的误差范围。
选项“4”与选项“3”相似,只是表带是平滑的。 如上图所示,应谨慎使用此选项,因为平滑算法可能会显示一些(巨大的)“弹跳”效果。 在某些情况下,它看起来比选项“3”更好(因为它很光滑),但它可能会具有误导性。svg和其他保存格式在显示上会略有差异,这里只做示意。论坛上关于弹跳的提问与解答https://root-forum.cern.ch/t/confidence-band-on-tf1-fit/34514
多重误差
TMultiErrors
是一个类,用于处理多个测量值的误差。它是ROOT数据分析框架中的一部分,用于处理数据的不确定性和误差。

多维误差
TGraphMultiErrors
的工作方式与TGraphAsymmErrors
基本相同。 它有可能定义多个y-Errors的类型/维度。 如果你想同时绘制统计和系统错误,这非常有用。
内置函数拟合
hist.Fit("gaus");
自定义拟合
root 支持用户自定义拟合函数。
#include <fstream>
int fit(){
TGraph *gr = new TGraph("data1.txt");
double pi=3.14159265;
TF1 *f1 = new TF1("f1","[0]*sin([1]*pi*x)*exp([2]*x)+[3]",0,10);
f1->SetParameter(0,1); // 第零个参数的参考值
f1->SetParameter(1,1);
f1->SetParameter(2,-1);
f1->SetParameter(3,0.5);
// be equal in value with below
// fi->SetParameters(1,1,-1,0.5);
gr->Fit("f1");
gr->Draw();
}
root会打印出其拟合参数的精确值:
[zhangzh@node01 test]$ rl fit.c
root [0]
Processing fit.c...
****************************************
Minimizer is Minuit / Migrad
Chi2 = 0.1458
NDf = 96
Edm = 3.61433e-08
NCalls = 123
p0 = 1.00186 +/- 0.0254347
p1 = 1.30098 +/- 0.00369848
p2 = -0.482759 +/- 0.0172724
p3 = 0.498504 +/- 0.00393919
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
(int) 0
拟合参数
TF1::SetParLimit(parnumber, parvalue);
TF1::SetParLimits(parvalue1, parvalue2, parvalue3);
参数获得
TF1::GetChisquare()
TF1::GetNDF()
TF1::GetParameter(int i)
TF1::GetParError(int i)
TF1::GetParErrorLow(int i)
,GetParErrorUp(int i)
多Graph拟合
合并拟合
用不同的marker表征不同的实验结果,但x轴范围不一致,那么拟合哪一段的数据最为准确?
使用TMultiGraph::TMultiGraph()
函数,将多个 graph 合并,既能保留不同 marker 的个性,也能让所有数据点参与拟合。
分别拟合

含本底拟合

拟合误差
拟合误差是用来描述实际观测值与拟合模型(如线性回归、曲线拟合等)之间的差异或偏差,即拟合模型对真实数据的逼近程度。
拟合误差通常以各种统计指标来表示,比如残差平方和(residual sum of squares)、平均绝对误差(mean absolute error)、均方根误差(root mean square error)等。
拟合框
设置直方图拟合框TStyle::SetOptFit(pcev)
。
p = 1;打印概率
c = 1; 打印卡方/自由度数
e = 1; 打印误差(如果 e = 1,v 必须为 1)
v = 1;打印参数名称/值
gStyle->SetOptFit(1)
表示“默认值”,等价于gStyle->SetOptFit(111)
,gStyle->SetOptFit(0)
表示关闭面板。
拟合框位置
适用于打开多个拟合面板
TPave::SetX1 (x1)
设置拟合框的左下角的值为x1。
TPave::
SetX1NDC
(newx1)
设置拟合框相对位置,取值范围(0,1)
。
TPave::SetX2NDC(newx2); TPave::SetY1NDC(newx2);TPave::SetY2NDC(newx2);同理。
拟合参数名
修改参数名称可以让你的拟合结果更加清晰,TF1::SetParNames("parname", "", "parname")
最多支持10位的参数名称重制,空字符串会被跳过。
或另外单独控制:TF1::SetParName(number, parname)
。
参数范围
TF1::SetParLimits(number, parmin, parmax)
Associated errors
By default, for each bin, the sum of weights is computed at fill time. You can also call TH1::Sumw2() to force the storage and computation of the sum of the square of weights per bin. If Sumw2()
has been called, the error per bin is computed as the sqrt(sum of squares of weights)
. Otherwise, the error is set equal to the `sqrt(bin content).
To return the error for a given bin number, use:
double error = h->GetBinError(bin);
Empty bins are excluded in the fit when using the Chi-square fit method. When fitting an histogram representing counts (that is with Poisson statistics) it is recommended to use the Log-Likelihood method (option L
or WL
), particularly in case of low statistics.
参数返回
// TFitResultPtr contains only the fit status.
int fitStatus = hist->Fit(myFunction);
// TFitResultPtr contains the TFitResult.
TFitResultPtr r = hist->Fit(myFunction,"S");
// Access the covariance matrix.
TMatrixDSym cov = r->GetCovarianceMatrix();
// Retrieve the fit chi2.
double chi2 = r->Chi2();
// Retrieve the value for the parameter 0.
double par0 = r->Parameter(0);
// Retrieve the error for the parameter 0.
double err0 = r->ParError(0);
// Print the full information of the fit including covariance matrix.
r->Print("V");
// Store the result in a ROOT file.
r->Write();
参考
Last updated