# Adapt Nonlinear Model Fits' PrecisionGoal and AccuracyGoal dynamically

The setting: I have some data of the form

data=Table[{{x,y},Error[y]}]


with a analytic fit function

FQQ[A_,M_,R_,c_]:=A*Exp[-M*R]/R+c;


for which I'd like to perform a Non Linear Model Fit of the form

Fitdata[beg_,end_]:=NonlinearModelFit[
Table[data[[i]][[1]],{i,beg+1,Length[data]-end}],
FQQ[A,M,R,c],{A,M,c},R,
Weights -> 1/Table[(data[[i]][[2]][[1]])^2,{i,beg+1,Length[data]-end}]
];


Note that Fitdata[0,0] includes all data points in data and non zero values cut the beginning or the end of the data set, respectively.

I'm now interested in the fit parameters as functions of the cutting.

What I tried untill now is

Table[{beg,end,A/.Fitdata[beg,end]["BestFitParameters"],
M/.Fitdata[beg,end]["BestFitParameters"],c/.Fitdata[beg,end]["BestFitParameters"]},
{beg,0,Length[data]-3,1},{end,0,Length[data]-3,1}]


which should generate a table of the form

Table{beg,end,A,M,c}


which I'd like to plot in three difeerent 3D-plots later on.

EDIT: I'm obviously an idiot... I cut too much, which lead to too few data points being available.

Table[{beg,end,A/.FitWLCV1H00[beg,end]["BestFitParameters"],
M/.FitWLCV1H00[beg,end]["BestFitParameters"],
c/.FitWLCV1H00[beg,end]["BestFitParameters"]},
{beg,0,Length[formatedDataTrimmedWLCV1H00]-3,1},
{end,0,Length[formatedDataTrimmedWLCV1H00]-beg-3,1}]


accounts for a lot of my problems.

Now I'm "only" left with error messages of the type

NonlinearModelFit::sszero: The step size in the search has become less than the tolerance prescribed by the PrecisionGoal option, but the gradient is larger than the tolerance specified by the AccuracyGoal option. There is a possibility that the method has stalled at a point that is not a local minimum.
NonlinearModelFit::cvmit: Failed to converge to the requested accuracy or precision within 100 iterations.


Any chance to adapt them on the fly? Hence, is it possible to alter PrecisionGoal and AccuracyGoal only if the fit does not converge? If so, what would be good values?

EDIT: As requested by @JimB, here's a example of the data that I'm working with. It is by far the smalles set. Others contain up to 1000 Points in a similar range.

{{{0.239975, 2.66302}, ErrorBar[0.00326151]},
{{0.358325, 2.33694}, ErrorBar[0.00308647]},
{{0.446625, 1.86911}, ErrorBar[0.0042742]},
{{0.485025, 1.64511}, ErrorBar[0.00425176]},
{{0.556175, 1.24764}, ErrorBar[0.00408676]},
{{0.6162, 0.956111}, ErrorBar[0.00515167]},
{{0.706675, 0.624671}, ErrorBar[0.00478222]},
{{0.7487, 0.501506}, ErrorBar[0.00338924]},
{{0.78755, 0.392851}, ErrorBar[0.00410899]},
{{0.82845, 0.34129}, ErrorBar[0.00536362]},
{{0.86895, 0.265029}, ErrorBar[0.00623553]},
{{0.900475, 0.217144}, ErrorBar[0.00404639]},
{{0.93585, 0.172457}, ErrorBar[0.0043178]},
{{0.997625, 0.117752}, ErrorBar[0.0046043]},
{{1.02973, 0.111512}, ErrorBar[0.00480712]},
{{1.0595, 0.098667}, ErrorBar[0.00455822]},
{{1.08963, 0.0603218}, ErrorBar[0.00519334]},
{{1.1166, 0.0835568}, ErrorBar[0.00408133]}}

