3
$\begingroup$

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};
data = Thread[{Range[1, 33], reported}];
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?

$\endgroup$
4
  • 2
    $\begingroup$ 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. $\endgroup$ Feb 15 at 14:41
  • $\begingroup$ 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? $\endgroup$
    – florin
    Feb 15 at 18:45
  • 3
    $\begingroup$ 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. $\endgroup$ Feb 15 at 21:00
  • 2
    $\begingroup$ 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. $\endgroup$ Feb 16 at 2:48

1 Answer 1

3
$\begingroup$

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]

Plot of root mean square errors for various values of gamma

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.)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.