# Active Portfolio Optimizer¶

The ActivePortfolioOptimizer inherits the BaseOptimizer to add several convenience methods. These methods include common optimization programs which would be tedious to craft with the BaseOptimizer over and over again. Of course, as an extension, it can do anything that the BaseOptimizer can. However, if that’s the goal, it would be better to stick with the BaseOptimizer to reduce confusion when reading the code.

ActivePortfolioOptimizer houses the following convenience methods:

maximize_eva

Maximize the expected value added of the portfolio. The objective function is the same with maximize returns as it just maximizes the total returns. For risk constraints, this method by default will constrain on tracking error and total CVaR. That is the volatility (tracking error) is calculated with the first variable (usually a passive portion) set to 0. Total CVaR has no treatments done to it. You can override these defaults in the method itself.

minimize_tracking_error

Minimizes the tracking error of the portfolio. Tracking error is calculated by setting the first variable to 0 whilst the rest are updated by the optimizer.

minimize_volatility

Minimizes the total portfolio volatility

minimize_cvar

Minimizes the conditional value at risk (expected shortfall of the portfolio)

maximize_info_ratio

Maximizes the information ratio of the portfolio. The information ratio of the portfolio is calculated like the Sharpe ratio. The only difference is the first variable is set to 0.

maximize_sharpe_ratio

Maximizes the Sharpe ratio of the portfolio.

class allopy.optimize.ActivePortfolioOptimizer(data, algorithm=40, cvar_data=None, rebalance=False, time_unit='quarterly', sum_to_1=False, *args, **kwargs)[source]
__init__(data, algorithm=40, cvar_data=None, rebalance=False, time_unit='quarterly', sum_to_1=False, *args, **kwargs)[source]

The ActivePortfolioOptimizer houses several common pre-specified optimization routines.

ActivePortfolioOptimizer assumes that the optimization model has no uncertainty. That is, the portfolio is expected to undergo a single fixed scenario in the future.

Notes

ActivePortfolioOptimizer is a special case of the PortfolioOptimizer where the goal is to determine the best mix of of the portfolio relative to ba benchmark. By convention, the first asset of the data is the benchmark returns stream. The remaining returns stream is then the over or under performance of the returns over the benchmark. In this way, the optimization has an intuitive meaning of allocating resources whilst taking account

For example, if you have a benchmark (beta) returns stream, 9 other asset classes over 10000 trials and 40 periods, the simulation tensor will be 40 x 10000 x 10 with the first asset axis being the returns of the benchmark. In such a case, the active portfolio optimizer can be used to optimize the portfolio relative to the benchmark.

Parameters
• data ({ndarray, OptData}) – The data used for optimization

• algorithm ({int, string}) – The algorithm used for optimization. Default is Sequential Least Squares Programming

• cvar_data ({ndarray, OptData}) – The cvar_data data used as constraint during the optimization. If this is not set, will default to being a copy of the original data that is trimmed to the first 3 years. If an array like object is passed in, the data must be a 3D array with axis representing time, trials and assets respectively. In that instance, the horizon will not be cut at 3 years, rather it’ll be left to the user.

• rebalance (bool, optional) – Whether the weights are rebalanced in every time instance. Defaults to False

• time_unit ({int, 'monthly', 'quarterly', 'semi-annually', 'yearly'}, optional) – Specifies how many units (first axis) is required to represent a year. For example, if each time period represents a month, set this to 12. If quarterly, set to 4. Defaults to 12 which means 1 period represents a month. Alternatively, specify one of ‘monthly’, ‘quarterly’, ‘semi-annually’ or ‘yearly’

• sum_to_1 (bool) – If False, the weights do not need to sum to 1. This should be False for active optimizer.

• args – other arguments to pass to the BaseOptimizer

• kwargs – other keyword arguments to pass into OptData (if you passed in a numpy array for data) or into the BaseOptimizer

BaseOptimizer

Base Optimizer

OptData

Optimizer data wrapper

