# NArgMax

NArgMax[f,x]

gives a position xmax at which f is numerically maximized.

NArgMax[f,{x,y,}]

gives a position {xmax,ymax,} at which f is numerically maximized.

NArgMax[{f,cons},{x,y,}]

gives a position at which f is numerically maximized subject to the constraints cons.

NArgMax[,xreg]

constrains x to be in the region reg.

# Details and Options    • NArgMax always attempts to find a global minimum of f subject to the constraints given.
• NArgMax is typically used to find the smallest possible values given constraints. In different areas, this may be called the best strategy, best fit, best configuration and so on.
• NArgMax returns a list of the form {xmin,ymin,}.
• • NArgMax[,{x,y,}] is effectively equivalent to {x,y,}/.Last[NMaximize[,{x,y,},].
• If f is linear or concave and cons are linear or convex, the result given by NArgMax will be the global maximum, over both real and integer values; otherwise, the result may sometimes only be a local maximum.
• If NArgMax determines that the constraints cannot be satisfied, it returns {Indeterminate,}.
• NArgMax supports a modeling language where the objective function f and constraints cons are given in terms of expressions depending on scalar or vector variables. f and cons are typically parsed into very efficient forms, but as long as f and the terms in cons give numerical values for numerical values of the variables, NArgMax can often find a solution.
• The constraints cons can be any logical combination of:
•  lhs==rhs equations lhs>rhs, lhs≥rhs, lhs
• NArgMax[{f,cons},xrdom] is effectively equivalent to NArgMax[{f,cons&&xrdom},x].
• For xrdom, the different coordinates can be referred to using Indexed[x,i].
• Possible domains rdom include:
•  Reals real scalar variable Integers integer scalar variable Vectors[n,dom] vector variable in Matrices[{m,n},dom] matrix variable in ℛ vector variable restricted to the geometric region • By default, all variables are assumed to be real.
• The following options can be given:
•  AccuracyGoal Automatic number of digits of final accuracy sought EvaluationMonitor None expression to evaluate whenever f is evaluated MaxIterations Automatic maximum number of iterations to use Method Automatic method to use PrecisionGoal Automatic number of digits of final precision sought StepMonitor None expression to evaluate whenever a step is taken WorkingPrecision MachinePrecision the precision used in internal computations
• The settings for AccuracyGoal and PrecisionGoal specify the number of digits to seek in both the value of the position of the maximum and the value of the function at the maximum.
• NArgMax continues until either of the goals specified by AccuracyGoal or PrecisionGoal is achieved.
• The methods for NArgMax fall into two classes. The first class of guaranteed methods uses properties of the problem so that, when the method converges, the maximum found is guaranteed to be global. The second class of heuristic methods uses methods that may include multiple local searches, commonly adjusted by some stochasticity, to home in on a global maximum. These methods often do find the global maximum, but are not guaranteed to do so.
• Methods that are guaranteed to give a global maximum when they converge to a solution include:
•  "Convex" use only convex methods "MOSEK" use the commercial MOSEK library for convex problems "Gurobi" use the commercial Gurobi library for convex problems
• Heuristic methods include:
•  "NelderMead" simplex method of Nelder and Mead "DifferentialEvolution" use differential evolution "SimulatedAnnealing" use simulated annealing "RandomSearch" use the best local minimum found from multiple random starting points

# Examples

open allclose all

## Basic Examples(4)

Find a maximizer point for a univariate function:

Find a maximizer point for a multivariate function:

Find a maximizer point for a function subject to constraints:

Find a maximizer point for a function over a geometric region:

Plot it:

## Scope(33)

### Basic Uses(12)

Maximize subject to constraints :

Several linear inequality constraints can be expressed with VectorGreaterEqual:

Use v>= or \[VectorGreaterEqual] to enter the vector inequality sign :

An equivalent form using scalar inequalities:

Use a vector variable :

The inequality may not be the same as due to possible threading in :

To avoid unintended threading in , use :

Use constant parameter equations to avoid unintended threading in :

VectorGreaterEqual represents a conic inequality with respect to the "NonNegativeCone":

To explicitly specify the dimension of the cone, use {"NonNegativeCone",n}:

Find the solution:

Maximize subject to the constraint :

Specify the constraint using a conic inequality with "NormCone":

Find the solution:

Maximize the function subject to the constraint :

Use Indexed to access components of a vector variable, e.g. :

Use Vectors[n,dom] to specify the dimension and domain of a vector variable when it is ambiguous: Specify non-negative constraints using NonNegativeReals ( ):

An equivalent form using vector inequality :

Specify non-positive constraints using NonPositiveReals ( ):

An equivalent form using vector inequalities:

Or constraints can be specified:

### Domain Constraints(4)

Specify integer domain constraints using Integers:

Specify integer domain constraints on vector variables using Vectors[n,Integers]:

Specify non-negative integer domain constraints using NonNegativeIntegers ( ):

Specify non-positive integer domain constraints using NonPositiveIntegers ( ):

### Region Constraints(5)

Maximize over a region:

Plot it:

Find the maximum possible distance between two points that are constrained to be in two different regions:

Plot it:

Find the maximum such that the triangle and ellipse still intersect:

Plot it:

Find the circle of maximum radius that contains the given three points:

Plot it:

Using Circumsphere gives the same result directly:

Use to specify that is a vector in with :

### Linear Problems(5)

With linear objectives and constraints when a maximum is found, it is global:

The constraints can be equality and inequality constraints:

Use Equal to express several equality constraints at once:

An equivalent form using several scalar equalities:

Use VectorLessEqual to express several LessEqual inequality constraints at once:

Use v<= to enter the vector inequality in a compact form:

An equivalent form using scalar inequalities:

Use Interval to specify bounds on a variable:

### Convex Problems(3)

Use "NonNegativeCone" to specify linear functions of the form :

Use v>= to enter the vector inequality in a compact form:

Maximize such that is positive semidefinite:

Show the maximizer on a plot of the objective function:

Maximize the concave objective function such that is positive semidefinite and :

Plot the region and the maximizing point:

### Transformable to Convex(1)

Maximize the quasi-concave function subject to inequality and norm constraints. The objective is quasi-concave because it is a product of two non-negative affine functions:

The maximization is solved by minimizing the negative of the objective, , that is quasi-convex. Quasi-convex problems can be solved as a parametric convex optimization problem for the parameter :

Plot the objective as a function of the level-set :

For a level-set value between the interval , the smallest objective is found:

The problem becomes infeasible when the level-set value is increased: ### General Problems(3)

Maximize a linear objective subject to nonlinear constraints:

Plot it:

Maximize a nonlinear objective subject to linear constraints:

Plot the objective and the maximizing point:

Maximize a nonlinear objective subject to nonlinear constraints:

Plot it:

## Options(6)

### AccuracyGoal & PrecisionGoal(2)

This enforces convergence criteria and :

This enforces convergence criteria and , which is not achievable with the default machine-precision computation: Setting a high WorkingPrecision makes the process convergent:

### EvaluationMonitor(1)

Record all the points evaluated during the solution process of a function with a ring of minima:

Plot all the visited points that are close in objective function value to the final solution:

### Method(1)

Specifying a non-default method could give a better solution:

### StepMonitor(1)

Steps taken by NArgMax in finding the maximum of a function:

### WorkingPrecision(1)

With the working precision set to , by default AccuracyGoal and PrecisionGoal are set to half of the working precision:

## Applications(7)

### Geometry Problems(3)

Find the half-lengths of the principal axes that maximize the volume of an ellipsoid with a surface area of at most 1:

The surface area can be approximated by:

Maximize the volume area by minimizing its reciprocal:

This is the sphere. Including additional constraints on the axes lengths changes this:

Plot the resulting ellipsoid:

Find the analytic center of a convex polygon. The analytic center is a point that maximizes the product of distances to the constraints:

Each segment of the convex polygon can be represented as intersections of half-planes . Extract the linear inequalities:

The objective is to maximize . Since the logarithm function is monotonic, this is equivalent to maximizing :

Solve the unconstrained problem:

Visualize the location of the center:

Find the maximum-area ellipse parametrized as that can be fitted into a convex polygon:

Each segment of the convex polygon can be represented as intersections of half-planes . Extract the linear inequalities:

Applying the parametrization to the half-planes gives . The term . Thus, the constraints are :

Maximizing the area is equivalent to maximizing :

Convert the parametrized ellipse into the explicit form as :

### Portfolio Optimization(1)

Find the distribution of capital to invest in six stocks to maximize return while minimizing risk:

The return is given by , where is a vector of expected return value of each individual stock:

The risk is given by ; is a risk-aversion parameter and :

The objective is to maximize return while minimizing risk for a specified risk-aversion parameter:

The effect on market prices of stocks due to the buying and selling of stocks is modeled by , which is modeled by a power cone using the epigraph transformation:

The weights must all be greater than 0 and the weights plus market impact costs must add to 1:

Compute the returns and corresponding risk for a range of risk-aversion parameters:

The optimal over a range of gives an upper-bound envelope on the tradeoff between return and risk:

Compute the weights for a specified number of risk-aversion parameters:

By accounting for the market costs, a diversified portfolio can be obtained for low risk aversion, but when the risk aversion is high, the market impact cost dominates, due to purchasing a less diversified stock:

### Investment Problems(2)

Find the allocation of a maximum of \$250,000 of capital to purchase two stocks and a bond such that the return on investment is maximized. Let be the amount to invest in the two stocks and let be the amount to invest in the bond:

The amount invested in the utilities stock cannot be more than \$40,000:

The amount invested in the bond must be at least \$70,000:

The total amount invested in the two stocks must be at least half the total amount invested:

The stocks pay a annual dividend of 9% and 4%, respectively. The bond pays a dividend of 5%. The total return on investment is:

The cost with purchasing the stocks and bonds and executing the transactions is :

The optimal amount to be invested can be found by maximizing the profit while minimizing cost:

The total amount invested and the annual dividends received from the investments are:

Find the optimal combination of investments that yields maximum profit. The net present value and the costs associated with each investment are:

Let be a decision variable such that if investment is selected. The objective is to maximize profits while minimizing costs:

There is a maximum \$14,000 available for investing:

Solve the maximization problem to get the optimal combination of investments:

### Traveling Salesman Problem(1)

Find the path that a salesman should take through cities such that each city is only visited once, travel cost savings are maximized and distance traveled is minimized. Generate the locations:

Let be the distance between city and city . Let be a decision variable such that if , the path goes from city to city :

The travel budget to go from one city to another is \$15. If two cities are are within 50 miles, the salesman pays \$5, and otherwise pays a \$10 flat rate. The total savings are:

The objective is to maximize savings while minimizing distance:

The salesman can arrive from exactly one city and can depart to exactly one other city:

The salesman cannot arrive in one city and depart to the same city:

The salesman must travel to all the locations in a single tour:

The decision variable is a binary variable and the dummy variable is :

Find the path that minimizes the distance:

Extract the path:

The distance traveled is:

The total savings are:

## Properties & Relations(5)

NMaximize gives the maximum value and rules for the maximizing values of the variables:

NArgMax gives a list of the maximizing values:

NMaxValue gives only the maximum value:

Maximizing a function f is equivalent to minimizing -f:

For convex problems, ConvexOptimization may be used to obtain additional solution properties:

Get the dual solution:

For convex problems with parameters, using ParametricConvexOptimization gives a ParametricFunction:

The ParametricFunction may be evaluated for values of the parameter:

Define a function for the parametric problem using NArgMax:

Compare the speed of the two approaches:

Derivatives of the ParametricFunction can also be computed:

For convex problems with parametric constraints, RobustConvexOptimization finds an optimum that works for all possible values of the parameters:

NArgMax may find a maximizer that gives a smaller maximum value for particular values of the parameters:

This minimizer does not satisfy the constraints for all allowed values of and :

The maximum value found for particular values of the parameters is greater than or equal to the robust minimum:

## Possible Issues(2)

The objective function may be unbounded: There may be no points satisfying the constraints: 