# SystemException[“MemoryAllocationFailure”, …] with NMaximize

I have a complicated analytical expression that needs to be maximized with respect to a parameter t:

qfB[t_,\[Lambda]_,\[Gamma]_,a_]=Uncompress["1:eJztnU9v3DYQxdf/NjWcQz5C+g0K5NZjgN6DoMdetkAMLJDGQJ2iX7uHAgkC11uvN5bF0ZAitdKIpH4Xw7E2Ivk4894TOeL++PvN++vz1Wp1u//x7uNft9cv9v+6ePjx6/aPD7fb1w+/XZ8+/e3dzd8f/rw9efhtsz31XPnl8MfmhmfuDe93u93j5z7frh9+/vbzT2/evjn04flDXx8+JO/9/Ontbn/5rN3K4U9qb0T7O7X9U+dDgeuiL281GFwAAl0To/5BQTTjUautt/p7170sun/iDPi5p4Gwiumtfl0N2NaQ9r26vnQ784+nM5vDp4l+LfpdPvGjJ0bt45M20zQDvncGfLjBWcQN6spA2X1/uCoQ+nqeANfLwuBy/3uYtZX/sP0vCFk8bq/Kwk1r/Xrdn+UahPfegRSeiqKNBZKRmGm/QhbOIqINxeYsi0dVesAEmM0GJgATYIXbPjfebz5vbz5tPj4+vnkH7WOA8FPewkxDMBZr5y0UYy7kUQwUwwo3j2LUv3aozRZrh4hALsgjAojA9LgNXRzQIKw2FZU2FkZGyMBcyCMDyIAVbvHPAmwvrYISsUCiEjPN9lL9HIsZwAzkDBdmwGQrqfSFQdGZbu4c3I4IJB2fqHjQ8jbneIDhbaGG4WF4I9wigTo6BjuIFbYR0mq9IcKXbrMa1Yyaq27rUYOuIb19NSqsJRyTf6WBMmK5UrXmQbQRFrcqERC6hnG1zU+MK8bVADfPo/fAbWt8RLWkhBzMhTxygBxY4Ra1EishhgvMJgkugAsMcLuKzOjxN/BLS0jRxtozzlAlfF0I+N8FqJZlRBtDTlIpHAFBm9HjLI0XRRv/Hs93hSNw6c32pcRAkqvCKmOVM4ULqzz+Y7Nov9pUg+TmQh6Sg+SscAttFcVCVGUSQn9zIQ/9QX9WuIW2Rpxr+y6oxPCiPSSIAWLIBi6IYQLcAstAzhgkmNWunIn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KSdZlRzSSZYY8koVkTY+bRsACGYp/jhHk0kDxMPyAnap6qVm0wX4btsA2P7EF2AID3DgEfSofURkCyMFcyCMHyIEVbnGHoF+1ux3/APFICGIo58vLZ9HGtzBeC0AghtEqIynRhsYViTFQGgKCNaLPyyqNVkUb2jQlxnrhCHw5nu9KQyBoSfzTj8nGZGcKFyZ7fJMt2q92LQWSmwt5SA6Ss8KNI6Shv8yQh/6gPyvcPKX9Ws9aY8mufF8Uaiqlp71jPsx5OHzC2dWHQcOei6nWpI4rQf38PS8clHQnodE0MVOt4TnttoH9xH6WARf2c3z7qcmGhvOwuKMuYEy9oi6AugDqAqgLqFaoqAsIWpyAzmLazaYI045pt8KNugBIDpLLHy5IjroA6K8e5KE/6M8Kt966AOcDZx5A5BxAFmazCFlAFla4NX3hAGsLy+V0LSZPtR0zaBI3i0CVAxcCNWWZQflfb+tEgKyHvXu642aERHJbopY11nWURtDUsvpUK710SGPkapVdtMGZhLhK2/zEVeIqDXAL7YPzeA6RFo08RAqRWuF21Gl+neQX3fZV5lacu6IN7XG7hdcCEPDXp1dLSNHvKETHQGkICIaNHmdpFCra0Da3EmO9cAQuvdm+lBhIMmC4alx1pnDhqidanujP4uJTDZKbC3lIDpKzwi2uTJ8tKOjPDHnoD/qzwi20cupc26PkLyYV8DcjPFHw7glRiMYsAiAaiMYKNxeZCwXKLhVp+XQAjPJ/yv9zhBbxMkMe8UK8rHBbQPk/B2BnGXldLTm0yEsDCbRePCjpLkOjcGKmWjPU/6CDNcWaZgoX1nRKayqQFDgPizsOwB5TrzgAmwOwOQCbA7CrFSrRBgdgR+sspt1sijDtmHYr3DgAG5KD5PKHC5Ibn+TE0BaYhNDfXMhDf9CfFW4cgA1ZQBaQRRRuTV84ANvCclEBi5stH3kECoGywq3+Ctj/AbVBr8I="];


For certain values of the parameters, for example with the following command

NMaximize[{qfB[t, 1, 2, 0.5162124455872646], t > 0}, t][[1]]


I obtain the following error

SystemException["MemoryAllocationFailure", {NMaximize[{qfB[t, 1, 2,
0.516212], t > 0}, t][[1]], OutputSizeLimitSkeleton[52], -1 + 4.5*10^-24300380321879}]


(very long output, even if compressed). This error doesn't appear in the documentation, and it causes the whole evaluation to stop.

I would like to understand why it comes up, and if there is a way around it or at least a way to handle it inside a computation.

EDIT

Following Oleksandr's suggestion I have filed a support request to Wolfram technical support and it has been forwarded to the developers as a bug.

• I got this as a Maximized answer after several trials, {0.0175048, {t -> 0.570513}}. – Chen Stats Yu Jan 4 '15 at 17:26
• @OleksandrR. Thanks for your comment. I'll investigate the analytical form of my function to fix the numerical instability. I've noticed that FindMaximum can be slow for certain parameters. I will try with NelderMeadMinimize. – Pincopallino Jan 4 '15 at 22:13
• The kernel crashes after a minute in V10.1 (Mac OSX, Macbook Pro, 2.7 GHz Intel Core i7, 16 GB RAM). Does that count as the bug being "fixed"? :) – Michael E2 Jul 18 '15 at 16:26

This error should probably be reported to WRI as a bug, most likely in ExperimentalNumericalFunction; you should not be seeing this come back up to the top level. I see no obvious reason why memory allocation should fail, as this is not really a large or difficult problem, despite the apparent complexity of the expression.

However, we do not really need the full global optimization machinery of NMaximize anyway, as the function is seen to be unimodal by plotting. Thus, we can manage with only local minimizers.

For what it's worth, neither FindMaximum or NelderMeadMinimize have a problem with this function--so it seems confined to NMinimize:

FindMaximum[{qfB[t, 1, 2, 0.5162124455872646], t > 0}, {t, 1}]
(* -> { 0.017504782445427220, {t -> 0.5705059672138650}} *)

NelderMeadMinimize[-qfB[t, 1, 2, 0.5162124455872646], t]
(* -> {-0.017504782449431693, {t -> 0.5705132464216982}} *)


If FindMaximum seems too slow, it's probably because it starts too close to $t=0$, where the expression diverges. This causes overflows and other numerical problems, which FindMaximum may attempt to deal with by ramping up the precision. Constrained optimization is also slower than unconstrained, although constraints are not really necessary here. We can fix any problem FindMaximum has by giving it a more favorable starting region:

FindMaximum[qfB[t, 1, 2, 0.516212445587264625], {t, 1/2, 1}]
(* -> {0.017504782449430056, {t -> 0.5705133885275231}} *)


This way is about 100 times faster than using the constraint, and actually a bit (~30%) faster even than NelderMeadMinimize, using the Mathematica VM. (NelderMeadMinimize is faster when using a C compiler, but most compilers will take a long time to compile this complicated expression, so there is no benefit unless you need to maximize this for very many combinations of parameters.)

• I have reported the problem with the error message to Wolfram. I've also tried your NelderMeadMinimize function and it works great, much faster than FindMaximum! – Pincopallino Jan 4 '15 at 22:31
• @Pincopallino if they confirm it is a bug, could you please update the question to add the "bugs" tag? Also, see my update about the poor performance of FindMaximum. Even though NelderMeadMinimize can be a bit faster, personally I think the difference is not large enough to be worth bothering with; just use FindMaximum without the constraint instead. – Oleksandr R. Jan 4 '15 at 23:23

This is the best I can get:

qfB = Uncompress["You expression here"];
myfun = qfB /. {\[Lambda] -> 1., \[Gamma] -> 2., a -> 0.5162124455872646} // N;
(* so that the expression only involves t *)

NMaximize[{myfun, t > 0}, t]


{0.0175048, {t -> 0.570513}}

Hope this helps.

• Thank you! However, I would like to understand the source of the error, because it appears to be undocumented. Does the error appear on you system? – Pincopallino Jan 4 '15 at 20:19
• @Pincopallino I had a similar error once. If I use your code, I got the same error as yours. I think it is just that the expression is too 'complicated' for MMA to handle. Sometimes, we have to give MMA a 'minimal' things to calculate. In this case, I have fixed other parameters before passing it on to NMaximize`. – Chen Stats Yu Jan 4 '15 at 21:07