# Using StepMonitor/EvaluationMonitor with DifferentialEvolution in NMinimize

My question about tracking the progress of a minimisation/fitting process is two-fold:

1. The first part of my question can be considered a slight duplicate/follow-up of this question. There the OP asked what exactly is passed to the StepMonitor-Option when using the "DifferentialEvolution"-method. The answer by Oleksandr R. states that only the fittest individual of each generation is passed. This is fine by me but when I try the following nothing is passed:

data = BlockRandom[SeedRandom;
Table[{x, Exp[-2.3 x/(11 + .4 x + x^2)] + RandomReal[{-.5, .5}]},
{x, RandomReal[{1, 15}, 20]}]];

nlm = Reap@NonlinearModelFit[data, Exp[a x/(b + c x)], {a, b, c}, x,
Method -> {NMinimize, Method -> {"DifferentialEvolution"}},
StepMonitor :> Sow[{a, b, c}]]


compare this to the same expression using EvaluationMonitor instead of StepMonitor.

Question: Is what is passed to/by EvaluationMonitor the fittest individual of the current generation (like Oleksandr R. suggested for StepMonitor which sadly seems not to be accurate any more)

Question 2 Is there a straightforward way to pass the value of the fitness/cost-function of said best individual i.e. the squared differences between estimate and data?

EvaluationMonitor is going to be called whenever the objective function is being evaluated, that is much more often than StepMonitor.

The reason for not getting any points back is that the StepMonitor specification is not propagated to the NMinimize call. Try the following syntax instead

nlm = Reap @ NonlinearModelFit[data, Exp[a x/(b + c x)], {a, b, c}, x,
Method -> {NMinimize, StepMonitor :> Sow[{a, b, c}],
Method -> "DifferentialEvolution"}]


For the values of the objective function at these points, one could build the sum of squared residuals by hand, but there is also an internal function that can be used (the added factor of two is because in the default 2-norm case the objective function is $\frac12 \bf r \cdot \bf r$, where $\bf r$ is the residual vector).

obj = OptimizationFindFitObjectiveFunction[data, Exp[a x/(b + c x)], {a, b, c}, x];
nlm = Reap @ NonlinearModelFit[data, Exp[a x/(b + c x)], {a, b, c}, x,
Method -> {NMinimize, StepMonitor :> Sow[2 obj[{a, b, c}]],
Method -> "DifferentialEvolution"}]

• Thank you for uncovering OptimizationFindFitObjectiveFunction, it is indeed a very convenient way to create a NumericalFunction! There is also OptimizationFindFitResidualFunction. – Alexey Popkov Jun 6 '16 at 3:26
• @ilian For the given example the sum of the squared residuals equals 1.2583 (see e.g. ANOVATable). This is exactly double of the last value coming out of OptimizationFindFitObjectiveFunction. – demm Jun 6 '16 at 19:14
• @demm Yes, the objective function being minimized is actually $\frac12 \bf r \cdot \bf r$. Thanks for pointing this out. – ilian Jun 6 '16 at 19:30
• A mathematical note, for the uninitiated: the reason for the factor of $\frac12$ is that it makes for tidy expressions for the corresponding gradient and Hessian. – J. M.'s technical difficulties Jun 6 '16 at 20:24
• What version of Mathematica do you use? I tried using the internal ObjectiveFunction but it does not work. Evaluating for instance obj[{1,2,3}] yields some unevaluated expression containing ExperimentalNumericalFunction I tried Needs["Optimization"] which results in an noopen-error – Sascha Jun 6 '16 at 21:45

One could also estimate the sum-of-squared residuals (or any other diagnostic, statistical property etc.) directly within Sow

  Reap@NonlinearModelFit[data, Exp[a x/(b + c x)], {a, b, c}, x,
Method -> {NMinimize,StepMonitor :>
Sow[Sum[(data[[i,2]]-Exp[a data[[i,1]]/(b+c data[[i,1]])])^2,
{i,1,Length[data]}]],Method->{"DifferentialEvolution"}}]
`