Using open source software for portfolio analysis

 is a compilation of open source software used to analyze portfolios. Topics covered include regression analysis, Monte Carlo simulation, and other statistical methods.

R
R is a language and environment for statistical computing and graphics.

RStudio is the free and open source integrated development environment (IDE) for R.

R has many packages available at CRAN Task Views. There is a page for finance that lists the available packages.

In the examples below, view the source code by clicking on the show/hide link of the title bar.

Asset correlation
Create a correlation matrix among 4 different funds. A matrix containing the correlations among the 4 funds, along with a set of plots is displayed.

The source code is available on finiki:  Asset allocation

Bond market simulation
Bogleheads forum member linuxizer has developed a preliminary bond market simulator in R.
 * maRketSim - Bond market simulation in R, in the Bogleheads forum.

Downloading fund data
Bogleheads forum member camontgo has written a script to import fund data from Yahoo! Finance.

See: [http://www.calculatinginvestor.com/2013/09/19/downloading-batch-returns/ Downloading a Batch of Returns Using Yahoo! Finance], from The Calculating Investor

Multifactor regression
R is the preferred tool for performing multifactor regression analysis; a number of scripts are available in the referenced article under "R".

Statistical distribution moments, value at risk
Plot the 2nd (variance), 3rd (skewness), and 4th (kurtosis) statistical distribution moments of a fund. Additionally, examples of Value At Risk (VaR) are shown. The plots can be seen in the following forum post: Re: Risk = ?? and Re: Risk = ?? in the Financial Webring Forum

The source code is available on finiki:  Statistical distribution moments, value at risk

MATLAB clones
MATLAB® (MATrix LABoratory) is a high-level language and interactive environment for numerical computation, visualization, and programming. It is neither open source nor free. However, it's an industry standard and open source clones have been developed which strive to emulate MATLAB functionality.

Below are examples which run in Octave or MATLAB. Bogleheads forum member camontgo developed and maintains this code at The Calculating Investor.

Efficient frontier (mean-variance optimization)
This is a 3-part series which calculates the efficient frontier for a set of securities. These efficient frontier calculations are not very practical. The results are too sensitive to small changes in the inputs, but it is educational since a lot of financial theory builds on some of the ideas underlying Markowitz.


 * Calculating the Efficient Frontier: Part 1, Part 2, Part 3, from The Calculating Investor

Rebalancing bonus
This is a replication of an analysis by William Bernstein which performs a Monte Carlo analysis of two portfolios to determine the rebalancing bonus. This simulation adds some math which allows simulation of rebalancing with correlated assets. Bernstein's original analysis assumed no correlation.


 * Will I Get a Rebalancing Bonus in my Portfolio?, from The Calculating Investor

Market timing
In 1975, Nobel laureate William Sharpe published a study titled “Likely Gains from Market Timing”. In this paper, Sharpe reportedly found that a market timer who switches between 100% stocks and 100% T-bills on an annual basis must be correct about 74% of the time (on average) to beat the market.

This Monte Carlo simulation uses a simple market timing strategy to determine the market timing accuracy required to outperform buy-and-hold. A comparison of market timing to buy-and-hold in terms of both total returns and risk-adjusted returns (measured by the Sharpe Ratio) is performed. The analysis shows that a surprisingly high (and unlikely) degree of accuracy is necessary to beat the market return through market timing. The factor data is obtained from the Kenneth R. French - Data Library, under Fama/French Factors (direct link). Be sure to remove the file headers and the extra data sets (starting around row 1046). Save the file as F-F_Factors_annual.txt.
 * Market Timing: How good is good enough?, from The Calculating Investor

This simulation takes a long time to run. To start, reduce the number of iterations from 10,000 to a lower number (such as 100) and ensure that the analysis is functioning correctly.

Python
Python is a rapid development scripting language that is suitable for many tasks. Using add in libraries like NumPy and pandas make it easy to do financial analysis. There are many IDEs

The asset correlation analysis described above is available in Python. See finiki for the source code.

LibreOffice
LibreOffice is a Microsoft Office replacement. Spreadsheets are probably the most used financial analysis software.