# Strange Behaviour of NMinimize

I have the following function:

msd[v0_, mu_, gamma_, delta_, kappa_, eta_, deltaT_, reps_] :=
Module[{squaredSum, terms, sum},
Print[{v0, mu, gamma, delta, kappa, eta}];
squaredSum[x_] := (DensityEmp[x] -
Re[p[deltaT, x, v0, mu, gamma, delta, kappa, eta]])^2;
terms = Map[squaredSum, reps];
sum = Total[terms];
If[Im[sum] <= 10^(-5), sum = Re[sum], sum]
];


here $p[deltaT, x, v0, mu, gamma, delta, kappa, eta]$ is a well-defined function containing a numerical Integration routine. (I have tested and retested it a couple of times and it works just as it is supposed to)

As you can see I am dealing with a a sqaured dinstance and my goal is to minimize $msd$ over a certain (plausbible) area.

For this purpose I use the following setup:

NMinimize[
{msd[v0, mu, gamma, delta, kappa, eta, dt, BinReps],
v0 <= 1 && v0 >= 0 &&
mu <= 1 && mu >= -1 &&
gamma <= 1  && gamma >= -1 &&
delta <= 1 && delta >= -1 &&
kappa <= 1  &&  kappa >= 0  &&
eta <= 1 && eta >= -1 &&
v0 != 0  && mu != 0  && gamma != 0  && delta != 0  && kappa != 0 && eta != 0},
{v0, mu, gamma, delta, kappa, eta}]


unfortunately I always recieve the following error

NIntegrate::inumr: "The integrand ...  has evaluated to non-numerical values for all sampling points in the region with boundaries {{-\[Infinity],0.}}."


When I run $msd$ without $NMinimize$ it works just fine. I have the feeling that $NMinimize$ doesn't pass any values to $msd$ at all and thus the function works with symbols only - this obviously can't produce any sensible results. $deltaT$ and $BinReps$ both contain the values they should contain.

 Print[{v0, mu, gamma, delta, kappa, eta}];


that I have added to $msd$ to debug this problem also only outputs the vetor

{v0, mu, gamma, delta, kappa, eta}


only symbols and no numbers. Obviously $NMinimize$ doesn't pass any numbers to $msd$ and I have no idea why.

Update: As suggested in the comments I have tried altering the functionheader a bit

msd[v0_?NumericQ, mu_?NumericQ, gamma_?NumericQ, delta_?
NumericQ, kappa_?NumericQ, eta_, deltaT_?NumericQ,
reps_?NumericQ]


this at least lead to the following new error messege:

General::stop: Further output of NIntegrate::inumr will be suppressed during this calculation. >>

NMinimize::nnum: The function value <<1>> is not a number at {v0,\[Gamma],\[Delta],\[Eta],\[Kappa],\[Mu]} = {0.974797,0.261174,0.279913,0.930797,0.306576,0.260902}. >>

-
DensityEmp[] and p[] aren't defined in your posted code –  belisarius Sep 29 at 14:25
I know that but they are not relevant here - $Print[{...}]$ only prints symbols thus $msd$ isn't passed any values at all. The error takes place even before $DensityEmp$ and $p$ are evaluated –  Probabilitnator Sep 29 at 14:27
I understand they could be non-relevant from a theoretical POV, but it's much easier for us to figure out what's happening by running a runnable code instead of by staring at yours. In any case, some short fake definitions may help if the actual ones are too complicated –  belisarius Sep 29 at 14:33
okey - this makes sense. you will need the set of statistical data I have been using. I uploaded the workbook and the respective .xlsx to (uploaded.net/file/hjjmcwdi). This way you do not even need to copy paste everything into a new notebook :) –  Probabilitnator Sep 29 at 14:38
Might try defining msd so it only takes explicitly numeric input, that is, msd[v0_?NumberQ, ...] := .... This will force NMinimize to treat it as a "black box" function. Depending on what exactly is going wrong, this could improve matters. –  Daniel Lichtblau Sep 29 at 16:18
show 1 more comment