Portfolio analysis addresses a problem of wealth management. It seeks to answer this fundamental question for an individual or institutional investor: What percentage of this investor's wealth should be allocated to which risky assets or asset classes (e.g. stocks, bonds, real estate)? The objective is to construct a portfolio that strikes an appropriate balance between the portfolio's return and a measure of the portfolio's risk.
This example presents an implementation of certain traditional methods of portfolio analysis. But it also demonstrates how large problems can be addressed in Hava, by breaking the entire program into a number of smaller Hava files, each of which contributes a share of the overall functionality.
To undertake portfolio analysis, certain generic operations are required that will be useful for solving other problems. These generic operations are grouped into Hava source files called library modules.
One such module, Matrix.hava, defines functions that perform matrix operations. Since Matrix.hava defines no variables, it produces no output in the right panel. To access these functions from another Hava program, you use the import statement, as shown here.
We will use a second module, RandomVariateGenerator.hava, to generate vectors of correlated normally-distributed random numbers. Note that the RandomVariateGenerator module imports the Matrix module to perform its task. To make use of RandomVariateGenerator functions, we must import it, as shown here.
To undertake portfolio analysis, information about the assets must be provided. A sample specification is provided in the module PortfolioAnalysisSpecification.hava. This sample is easily overridden to represent your problem instance. For example, to learn more about the tails of the return distribution, one might create the modified specification shown here.
The expected return and standard deviation of a given portfolio can be calculated directly. It is also of interest to compute an empirical cumulative distribution of returns, and compute sample moments. A collection of random return scenarios must be generated for this purpose. This purpose is served by the module PortfolioAnalysisScenarios.hava. Notice that it imports all three modules discussed earlier: Matrix.hava, PortfolioAnalysisSpecification.hava, and RandomVariateGenerator.hava. This is because scenario generation uses matrix operations, requires a problem specification, and obviously needs to generate random numbers.
In extending the library module, the user might modify the number scenarios to generate. The user also has the option to enable replication, which ensures that random numbers will be generated identically every time the program is executed. Finally, when replicating, the user can specify a particular randomly generated dataset (identified by the variable
GeneratedDataSetIndex). All of which is
The module PortfolioAnalysis.hava automatically generates several common portfolios for evaluation. These include the equally weighted and minimum variance portfolios, as well as several portfolios that are mean-variance efficient. For each portfolio, several statistics as well as the empirical return distribution are displayed as output.
Our hypothetical user can, of course, import this file and provide different portfolios for evaluation, as shown here. Hava makes it easy to display this output in convenient
Notice that the imported analysis output is no longer displayed since
private has been added to the import statement.
Using the downloadable application, which support additional features, one might even import a list of scenario data from a spreadsheet, as shown here. This time, the import statement was placed at the bottom, so that the new scenario data would appear before the imported analysis output.