필터 지우기
필터 지우기

Using nlinfit to fit a Gaussian pdf to x,y paired data

조회 수: 8 (최근 30일)
Doug Barrett
Doug Barrett 2013년 11월 28일
댓글: Doug Barrett 2013년 12월 2일
I am trying to use Matlab's nlinfit function to estimate the best fitting Gaussian pdf for x,y paired data. In this case, x is a range of 2D orientations and y is the probability of a "yes" response in a yes-no task.
I have borrowed the form for a Gaussian (@norm_funct) from relevant posts and I'd like to estimate the best fitting pdf to obtain its mean and standard deviation. At the moment, the fitted function appears to be incorrectly scaled and is not smooth (see plots)
The figure below, which was produced using Prism to fit 2 pdfs, is what I'd like to replicate in Matlab - many thanks
x = -30:5:30;
y = [0,0.20,0.05,0.15,0.65,0.85,0.88,0.80,0.55,0.20,0.05,0,0;];
% plot raw data
figure(1)
plot(x, y, ':rs');
axis([-35 35 0 1]);
% initial paramter guesses (based on plot)
initGuess(1) = max(y); % amplitude
initGuess(2) = 0; % mean centred on 0 degrees
initGuess(3) = 10; % SD in degrees
% equation for Gaussian distribution
norm_func = @(p,x) p(1) .* exp(-((x - p(2))/p(3)).^2);
% use nlinfit to fit Gaussian using Least Squares
[bestfit,resid]=nlinfit(y, x, norm_func, initGuess);
% plot function
xFine = linspace(-30,30,100);
figure(2)
plot(x, y, 'ro', x, norm_func(xFine, y), '-b');
  댓글 수: 1
Image Analyst
Image Analyst 2013년 11월 28일
편집: Image Analyst 2013년 11월 28일
Should I delete your apparent duplicate http://www.mathworks.com/matlabcentral/answers/107961-using-nlinfit-to-fit-a-gaussian-pdf-to-x-y-paired-data? Or are they really different questions? Why don't we have just one thread so you don't have to look in two different places for answers?

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

답변 (1개)

Matt J
Matt J 2013년 11월 29일
편집: Matt J 2013년 11월 29일
Instead of
plot(x, y, 'ro', x, norm_func(xFine, y), '-b');
I think you want
plot(x, y, 'ro', xFine, norm_func(bestfit,xFine), '-b');
From this, I obtain the following (but from fminsearch, not nlinfit), and it looks fine to me,
  댓글 수: 3
Matt J
Matt J 2013년 11월 29일
편집: Matt J 2013년 11월 29일
Doug,
Here's how I modified your original code to work with fminsearch. However, I wouldn't conclude that fminsearch is preferable to nlinfit. Fminsearch is, in fact, a rather ad hoc and less robust solver. The only reason I used it instead of nlinfit is because I don't have the Stats Toolbox.
x = -30:5:30;
y = [0,0.20,0.05,0.15,0.65,0.85,0.88,0.80,0.55,0.20,0.05,0,0;];
% plot raw data
figure(1)
plot(x, y, ':rs');
axis([-35 35 0 1]);
% initial paramter guesses (based on plot)
initGuess(1) = .9*max(y); % amplitude
initGuess(2) = 0; % mean centred on 0 degrees
initGuess(3) = 10; % SD in degrees
% equation for Gaussian distribution
norm_func = @(p,x) p(1) .* exp(-((x - p(2))/p(3)).^2);
% use nlinfit to fit Gaussian using Least Squares
[bestfit,resid]=fminsearch(@(p) norm(norm_func(p,x)-y), initGuess);
% plot function
xFine = linspace(-30,30,1000);
figure(2)
plot(x, y, 'ro', xFine, norm_func(bestfit,xFine), '-b');
Doug Barrett
Doug Barrett 2013년 12월 2일
Hi Matt,
Thanks for that, it's very neat. I'll look into the nlinfit more now I have a better idea of how to specify and call the function - I might look at 'mle' too (statstoolbox), as it looks as though I could use a binomial function to fit the accracy data and specify upper and lower bounds for the estimates.
All the best,
Doug.

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

Community Treasure Hunt

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

Start Hunting!

Translated by