Last active
August 29, 2015 14:16
-
-
Save haroldkyle/432e6f4eb714ed03191c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: 'DMD Problem Set #2' | |
author: "Harold Kyle" | |
output: | |
pdf_document: | |
fig_height: 7 | |
fig_width: 8 | |
toc: yes | |
--- | |
#Introduction | |
This is my time using R, so please excuse the roughness of this. | |
I am using R Markdown to combine inline text with R. | |
Code is online here: | |
[R code](https://gist.github.com/haroldkyle/6cb7ab44162c352b5208) | |
[R Markdown code](https://gist.github.com/haroldkyle/432e6f4eb714ed03191c) | |
# 2.16 | |
```{r} | |
sailboats <- c(2,3,4,5,6,7) | |
prob <- c(0.15,0.20,0.30,0.25,0.05,0.05) | |
``` | |
## a) | |
### Mean: | |
```{r} | |
sum(sailboats*prob) | |
``` | |
### Standard Deviation: | |
```{r} | |
sd(sailboats*prob) | |
``` | |
## b) | |
### Mean: | |
```{r} | |
bmean <- sum(sailboats*prob)*4800+30000 | |
print(bmean) | |
``` | |
### Standard Deviation: | |
Fixed costs do not affect variation or standard deviation. | |
```{r} | |
bsd <- sd(sailboats*prob*4800) | |
print(bsd) | |
``` | |
## c) | |
### Mean: | |
Increase by the difference of the fixed costs | |
```{r} | |
cmean <- bmean + 53000-30000 | |
print(cmean) | |
``` | |
### Standard Deviation: | |
Fixed costs do not affect variation, so this doesn't change from b. | |
```{r} | |
csd <- bsd | |
print(csd) | |
``` | |
double check: | |
```{r} | |
sum(sailboats*prob)*4800+53000 == cmean | |
sd(sailboats*prob*4800) == csd | |
``` | |
# 2.22 | |
*Large store (L)* | |
E(L) = 147.8 | |
Std(L) = 51 | |
Var(L) = Std(D)^2 | |
```{r} | |
51^2 | |
``` | |
a (price) = $17 | |
*Discount store (D)* | |
E(D) = 63.2 | |
Std(D) = 37 | |
Var(D) = Std(D)^2 | |
```{r} | |
37^2 | |
``` | |
b (price) = $9 | |
Corr(L,D) = 0.7 | |
mean revenue is the sum of the means | |
E[L+D] = E[L] + E[D] | |
## Revenue: | |
Revenue[L,D] = aE[L] + bE[D] | |
```{r} | |
(147.8*17)+(63.2*9) | |
``` | |
## Variance: | |
Var(L+D) = a^2\*Var(L) + b^2\*Var(D) + 2ab\*Std(L)\*Std(D)*Corr(L,D) | |
```{r} | |
17^2*2601 + 9^2*1369 + 2*17*9*51*37*0.7 | |
``` | |
## Standard Deviation: | |
Std(L+D) = sqrt( Var(L+D) ) | |
```{r} | |
sqrt( 1266773.4 ) | |
``` | |
# 2.24 | |
First I will load some libraries and define some helper functions... | |
```{r, echo=FALSE} | |
# install.packages("ggplot2") | |
# install.packages("reshape") | |
# install.packages("gridExtra") | |
require(ggplot2) | |
require(reshape2) | |
library(gridExtra) | |
``` | |
```{r} | |
### 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 ) | |
} | |
``` | |
## a) | |
### Mean: | |
```{r} | |
dmd$GetExpectedReturn( c(0.5,0.5), aReturn, bReturn ) | |
``` | |
### Standard Deviation: | |
```{r} | |
dmd$GetCombinedStd( c(0.5,0.5), aReturn, bReturn, aStd, bStd, 0.3) | |
``` | |
## b) | |
### Mean: | |
The mean will remain the same because it is not affected by changes in correlation. | |
### Standard Deviation: | |
```{r} | |
dmd$GetCombinedStd( c(0.5,0.5), aReturn, bReturn, aStd, bStd, 0.6) | |
dmd$GetCombinedStd( c(0.5,0.5), aReturn, bReturn, aStd, bStd, -0.6) | |
dmd$GetCombinedStd( c(0.5,0.5), aReturn, bReturn, aStd, bStd, -0.3) | |
dmd$GetCombinedStd( c(0.5,0.5), aReturn, bReturn, aStd, bStd, -0) | |
``` | |
## c) and d) | |
```{r} | |
### 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") | |
} | |
``` | |
```{r} | |
### TABLE OF DATA ### | |
grid.table(combinedData,gp=gpar(fontsize=7, lwd=2)) | |
``` | |
```{r} | |
### 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