将双峰正态分布拟合到值的向量,而不是它的直方图

Fit bimodal normal distribution to a vector of values, not its histogram

我想将双峰正态分布拟合到看起来呈双峰分布的数据,例如下面的示例 (plot(x)):

根据 MA​​TLAB 文档,我考虑使用带有函数句柄的 mle 函数来混合两个高斯函数:

@(x,p,mu1,mu2,sigma1,sigma2)p*normpdf(x,mu1,sigma1)+(1-p)*normpdf(x,mu2,sigma2)

然而,mle 函数适合 x 的直方图,而我想要 x 本身的近似值。我怎样才能做到这一点?

我们的想法是创建数组 y,其直方图看起来像您的函数 x(应该在任何地方都是正数):

%// Calculate
N  = numel(x);
xi = fix(x/max(x)*100);  % Limit the memory damage
yp = arrayfun(@(n) n*ones(1,xi(n)), 1:N, 'UniformOutput', false);
y = [yp{:}];

%// Visually inspect
ax = axes('Parent', figure());
plot(ax, xi); hold(ax, 'on');
hist(ax, y, N);

Matlab 具有内置的双峰拟合

f =@(A,m,s,x) A(1) * exp(-((x-m(1))/s(1)).^2) + A(2) * exp(-((x-m(2))/s(2)).^2);
g =@(x) f([5, 10], [1, 10], [3, 5], x);
x = (-20:0.01:20)';
y = g(x) + 0.25*(rand(size(x)) - 0.5);
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
[fitresult, gof] = fit( x, y, 'gauss2', opts );
plot( fitresult, x, y );

%u可以使用该功能 histfit(你的数据,nbins,'Kernel','normal'); %有时它不收敛;在这种情况下,您可以求助于 distributionFitter 应用程序 distributionFitter(你的数据); %select 非参数;然后正常