I can plot my function penalty[v] just fine, but FindMinimum doesn't work, it just complains non-numerical values from NIntegrate.
gau[x_, v_] := Exp[-(x^2)/(2*v)]/Sqrt[2*Pi*v];
data = {-5, -4, -3, 1, 2};
p[x_, v_] := Total[gau[x - data, v]]/Length[data];
penalty[v_] := Sum[Log[p[data[[i]], v]], {i, 1, Length[data]}]/Length[data]-NIntegrate[p[x, v]*Log[p[x, v]], {x, -Infinity, Infinity}];
Plot[penalty[v], {v, .01, 2}]
FindMinimum[penalty[v], {v, .25}]
What am I doing wrong?
(Side note: In my definition of penalty, I had to use Sum instead of Mean because when data is passed into p[x_,v_], the data-data yields zeros instead of the cross terms. Should I have coded that part differently too?)
NumericQi.e. definepenalty[v_?NumericQ] := restOfCode. That should then work: result. See: http://mathematica.stackexchange.com/a/26037/27951 – MarcoB Jun 09 '16 at 20:49