【问题标题】:Specifying bin edges when fitting a normal distribution using histfit使用 histfit 拟合正态分布时指定 bin 边缘
【发布时间】:2021-02-16 08:41:49
【问题描述】:

我想使用预定义的 bin 为一些数据拟合直方图。我所有的数据点都在 1 到 10 之间,所以我希望 bin 从xmin=1 开始,到xmax=10 结束,步长为0.5

我使用以下命令:

x = d1.data(:,4); % x is my data
H = histfit(x,10,'normal'); % fits a histogram using 10 bins 

但是,在执行上述操作时,bin 会根据数据集自动确定,并且与我想要的边缘不对应。如何确保所有数据集使用相同的 bin 边缘?

【问题讨论】:

  • 你可以使用曲线拟合工具箱吗?
  • 我在编辑->图形属性->轴->高斯曲线。是你的意思吗?我正在使用 matlab 2019a 我适合帮助你
  • 是的,我几乎做到了。现在该怎么办?

标签: matlab histogram curve-fitting normal-distribution binning


【解决方案1】:

如果您可以访问Curve Fitting Toolbox,我会建议另一种提供所需灵活性的方法。这涉及“自己”做适合而不是依赖histfit

% Generate some data:
rng(66221105) % set random seed, for reproducibility
REAL_SIG = 1.95;
REAL_MU = 5.5;
X = randn(200,1)*REAL_SIG + REAL_MU;

% Define the bin edges you want
EDGES = 1:0.5:10;

% Bin the data according to the predefined edges:
Y = histcounts(X, EDGES);

% Fit a normal distribution using the curve fitting tool:
binCenters = conv(EDGES, [0.5, 0.5], 'valid'); % moving average
[xData, yData] = prepareCurveData( binCenters, Y );

ft = fittype( 'gauss1' );
fitresult = fit( xData, yData, ft );
disp(fitresult); % optional

% Plot fit with data (optional)
figure(); 
histogram(X, EDGES); hold on; grid on;
plot(fitresult); 

这会产生以下情节:

和拟合模型:

 General model Gauss1:
 fitresult(x) =  a1*exp(-((x-b1)/c1)^2)
 Coefficients (with 95% confidence bounds):
   a1 =       19.65  (17.62, 21.68)
   b1 =        5.15  (4.899, 5.401)
   c1 =       2.971  (2.595, 3.348)

【讨论】:

  • 好的,我想问你,我该怎么做才能取正态分布曲线? matlab 中的 histfit 命令给出了正态分布曲线,但我无法根据需要指定我的集群。你能帮帮我吗?
  • @MRpapas 我不明白您的问题,我提供的代码为您提供了拟合对象(绝对可以视为“正态分布曲线”)和高斯参数 -这是绘制曲线所需的一切。请澄清。
  • 好吧,在你提到代码之前,我使用了“histfit”命令(参见下面的信息:mathworks.com/help/stats/histfit.html)。使用这个命令,我有 10 个集群和一个正态分布拟合。问题是使用 histfit 时,bin 没有像我上面提到的那样定义(我的意思是最小、最大值和步长)。我尝试了您的代码,但曲线与我拥有的第一条曲线不同。你知道该怎么做吗?
  • @MR 当然它是不同的 - 因为箱是不同的(即 x,y 值在两种情况下不同,这不太可能给出完全相同的结果)。如果您使用histfit 使用此答案中提供的方法自动检测到的相同箱,您应该得到相同的曲线。在您的问题中,您希望能够指定垃圾箱,这正是我所展示的。如果您正在寻找不同的解决方案,您可能需要解释更广泛的问题。
猜你喜欢
  • 2014-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-22
  • 2017-02-19
  • 2018-08-21
  • 2013-03-15
相关资源
最近更新 更多