# Is it normal that adding constraints on the parameters of NonlinearModelFit increases execution time 100 times??

Normally, reducing the optimization domain by adding constraints should reduce the time of execution. In my case however, this puts the NonlinearModelFit in a coma. Without constraints, 8 seconds suffice:

pobTotal = 4950738;
mu = 7*41157/365 // N(* weekly births *);
d = mu/pobTotal;
reported = {107, 135, 612, 195, 626, 619, 491, 1164, 1137, 511, 1036,
1144, 2650, 3162, 6074, 6693, 8253, 6639, 6148, 4345, 3141, 1958,
1130, 484, 356, 296, 195, 121, 208, 101, 67, 128, 20};
T = Length[reported ];
mod[\[Gamma]_?NumericQ, \[Beta]_?NumericQ, s0_?NumericQ,
i0_?NumericQ] :=
NDSolveValue[{s'[t] ==
mu - (\[Beta]*s[t]*i[t]/pobTotal) - d*s[t],
i'[t] == (\[Beta]*s[t]*i[t]/pobTotal) - (\[Gamma] + d)*i[t],
s[0] == s0,
i[0] == i0}, {s, i}, {t, 0, 33}, MaxStepSize -> 400][[2]];

Timing[nlm =
NonlinearModelFit[
data, { p*mod[\[Gamma], \[Beta], s0, i0][t]}, {\[Gamma], \[Beta],
s0, i0, p}, t]] // Quiet

{ \[Gamma]2, \[Beta]2, s02, i02,
p2} = { \[Gamma], \[Beta], s0, i0, p} /. nlm["BestFitParameters"]


With constraints:

    Timing[nlm2 =
NonlinearModelFit[
data, {p* mod[\[Gamma], \[Beta], s0, i0][t], 1 < \[Gamma] < 1.4 ,
0 < \[Beta], s0 > 1, 1 < i0, 0 < p},
{\[Gamma], \[Beta], s0, i0, p}, t]] // Quiet
{\[Gamma]3, \[Beta]3, s03, i03,
p3} = {\[Gamma], \[Beta], s0, i0, p} /. nlm2["BestFitParameters"]


the execution times gets multiplied by a 100 (900 secs). How to explain this; is it possible to reduce this time?

• Possibly this forced use of a different (slower) optimizer. Also you might want to see if changing from strong to weak inequalities makes a difference. Feb 15 at 14:41
• Dear Daniel Changing the inequalities did not help. I do not understand what you mean by "forced use of a different (slower) optimizer". You mean adding constraints forces a change to a different (default) optimizer? Feb 15 at 18:45
• A constrained optimization algorithm might need more function evaluations if e.g. it is using a penalty method, or at least in checking if the constraints are not violated. Feb 15 at 21:00
• Not all optimizers can handle constraints. I was thinking possibly the default might be a quasi-Newton variant and the constrained case might use (typically slower) nonlinear interior point. Feb 16 at 2:48

This concerns the set of equations in your previous questions and those equations impose difficulties with optimizing routines because of the high correlations among the parameter estimators.

Because of those difficulties, if restricting $$\gamma$$ to a specific range is of interest, it is probably better to set specific values of $$\gamma$$ and use "good" starting values.

For the particular restrictions it appears that the "best" value of $$\gamma$$ is at the border (which is not atypical). Below is a plot of the root mean square error (rmse) for various values of $$\gamma$$ in the restricted range:

rmse = Table[{γ0, (NonlinearModelFit[data, {p*mod[γ0, β, s0, i0][t]},
{{β, 100}, {s0, 86760.5}, {i0, 1.93365}, {p, 1.15771}}, t] // Quiet)
["EstimatedVariance"]^0.5}, {γ0, 1.1, 1.4, 0.01}];
ListPlot[rmse]


So I would "blame" your particular equations and data rather than the addition of restrictions to NonlinearModelFit. (That's not at all to say your equations and data are wrong. You're just dealing with a difficult situation.)