Fit two peak model

조회 수: 144 (최근 30일)
cliffy
cliffy 2020년 6월 28일
댓글: Walter Roberson 2020년 7월 1일
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?

채택된 답변

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020년 6월 28일
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 https://de.mathworks.com/matlabcentral/answers/482688-get-parameters-of-gaussian-distributions-from-ksdensity-function#answer_393989?s_tid=prof_contriblnk . 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
figure
plot(xi,f);
hold on;
plot(xi,y1);
plot(xi,y2);
plot(xi,y2+y1);
legend({'ksdensity',['\mu : ',num2str(x(2)),'. \sigma :',num2str(x(1))],...
['\mu : ',num2str(x(4)),'. \sigma :',num2str(x(3))],'pdf1+pdf2'})
  댓글 수: 5
cliffy
cliffy 2020년 6월 30일
편집: cliffy 2020년 7월 1일
Thank you very much! You just mentioned to find a "nearby value". If the histograms of the the sets of my data are all very similar, do you think applying fmincon() with a known "nearby value" could help solve the problem?
Walter Roberson
Walter Roberson 2020년 7월 1일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 6월 28일
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.
  댓글 수: 4
cliffy
cliffy 2020년 6월 30일
Ok, I got what you mean. I probably didn't make it clear. I don't have y data, all I have is just the x data and the histogram of it. Should I use the frequency of x data to replace y?
Image Analyst
Image Analyst 2020년 6월 30일
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by