How could I fit a mixture of gaussians to 1D data?
Show older comments
I have a data and i want to fit it by a mixture of gaussian, but I didn't know the existing number of gaussians.
I use this code:
[ndata text alldata] = xlsread('battery2.xlsx','li ion');
[R2,C2]=size(alldata);
Life = ndata(:,72);
A=round(Life);
[valu,idxC, idxV] = unique(A);
n = accumarray(idxV,1);
m= [n idxC] ;
y = [valu n];
binWidth = 1; %2 1
binCtrs = 0:binWidth:172;
figure
count= hist(Life,binCtrs);
hist(Life,binCtrs,'k');
%%rescale
counts = hist(Life,binCtrs);
nu = length(Life);
prob = counts / (nu * binWidth);
figure
plot(binCtrs,prob,'ko');hold on
k=2
paramEsts= gmdistribution.fit(Life,k)
xgrids = linspace(0,173,100);
y1 = gaussian1D(xgrids, paramEsts.mu(1), paramEsts.Sigma(1));
y2 = gaussian1D(xgrids, paramEsts.mu(2), paramEsts.Sigma(2));
% line(xgrids,y1,'color','r', 'LineWidth',2)
axis([-10 175 0 0.02]);
C=5;
w= [paramEsts.PComponents(1) paramEsts.PComponents(2)];
x=xgrids;
values=binCtrs;
[mu_est, sigma_est, w_est, counter, difference] = gaussian_mixture_model(values, C, 1.0e-3);
mu_est'
sigma_est'
w_est'
% compare empirical data to estimated distribution
p1_est = w_est(1) * norm_density(x, mu_est(1), sigma_est(1));
p2_est = w_est(2) * norm_density(x, mu_est(2), sigma_est(2));
p3_est = w_est(3) * norm_density(x, mu_est(3), sigma_est(3));
p4_est = w_est(4) * norm_density(x, mu_est(4), sigma_est(4));
p5_est = w_est(5) * norm_density(x, mu_est(5), sigma_est(5));
plot2 = plot(x, p1_est+p2_est+p3_est+p4_est+p5_est, 'r--', 'linewidth', 2);

but it didn't fit, how i could change the code to fit the data?
Accepted Answer
More Answers (2)
Amr Hashem
on 25 Mar 2017
Edited: Amr Hashem
on 25 Mar 2017
Amr Hashem
on 9 Apr 2017
Edited: Amr Hashem
on 9 Apr 2017
0 votes
Categories
Find more on Descriptive Statistics and Insights in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!