Skip to content

Instantly share code, notes, and snippets.

@haroldkyle
Last active August 29, 2015 14:16
Show Gist options
  • Save haroldkyle/6cb7ab44162c352b5208 to your computer and use it in GitHub Desktop.
Save haroldkyle/6cb7ab44162c352b5208 to your computer and use it in GitHub Desktop.
# install.packages("ggplot2")
# install.packages("reshape")
# install.packages("gridextra")
require(ggplot2)
require(reshape)
library(gridextra)
### ASSUMPTIONS ###
aReturn <- 0.15
bReturn <- 0.20
aStd <- 0.05
bStd <- 0.06
# correlations that we want to test
corrs = seq(from=-0.6, to=0.6, by=0.3)
### FUNCTIONS ###
# dmd "namespace"
dmd = new.env()
dmd$ValidateRatio <- function(ratio){
# Ratios of asset a to asset b need to be pairs of numbers that add to 1 (100%)
if( length(ratio) != 2 || ratio[1] + ratio[2] != 1 ){
stop("Ratios need to be pairs of values that add to 1.")
}
}
dmd$GetRatioLabel <- function(ratio){
# concatenate ratio values to create a label
# Args:
# ratio between asset a and asset b, list of two values whose sum is 1 b<-ratio[2]
dmd$ValidateRatio(ratio)
a<-ratio[1]
b<-ratio[2]
paste(round(a,2),":",round(b,2))
}
dmd$GetExpectedReturn <- function(ratio, aReturn, bReturn){
# finds the mean (expectation) for a given asset ratio
# Args:
# ratio: Ratio between asset a and asset b, list with two values whose sum is 1
# aReturn: Expected return on asset a
# bReturn: Expected return on asset b
dmd$ValidateRatio(ratio)
a<-ratio[1]
b<-ratio[2]
a*aReturn+b*bReturn
}
dmd$GetCombinedStd<-function(ratio, aReturn, bReturn, aStd, bStd, corr){
# find the standard deviation for a given asset ratio combination and correlation
# Args:
# ratio: Ratio between asset a and asset b, list with two values whose sum is 1
# aReturn: Expected return on asset a
# bReturn: Expected return on asset b
# aStd: Standard deviation of asset a
# bStd: Standard deviation of asset b
# corr: Correlation between asset a and asset b
dmd$ValidateRatio(ratio)
a<-ratio[1]
b<-ratio[2]
# standard deviation is square root of the combined variances
sqrt( a^2*aStd^2 + b^2*bStd^2 + 2*a*b*aStd*bStd*corr )
}
### DATA ###
# set up ratios of asset A to asset B.
aamt=seq(from=0,to=1,by=0.05)
# As percentages, these ratios will always combine to equal 1
bamt=1-aamt
# merge ratio values into a single vector
assets = cbind(aamt, bamt)
# add labels for ratios (to display alongside data and in graph)
ratioLabels <- apply( assets, 1, dmd$GetRatioLabel )
# create a data frame combinedData to contain data, with ratio labels as the first column
combinedData <- data.frame( label = ratioLabels )
# find the expected return for each asset blend
expectedReturns = apply(assets, 1, dmd$GetExpectedReturn, aReturn, bReturn)
# add expected returns to the combinedData data frame as the "mean" column
combinedData <- cbind(combinedData, mean = expectedReturns)
# iterate through correlations to test each at every asset ratio
for(corr in corrs){
# add data for each correlation to our data frame
combinedData <- cbind(combinedData, apply(assets, 1, dmd$GetCombinedStd, aReturn, bReturn, aStd, bStd, corr))
# label the column with the correlation value
idx = length(combinedData)
colnames(combinedData)[idx] <- paste("Std @",corr, " Corr")
}
grid.table(combinedData,gp=gpar(fontsize=6, lwd=2))
### GRAPHING ###
# melt data for graphing
melted = melt(combinedData, id.vars="label")
# ...and graph it!
ggplot() + geom_line(data=melted, aes(label,value,group=variable,color=variable)) + theme(axis.text.x = element_text(hjust=1, angle=45)) + xlab("Ratio of asset a to asset b")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment