Using open source software for portfolio analysis
|This article is coordinated with our sister Canadian wiki, finiki, the Canadian financial Wiki. See: Using open source software for portfolio analysis (finiki) On-going collaboration is in this Bogleheads' forum thread: [Wiki] - Using open-source software for portfolio analysis.|
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.
GnuCash is a free and open-source personal and business accounting software that uses professional accounting principles (double entry accounting), but yet is simple to use.
R is a language and environment for statistical computing and graphics.
RStudio is the free and open source integrated development environment (IDE) for R.
In the examples below, view the source code by clicking on the show/hide link of the title bar.
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.
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.
- Package info: CRAN - Package maRketSim, from CRAN, The Comprehensive R Archive Network. The reference manual can be found on this page.
- Working examples: maRketSim: maRketSim market simulator for R, from rdrr.io, the R documentation repository.
Downloading fund data
Bogleheads forum member camontgo has written a script to import fund data from Yahoo! Finance.
R is the preferred tool for performing multifactor regression analysis; a number of scripts are available in the referenced article under "R".
Risk parity strategy using 3x leveraged ETFs
|This script is intended for investing enthusiasts who intentionally deviate from a total market approach. The ability, willingness, and need to take this additional risk is understood. Please ask in the forum for advice.|
This script is a companion to Bogleheads® forum topic:.
Steps to make it work:
- Install R (and Rstudio).
- Download all the files from this link to the same folder--don't change any file names.
- Open the script, make sure your working directory is the folder with all the files and install the required packages at the top of the script.
You should be able to reproduce the chart in this forum post and the table in the 2nd post down from there.
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 Wisdom Forum
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.[note 1]
Efficient frontier (mean-variance optimization)
This is a 3-part series which calculates the efficient frontier for a set of securities.[note 2] 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.
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.
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.
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.
Forum member AlohaJoe has written several scripts:
- See if using CAPE10 to market time rebalancing makes a difference when using monthly data
- Build a bond fund simulator that allows me to simulate historical performance of bonds
- Extend that to do the same for Japanese government bonds, to see how bonds outside the US have performed
- Compare 18 different withdrawal strategies in retirement
- Compare 8 different "harvesting" strategies during retirement
- Compare the relative importance of early "bad returns" and "high inflation" on retirement
- Calculate safe withdrawal rates in Japan for various portfolio allocations
- Compare various income smoothing strategies in retirement
- Calculate how many months of gains are wiped out by various market corrections
Additional Python code and techniques, such as extracting Yahoo financial info from Yahoo!, can be found in this Bogleheads® forum topic:.
Forum member Simbilis has written a Python script which will scrape the Thrift Savings Plan website to extract a CSV (Comma Separated Value) file which can be imported into Quicken.
On-going support and discussion can be found in this forum thread: TSP share prices for Quicken
|TSP share prices for Quicken|
LibreOffice is a Microsoft Office replacement. Spreadsheets are probably the most used financial analysis software.
- See Matlab Clones for a detailed overview.
- The example covariance matrix could be replaced with a real covariance matrix. For example, the R-script for downloading Yahoo Finance returns could be used to get returns for a set of assets, and the covariance matrix could be calculated and used as the input to these scripts...though the scaling would probably need to be changed.
- The R Project for Statistical Computing
- MATLAB - The Language of Technical Computing
- MATLAB (on Wikipedia)
- Matlab Clones
- GNU Octave
- [Wiki] - Using open-source software for portfolio analysis, forum discussion.