1

我有一个由大约 200 万个样本组成的数据向量,我怀疑这些样本是两个高斯的混合。我尝试使用 matlab 的 fitgmdist 将数据 Data 拟合到混合物中。

从直方图:

% histogram counts of X with 1000 bins.
[Yhist, x] = histcounts(Data, 1000, 'normalization', 'pdf');
x = (x(1:end-1) + x(2:end))/2;

使用 fitgmdist:

% Increase no. of iterations. default is 100.
opts.MaxIter = 300;

% Ensure that it does all iterations.
opts.TolFun = 0;

GMModel = fitgmdist(Data, 2, 'Options', opts, 'Start', 'plus');
wts = GMModel.ComponentProportion;
mu = GMModel.mu;
sig = sqrt(squeeze(GMModel.Sigma));
Ygmfit = wts(1)*normpdf(x(:), mu(1), sig(1)) + wts(2)*normpdf(x(:), mu(2), sig(2));

使用 fitgmdist 混合结果:wts = [0.6780, 0.322], mu = [-7.6444, -9.7831], sig = [0.8243, 0.5947]

接下来我尝试使用 nlinfit:

% Define the callback function for nlinfit.
function y = nmmix(a, x)
   a(1:2) = a(1:2)/sum(a);
   y = a(1)*normpdf(x(:), a(3), a(5)) + a(2)*normpdf(x(:), a(4), a(6));
end

init_wts = [0.66, 1-0.66];
init_mu = [-7.7, -9.75];
init_sig = [0.5, 0.5];
a = nlinfit(x(:), Yhist(:), @nmmix, [init_wts, init_mu, init_sig]);
wts = a(1:2)/sum(a(1:2));
mu = a(3:4);
sig = a(5:6);
Ynlinfit = wts(1)*normpdf(x(:), mu(1), sig(1)) + wts(2)*normpdf(x(:), mu(2), sig(2));

使用 nlinfit 混合结果:wts = [0.6349, 0.3651], mu = [-7.6305, -9.6991], sig = [0.6773, 0.6031]

% Plot to compare the results
figure;
hold on
plot(x(:), Yhist, 'b');
plot(x(:), Ygmfit, 'k');
plot(x(:), Ynlinfit, 'r');

蓝色:数据直方图,红色:非线性拟合,黑色:Matlab fitgmdist

似乎非线性拟合(红色曲线)直观地比“fitgmdist”(黑色曲线)更接近直方图(蓝色曲线)。即使我使用更精细的直方图,例如使用 100,000 个 bin,结果也是相似的。

这种差异的根源是什么?

后来补充:当然人们不会期望结果是相同的,但我希望两次拟合的视觉质量具有可比性。

4

0 回答 0