Skip to content

Instantly share code, notes, and snippets.

@kamermanpr
Created January 15, 2019 14:02
Show Gist options
  • Save kamermanpr/11197134de0ba0dfb5deee31e568363f to your computer and use it in GitHub Desktop.
Save kamermanpr/11197134de0ba0dfb5deee31e568363f to your computer and use it in GitHub Desktop.
Illustrating compound growth using absolute investment value, change from initial investment, and percentage change from initial investment.
############################################################
# #
# The effect of starting investment amount #
# on returns realised. The magic of compounding #
# #
############################################################
# Load packages
library(ggplot2)
# Define compounding function (return a vector of length: period_of_investment)
compounder <- function(initial_investment = 1000,
period_of_investment = 30,
annual_growth = 0.05) {
# Create a list the length of the investment period
period <- vector(mode = 'list',
length = period_of_investment)
# Set the initial_investment amount as the first value of period
period[[1]] <- initial_investment
# Compound the initial investment over the investment period
# by a constant rate of annual growth
for(i in 2:length(period)) {
period[[i]] <- period[[i - 1]] + (annual_growth * period[[i - 1]])
}
# Return
unlist(period)
}
# Generate a vector of compound growth over 30 years for a:
## i) large initial investment and a constant annual growth rate of 2.5%
large_2.5p <- compounder(initial_investment = 70000,
period_of_investment = 30,
annual_growth = 0.025)
## ii) small initial investment and a constant annual growth rate of 2.5%
small_2.5p <- compounder(initial_investment = 7000,
period_of_investment = 30,
annual_growth = 0.025)
## iii) small initial investment and a constant annual growth rate of 5.0%
small_5.0p <- compounder(initial_investment = 7000,
period_of_investment = 30,
annual_growth = 0.05)
# Make a dataframe (long format)
df <- data.frame(period = rep(1:30, 3),
investment = c(rep('large (2.5% growth)', 30),
rep('small (2.5% growth)', 30),
rep('small (5.0% growth)', 30)),
value = c(large_2.5p, small_2.5p, small_5.0p),
starting_value = c(rep(70000, 30),
rep(7000, 30),
rep(7000, 30)))
# Plot the raw data
ggplot(data = df) +
aes(x = period,
y = value,
colour = investment) +
geom_line() +
geom_point(size = 4) +
labs(title = 'Investment growth (raw data)',
x = 'Years',
y = 'Investment value') +
scale_colour_brewer(name = 'Investment senario',
type = 'qual',
palette = 'Dark2') +
theme_bw(base_size = 16)
# Plot change from baseline investment value
df$change <- df$value - df$starting_value
ggplot(data = df) +
aes(x = period,
y = change,
colour = investment) +
geom_line() +
geom_point(size = 4) +
labs(title = 'Investment growth (change from baseline investment)',
x = 'Years',
y = 'Change investment value') +
scale_colour_brewer(name = 'Investment senario',
type = 'qual',
palette = 'Dark2') +
theme_bw(base_size = 16)
# Plot percentage change from baseline investment value
df$percent_change <- 100 * (df$change / df$starting_value)
ggplot(data = df) +
aes(x = period,
y = percent_change,
colour = investment) +
geom_line() +
geom_point(size = 4) +
labs(title = 'Investment growth (percent change from baseline investment)',
x = 'Years',
y = 'Percent change investment value (%)',
caption = "The 'large (2.5% growth)' data are obscured by the 'small (2.5% growth)' data") +
scale_colour_brewer(name = 'Investment senario',
type = 'qual',
palette = 'Dark2') +
theme_bw(base_size = 16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment