Bivariate Guassian mixed model on 1D data

Hi, I am trying to plot a guassian curve on the histogram distribution of my 1D data. This is my code:
data = xlsread('csf.xlsx','ab');
mu1 = mean('data');
sigma1 = std('data');
X = [mvnrnd(mu1,sigma1,1000)];
numComponents=2;
paramEsts = fitgmdist(X,numComponents);
MU=[paramEsts.mu(1);paramEsts.mu(2)];
SIGMA=cat(3,[paramEsts.Sigma(1)],[paramEsts.Sigma(2)]);
PPp=[paramEsts.PComponents(1),paramEsts.PComponents(2)];
gm = gmdistribution(MU,SIGMA,PPp);
dimension = gm.NumVariables
rng(1);
min_val = min(data(:)-100); max_val = max (data(:)+1000);
% min_val = min('data'); max_val = max ('data');
histogram(data, 'Normalization', 'pdf', 'EdgeColor', 'none')
x= min_val:0.01:max_val;
xlim([min_val max_val])
hold on;
xgrid = linspace (50,3000,500)';
n1 = makedist('normal',paramEsts.mu(1),sqrt(paramEsts.Sigma(1)));
n2 = makedist('normal',paramEsts.mu(2),sqrt(paramEsts.Sigma(2)));
p = paramEsts.ComponentProportion;
y = p(1)*pdf(n1,xgrid) + p(2)*pdf(n2,xgrid);
hold on; plot(xgrid,y,'b'); hold off
And this is what I get:
However, I am trying to get something like this:
Please, how do I achieve this?

2 件のコメント

the cyclist
the cyclist 2018 年 8 月 24 日
Can you upload either the Excel file or the resulting data variable in a MAT file (using that little paperclip icon)?
Trisha Kibaya
Trisha Kibaya 2018 年 8 月 27 日
Sure. Thanks.

サインインしてコメントする。

回答 (1 件)

Image Analyst
Image Analyst 2018 年 8 月 24 日

0 投票

Try something like the attached demo that fits two Gaussians to data. Adapt as needed (like replace blue dots on plot with white bars or whatever).

7 件のコメント

Trisha Kibaya
Trisha Kibaya 2018 年 8 月 27 日
Hi I don't have the Statistics and Machine Learning Toolbox. Is there another way I can do this only on Matlab?
Image Analyst
Image Analyst 2018 年 8 月 27 日
You might be able to get somewhat close by taking the log of your data and doing a multilinear regression with polyfit. See the attached and try to adapt the formula in it to be a sum of two Guassians.
Trisha Kibaya
Trisha Kibaya 2018 年 8 月 28 日
Thanks a lot for the help! I have been able to get the results I was looking for (i.e. to plot two gaussians that overlap each other). I'd like to know if it's possible to tell matlab to let me know the values of the intersection (i.e the point where both gaussians are equal). I have attached the image of my results to this question.
Image Analyst
Image Analyst 2018 年 8 月 28 日
Yes. What do you have? Formulas, or vectors?
Trisha Kibaya
Trisha Kibaya 2018 年 8 月 28 日
Formulas. This is are the codes I used to plot them: xgrid = linspace (40,1000,112110'); n1 = makedist('normal',paramEsts.mu(1),sqrt(paramEsts.Sigma(1))); n2 = makedist('normal',paramEsts.mu(2),sqrt(paramEsts.Sigma(2))); p = paramEsts.ComponentProportion; y1 = p(1)*pdf(n1,xgrid); hold on; plot (xgrid, y1,'b'); hold off y2 = p(2)*pdf(n2,xgrid); hold on; plot (xgrid, y2,'r'); hold off
Image Analyst
Image Analyst 2018 年 8 月 28 日
Trisha Kibaya
Trisha Kibaya 2018 年 8 月 28 日
Thanks. I am still new at this, I had no idea that I posted my code in the wrong way. Sorry about that.
xgrid = linspace (40,1000,112110');
n1 = makedist('normal',paramEsts.mu(1),sqrt(paramEsts.Sigma(1)));
n2 = makedist('normal',paramEsts.mu(2),sqrt(paramEsts.Sigma(2)));
p = paramEsts.ComponentProportion;
y1 = p(1)*pdf(n1,xgrid); hold on; plot (xgrid, y1,'b'); hold off
y2 = p(2)*pdf(n2,xgrid); hold on; plot (xgrid, y2,'r'); hold off

サインインしてコメントする。

質問済み:

2018 年 8 月 24 日

コメント済み:

2018 年 8 月 28 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by