maximize_eva(max_vol=None, max_cvar=None, percentile=5.0, x0=None, *, as_tracking_error=True, as_active_cvar=False, tol=0.0)[source]

Optimizes the expected value added of the portfolio subject to max tracking error and/or cvar constraint. At least one of the tracking error or cvar constraint must be defined.

If max_te is defined, the tracking error will be offset by that amount. Maximum tracking error is usually defined by a positive number. Meaning if you would like to cap tracking error to 3%, max_te should be set to 0.03.

Parameters
• max_vol (float, optional) – Maximum tracking error allowed

• max_cvar (float, optional) – Maximum cvar_data allowed

• percentile (float) – The CVaR percentile value. This means to the expected shortfall will be calculated from values below this threshold

• as_active_cvar (bool) – If True, the cvar constraint is calculated using the active portion of the weights. That is, the first value is forced to 0. If False, the cvar constraint is calculated using the entire weight vector.

• as_tracking_error (bool) – If True, the volatility constraint is calculated using the active portion of the weights. That is, the first value is forced to 0. If False, the volatility constraint is calculated using the entire weight vector. This is also known as tracking error.

• tol (float) – A tolerance for the constraints in judging feasibility for the purposes of stopping the optimization

Returns

Optimal weights

Return type

ndarray

maximize_info_ratio(x0=None)[source]

Maximizes the information ratio the portfolio.

Parameters

Returns

Optimal weights

Return type

ndarray

maximize_sharpe_ratio(x0=None)[source]

Maximizes the Sharpe ratio the portfolio.

Parameters

Returns

Optimal weights

Return type

ndarray

minimize_cvar(min_ret=None, x0=None, *, percentile=5.0, as_active_cvar=False, as_active_return=False, tol=0.0)[source]

Minimizes the conditional value at risk of the portfolio. The present implementation actually minimizes the expected shortfall.

If the min_ret is specified, the optimizer will search for an optimal portfolio where the returns are at least as large as the value specified (if possible).

Parameters
• min_ret (float, optional) – The minimum returns required for the portfolio

• percentile (float) – The CVaR percentile value for the objective. This means to the expected shortfall will be calculated from values below this threshold

• as_active_cvar (bool, optional) – If True, minimizes the active cvar instead of the entire portfolio cvar. If False, minimizes the entire portfolio’s cvar

• as_active_return (bool, optional) – If True, the returns constraint is calculated using the active portion of the weights. That is, the first value is forced to 0. If False, the returns constraint is calculated using the entire weight vector.

• tol (float) – A tolerance for the constraints in judging feasibility for the purposes of stopping the optimization

Returns

Optimal weights

Return type

ndarray

minimize_tracking_error(min_ret=None, x0=None, *, as_active_return=False, tol=0.0)[source]

Minimizes the tracking error of the portfolio

If the min_ret is specified, the optimizer will search for an optimal portfolio where the returns are at least as large as the value specified (if possible).

Parameters
• min_ret (float, optional) – The minimum returns required for the portfolio

• as_active_return (boolean, optional) – If True, the returns constraint is calculated using the active portion of the weights. That is, the first value is forced to 0. If False, the returns constraint is calculated using the entire weight vector.

• tol (float) – A tolerance for the constraints in judging feasibility for the purposes of stopping the optimization

Returns

Optimal weights

Return type

ndarray

minimize_volatility(min_ret=None, x0=None, *, as_active_return=False, tol=0.0)[source]

Minimizes the volatility of the portfolio

If the min_ret is specified, the optimizer will search for an optimal portfolio where the returns are at least as large as the value specified (if possible).

Parameters
• min_ret (float, optional) – The minimum returns required for the portfolio

as_active_return: boolean, optional

If True, the returns constraint is calculated using the active portion of the weights. That is, the first value is forced to 0. If False, the returns constraint is calculated using the entire weight vector.

tol: float

A tolerance for the constraints in judging feasibility for the purposes of stopping the optimization

Returns

Optimal weights

Return type

ndarray