Fit two peak model

145 views (last 30 days)
cliffy on 28 Jun 2020
Commented: Walter Roberson on 1 Jul 2020
I wonder if it is possible to fit a familiar model to this data?
My first thought tends to use the mixed Gaussian model to fit a bimodal distribution. I don't know if it can work, please give some advice. Also, is there any other distribution that I can use to fit this data more properly?

Accepted Answer

Thiago Henrique Gomes Lobato
You can estimate a continious function from your data using ksdensity and then fit two gaussians on it. My answer for the following questions does this . For sake of breviety, here is the copied code from the answer:
[f,xi] = ksdensity(x); % x is your data
% Here I generate a function from two Gaussians and output
% the rms of the estimation error from the values obtained from ksdensity
fun = @(xx,t,y)rms(y-(xx(5)*1./sqrt(xx(1)^2*2*pi).*exp(-(t-xx(2)).^2/(2*xx(1)^2))+...
xx(6)*1./sqrt(xx(3)^2*2*pi).*exp(-(t-xx(4)).^2/(2*xx(3)^2)) ) );
% Get the parameters with the minimum error. To improve convergence,choose reasonable initial values
[x,fval] = fminsearch(@(r)fun(r,xi,f),[0.2,4.7,0.2,5.3,0.5,0.5]);
% Make sure sigmas are positive
x([1,3]) = abs(x([1,3]));
% Generate the Parametric functions
pd1 = makedist('Normal','mu',x(2),'sigma',x(1));
pd2 = makedist('Normal','mu',x(4),'sigma',x(3));
% Get the probability values
y1 = pdf(pd1,xi)*x(5); % x(5) is the participation factor from pdf1
y2 = pdf(pd2,xi)*x(6); % x(6) is the participation factor from pdf2
% Plot
hold on;
legend({'ksdensity',['\mu : ',num2str(x(2)),'. \sigma :',num2str(x(1))],...
['\mu : ',num2str(x(4)),'. \sigma :',num2str(x(3))],'pdf1+pdf2'})
Walter Roberson
Walter Roberson on 1 Jul 2020
I think I might still suggest fminsearch() first, using the value from the previous search, and then fmincon() to narrow down more precisely
You might also want to look in the File Exchange as John D'Errico has posted fminsearchbnd for bounded search.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 28 Jun 2020
I've attached code, fit_two_Gaussians.m, to find two Gaussians with a slope in the x direction (to give a slightly better fit). Replace the demo (x,y) with your (x,y) and it will fit your data.
I'm also attaching a demo that fits any number of Gaussians to the data. The demo uses 6 Gaussians but you can tell it how many you want it to find.
Image Analyst
Image Analyst on 30 Jun 2020
What you call x is the dependent variable. So in your case the independent variable would simply be the index. So to make your data be x,y, you'd do
y = x; % Make a copy of the x variable in a variable called y.
x = 1 : length(y); % The x, or independent variable, is simply the index number.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by