### Optimization Goals & Constraints

A classic problem of constructing a portfolio that meets certain maximization/minimization goals and constraints is addressed in our version of a multi-start portfolio optimization algorithm. At every time step optimization algorithm tries to find position weights that best meet optimization goals and constraints.

- A multi-start approach is used to compare local optima with each other and select a global optimum. Local optima are computed using Hooke and Jeeves family of local optimization algorithms.
- Optimization algorithm ignores past history of optimal weights. Current weights only depend on the metric values and ignore weights computed at previous steps.
- When optimization algorithm is supplied with mutually exclusive constraints, it would try to produce result that is equally close (in absolute terms) to all constraint boundaries. For instance, constraints 'x > 6' and "x < 4" are mutually exclusive, so the optimization algorithm would choose "x = 5", which is a value that has the smallest distance to both constraints.
- Portfolio metrics change over time, but optimization uses only the latest value in the time series. Therefore, the faster metric series would change, the more likely current optimal weights would deviate from the optimal weights at the next time step.
- Optimization results depend on provided portfolio settings. For example, short windowLength would produce "spot" versions of portfolio metrics and computed optimal weights would change faster to reflect shortened metric horizon.

##### Optimization Goals

Optimization algorithm requires a single maximization/minimization goal to be set using optimization_goal method that operates on a portfolio (see portfolio construction). Returned optimizer object could be used to add optional optimization costraints and then passed to the optimization_run method to launch portfolio optimization.

```
# create portfolio and add positions
portfolio=portfolio_create(fromTime="2014-10-02 09:30:00", toTime="2014-10-03 16:00:00")
portfolio_addPosition(portfolio, symbol=c('C','GOOG'), quantity=c(500,600))
# set optimization goal
optimizer=optimization_goal(portfolio, goal="Return", direction="maximize")
# launch optimization and obtain optimal portfolio
optimalPortfolio=optimization_run(optimizer)
plot(optimalPortfolio)
```

```
% create portfolio and add positions
portfolio=portfolio_create('fromTime','2014-10-02 09:30:00', 'toTime','2014-10-03 16:00:00');
portfolio_addPosition(portfolio, {'C';'GOOG'}, [500;600]);
% set optimization goal
optimizer=optimization_goal(portfolio, 'goal', 'Return','direction', 'maximize');
% launch optimization and obtain optimal portfolio
optimalPortfolio=optimization_run(optimizer);
plot(optimalPortfolio)
```

The following portfolio metrics could currently be used as optimization goals:

- Variance
- portfolio returns variance
- VaR
- portfolio Value-at-Risk
- CVaR
- portfolio Conditional Value-at-Risk (Expected Tail Loss)
- ExpectedReturn
- portfolio expected return
- Return
- portfolio return
- SharpeRatio
- portfolio Sharpe Ratio
- ModifiedSharpeRatio
- portfolio modified Sharpe Ratio
- StarrRatio
- portfolio STARR Ratio
- ContraintsOnly
- no optimization is performed. This is used for returning an arbitrary portfolio that meets specified set of constraints
- None
- no optimization is performed and constraints are not processes. Portfolio positions are returned with equal weights

##### Optimization Constraints

Provided optimization constraints include metric-based and weight-based constraints. Metric-based constraints limit portfolio-level metrics to a certain range of values. For example, zero beta constraint would produce market-neutral optimal portfolio. Weight-based constraints operate on optimal position weights or sum of weights to give control over position concentration risks or short-sales assumptions.

Since position quantities are integer numbers and weights are decimals, a discretization error is introduced while converting optimal position weights to corresponding quantities. By default, optimal portfolio starts with a value of the initial portfolio. Portfolio value could be fixed to a constant level at every optimization step (see corresponding constraint below). Higher portfolio value could be used to keep difference between computed optimal weights and effective weights based on position quantities small. Lower portfolio value or higher asset price would normally increase discretization error.

Constraint methods could be chained to produce complex optimization rules:

```
# create portfolio and add positions
portfolio=portfolio_create(fromTime="2014-10-02 09:30:00", toTime="2014-10-03 16:00:00")
portfolio_addPosition(portfolio, symbol=c('C','GOOG'), quantity=c(500,600))
# set optimization goal
optimizer=optimization_goal(portfolio, goal="SharpeRatio", direction="maximize")
# add constraints
optimization_constraint_beta(optimizer,"=",0)
optimization_constraint_weight(optimizer,"<=",0.5, "GOOG")
optimization_constraint_variance(optimizer,">=",0.02)
# launch optimization and obtain optimal portfolio
optimalPortfolio=optimization_run(optimizer)
plot(optimalPortfolio)
```

```
% create portfolio and add positions
portfolio=portfolio_create('fromTime','2014-10-02 09:30:00', 'toTime','2014-10-03 16:00:00');
portfolio_addPosition(portfolio, {'C';'GOOG'}, [500;600]);
% set optimization goal
optimizer=optimization_goal(portfolio, 'goal', 'Return','direction', 'maximize');
% add constraints
optimizer=optimization_constraint_beta(optimizer,'=',0);
optimizer=optimization_constraint_weight(optimizer,'<=',0.5, 'GOOG');
optimizer=optimization_constraint_variance(optimizer,'>=',0.02);
% launch optimization and obtain optimal portfolio
optimalPortfolio=optimization_run(optimizer);
plot(optimalPortfolio)
```

The following constraint methods are available:

- optimization_constraint_allWeights
- portfolio weights of all positions
- optimization_constraint_weight
- portfolio position weights
- optimization_constraint_sumOfAbsWeights
- portfolio's sum of absolute positions weights for selected positions
- optimization_constraint_return
- portfolio return
- optimization_constraint_expectedReturn
- portfolio expected return
- optimization_constraint_variance
- portfolio returns variance
- optimization_constraint_beta
- portfolio beta
- optimization_constraint_VaR
- portfolio Value-at-Risk
- optimization_constraint_CVaR
- portfolio Conditional Value-at-Risk (Expected Tail Loss)
- optimization_constraint_modifiedSharpeRatio
- portfolio modified Sharpe Ratio
- optimization_constraint_sharpeRatio
- portfolio Sharpe Ratio
- optimization_constraint_starrRatio
- portfolio STARR Ratio
- optimization_constraint_portfolioValue
- portfolio value