Portfolio Optimization

Summary

This demo illustrates how the JMSL Library can be used in portfolio optimization. Given a set of stocks, what is the distribution that gives the best rate of return for an acceptible amount of risk? First, we can compute the Efficient Frontier, which gives a set of portfolios on the boundary of the acheivable risk-return space which give the least risk for a given rate of return. To find the best portfolio of that set, one draws a line tanget to the Efficient Frontier through the risk-free rate of return.

Usage

To constrain the content of the portfolio, use the slider to set the minimum and maximum percentage for each index. If you want to exclude an index, un-check the box, and it's minimum and maximum will be set to zero. Set the risk free rate of return using the slider. Press the Apply Constraints button to compute the Efficient Frontier and find the optimal portfolio. The parameter summary and solution text area will be updated, and the optimal portfolio's contents will be shown in a pie chart.

Move the mouse over the Efficient Frontier curve to see the contents of the portfolio at that location graphically as a smaller pie chart.

JMSL Library Math/Stat Classes

com.imsl.stat.Covariances - This class computes the variance-covariance matrix of all the securities to consider.

com.imsl.math.LinearProgramming - This class is used to compute the extrema of the Efficient Frontier. It is called initially to find the minimum possible return. Since it is a minimization routine, it is called a second time with the negative values of the expected returns for each stock to find the maximum possible return.

com.imsl.math.QuadraticProgramming - This class is called in a loop for one hundred values of return between the extrema found with LinearProgramming. For each value of return, this class helps find the portfolio with the minimum amount of risk. The solution is a vector of weights for the stocks to build this optimal portfolio.

JMSL Library Charting Classes

This examples has a chart with two lines (the Efficient Frontier curve and the Capital Market Line tangent to the Efficient Frontier and going through the risk-free rate of return). It also contains two Pie charts which are inset using the setViewport() method.

Java Code

The output required from QuadraticProgramming is actually the "optimal object function found", which is a keyword for the IMSL C Numerical Library version of the class (quadratic_prg), but for JMSL, the value must be computed manually. Mathematically, the value is .

qp = new QuadraticProgramming(varcov, qg, null, null, qa, qb) {
double[] distWeights = qp.getSolution();

for (int j=0; j<m; j++) {
    tmp += distWeights[j] * dot(m, varcov[j], distWeights);
}
double obj = 0.5*tmp + dot(m, varcov[j], distWeights);
solution[i][j] = Math.sqrt (0.5*obj)

For more details, consult the EfficientFrontier.java source.

Link to Source Code

Portfolio.java The Portfolio class extends JFrameChart. This class has an interface to set constraints and displays the solution graphically and as text.
EfficientFrontier.java The EfficientFrontier class accepts historical time series as input and returns the Efficient Frontier curve in risk-return space and the portfolio contents for each point.

Running This Demo

Two alternatives are available to run this demo:

1) Use the source code in your development environment as any other Java code. More information is available in the How To.

2) An executable jar file containing all of the demos referenced in this guide is included in the jmsl/lib directory. On Windows, you may double-click the file to run it if files with a ".jar" extension are properly registered with javaw.exe. Alternatively, for both Windows and UNIX environments, the jar file may be executed from the command line using java -jar gallery.jar.

As list of buttons, one for each demo, is created. Demos can be subsetted as they relate to specific areas (Math, Stat, Finance, Charting) by choosing the appropriate selection on the JComboBox. To run the Additional Demos, select Quick Start in the JComboBox.