Fitting Cumulative Gaussian Function to data

Hello ,
I am trying to fit the cumulative Gaussian Function to my data points, to find out the PSE. So far I used this function:
f = @(b,x) normcdf(x, b(1), b(2)); % Objective Function
NRCF = @(b) norm(y - f(b,x)); % Norm Residual Cost Function
B1 = fminsearch(NRCF, [-1; 5]); % Estimate Parameters
x = 0:5
y = [0.7 0.4 0.2 0.4 0.6 0.9]
I am getting: 1.91293286
However, the correct value should be: 1.81051. Does anyone have ideas? Is there a better way to fit the cumulative function to my data in Matlab?
Thank you for your help :)

1 Comment

After I posted my answer, I happened to notice that you already had a post about this question here. @Matt J also pointed out that the model is a very poor one for your data.
Also, what is "PSE"? I don't recognize that in this context.

Sign in to comment.

 Accepted Answer

I get the same coefficient that you did, using the fitnlm function instead.
This seems to be a terrible model for your data, which is obvious when you plot them. (Always plot the data!) Why did you choose it?
f = @(b,x) normcdf(x, b(1), b(2));
x = 0:5;
y = [0.7 0.4 0.2 0.4 0.6 0.9];
mdl = fitnlm(x,y,f,[1 1])
mdl =
Nonlinear regression model: y ~ normcdf(x,b1,b2) Estimated Coefficients: Estimate SE tStat pValue ________ ______ _______ _______ b1 1.9129 2.238 0.85476 0.44086 b2 7.8396 9.9659 0.78664 0.47548 Number of observations: 6, Error degrees of freedom: 4 Root Mean Squared Error: 0.259 R-Squared: 0.143, Adjusted R-Squared -0.0709 F-statistic vs. zero model: 13, p-value = 0.0177
xq = (-2 : 0.1 : 8)';
yq = predict(mdl,xq);
figure
hold on
hd = plot(x,y,'.');
set(hd,'MarkerSize',24)
hf = plot(xq,yq);
set(hf,'LineWidth',2)
ylim([0 1])
legend([hd,hf],{'data','fit'},'Location','SouthWest')

4 Comments

Hello,
thank you for your quick answer and your time! That is an interesting model you propose here.
If I see it correctly you firstly used the gaussian function and then introduced another fit parameter? Afterwards you introduced a prediction model? I tried to use this model for my other data, but for some it did not work.
It is true, I asked this question already. However, I had some confusing about the paramters, so the answer was not quite satisfying for me. Hence, I wanted to acknowdlege the work the other person did.
e.g. y = 0.100000000000000 0 0 1 1 1
Error using nlinfit>checkFunVals (line 649)
The function you provided as the MODELFUN input has returned Inf or NaN values.
Error in nlinfit>LMfit (line 596)
if funValCheck && ~isfinite(sse), checkFunVals(r); end
Error in nlinfit (line 284)
[beta,J,~,cause,fullr] = LMfit(X,yw, modelw,beta,options,verbose,maxiter);
Error in NonLinearModel/fitter (line 1127)
nlinfit(X,y,F,b0,opts,wtargs{:},errormodelargs{:});
Error in classreg.regr.FitObject/doFit (line 94)
model = fitter(model);
Error in NonLinearModel.fit (line 1446)
model = doFit(model);
Error in fitnlm (line 99)
model = NonLinearModel.fit(X,varargin{:});
I'm a little confused by your remark, "That is an interesting model you propose here."
The model is exactly the same as in your original question. I just used a different MATLAB function to find the best fit. There is no "extra" fitting parameter. Your code reports the two coefficients, just the same:
x = 0:5
x = 1×6
0 1 2 3 4 5
y = [0.7 0.4 0.2 0.4 0.6 0.9];
f = @(b,x) normcdf(x, b(1), b(2)); % Objective Function
NRCF = @(b) norm(y - f(b,x)); % Norm Residual Cost Function
B1 = fminsearch(NRCF, [-1; 5]) % Estimate Parameters
B1 = 2×1
1.9129 7.8395
And then, since this is a model that fits a model of the form f(x) = y, all I do in the last part is create a sequence of uniformly space x values, and find the "predicted" y values given by the fitted equation. You could have done that same with your output. Here is a tiny example:
xq = [-1 0 1];
yq = f(B1,xq)
yq = 1×3
0.3551 0.4036 0.4536
I still don't understand why you are trying to fit this function to the data points, which are shaped nothing like a CDF.
Ah, its true. Basically you also get the same value with 1.91 and not 1.81.
I have a data set for whom I would like calculate psychometric functions. With 80% of my data this works fine, but for some data points I get different values compared to fitiing with CDF in other programs. This counts especially for data that also do not really look like CDF (like the example I posted). That is why I thought I am not using a good model right now in Matlab and there would be possibilities to change the fit.

Sign in to comment.

More Answers (0)

Asked:

on 19 Mar 2021

Commented:

on 22 Mar 2021

Community Treasure Hunt

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

Start Hunting!