curve fit gaussian CDF

10 views (last 30 days)
Budi Mulyanto
Budi Mulyanto on 23 Aug 2022
Edited: Torsten on 23 Aug 2022
I have 20 data points that follow gaussian CDF and would like to find its mean and standard deviation.
If I understand it correctly, normfit is for gaussian PDF.
So, basically, I am looking for a command like normfit but for gaussian CDF.
I did it in Python but could not find a way to do this in matlab.
The picture below is the result from Python.
y = [ 0.0010 0 0.0020 0.0060 0.0210 0.0400 0.0840 0.1890 0.2790 0.4500 0.6180 0.7550 0.8790 0.9330 0.9770 0.9940 0.9980 1.0000 1.0000 1.0000]
x = [0.3100 0.3115 0.3130 0.3145 0.3160 0.3175 0.3190 0.3205 0.3220 0.3235 0.3250 0.3265 0.3280 0.3295 0.3310 0.3325 0.3340 0.3355 0.3370 0.3385]
And this is the result with curve fit app in matlab.

Accepted Answer

Star Strider
Star Strider on 23 Aug 2022
Edited: Star Strider on 23 Aug 2022
Consider using normcdf with mle or fitnlm.
Try this —
y = [ 0.0010 0 0.0020 0.0060 0.0210 0.0400 0.0840 0.1890 0.2790 0.4500 0.6180 0.7550 0.8790 0.9330 0.9770 0.9940 0.9980 1.0000 1.0000 1.0000];
x = [0.3100 0.3115 0.3130 0.3145 0.3160 0.3175 0.3190 0.3205 0.3220 0.3235 0.3250 0.3265 0.3280 0.3295 0.3310 0.3325 0.3340 0.3355 0.3370 0.3385];
xv = linspace(min(x), max(x));
ncdf = @(b,x) normcdf(x,b(1),b(2));
ncdfmdl = fitnlm(x,y,ncdf,[0.32 0.01])
ncdfmdl =
Nonlinear regression model: y ~ normcdf(x,b1,b2) Estimated Coefficients: Estimate SE tStat pValue _________ __________ ______ __________ b1 0.32394 3.0203e-05 10725 1.0431e-62 b2 0.0036247 4.2714e-05 84.859 6.9165e-25 Number of observations: 20, Error degrees of freedom: 18 Root Mean Squared Error: 0.00688 R-Squared: 1, Adjusted R-Squared 1 F-statistic vs. zero model: 9.36e+04, p-value = 7.04e-37
B = ncdfmdl.Coefficients.Estimate;
figure
plot(x, y, 'p')
hold on
plot(xv, ncdf(B,xv), '-r')
hold off
grid
.
  2 Comments
Budi Mulyanto
Budi Mulyanto on 23 Aug 2022
Thank you! It works. Is there any idea why it is more straight forward in Python?
Star Strider
Star Strider on 23 Aug 2022
As always, my pleasure!
Is there any idea why it is more straight forward in Python?
I have no experience with Python, so I can only venture a guess. It may be that Python is relatively young and evolved in a different environment, while MATLAB has been around for several decades (my first experience with it was in 1992 and it was not new then) and so it needs to be backward-compatible with legacy code.
.

Sign in to comment.

More Answers (1)

Torsten
Torsten on 23 Aug 2022
Edited: Torsten on 23 Aug 2022
ydata = [ 0.0010 0 0.0020 0.0060 0.0210 0.0400 0.0840 0.1890 0.2790 0.4500 0.6180 0.7550 0.8790 0.9330 0.9770 0.9940 0.9980 1.0000 1.0000 1.0000];
xdata = [0.3100 0.3115 0.3130 0.3145 0.3160 0.3175 0.3190 0.3205 0.3220 0.3235 0.3250 0.3265 0.3280 0.3295 0.3310 0.3325 0.3340 0.3355 0.3370 0.3385];
fun = @(mu,sigma) 0.5*(1+erf((xdata-mu)/(sqrt(2)*sigma)));
fun1 = @(mu,sigma) fun(mu,sigma) - ydata;
sol = lsqnonlin(@(x)fun1(x(1),x(2)),[0.325,0.005]);
Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
mu = sol(1)
mu = 0.3239
sigma = sol(2)
sigma = 0.0036
plot(xdata,ydata)
hold on
plot(xdata,fun(mu,sigma))
  2 Comments
Budi Mulyanto
Budi Mulyanto on 23 Aug 2022
Thank you! your solution also works. But the other answer came slightly earlier. Thank you once again.
Torsten
Torsten on 23 Aug 2022
Edited: Torsten on 23 Aug 2022
No problem. And although the results are equal, Star Strider's solution has a strong statistical foundation.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!