Prev: Uimenu Icon
Next: colorbarf, pcolor plot
From: olsonaj on 14 May 2010 15:44 Tom, That makes things a little clearer for me as to the intended purpose of gmdistribution. I see now that my application is a bit different then it intends, but maybe it will still work. I have the density distribution curve that is a Gaussian bimodal in form (and I don't have raw data, as I'm using another program to generate the density distribution at each point along the line given by plotGx). Is there a good way to fit bimodal density distribution Gaussian curves with Matlab? (I've only been able to find <a href="http:// www.mathworks.com/matlabcentral/fileexchange/11733-gaussian-curve-fit" title="http://www.mathworks.com/matlabcentral/fileexchange/11733- gaussian-curve-fit">Gaussian Curve Fit</a> but that only does uni- modal curves). My data is on pastebin http://pastebin.com/B4SasuQP. Thanks!
From: Tom Lane on 14 May 2010 17:07 > Is there a good way to fit bimodal density distribution Gaussian > curves with Matlab? (I've only been able to find <a href="http:// > www.mathworks.com/matlabcentral/fileexchange/11733-gaussian-curve-fit" > title="http://www.mathworks.com/matlabcentral/fileexchange/11733- > gaussian-curve-fit">Gaussian Curve Fit</a> but that only does uni- > modal curves). First check out the link that I sent earlier. That fits a Weibull density to some x/y data. Then play around with this, and see if you can adapt it to your problem: Get some bimodal data. >> x = [randn(100,1); 10+randn(100,1)]; Make a histogram out of it. Divide by the product of the bin width (here, 1) and the number of points (200) so the result is a real density (integrates to 1). >> [a,b] = hist(x,-4:14); a=a/200; bar(b,a) Create a function that represents a binomodal distribution. Here I have a mixture of two normals. The parameters are, in order: [mixture proportion, mean1, log of stdev1, mean2, log of stdev2] >> f = >> @(p,x)p(1)*normpdf(x,p(2),exp(p(3)))+(1-p(1))*normpdf(x,p(4),exp(p(5))); Fit using nlinfit or your favorite: >> nlinfit(b,a,f,[.5 0 0 10 0]) ans = 0.5015 -0.2394 -0.0243 10.0385 0.0947 You may notice I gave it excellent starting values -- the known parameter values. Some things to watch out for are the standard deviation going negative (I used log(sigma) to prevent that), the mixture proportion going out of range (did not protect against that), and poor starting values (the fit may have a better chance if you make the sigma values larger rather than small). Good luck. -- Tom
From: olsonaj on 17 May 2010 00:15
Thanks! That's very helpful and clarifying. Following the way you detailed (after a bit of moving the starting values around) worked. -Abe On May 14, 5:07 pm, "Tom Lane" <tl...(a)mathworks.com> wrote: > > Is there a good way to fit bimodal density distribution Gaussian > > curves with Matlab? (I've only been able to find <a href="http:// > >www.mathworks.com/matlabcentral/fileexchange/11733-gaussian-curve-fit" > > title="http://www.mathworks.com/matlabcentral/fileexchange/11733- > > gaussian-curve-fit">Gaussian Curve Fit</a> but that only does uni- > > modal curves). > > First check out the link that I sent earlier. That fits a Weibull density to > some x/y data. Then play around with this, and see if you can adapt it to > your problem: > > Get some bimodal data. > > >> x = [randn(100,1); 10+randn(100,1)]; > > Make a histogram out of it. Divide by the product of the bin width (here, 1) > and the number of points (200) so the result is a real density (integrates > to 1). > > >> [a,b] = hist(x,-4:14); a=a/200; bar(b,a) > > Create a function that represents a binomodal distribution. Here I have a > mixture of two normals. The parameters are, in order: > [mixture proportion, mean1, log of stdev1, mean2, log of stdev2] > > >> f = > >> @(p,x)p(1)*normpdf(x,p(2),exp(p(3)))+(1-p(1))*normpdf(x,p(4),exp(p(5))); > > Fit using nlinfit or your favorite:>> nlinfit(b,a,f,[.5 0 0 10 0]) > > ans = > 0.5015 -0.2394 -0.0243 10.0385 0.0947 > > You may notice I gave it excellent starting values -- the known parameter > values. Some things to watch out for are the standard deviation going > negative (I used log(sigma) to prevent that), the mixture proportion going > out of range (did not protect against that), and poor starting values (the > fit may have a better chance if you make the sigma values larger rather than > small). > > Good luck. > > -- Tom |