Click here to Skip to main content
13,257,236 members (48,230 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 15 Jul 2010

Investment Performance with Monte Carlo Simulation

, 15 Jul 2010
Rate this:
Please Sign up or sign in to vote.
Provides a class for random sampling of an approximated normal distribution and a form which implements a simple investment example.


Monte Carlo simulation is a technique that relies on random sampling to compute meaningful predictions. It is especially useful when studying systems with significant uncertainty or risk. Although statistical models often exist for modeling processes in a general way, Monte Carlo simulation provides an opportunity to examine experimental values and to study the range and tendencies of outcomes.  It is also readily adaptable to extremely specific scenarios, including those in which multiple variables or processes may depend on or be influenced by each other.

Although there is not a single preferred technique for creating a Monte Carlo simulation, approaches to this task usually involve a range of inputs, randomly selected from a population/distribution with known characteristics (mean, standard deviation, skewness, etc.) After sampling the distribution for values, the simulator performs a deterministic calculation to determine an outcome based on the set of inputs specific to that iteration. Monte Carlo simulations derive their main credibility from the analyst's ability to run the same simulation hundreds or thousands of times to generate a pool of outcomes from which statistically significant conclusions can be drawn.

Using the code

The included code consists of a Simulation class and a form which demonstrates the use of that class to solve a specific problem. Clearly, the class could be adapted or used as is to investigate other simulation scenarios. In this case, however, I have modeled outcomes useful to someone trying to decide if they have enough money to retire. Simply, the user specifies an initial investment (their savings), an expected rate of return at which those savings are invested, the historic or expected standard deviation of the investment, and an annual withdrawal rate (say, $35,000).

The example would be more accurate if it took into account other factors, for example, a variable rate of inflation. Additionally, it would be straightforward to expand the example to model multiple investments, each with its own expected rate of return and risk. The results table could sum the expected outcomes of each investment's performance and produce a combined forecasted value. However, for the sake of simplicity, I have chosen to keep the demonstration form simple and easily understood.

When inputs are entered and a request to calculate is made, a table is constructed which contains the outcome of each simulation trial (default, 2000 trials). Similarly, a table is built and displayed which contains some informational statistics which summarize the outcomes. This displays the relative year number, the account balance, and the likelihood that the investor has reached the end of their savings in a particular year. The experimental data is ripe for additional analysis, and could be expanded to include confidence intervals, or to answer slightly different but related questions.

The simulation class itself is constructed with parameters which describe the distribution to be sampled. Here, we assume the normal distribution, and describe it with a mean, mu, and a standard deviation, sigma. Clearly, other distributions may be more appropriate for specific modeling problems, for instance, a lognormal distribution or a binomial distribution. The instanced class exposes a method, GetSimulatedValue(), which returns a single random sample value from a pool matching the specified distribution's characteristics.

The class can be instanced and the sampling method called, like so:

Simulation simulator = new Simulation(mu, sigma);
for (Int32 instance = 0; instance < 2000; instance++)
    Double simValue = simulator.GetNextSimValue();
    //collect the retrieved values in an array, datatable, or collection...
//summarize the results by calculating relevant statistics


This code has not been thoroughly tested and may contain bugs. It is intended to be instructive about simulation techniques, and should not be relied upon for actual data to be used in decision-making. It has not been optimized for performance or efficiency, and the code as it is written is not particularly elegant or flexible. My goal was to make it as easy to read and understand as possible, so that others can create their own functions which implement the concepts described. That said, I do welcome constructive feedback if you see a bug or some glaring omission, or perhaps you feel I could have explained something more clearly.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kerry Cakebread
Software Developer
United States United States
I learned my first programming language--Apple Basic--in 1989. Over the years, I've done projects in C/C++, Pascal, FoxPro, 4D, AS/400, dBase, perl/CGI, Access, MSSQL, VB5-6/COM+, Classic ASP, uncounted legions of Windows, Mac, and *nix scripting languages and now C# ASP.NET/MVC/Razor/jQuery.

I started getting paid to do this stuff in 1993 as the admin for a 100 node network and began writing one-off apps for the company in my spare time. By 2002, I was developing and managing enterprise software projects full time.

I sat through so many Microsoft classes that they should offer me an honorary MCSE, MCSD, and a bunch of other letters, plus name something on campus after me. I took an undergraduate degree in Management & Business Information Systems, earned an MBA, and I hold a PMP credential, though trying to bring projects to successful completion (as opposed to tracking processes into perpetuity) using the PMBOK is like trying to get to a nice restaurant in a big city by reading a book about its architecture. But, I digress...

I've worked in the building materials industry supporting wholesale trading since 1993. I maintain an unhealthy level of interest in exchange-based and cash forward trading, derivatives, simulation, forecasting, project management, and other quantitative analysis topics (e.g. queue theory, optimal inventory policy, etc.) Most recently, I finished a Systems Science certificate in Computer Modeling & Simulation.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
dbswinford19-Jul-10 12:39
memberdbswinford19-Jul-10 12:39 
GeneralRe: My vote of 4 Pin
Kerry Cakebread20-Jul-10 8:43
memberKerry Cakebread20-Jul-10 8:43 
GeneralRe: My vote of 4 Pin
Southmountain10-Sep-17 16:14
memberSouthmountain10-Sep-17 16:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 15 Jul 2010
Article Copyright 2010 by Kerry Cakebread
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid