Is fit() deterministic when StartPoint is specified?
Mostrar comentarios más antiguos
Hi! Running the same script on the same data I got slightly different coefficients. Since StartPoint is always specified, I expected deterministic results. Is this expected behavior, and what could cause it? Here following the snippet I use:
% Power-law fit (log-log space)
ft = fittype('log(a) + n*log(x)', 'dependent', 'y', 'independent', 'x');
[fitresult, gof] = fit(x', log(y)', ft, 'Startpoint', [1 -1]);
% Exponential fit with weights
w = 1./y.^2;
ft = fittype('a*exp(b*x) + c', 'independent', 'x');
[fitresult, gof] = fit(x', y', ft, 'Startpoint', [110 -0.03 2], 'weights', w);
If useful: MATLAB Version: 24.2.0.2923080 (R2024b) Update 6
3 comentarios
John D'Errico
hace alrededor de 2 horas
Editada: John D'Errico
hace alrededor de 2 horas
If you have a specific example that yields varying results, please show it with the data, so we can go more deeply.
The example you show has two TOTALLY different models to be fit, one with weights, one not, so I'm not sure what your examples signify.
Not sure why you don't use the built-in fittypes 'poly1' and 'exp2'
% Power-law fit (log-log space)
[fitresult, gof] = fit(log(x)', log(y)', 'poly1');
% Exponential fit with weights
w = 1./y.^2;
lb=[-inf,-inf,-inf,0];
[fitresult, gof] = fit(x', y', 'exp2', Weights=w,Lower=lb, Upper=-lb);
Respuestas (2)
John D'Errico
hace alrededor de 15 horas
Editada: John D'Errico
hace alrededor de 15 horas
If the start point is specified, then yes, FIT should be deterministic. At that point there is no reason for any pseudo-random numbers to be involved. Two calls will be perfectly reflected, down to the LSB.
In the case of purely linear models, and FIT knows them to be linear, then FIT wil use the standard linear algebraic methods. These are perfectly deterministic, as no starting values are ever employed.
In the case of a nonlinear model, then again, the algorithms employed by FIT are well understood, and IF you fully specify the starting values, then FIT should be deterministic.
HOWEVER!!!!!!! I can see at least one caveat in that claim. There may be floating point issues. For exmple, if you run the same problem on two different machines, and they have a different number of cores, AND there was reason for the computations to become multi-threaded, then I would expect to see floating point trash appear due to a different order of computations, spread out between a different number of cores. And since it can often be the case that a nonlinear estimation is ill-posed, then that floating point trash will become amplified in your result.
Of course, there may be other issues. If FIT could be forced to use a tool like GA as the solver, then all bets are off. So I did a quick check through the options for fit. Algorithms like the interior point method and Levenberg-Marquardt are both going to be deterministic. I see nothing in the various options that would allow any variation, except for the case I have already mentioned. (I may have missed something of course, but I don't think so.)
For example:
x = randn(20,1);
y = 1 + 3*exp(x/2) + randn(size(x))/10;
w = 1./y.^2;
ft = fittype('a*exp(b*x) + c', 'independent', 'x');
format long g
[fitresult, gof] = fit(x, y, ft, 'Startpoint', [1.5 1.5 1.5], 'weights', w)
abc1 = [fitresult.a,fitresult.b,fitresult.c]
[fitresult, gof] = fit(x, y, ft, 'Startpoint', [1.5 1.5 1.5], 'weights', w)
abc2 = [fitresult.a,fitresult.b,fitresult.c]
isequal(abc1,abc2)
As expected, the two results are identical down to the LSB.
So if you have an example where this fails to be true, then you need to show it, AND provide the data.
dpb
hace alrededor de 3 horas
0 votos
The "Algorithms" section of fit notes that "If the fit type expression input is a character vector, string scalar, or anonymous function, then the toolbox uses a nonlinear fitting algorithm to fit the model to data."
"If the fit type expression input is a cell array or string array of terms, then the toolbox uses a linear fitting algorithm to fit the model to data."
Since you've given the char() vector description the nonlinear fitting techniques will be called and they are reandomized so the starting points will be somewhat different.
1 comentario
Categorías
Más información sobre Get Started with Curve Fitting Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!