6
$\begingroup$

I am fitting some data to my model. I would like to print out parameters values say every 100 iterations (or 500) to check values getting steady state. Actually, I have a larger data and more complicate model than this and I am planning to set MaxIteration->10^5 and print out values at every 10^4 step. I don't wanna plot result at every step. Is there a way to do it? Thanks in advance.

data1= {{1.*10^-10, 380.38}, {0.001, 457.448}, {0.00215443, 548.79}, {0.00464159, 7651.56}, {0.01, 13596.2}, {0.0215443,  16416.9}, {0.0464159, 16922.2}, {0.1, 18109.2}, {0.215443, 19106.4}, {0.464159, 19317.9}, {1., 19797.5}, {10., 20514.7}};

f1[i_] := (c1 i^p1)/(is1^p1 + i^p1) + \[Epsilon]1

parameters1 = {\[Epsilon]1, c1, p1, is1};

model1 = Sum[ (Log10[f1@data1[[i, 1]]] - Log10[data1[[i, 2]]])^2, {i,12}];

fit1 = NMinimize[{model1, c1 > 0 && \[Epsilon]1 > 0 && 0 < p1 < 10 && 10^-10 < is1 <= 10}, parameters1, Method -> "DifferentialEvolution", MaxIterations ->2000]

Thread[{\[Epsilon]1, c1, p1, is1} = parameters1 /. Last@fit1];

Show[LogLogPlot[f1[i], {i, 10^-4, 10.02}, PlotRange -> All,   Frame -> True, Axes -> False, ImageSize -> 400],  ListLogLogPlot[Rest@data1, PlotStyle -> Red]]
$\endgroup$
7
$\begingroup$

You can use StepMonitor or EvaluationMonitor together with Reap / Sow to extract intermediate results from fitting functions. See Collecting expressions during evaluation.

Below I am using your definitions of parameters1, model1, and f1, but I changed the definition of fit1, and added intermediates to collect the running values of the parameters. I also did not assign the values to the parameters, which would give you trouble when running the code more than once; instead I use a replacement rule to plot the fit (see argument of LogLogPlot).

{fit1, intermediates} = Reap[
   NMinimize[
    {model1, 
     c1 > 0 && ϵ1 > 0 && 0 < p1 < 10 && 10^-10 < is1 <= 10}, 
    parameters1,
    MaxIterations -> 1000, Method -> "DifferentialEvolution",
    StepMonitor :> Sow[parameters1]
    ]
   ];

Show[
 LogLogPlot[f1[i] /. Last@fit1, {i, 10^-4, 10.02}, PlotRange -> All, 
  Frame -> True, Axes -> False, ImageSize -> 400],
 ListLogLogPlot[Rest@data1, PlotStyle -> Red]
 ]

fit

(Note that at least in this case the differential evolution method was much slower than the default chosen by NMinimize. You may want to experiment with your actual data.)

Now you can plot the evolution of the parameter values during the fit. You can post-select how many to plot as well; for instance, below I will plot one value for each 2nd iteration:

ListPlot /@ Transpose@intermediates[[1, ;; ;; 2]]

params

$\endgroup$
  • $\begingroup$ Wow!! This is awesome. Thank you so much. $\endgroup$ – OkkesDulgerci Jun 13 '17 at 17:01
  • $\begingroup$ Quick question: How can I make sure I am finding global minimum? $\endgroup$ – OkkesDulgerci Jun 13 '17 at 17:02
  • 1
    $\begingroup$ @OkkesDulgerci It can be very, very hard to know whether you found a global minimum, or you just got stuck in a local one. You might be interested in this tutorial: Numerical Nonlinear Global Optimization. $\endgroup$ – MarcoB Jun 13 '17 at 17:07
  • 1
    $\begingroup$ Thank you for reference and kind help. $\endgroup$ – OkkesDulgerci Jun 13 '17 at 17:11

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.