Skip to content

Instantly share code, notes, and snippets.

@actuaryactually
Last active November 18, 2016 14:06
Show Gist options
  • Save actuaryactually/5671bc7dc243757cc8cadb23eee090b6 to your computer and use it in GitHub Desktop.
Save actuaryactually/5671bc7dc243757cc8cadb23eee090b6 to your computer and use it in GitHub Desktop.
Investment
#--------------------------------------------------------------------------------------#
#
# FANPLOT EXAMPLE
#
# Made famous by the BoE inflation report, fanplots are good for showing distributions where
# there is a time series. In this script I have created a toy dataset that looks like a dispersing time series
# to illustrate the fan syntax and .
# In future posts I'll show how to build a time-series using arbitrary data for an equity share
#
# D Menezes 20161111
#--------------------------------------------------------------------------------------#
#Step 1: Input constants that will be used below
pkg.list<-c("fanplot","RColorBrewer")
#Step 2: Install packages and load:
for(i in seq_along(pkg.list)) {
if(!require(pkg.list[i], character.only=TRUE)) {
install.packages(pkg.list[i])
library(pkg.list[i], character.only=TRUE)
}
}
#Step 3: Simulate some toy data
t<-1:20 #20 time steps
trend<-0.1 #increasing return
vol<-0.2 #volatility at t = 0
mu<-trend*t #on average the investment return will increase linearly
stddev<-vol*sqrt(t) #on average, the volatility parameter will increase at a slower rate - cf http://www.macroption.com/why-is-volatility-proportional-to-square-root-of-time/
n.sims<-10000 #no of different realisations of the future
initial.data<-c(0.06,0.08,0.04,0.08,0.1) #data prior to the fan - will need this later
#create an empty matrix for results
output.sims<-matrix(NA,nrow=n.sims,ncol=length(t))
#fill the matrix
for(i in seq_along(t)) {
output.sims[,i]<- rnorm(n=n.sims, mean=mu[i], sd = stddev[i])
}
head(output.sims)
#check - a vs e so that we're comfortable our simulation is doing what we want:
mean.check<-cbind(mu,
apply(output.sims,2,mean)
)
vol.check<-cbind(stddev,
apply(output.sims,2,sd)
)
colnames(mean.check)<-c("expected/input","actual/output")
colnames(vol.check)<-c("expected/input","actual/output")
mean.check
vol.check
#results are ok.
#plot - box plot to check - only used when testing the code
#boxplot(output.sims)
# plot initial values
plot(initial.data, type = "l", lwd = 2,
xlim = c(0,25), ylim = range(output.sims)*0.85,
main="Range of investment returns",xlab="Years", ylab="%")
# add fan (note - you need an existing plot window open, this can even be an empty plot, but the syntax won't instantiate the plot window, slightly odd
fan(data=output.sims,start=6,rlab=c(0.2,0.5,0.8),anchor = initial.data[length(initial.data)])
#6 to only tag on the distribution from that point
#anchor so that things fan out from a point, rather than immediately showing the 1st yr spread
#rlab are the selected percentiles
#Other color options avaialble - see here and examples below:
#http://www.datavis.ca/sasmac/brewerpal.html
#example 1
fan(data=output.sims,start=6,rlab=c(0.2,0.5,0.8),anchor = initial.data[length(initial.data)],
fan.col = colorRampPalette(colors = brewer.pal(11,"Spectral")))
#example 2
fan(data=output.sims,start=6,rlab=c(0.2,0.5,0.8),anchor = initial.data[length(initial.data)],
fan.col = colorRampPalette(colors = brewer.pal(9,"RdYlBu")))
#example 3
fan(data=output.sims,start=6,rlab=c(0.2,0.5,0.8),anchor = initial.data[length(initial.data)],
fan.col = colorRampPalette(colors = brewer.pal(9,"OrYlBu"))) #this one doesn't work. the no 9, is how many colors in the palette
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment