4
$\begingroup$

I am trying to find out how many function, gradient and hessian evaluations are made when i run NonlinearModelFit for example in this case

data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}, {6, 4}, {7, 5}};
nlm = NonlinearModelFit[data, Log[a + b x^2], {a, b}, x, Method-> "Newton"]

So far i have only managed to know, what i assume are, the function evaluations

Block[{c = 0}, {nlm = 
NonlinearModelFit[data, Log[a + b x^2], {a, b}, x, 
Method -> "Newton", EvaluationMonitor :> c++], c}]

Thanks

$\endgroup$
2
  • $\begingroup$ Trace[nlm = NonlinearModelFit[data, Log[a + b x^2], {a, b}, x, Method-> "Newton"],Log[__]] $\endgroup$
    – chris
    Commented Aug 13, 2015 at 9:52
  • 3
    $\begingroup$ Take a look at the FindMinimum documentation on how to use EvaluationMonitor and StepMonitor. Also look at the tutorial on unconstrained and constrained optimization (Tutorials at top of relevant doc pages) and the FindMinimumPlot functions from ExtraPackages -> Optimization -> UnconstrainedProblems.m (this is used in the tutorials). $\endgroup$
    – Szabolcs
    Commented Aug 13, 2015 at 9:54

2 Answers 2

4
$\begingroup$

Following Szabolcs advice, it seems that the Documentation page for EvaluationMonitor contains all what you need for Method -> "Newton":

data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}, {6, 4}, {7, 5}};

Clear[evalCount];
evalCount[_] = 0;
nlm = NonlinearModelFit[data, Log[a + b x^2], {a, b}, x, 
   EvaluationMonitor :> ++evalCount["Function"], 
   Gradient -> {"Symbolic", EvaluationMonitor :> ++evalCount["Gradient"]}, 
   Method -> {"Newton", 
     "Hessian" -> {"Symbolic", EvaluationMonitor :> ++evalCount["Hessian"]}}];
TableForm[evalCount /@ #, TableHeadings -> {#, None}] &@{"Function", "Gradient", 
  "Hessian"}

table

Other Methods take different suboptions, for example "LevenbergMarquardt":

Clear[evalCount];
evalCount[_] = 0;
nlm = NonlinearModelFit[data, Log[a + b x^2], {a, b}, x, 
   EvaluationMonitor :> ++evalCount["Function"], 
   Gradient -> {"Symbolic", EvaluationMonitor :> ++evalCount["Gradient"]}, 
   Method -> {"LevenbergMarquardt", 
     "Residual" -> {"Symbolic", EvaluationMonitor :> ++evalCount["Residual"]}, 
     "Jacobian" -> {"Symbolic", EvaluationMonitor :> ++evalCount["Jacobian"]}}];
TableForm[evalCount /@ #, TableHeadings -> {#, None}] &@{"Function", "Gradient", 
  "Residual", "Jacobian"}

table

Although I'm not sure why inside of Method suboptions EvaluationMonitor does not catch anything for NonlinearModelFit...

$\endgroup$
2
$\begingroup$

So following @Szabolcs advice,

 {nl,ncounts}=Block[{c = 0}, {NonlinearModelFit[data, Log[a + b x^2], {a, b}, x, 
    Method -> "Newton", EvaluationMonitor :> c++], c}]

or (doing something slightly different) counting the number of Log Calls.

  Trace[nlm = NonlinearModelFit[data, Log[a + b x^2], {a, b}, x, 
   Method-> "Newton"],Log[__]]//Flatten//Length

but it seems you have changed your question…

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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