-
Notifications
You must be signed in to change notification settings - Fork 3
/
finance-R-solutions.R
104 lines (71 loc) · 3.22 KB
/
finance-R-solutions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
library(tidyquant)
library(rJava)
library(ggplot2)
library(stats)
getSymbols('^GSPC', src='yahoo', auto.assign = TRUE)
allReturns(GSPC)
sp.returns <- periodReturn(GSPC, period='monthly', type='log')
sp.df <- as.data.frame(sp.returns) %>% rownames_to_column('date')
getSymbols('DNKN', src='yahoo', auto.assign = TRUE)
allReturns(DNKN)
dnkn.returns <- periodReturn(DNKN, period='monthly', type='log')
dnkn.df <- as.data.frame(dnkn.returns) %>% rownames_to_column('date')
getSymbols('MCD', src='yahoo', auto.assign = TRUE)
allReturns(MCD)
mcd.returns <- periodReturn(MCD, period='monthly', type='log')
mcd.df <- as.data.frame(mcd.returns[55:143,]) %>% rownames_to_column('date')
getSymbols('SBUX', src='yahoo', auto.assign = TRUE)
allReturns(SBUX)
sbux.returns <- periodReturn(SBUX, period = 'monthly', type = 'log')
sbux.df <- as.data.frame(sbux.returns[55:143,]) %>% rownames_to_column('date')
head(sbux.df)
er <- c(mean(sbux.df$monthly.returns), mean(dnkn.df$monthly.returns), mean(mcd.df$monthly.returns))
excess.return <- matrix(c((sbux.df$monthly.returns - er[1]),
(dnkn.df$monthly.returns - er[2]),
(mcd.df$monthly.returns - er[3])), ncol = 3)
varcovmatrix <- matrix(t(excess.return) %*% excess.return, ncol=3) * (1/(88- 1))
varcovmatrix
(mean(sbux.df$monthly.returns) * (1/3)
+ mean(dnkn.df$monthly.returns) * (1/3) +
mean(mcd.df$monthly.returns) * (1/3))
sqrt(var(sbux.df$monthly.returns) * (1/3)^2
+ var(dnkn.df$monthly.returns) * (1/3)^2
+ var(mcd.df$monthly.returns) * (1/3)^2
+ 2 * (1/3)^2 * cov(sbux.df$monthly.returns, dnkn.df$monthly.returns)
+ 2 * (1/3)^2 * cov(sbux.df$monthly.returns, mcd.df$monthly.returns)
+ 2 * (1/3)^2 * cov(sbux.df$monthly.returns, mcd.df$monthly.returns))
install.packages("IntroCompFinR", repos="http://R-Forge.R-project.org")
library(IntroCompFinR)
ef <- efficient.frontier(er, varcovmatrix, nport=100, shorts=TRUE)
r.free <- 0.0025
# compute the tangency portfolio
tan.port <- tangency.portfolio(er, varcovmatrix, r.free)
# compute global minimum variance portfolio
gmin.port <- globalMin.portfolio(er, varcovmatrix)
# plot
plot(ef, cex=.25)
points(gmin.port$sd, gmin.port$er, col="orange", pch=16, cex=1.5)
points(tan.port$sd, tan.port$er, col="blue", pch=16, cex=1.5)
text(gmin.port$sd, gmin.port$er, labels="Minimum Variance", pos=4, cex=.8)
text(tan.port$sd, tan.port$er, labels="Tangent Line", pos=4, cex=.8)
sr.tan <- (tan.port$er - r.free)/tan.port$sd
abline(a=r.free, b=sr.tan, col="blue", lwd=1.5)
gmin.port
tan.port
getSymbols('DNKN', src='yahoo', auto.assign = TRUE)
allReturns(DNKN)
dnkn.yrs <- periodReturn(DNKN, period='yearly', type='log')
dnkn.yrs.df <- as.data.frame(dnkn.yrs) %>% rownames_to_column('date')
getSymbols('^GSPC', src='yahoo', auto.assign = TRUE)
allReturns(GSPC)
sp.yrs <- periodReturn(GSPC, period='yearly', type='log')
sp.yrs.df <- as.data.frame(sp.yrs[5:12,]) %>% rownames_to_column('date')
plot(x=sp.yrs.df$yearly.returns, y=dnkn.yrs.df$yearly.returns)
capm.fit <- lm(dnkn.yrs.df$yearly.returns ~ sp.yrs.df$yearly.returns)
summary(capm.fit)
abline(capm.fit)
yr.r.free <- 0.03
mkt.er <- mean(sp.yrs.df$yearly.returns)
beta <- 1.23942
capm.dnkn <- yr.r.free + ((mkt.er - yr.r.free) * beta)
capm.dnkn