• What do you mean by "no fit" ? Is there a lack of convergence? Are there value of the parameter estimates that result in imaginary numbers? Maybe better starting values, restrictions on parameters, and increasing the MaxIterations might resolve all of the issues. – JimB Oct 16 '17 at 15:40
• @JimB I added the exact error message. Additionally there's a bunch of sszero messages around that I have never heard of and am unable to find information about. I'll take a look at your suggested MaxIterations approach. – gothicVI Oct 17 '17 at 6:29
• If your desire is to reduce accuracy or precision on the fly so that one obtains convergence, I don't think that's a good idea. I would certainly want to see if increasing the number of iterations achieves convergence. Also, lack of quick convergence might be because of the parameter estimators being highly correlated or the model is nearly unidentifiable given the data (of which highly correlated parameter estimators is a symptom). To give any more definite advice, I think you'd need to share at least a subset of your data. – JimB Oct 17 '17 at 17:48
• @JimB Thanks for your reply. I do not want to decrease but to increase precision/accuracy etc. as needed. However, I'd like to avoid doing it globally in order to keep the computation time as short as possible. Thus my desired goal would be to dynamically increase said parameters if and only if it is neede. However, I'm not aware if that is possible at all. I'll share a sample above. – gothicVI Oct 18 '17 at 7:35

I don't think that increasing the precision of the calculations will achieve what you want to achieve. You simply need to remove fewer points and increase the number of MaxIterations.

Increasing MaxIterations to 5000 and keeping at least 4 points will allow all regressions to converge (although still with the "step size" warning). As you have 4 parameters A, M, c, and the error variance, you certainly wouldn't want fewer than 4!)

If you want to get rid of all of the "step size" warnings, then for this particular dataset, you'll need to keep at least 13 points. Here is some code for doing so:

data = {{{0.239975, 2.66302}, ErrorBar[0.00326151]}, {{0.358325, 2.33694}, ErrorBar[0.00308647]},
{{0.446625, 1.86911}, ErrorBar[0.0042742]}, {{0.485025, 1.64511}, ErrorBar[0.00425176]},
{{0.556175, 1.24764}, ErrorBar[0.00408676]}, {{0.6162, 0.956111}, ErrorBar[0.00515167]},
{{0.706675, 0.624671}, ErrorBar[0.00478222]}, {{0.7487, 0.501506}, ErrorBar[0.00338924]},
{{0.78755, 0.392851}, ErrorBar[0.00410899]}, {{0.82845, 0.34129}, ErrorBar[0.00536362]},
{{0.86895, 0.265029}, ErrorBar[0.00623553]}, {{0.900475, 0.217144}, ErrorBar[0.00404639]},
{{0.93585, 0.172457}, ErrorBar[0.0043178]}, {{0.997625, 0.117752}, ErrorBar[0.0046043]},
{{1.02973, 0.111512}, ErrorBar[0.00480712]}, {{1.0595, 0.098667}, ErrorBar[0.00455822]},
{{1.08963, 0.0603218}, ErrorBar[0.00519334]}, {{1.1166, 0.0835568}, ErrorBar[0.00408133]}};

(* Turn data into nice triplets *)
data = Flatten[{#[[1]], #[[2]]} /. ErrorBar[x_] -> x] & /@ data

FQQ[A_, M_, R_, c_] := A*Exp[-M*R]/R + c;

Fitdata[beg_, end_] :=
NonlinearModelFit[data[[beg + 1 ;; Length[data] - end, {1, 2}]],
FQQ[A, M, R, c], {A, M, c}, R,
Weights -> 1/data[[beg + 1 ;; Length[data] - end, 3]]^2,
MaxIterations -> 5000];

t = Flatten[Table[{beg, end, A, M, c} /. Fitdata[beg, end]["BestFitParameters"],
{beg, 0, Length[data] - 13, 1},
{end, 0, Length[data] - beg - 13, 1}], 1];
t // TableForm


• Thank you so much! – gothicVI Oct 19 '17 at 10:45