Over the past couple of years, I have been innately fascinated by the emergence of scala. Discovering it’s incredible power of type inference, a very extensive collections API, pattern matching, and many other things have been very alluring.
Recently, I had a question pop into my head regarding financial data. You see, I often hear people cite things like “the average performance of the S&P 500 is 12%”. What? That is crazy. I know that isn’t true, and betting your retirement on it is not good. Where does this come from? I saw a short video clip where two financial advisor explained that many people use the arithmetic mean instead of the geometric mean to calculate this value. But I’m getting ahead of myself.
I went and tracked down a website that listed the performance of the S&P 500 back to 1951. I grabbed the numbers from 2001-2010, and punched them into a spreadsheet. Using SUM, I was easily able to calculate the arithmetic mean. Then I tried to calculate the geometric mean. This meant taking all those percents and multiplying them together. Guess what? Neither LibreOffice nor Google Docs spreadsheet have a MULTIPLY nor a PRODUCT function. Well, at least one that handles a list instead of two values. As my mind wandered away from spreadsheets and into software solutions, I realized this was the perfect thing to write a tiny scala app with! And if you are looking to increase your savings, then you may consider check out out this ap automation cost saving calculator.
First, we need to create a simple app. We do this by extending scala’s App trait (NOT the dated Application trait).
Next, let’s load up the data using scala’s List function. In this case, we are storing a tuple using () notation, containing the year and the relative change.
Next, we can write a foldLeft to start with 0.0, and then add each entry’s second item using the ._2 method. At the tail end of our foldLeft, we divide it by the size of the sequence.
See how easy this function was to write? We can tabulate everything, or just a slice but the simplicity of this function shows why many people use it to write simple financial advice. But that’s not enough. For a more accurate evaluation, we need to figure out the geometric mean.
First, we need to convert one of these relative percents into an absolute multiplier. Since I like to read outputs in relative percent format, we will need another function to convert back.
What could be a single function, I decided to break out into two. It’s nice to know total growth as well as average growth per year (also called annualized growth). Given that function, we just need to take the nth root based on the length of the list.
With these functions, it is easy to run our entire list of historical data and find the average growth of the stock market. But that isn’t all I wanted to know. I really wanted to see what would happen if my money was invested in an EIUL, meaning that in negative years, growth would be capped at 0.0%, and during booms, growth would be capped at 15%.
I wrote one solution, but it was clunky. I got some help on stackoverflow, and instead coded a way to basically sort each year’s performance against List(0.0, 15.0), and pick the one in the middle. For negative years, 0.0 is in the middle. For big booms, 15.0 is in the middle. For everything else, the stock market number itself is in the middle.
A cornerstone of functional programming is working with lists of data, and transforming them based on your needs. In this case, we need a function that applies the conversion function above to an list of stock data. We can do this easily with scala’s map function.
So, now I can evaluate the entire performance of my money if it was invested in some 500 index fund and compare it with the growth potential of putting that in an EIUL. And it was pretty simple! Imagine what this would have taken to write in Java. It would be clunky, hard to decipher, and probably littered with many more bugs.
I have heard it said that scala makes hard stuff easy and impossible stuff reachable. So I pushed myself. I remember speaking with one of my financial advisors (I have several to provide multiple sources of input to my plans) and he was talking about the average rolling performance of 15 year windows over the past 30 years. Could I write a little more code, and do that myself? I think you know the answer. Start at the first entry of the list, grab n items, then step to the next one and do the same. Before I could write this myself, I checked scala’s collections API only to find a sliding method call to already do this for me.
To have this analysis carry some statistical weight, let’s write a function to calculate standard deviation.
Given all this, it’s time to crank out some output code. Let’s analyze the performance of the S&P 500 and our EIUL, comparing all 10-year, 15-year, 20-year, 25-year, and 30-year intervals.
Let’s wrap it up with a closing brace.
Let’s run it!
This shows that EIULs should pretty consistently beat any index fund based on the S&P 500. It’s possible, due to the variance, that the S&P 500 can beat an EIUL. But the standard deviation of the EIULs are much slimmer showing you a more consistent expected return on investment. S&P 500 has much wider variability meaning you can strike a much better rate, or be passed up my inflation.
And what’s more important: neither of these solutions will make you rich. To do that, you need to be consistently hitting double digit returns. The only type of investment vehicle that has evidence of doing that is either real estate or something with higher risk, such as running your own business. Funds and EIULs aren’t made to make you rich. EIULs, however, are great ways to store money you make through your investment endeavors and later pay a back to you in a consistent, predictable, and TAX FREE manner.
UPDATE (5/18/2012): Since my blog entry was cross posted at Uncommon Financial Wisdom, I have updated the scala app to also find the minimums and maximums for each window. See https://github.com/gregturn/finance for the code updates as well as the results displayed in the README file. Visit David Shafer’s blog and give him a call if you want to talk to a real wealth building expert.
I am not a licensed financial advisor nor an insurance agent, and cannot give out financial advice. This is strictly wealth building opinion and should be treated as such.
FYI: I had a big chunk of code missing, so I added it in, and wrote the concluding two paragraphs as well.