"Migrating from Python to the Wolfram Language and Lessons Learned" Addendum: A Sample Monte Carlo Simulation Project
Author
David Stevens
Title
"Migrating from Python to the Wolfram Language and Lessons Learned" Addendum: A Sample Monte Carlo Simulation Project
Description
Project specifications for a Monte Carlo simlulation student assignement and code to compare the speeds of the Wolfram Language and Python.
Category
Essays, Posts & Presentations
Keywords
Education, Other Application Areas, Wolfram Cloud
URL
http://www.notebookarchive.org/2021-11-1vq23ne/
DOI
https://notebookarchive.org/2021-11-1vq23ne
Date Added
2021-11-04
Date Last Modified
2021-11-04
File Size
91.46 kilobytes
Supplements
Rights
Redistribution rights reserved
Download
Open in Wolfram Cloud
“Migrating from Python to the Wolfram Language and Lessons Learned” Addendum: A Sample Monte Carlo Simulation Project
“Migrating from Python to the Wolfram Language and Lessons Learned” Addendum: A Sample Monte Carlo Simulation Project
David Stevens, Academic Key Account Manager, Academic Sales
Example Project Layout
Computer Science 1XX
Computer Science 1XX
Project 1: Financial Monte Carlo Simulation
Due: October X
Project Overview
Project Overview
In this project, you will use a Monte Carlo simulation to estimate a simple return on investment, based on previous returns. You must implement your Monte Carlo simulation solution in the Wolfram Language and analyze your results. Your submission must also include a writeup discussing your approach, your implementation and your results, including the projected return on investment.
Problem Overview
Problem Overview
You plan to invest $1,000 for a single year in an S&P index fund, starting on January 1, 2015. You want to use the last 10 years of S&P returns on that fund as a basis.
Year | %Return |
2004 | 10.75 |
2005 | 4.79 |
2006 | 15.69 |
2007 | 5.39 |
2008 | -36.97 |
2009 | 26.42 |
2010 | 14.93 |
2011 | 2.06 |
2012 | 15.84 |
2013 | 32.21 |
As an example of how a return is computed, presume that you invested $1,000 in the fund at the start of 2007. The return as a proportion would have been 0.0539 (just divide the percent return by 100), so your proportional return on investment would have been 1 + 0.0539 = 1.0539. The 1 represents the proportion of your initial investment, and the return as a proportion will determine whether that amount goes up or down, based on whether the return is positive or negative. In other words, the proportion of your initial investment at the end of the year would have been 1.0539, or 105.39%. Therefore, your amount of money at the end of 2007 would have been $1,000(1.0539) = $1,053.90. As another example, if you invested $1,000 at the start of 2008, your amount of money at the end of 2008 would have been $1,000(1–0.3697) = $630.30.
More generally, if you invest dollars and the return (as a proportion, not a percentage) is , the amount of money at the end of the year will be dollars.
D
p
D(p+1)
Using a Monte Carlo Simulation
Using a Monte Carlo Simulation
In this context, you want to use the Monte Carlo simulation by:
◼
Sampling a percent return at random
◼
Computing the amount of money that would be in your account based on that percent return
◼
Repeating the two-step process above many times, producing many simulated returns on investments
Then you will want to perform statistical analyses on the results (e.g. mean of the simulated returns, standard deviation of the simulated returns and histogram of the distribution of simulated returns).
Selecting one of the 10 returns from the table earlier is too limiting, however, as you will never get anything other than one of those 10 values. Instead, you want to model those returns using a normal(µ,σ) distribution, which you may know better as a bell curve. The normal distribution requires two parameters: , the mean of the distribution, and σ, the standard deviation of the distribution. For example, the mean of the proportional returns above is and the standard deviation is . Therefore, an appropriate distribution to use for sampling returns is normal(0.0911, 0.1799), which looks like the following figure:
µ
µ=0.0911
σ=0.1799
In[]:=
Plot[PDF[NormalDistribution[0.0911,0.1799],x],{x,-1,1}]
Therefore, your approach to the Monte Carlo simulation changes slightly according to the following:
◼
Draw a sample return from your normal(µ, σ) distribution
◼
Compute the amount of money that would be in your account at year’s end based on that return
◼
Repeat the previous two-step process many times, producing many simulated returns on investment
Implementation Suggestions
Implementation Suggestions
Specifications
Specifications
1
.Implement a method that accepts one parameter and a list of investment returns, then computes and returns the average of those investment returns (for computing µ for normal(µ,σ)).
2
.Implement a method that accepts two parameters, a list of investment returns and the mean of investment returns, then computes and returns the standard deviation for those investment returns (for computing σ). Recall that the standard deviation is computed using the squared difference about the mean according to the following formula: ,where is the number of investment returns in the list, is the mean of the investment returns in the list and is the investment return in the list.
σ=
1
n
n-1
Σ
i=0
2
(-µ)
r
i
n
µ
r
i
th
i
3
.Implement a method that accepts two parameters, and , and returns one sample from the normal(µ,σ) distribution. (Google “Wolfram Language normal variate” for help.)
µ
σ
4
.Implement a method that performs a Monte Carlo simulation of the investment problem as outlined in the earlier bulleted section. Your method must accept one parameter corresponding to the number of replications. Print each simulated amount of return on investment in dollars.
Analysis
Analysis
◼
◼
Compute the mean, standard deviation, and 5th and 95th quantile percent values for your distribution
Repeat this analysis for a few different runs of your Monte Carlo simulation using small to large values for the number of replications ().
n
Writeup
Writeup
Include a writeup with your submission in which you discuss:
◼
The overall objective of this project
◼
A clear but concise summary of your approach, discussed at a high level (no discussion of code)
◼
A discussion of your algorithmic approach: talk in terms of pseudocode (what you might draw on the whiteboard), not specifics of your implementation
◼
Analysis of your results, including histograms with corresponding statistics, dependence on number of replications, etc.
◼
Summary conclusions/suggestions based on your analysis (e.g. whether this seems to be a good fund to invest in, how you might improve your results, etc.)
Show your code at the bottom of the Wolfram Notebook in a separate section. A person should be able to read your writeup and understand the problem, your approach, results and conclusions without having to look at your program.
Be sure to include comments and good naming conventions in your code.
Be sure to include comments and good naming conventions in your code.
Project Writeup
Project Writeup
This study shows the need for large sample sizes (Table 1). Although a smaller sample size of 100 draws from the normal distribution curve gives a quick ballpark estimate of the returns if $1,000 were invested in the S&P index fund, the larger sample of 100,000 draws gives much more accurate results. The mean of the annual returns for 100 draws is about $25 below that of the annual return from 10,000,000 draws. Although the standard deviations are relatively similar, the 95% quantile is also lower by ~$30. Interestingly, the 5% quantile for 100 draws is larger than 10,000,000 draws by $15. This information is indicative that in this one set of 100 draws—although the draws were entirely randomly picked from the normal distribution—they had a bias toward the lower end of the spectrum. With a larger sample size, I was able to bypass this bias.
Table 1: Statistical studies on the estimated annual return from an S&P index fund if $1,000 were invested. (The larger the number of iterations, the more accurate your results will be.)
Table 1: Statistical studies on the estimated annual return from an S&P index fund if $1,000 were invested. (The larger the number of iterations, the more accurate your results will be.)
If I had $1,000 dollars to invest into a fund, I would invest into this fund. There were certain considerations I needed to take into account to make this decision. The first consideration is the economic occurrences during the 10 years from which the raw input was taken. In 2008, the United States underwent large economic instability that greatly affected investments and all of Wall Street. This must be taken into consideration for the large negative return in 2008. In order to remedy this situation, two different approaches can be taken for a better, unbiased view of the potential investments: delete the highest and lowest outliers or take in more input. The most unbiased approach would be to add more inputs into this simulation. Instead of simply the last 10 years, take data from the last 50. Throughout the past 50 years, the US has experienced high and low economic times, which will give the distribution curve a better estimate of the true average annual return than this S&P index fund. Additionally, larger sample sizes from the normal distribution curve are required and can easily be viewed by comparing the histograms from 100 draws to 100,000 draws. For 100 draws, it is evident that there is a bias toward the lower numbers, and the histogram is nonsymmetrical. In comparison, the histogram from 100,000 draws looks quite symmetrical.
Numberofdrawsfromnormaldistribution | Mean | StandardDev. | 95%quantile | 5%quantile |
100 | $1067.84 | $176.40 | $1350.01 | $783.16 |
1000 | $1092.07 | $175.36 | $1373.58 | $799.60 |
100K | $1091.17 | $178.07 | $1384.56 | $798.63 |
10million | $1091.13 | $177.85 | $1383.70 | $798.55 |
Project Comments
Project Comments
Name
Project 1
All coding statements that are commented out were used for testing and debugging purposes.
Project 1
All coding statements that are commented out were used for testing and debugging purposes.
Project Code
Project Code
Insert code here.
Wolfram Language vs. Python Timing Scripts
Wolfram Language vs. Python Timing Scripts
The following sections have the various inputs I used for the timing portion of my blog post.
Iterative Style Wolfram Code
Iterative Style Wolfram Code
User-Defined Function Programming Style Wolfram Code
User-Defined Function Programming Style Wolfram Code
Built-in Functions Style Wolfram Code
Built-in Functions Style Wolfram Code
Iterative Style Python Code
Iterative Style Python Code
User-Defined Function Programming Style Python Code
User-Defined Function Programming Style Python Code
Built-in Functions Style Python Code
Built-in Functions Style Python Code
Boilerplate Format Python Code
Boilerplate Format Python Code
List Comprehension Python Code
List Comprehension Python Code
Cite this as: David Stevens, ""Migrating from Python to the Wolfram Language and Lessons Learned" Addendum: A Sample Monte Carlo Simulation Project" from the Notebook Archive (2021), https://notebookarchive.org/2021-11-1vq23ne
Download