Risk Analysis

Summary

This demo illustrates the process of analyzing several indices to build a portfolio based on balancing expected returns and acceptable risk. There are five indices that form the basis of the analysis: Financial Times 100, Nikkei 225, S&P 500, 10 year US Treasuries, and Gold. The probability of expected daily returns can be computed for each index individually or combined into a portfolio. This example is different than many others because of the use of a driving window to lead the user through each step.

Usage

The demo starts up with just the driver window; press the right arrow to begin. The first chart is the historical time series of each of the five indices used for analysis. The separate y axes are linked by color to the represented line. Clicking the right arrow again leads to the summary view for each individual index. Select on index in the radio box in the "Select" dialog. Basic summary statistics are presented in a text dialog while the probability of daily returns is plotted as a histogram in the main chart window. Note that the breadth of the histogram is a measure of volatility: the wider the plot, the more volatile the index.

Clicking the right arrow again brings us to the final set of windows. Here, the composition of the portfolio and the number of samples to use in the simulation can be changed in the "Edit" dialog. Pressing "Update" will update the pie chart in the "Portfolio" window and will run the Monte Carlo simulation for the portfolio. As before, the probability of daily returns is plotted as a histogram. To see the effect of changing the portfolio composition, set the value for tnx to 1000.0. This adds 1000 shares of T-Bills to the portfolio. As these are a historically stable investment, you should see the histogram become narrower (less volatility) when the simulation is rerun by pressing "Update."

JMSL Library Math/Stat Classes

com.imsl.stat.Covariances - this class is used to compute the covariance matrix of the returns of the indices. It is called from Driver.java and the results are passed to the constructor of Simulate.java.

com.imsl.math.Cholesky - the Cholesky factorization of the computed covariance matrix is created with this class. The resulting object is used in the nextMultivariateNormal() method discussed below.

com.imsl.stat.Random - specifically, the nextMultivariateNormal() method of this class is used in the runMonteCarlo() method of Simulate.java to simulate the future behavior of the modeled indices.

com.imsl.stat.Summary - this class is used to report the summary statistics such as Minimum, Maximum, Mean, Standard Deviation, Variance, Skewness and Kurtosis. Values from a time series are added to a Summary object using the update() method; the statistics are returned using the get methods, like getMinimum(). This class is used in both ChartReturns.java and Simulate.java.

com.imsl.IMSLException - this class is used in Simulate.java because the Cholesky class can throw a number of exceptions. Any of these exceptions are caught and simply reported in the update() method of Driver.java.

JMSL Library Charting Classes

A variety of charting classes are used in this application. The main chart uses com.imsl.chart.Candlestick or com.imsl.chart.HighLowClose while the Volume chart uses com.imsl.chart.Bar. When just line charts are requested, a new com.imsl.chart.Data object is created and setDataType(Data.DATA_TYPE_LINE) is used. For the filled area volume chart, setFillColor("blue") is used instead of setDataType().

When an overlay is selected, a Legend is drawn by setting chart.getLegend().setPaint(true). The setViewport() method is called to position the legend in the upper right corner, so as to not impinge on the main chart. When the overlay is set to "None", we call chart.getLegend().setPaint(false).

Java Code

The data for this demo are stored in several csv files. For code regarding the reading of the files and the storing of the time series into a Hashmap, consult the code in Database.java and ReadYahooCSV.java.

To build a histogram using the Bar class is fairly straightforward. These few lines of code build the histogram of daily returns in ChartReturns.java in the compute() method:

int nSamples = series.close.length - 1;
int nBins = 50;
double
max = 0.05;

// Setup the bins
double bins[] = new double[nBins];
double
dx = 2.*max/nBins;
double
x[] = new double[nBins];
for
(int k = 0; k < nBins; k++) {
    x
[k] = -max + (k+0.5)*dx;
}

for
(int k = 0; k < nSamples; k++) {
    double
t = (series.close[k+1]-series.close[k])/series.close[k];
    int j = (int)Math.round((t+max-0.5*dx)/dx);
    if
(j >= 0 && j < nBins) bins[j]++;     // Key step in creating the histogram
}

for (int k = 0; k < bins.length; k++) bins[k] /= nSamples;
bar
= new Bar(axis, x, bins);
bar
.setBarType(bar.BAR_TYPE_VERTICAL);
bar
.setFillColor("green");
bar
.setBarWidth(0.5*dx);

 

Links to Source Code

Driver.java This is the main class for this demo. It is a JFrame that holds the navigation arrows and the JEditorPane with the HTML descriptions. ChartSeries, ChartReturns, and Simulate are all spawned from here.
ChartReturns.java This class extends JFrameChart and charts the individual indices as a histogram. The Statistics JDialog and Options JDialog classes are here too.
ChartSeries.java This JFrameChart creates the line chart showing all five indices.
Database.java The class calls ReadYahooCSV to read in all the time series from disk and creates a Hashmap that can be accessed through its public getSeries() methods.
MonteCarloProgress.java This class is a fairly standard implementation of a JProgressBar, extending JDialog.
PortfolioPie.java This class JDialog contains a JPanelChart that holds a Pie chart. It is created by Simulate.
ReadYahooCSV.java This class reads the comma separated value files from disk.
Simulate.java This class extends JFrameChart and contains the portfolio simulation code, creates a PortfolioPie, and uses the MonteCarloProgress class.

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.