Portfolio Optimizer

The PortfolioOptimizer 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.

Using the PortfolioOptimizer assumes that there is a returns stream from which all other asset classes are benchmarked against. This is the first index in the assets axis.

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.

The PortfolioOptimizer houses the following convenience methods:

maximize_returns

Maximize the returns of the portfolio. You may put in volatility or CVaR constraints for this procedure.

minimize_volatility

Minimizes the total portfolio volatility

minimize_cvar

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

maximize_sharpe_ratio

Maximizes the Sharpe ratio of the portfolio.

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

PortfolioOptimizer houses several common pre-specified optimization routines.

PortfolioOptimizer assumes that the optimization model has no uncertainty. That is, the portfolio is expected to undergo a single fixed scenario in the future. By default, the PortfolioOptimizer will automatically add an equality constraint that forces the portfolio weights to sum to 1.

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 – If True, portfolio weights must sum to 1.

  • 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

See also

BaseOptimizer

Base Optimizer

OptData

Optimizer data wrapper

maximize_returns(max_vol=None, max_cvar=None, x0=None, *, percentile=5.0, tol=0.0, initial_solution='random', random_state=None)[source]

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

If max_vol 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 (scalar, optional) – Maximum tracking error allowed

  • max_cvar (scalar, optional) – Maximum cvar_data allowed

  • x0 (ndarray) – Initial vector. Starting position for free variables

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

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

  • initial_solution (str, optional) – The method to find the initial solution if the initial vector x0 is not specified. Set as None to disable. However, if disabled, the initial vector must be supplied.

  • random_state (int, optional) – Random seed. Applicable if initial_solution is not None

Returns

Optimal weights

Return type

ndarray

maximize_sharpe_ratio(x0=None, *, initial_solution='random', random_state=None)[source]

Maximizes the sharpe ratio the portfolio.

Parameters
  • x0 (array_like, optional) – Initial vector. Starting position for free variables

  • initial_solution (str, optional) – The method to find the initial solution if the initial vector x0 is not specified. Set as None to disable. However, if disabled, the initial vector must be supplied.

  • random_state (int, optional) – Random seed. Applicable if initial_solution is not None

  • initial_solution – The method to find the initial solution if the initial vector x0 is not specified. Set as None to disable. However, if disabled, the initial vector must be supplied.

  • random_state – Random seed. Applicable if initial_solution is not None

Returns

Optimal weights

Return type

ndarray

minimize_cvar(min_ret=None, x0=None, *, percentile=5.0, tol=0.0, initial_solution='random', random_state=None)[source]

Maximizes the conditional value at risk of the portfolio. The present implementation actually minimizes the expected shortfall. Maximizing this value means you stand to lose less (or even make more) money during bad times

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

  • x0 (ndarray) – Initial vector. Starting position for free variables

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

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

  • initial_solution (str, optional) – The method to find the initial solution if the initial vector x0 is not specified. Set as None to disable. However, if disabled, the initial vector must be supplied.

  • random_state (int, optional) – Random seed. Applicable if initial_solution is not None

Returns

Optimal weights

Return type

ndarray

minimize_volatility(min_ret=None, x0=None, *, tol=0.0, initial_solution='random', random_state=None)[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

  • x0 (ndarray) – Initial vector. Starting position for free variables

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

  • initial_solution (str, optional) – The method to find the initial solution if the initial vector x0 is not specified. Set as None to disable. However, if disabled, the initial vector must be supplied.

  • random_state (int, optional) – Random seed. Applicable if initial_solution is not None

Returns

Optimal weights

Return type

ndarray