# GLPK/Portfolio Optimization

This example is taken from investment theory, a branch of financial economics. The aim is to achieve the best tradeoff between return and an investor's tolerance for risk when allocating capital among a set of potential investments with known historical performance.

## Summary

This tutorial example shows how to use GLPK/GMPL to optimize investment portfolios where investment risk is measured with the Mean Absolute Deviation (MAD) criterion. In addition to portfolio optimization, the example highlights techniques that may be useful in other GMPL applications, including
* Cholesky decomposition of positive definite matrices.
* Generating samples from a multivariate Normal distribution for the simulation of historical returns.
* Seeding GLPK's random number generator from GMPL. Note: a new randseed option is currently under evaluation.
Additional examples by the same author are available at Introduction to Operations Research.

## Portfolio Optimization for Mean Absolute Deviation (MAD)

Portfolio optimization refers to the process of allocating capital among a set of financial assets to achieve a desired tradeoff between risk and return. The classical Markowitz approach to this problem measures risk using the expected variance of the portfolio return. This criterion yields a quadratic program for the relative weights of assets in the optimal portfolio.
In 1991, Konno and Yamazaki proposed a linear programming model for portfolio optimization whereby risk is measured by the mean absolute deviation (MAD) from the expected return. Using MAD as the risk metric produces portfolios with several desirable properties not shared by the Markowitz portfolio, including second order stochastic dominance.
As originally formulated by Konno and Yamazaki, one starts with a history of returns $R_{i}(t_{n})$ for every asset in a set $S$ of assets. The return at time $t_{n}$ is determined by the change in price of the asset, $R_{i}(t_{n})={(P_{i}(t_{n})-P_{i}(t_{n-1}))}/{P_{i}(t_{n-1})}$. For each asset, the expected return is estimated by
${\bar {R}}_{i}\approx {\frac {1}{N}}\sum _{n=1}^{N}R_{i}(t_{n})$
The investor specifies a minimum required return $R_{p}$. The portfolio optimization problem is to determine the fraction of the total investment allocated to each asset, $w_{i}$, that minimizes the mean absolution deviation from the mean
$\min _{w_{i}}{\frac {1}{N}}\sum _{n=1}^{N}|\sum _{i\in S}w_{i}(R_{i}(t_{n})-{\bar {R}}_{i})|$
subject to the required return and a fixed total investment:
${\begin{array}{rcl}\sum _{i\in S}w_{i}{\bar {R}}_{i}&\geq &R_{p}\\\quad \\\sum _{i\in S}w_{i}&=&1\end{array}}$
The value of the minimum required return, $R_{p}$ expresses the investor's risk tolerance. A smaller value for $R_{p}$ increases the feasible solution space, resulting in portfolios with lower values of the MAD risk metric. Increasing $R_{p}$ results in portfolios with higher risk. The relationship between risk and return is a fundamental principle of investment theory.
This formulation doesn't place individual bounds on the weights $w_{i}$. In particular, $w_{i}<0$ corresponds to short selling of the associated asset. Constraints can be added if the investor imposes limits on short-selling, on the maximum amount that can be invested in a single asset, or other requirements for portfolio diversification. Depending on the set of available investment opportunities, additional constraints can lead to infeasible investment problems.

## Implementation Details

The purpose of this example is to demonstrate the modeling of portfolio optimization problems in GMPL. The main tasks are to recast the mean absolute deviation objective in linear form, and to simulate historical returns given the mean and covariances for a set of potential investments.

### Reformulation of the MAD Objective

The following formulation of the objective function is adapted from "Optimization Methods in Finance" by Gerald Curnuejols and Reha Tutuncu (2007) which, in turn, follows Feinstein and Thapa (1993). The model is streamlined by introducing decision variables $y_{n}\geq 0$ and $z_{n}\geq 0$, $n=1,\ldots ,N$ so that the objective becomes
$\min _{w_{i},y_{n},z_{n}}{\frac {1}{N}}\sum _{n=1}^{N}(y_{n}+z_{n})$
subject to constraints
${\begin{array}{rcl}\sum _{i\in S}w_{i}{\bar {R}}_{i}&\geq &R_{p}\\\\\sum _{i\in S}w_{i}&=&1\\\\y_{n}-z_{n}&=&\sum _{i\in S}w_{i}(R_{i}(t_{n})-{\bar {R}}_{i})\quad {\mbox{for}}\quad n=1,\ldots ,N\end{array}}$
As discussed by Feinstein and Thapa, this version reduces the problem to $N+2$ constraints in $2N+{\mbox{card}}(S)$ decision variables, noting the card function.

### Seeding the GLPK Pseudo-Random Number Generator

Unfortunately, GMPL does not provide a method to seed the pseudo-random number generator in GLPK. Instead, the following GMPL code fragment uses the function gmtime to find the number of seconds since 1970. Dropping the leading digit avoids subsequent overflow errors, and the square returns a number with big changes every second. Extracting the lowest digits produces a number between 0 and 100,000 that determines how many times to sample GLPK's pseudo-random number generator prior to subsequent use. This hack comes with no assurances regarding its statistical properties.

### Simulation of the Historical Returns

For this implementation, historical returns are simulated assuming knowledge of the means and covariances of asset returns. We begin with a vector of mean returns ${\bar {R}}$ and covariance matrix $\Sigma$ estimated by
$\Sigma _{ij}\approx {\frac {1}{N-1}}\sum _{n=1}^{N}(R_{i}(t_{n})-{\bar {R}}_{i})(R_{j}(t_{n})-{\bar {R}}_{j})$
Simulation of historical returns requires generation of samples from a multivariable normal distribution. For this purpose we compute the Cholesky factorization where, for a positive semi-definite $\Sigma$, $\Sigma =CC^{T}$ and $C$ is a lower triangular matrix. The following GMPL code fragment implements the Cholesky-Crout algorithm.
Without error checking, this code fragment fails unless $\Sigma$ is positive definite. The covariance matrix is normally positive definite for real-world data, so this is generally not an issue. However, it does become an issue if one attempts to include a risk-free asset, such as a government bond, in the set of investment assets.
Once the Cholesky factor $C$ has been computed, a vector of simulated returns $R(t_{n})$ is given by $R(t_{n})={\bar {R}}+CZ(t_{n})$ where the elements of $Z(t_{n})$ are independent samples from a normal distribution with zero mean and unit variance.

## PortfolioMAD.mod

Save this model as PortfolioMAD.mod.

## Results

Typical output follows. The results show that a well designed portfolio can exhibit a substantially improved risk/return performance compared to the risk/return performance of individual assets.

## Possible Extensions

There are number of extensions to this example that would make it more useful in real world settings:
* Add error checking for the Cholesky factorization.
* Add upper and lower (for example, no short selling) bound constraints on weighting coefficients.
* Add the ability to specify risk-free assets for the portfolio.
* Add constraints to impose diversification requirements.
* Add parametric analysis for the risk/return tradeoff (will require external scripting)
* Develop a better means of seeding GLPK's random number generator from within GMPL.
* Add an ODBC database interface for empirical (rather than simulated) historical returns data.

## References

* ↑ Konno, Hiroshi; Yamazaki, Hiroaki (1991). "Mean-absolute deviation portfolio optimization model and its applications to Tokyo stock market". Management Science. 37: 519–531.
* ↑ Curnuejols, Gerald; Tutuncu, Reha (2007). Optimization Methods in Finance. Cambridge University Press.
* ↑ Feinstein, Charles D.; Thapa, Mukund N. (1993). "A Reformulation of a Mean-Absolute Deviation Portfolio Optimization Model". Management Science. 39: 1552–1553.