Message Boards Message Boards

0
|
4566 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

NMinimize "Function value is not a number" problem

Posted 3 years ago

I am trying to calculate the root mean squared deviation (RMSD) and minimise it using NMinimize between experimental data and a model. My model function has 2 parameters and the value of the function is an output from an external program which Mathematica calls and provides the inputs. The 2 parameters to be optimised by NMinimize are these inputs.

NMinimize runs for a while and complains that the function value is not a number at {x,y} (x, and y are the parameters). Whereas if I evaluate the function at those {x,y}, I do get an output which is a number. Any idea what is going wrong ? Below I paste part of my code:

simu[{x_?NumberQ, y_?NumberQ}] := (Interpreter["Number"][
   StringSplit[
    RunProcess[{"/program" , 
      "path/to/script/", ToString[x], ToString[y]]}, 
     "StandardOutput"]]])

RMSD[A_, B_, N_] := Sqrt[(Plus @@ ((A - B)^2)/N)]

NMinimize[RMSD[data, simu[{x, y}], 10], , {x, y}, MaxIterations -> 100]

Looking forward,

POSTED BY: Riddhiman Sarkar
2 Replies

NMinimize initializes with symbolic values, you want to prevent your functions evaluating with symbolic values.

so I guess making them conditional while only matching numerical inputs it will run.

In[1]:= (*Simulate some data and make a function to reproduce the data*)
n = 4;
data = Flatten[
   With[{x = 2, y = 4}, Table[(x i^2 + y j), {i, 1, n}, {j, 1, n}]] + 
    RandomReal[{-0.5, 0.5}, {n, n}]];

simu[x_, y_] := 
  Flatten[Table[(x i^2 + y j), {i, 1, n}, {j, 1, n}]] /; (NumberQ[x] &&
      NumberQ[y]);

In[4]:= (*always evaluates*)
RMSDA[A_, B_] := RootMeanSquare[A - B]
(*only evaluates numeric values*)
RMSDN[A_, B_] := RootMeanSquare[A - B] /; (ListQ[A] && ListQ[B] && AllTrue[A, NumericQ] && AllTrue[B, NumericQ])

In[6]:= NMinimize[RMSDA[data, simu[x, y]], {x, y}]

During evaluation of In[6]:= NMinimize::nnum: The function value {26.2704,25.6377,25.0096,24.3863,23.8636,23.2503,22.6432,22.0426,20.0527,19.4886,<<6>>} is not a number at {x,y} = {0.918621,0.716689}.

Out[6]= NMinimize[
 1/4 \[Sqrt]((5.52371 - simu[x, y])^2 + (10.3453 - 
       simu[x, y])^2 + (11.8308 - simu[x, y])^2 + (13.6971 - 
       simu[x, y])^2 + (15.5564 - simu[x, y])^2 + (18.3346 - 
       simu[x, y])^2 + (20.0972 - simu[x, y])^2 + (22.0655 - 
       simu[x, y])^2 + (24.0908 - simu[x, y])^2 + (25.778 - 
       simu[x, y])^2 + (29.8389 - simu[x, y])^2 + (33.9488 - 
       simu[x, y])^2 + (35.875 - simu[x, y])^2 + (40.0954 - 
       simu[x, y])^2 + (43.7572 - simu[x, y])^2 + (47.6609 - 
       simu[x, y])^2), {x, y}]

In[7]:= NMinimize[RMSDN[data, simu[x, y]], {x, y}]

Out[7]= {0.23916, {x -> 1.9883, y -> 4.00557}}
POSTED BY: Martijn Froeling

Thanks a lot. I believe that was the issue.

POSTED BY: Riddhiman Sarkar
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract