# How to find the maximum of this using Nmaximise?

I have a function

 f[p_, q_, c_, x_] :=
Log[c]*(2*x + 1 - p - q) + Log[1 - c]*(p + q - 2*x) +
q*((x/q)*Log[x/q] - (1 - x/q)*Log[1 - x/q]) + (1 -
q)*((1 - (p - x)/(1 - q))*
Log[1 - (p - x)/(1 - q)] - ((p - x)/(1 - q))*
Log[(p - x)/(1 - q)])


This is subject to certain conditions. $$0, I tried to maximise this f subject to these conditions.

NMaximize[{f[p, q, c, x], 0 < p < 1 && 0 < q < 1 && 0 < c < 1 && 0 < x < p}, {p, q, c, x}]

It gives me an error saying "0.2" is not a valid variable". How can I do this?

This is an extremely difficult optimization problem. First, the maximum value of a continuous function on a noncompact set may not be achieved. Second, the arguments of the logarithms must be positive. Here is my best done with NMaximize:

NMaximize[{Log[c]*(2*x + 1 - p - q) + Log[1 - c]*(p + q - 2*x) + q*((x/q)*Log[x/q] -
(1 - x/q)*Log[1 - x/q]) + (1 - q)*((1 - (p - x)/(1 - q))*Log[1 - (p - x)/(1 - q)] -
((p - x)/(1 - q))*Log[(p - x)/(1 - q)]),
0 <= p <= 1 && 0 <= q <= 1 && 0 <= c <= 1 && 0 <= x <= p &&
1 - (p - x)/(1 - q) > 0 && (p - x)/(1 - q) > 0 && 1 - x/q > 0}, {p, q, c, x},
Method -> {"RandomSearch", "SearchPoints" -> 6000},
AccuracyGoal -> 8, PrecisionGoal -> 8, WorkingPrecision -> 100]


{-0.040235724512206291627660187339000988341300360257660957486419128212
42038286521895562117758792508277991, {p -> 0.43170814406681159730265583187083824112441622120650523314960727729
69897992977574812748455238551242614, q -> 0.4301340679090147534723708508594863670430177236954453316714468
492587270496156672563111251203220147869, c -> 0.9569090295389766009595904849664351705051956310898170047261569
646576489145425430011107039749579302614, x -> 0.4298933184280109540416959995873164308236214711970976650767745
555271997494320105427232650983623018400}}

I think the supremum equals 0 , considering $$p=q$$, $$c$$ being close to $$1$$, and $$x being close to $$p$$. The result of the command

MaxLimit[Log[c]*(2*x + 1 - p - q) + Log[1 - c]*(p + q - 2*x) +
q*((x/q)*Log[x/q] - (1 - x/q)*Log[1 - x/q]) + (1 -
q)*((1 - (p - x)/(1 - q))*
Log[1 - (p - x)/(1 - q)] - ((p - x)/(1 - q))*
Log[(p - x)/(1 - q)]) /. {p -> 1/2, q -> 1/2}, {c, x}->{1, 1/2},Direction -> "FromBelow"]


0

and Maple's results

Matrix(3, 3, [[-0.526452049410^(-8), [c = 0.9999999943976654, p = 0.24140178813121238, q = 0.2414017880807429, x = 0.2414017880467214], 579], [-0.228576294210^(-7), [c = 0.9999999871956152, p = 0.9546206287899156, q = 0.954620639904515, x = 0.9546206287884551], 491], [-0.2347957064*10^(-7), [c = 0.9999999729593567, p = 0.15817527515336488, q = 0.15817527345368837, x = 0.15817527263676995], 335]])

confirm it.

• I have plotted this function and I have been checking it out. This thing probably makes sense. But I need some time. Let me check it more and get back. – user07 Jun 23 '20 at 10:20
• Even if you want to minimise, you do the same thing except with NMinimise function right? – user07 Jun 23 '20 at 10:27
• @newgirl: Yes, of course. Don't hesitate to ask for further explanation in need. – user64494 Jun 23 '20 at 13:03
• @newgirl: The result of MinLimit[Log[c]*(2*x + 1 - p - q) + Log[1 - c]*(p + q - 2*x) + q*((x/q)*Log[x/q] - (1 - x/q)*Log[1 - x/q]) + (1 - q)*((1 - (p - x)/(1 - q))* Log[1 - (p - x)/(1 - q)] - ((p - x)/(1 - q))* Log[(p - x)/(1 - q)]) /. {p -> 1/3, q -> 1/3}, {c, x} -> {1, 1/3}, Direction -> "FromBelow"] which is -\[Infinity] shows that the target function is unbounded from below on the feasible set. – user64494 Jun 23 '20 at 13:55
• this is hazy. When I see the graph I made, Manipulate[ Plot[{f[p, q, c, x]}, {x, 0, p}, ImageSize -> {400, 400}], {c, 0.001, 1}, {p, 0.001, 1}, {q, 0.001, 1}] I feel like two different sets of values of p and q, can give the same maximum. I want to make sure Also, I wanted to add one more condition to this, $p<=q$ , this gives me f value to be around -0.09. Can you check this? I want the maximum I am getting to be unique. (Because I want to use this for a saddle point approximation) – user07 Jun 23 '20 at 16:34

In this approach I changed the constraints slightly to force "real f"

NMaximize[{f[p, q, c, x],
{0 < c < 1,1 - (p - x)/(1 - q) > 0
, (p - x)/(1 - q) > 0, 0 < x < q}}
, {p, q,c, x}]
(*{-0.721159, {p -> 0.160626, q -> 0.614775, c ->0.340624,x -> 0.0928292}}*)

• I am still getting the same error – user07 Jun 23 '20 at 6:54
• Restart your Mathematica kernel and try again! – Ulrich Neumann Jun 23 '20 at 6:58
• How are you getting those inequality constraints? If I solve them, I dont get the same inequalities as my original ones. Also I have tried to plot this function and see. It's weird. The maximums you are getting doesnt correspond to it somehow. This is the code that I have used. Manipulate[ Plot[{f[p, q, c, x]}, {x, 0, p}, ImageSize -> {400, 400}], {c, 0.001, 1}, {p, 0.001, 1}, {q, 0.001, 1}] – user07 Jun 23 '20 at 10:16
• What do you think of user64494's approach? – user07 Jun 23 '20 at 10:18
• @newgirl I'm uncertain, because the maximum depends very sensitivly on the setting of WorkingPrecision... If you are looking for the constraints to get real f try FunctionDomain[f[ p, q, c, x ], {p, q, c, x}]` – Ulrich Neumann Jun 23 '20 at 10:29