- Photo Gallery
- Chicagoland Good & Cheap Restaurants
- Chicagoland Filipino Food Sources
- Coffee shops near Chicago 'El' Stations
- My salad foodhack
- OmniCode
- Backing up photographs: my way
- Badminton photography: my way...
- How I manage my social life…
- Monte Carlo Simulations for Retirement planning (in Perl)
- What D&D Character Am I?
- Skype Settings with Ubuntu 8.10 Intrepid Ibex
- Laser pointer sniper game
- CTA Text Tracker
- Establishing a Low-cost Web Presence
- Homebrew bike lights
- How to enable remote XWindows on Portable Ubuntu
- Combining mod_security and phpMyAdmin for Ubuntu
- Perl Metromix scraper
- Script to copy Podcast files to USB drive
- Updating ipfilter.dat using wget
- Personal Finance 1
- Personal Finance 2
- Beer I've Tasted
- Sports Selection via Pondex
- My Public Key
- Contact us
Monte Carlo Simulations for Retirement planning (in Perl)
I got hit by inspiration today so I wrote a short
program (Took me about 2 hours) that does Monte
Carlo Simulations for retirement planning
purposes. I've GPL'ed it so people can extend it.
In this example, I've put the variables as:
The person is 18 yrs old and currently has $5,000
in his/her investments. This person adds $10,000
yearly to the investments. The person plans to
retire at 65 and hopes to have $3,000,000 by then.
The yearly rate of return on his/her investments
can range anywhere from -20.25% to 23.56%. Will this person
make it?
In one run of the simulation, the answer is:
"Average Successes = 97.66%".
Subsequent runs yield 97.5%, 97.61%, 97.67%.
So it seems this person has a very high
probability of reaching his/her goal.
UPDATE 10-16-2009: Modified the code to include performance
from the 2007-2009 recession. Based on past performance, the
chances of reaching your goal seem to have gotten dramatically
worse. One run with the same assumptions above gave a 0.71%
chance of success....
The program will likely have some bugs. If you
have the capability of fixing it and adding
features, please share the code with me. Thanks!
#!/usr/bin/perl
#
# montecarloretire.pl - Monte Carlo simulation for retirement planning
# Copyright (C) 2009 The Reader
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
#
# ---------------------------------------------------------------------
# NOTE: This does NOT take into account inflation!!!
# The program takes a few seconds to run on a Linux 2Ghz. Pentium
#
# I used Coffeehouse portfolio's range: -20.25% to 23.56% return
# http://www.coffeehouseinvestor.com/?page_id=5
# This models the market conditions better (instead of annualized and
# consistent) as the annual returns are random. Aim to have "Average
# Successes between 75% and 90%.
# UPDATES:
# 10/6/2009 - modified code to exit subroutine if target amount is reached before target age. Resulting in slightly higher chances of reaching target amount.
#
# For more info, check:
# http://www.nysscpa.org/cpajournal/2005/905/perspectives/p12.htm
# http://www.analycorp.com/suntimes_7-8-04.htm
# http://www.businessweek.com/2001/01_04/b3716156.htm
# http://www.latimes.com/business/investing/la-plan4retire-story15,1,23551...
# http://physiciansnews.com/finance/603.html
# http://registeredrep.com/mag/finance_why_advisors_say/
$successcount = 0;
$totalcount = 10000; # do 10000 attempts then divide by 100 to get an average
for($count=1;$count<=$totalcount;$count++) {
$principal = 5000; # place how much you have now
$addyearly = 10000; # place yearly savings here
$targetamount = 3000000; # place your target amount here
$targetage = 65; # place when you want to retire
$age = 18; # place your current age
for($i=$age;$i<=$targetage;$i++)
{
print "Age = $i\n";
$ratereturn = random_int_in(-2025,2356)/100;
print "Rate of Return = $ratereturn";
$pct = 1 + ($ratereturn/100);
#print "$pct\n";
$principal = ($principal + $addyearly)*$pct;
if ($principal >= $targetamount) {
print " retire at $i\n";
$i = $targetage;
} else {
print "\n";
}
$output = sprintf("%.0f", $principal);
print "Final amount = $output\n\n";
if ($i == $targetage){
print "\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\n";
if ($principal >= $targetamount) {
print "SUCCESS!\n\n\n";
$successcount++;
#print "Successes = $successcount\n";
} else {
print "FAIL!\n\n\n";
}
}
}
if ($count == $totalcount) {
$avesuccess = $successcount/100;
print "Average Successes = $avesuccess%\n\n\n";
}
}
sub random_int_in ($$) {
my($min, $max) = @_;
# Assumes that the two arguments are integers themselves!
#source http://perl.active-venture.com/pod/perlfaq4-datanumber.html
return $min if $min == $max;
($min, $max) = ($max, $min) if $min > $max;
return $min + int rand(1 + $max - $min);
}
- Wordpress category:
