# Copyright 2013 LBB. All rights reserved.
# Author: Gray Fox
# Description: PortOpt-3Fund.R
# Optimize 3-Fund Portfolio
# Date: 07-March-2013
## source and library statements
library(tseries)
getwd()
setwd('c:/rdata')
## CHANGE THE PATH TO YOUR LOCAL DIRECTORY
source("portfolio_noshorts.r")
options(digits=4, width=70)
## constants
shorts=FALSE
library(tseries)
library(quadprog)
getwd()
setwd('c:/rdata')
###here kospi data
dat <- read.csv('port.csv', header=TRUE)
#dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:nrow(dat)),]
attach(odat)
x<-odat[order(Date),]
#x<-odat
price1 <- as.numeric(x$로만손)
price2 <- as.numeric(x$KB금융)
price3 <- as.numeric(x$동서)
#price4 <- as.numeric(x$동서)
ret1 <- 0
ret2 <- 0
ret3 <- 0
#ret4 <- 0
for (i in 1:length(price1)) {
ret1[i] <- (price1[i]-price1[1])/price1[1]
ret2[i] <- (price2[i]-price2[1])/price2[1]
ret3[i] <- (price3[i]-price3[1])/price3[1]
#ret4[i] <- (price3[i]-price3[1])/price3[1]
}
Returns.z <- cbind(ret1, ret2, ret3)
## constants
shorts=FALSE
r.free.a <- 0.025 # T-bill Rate
r.free <- r.free.a/12
start_date <- "2013-11-13"
end_date <- "2014-02-21"
#Prices.z <- merge(price1, price2, price3)
colnames(Returns.z) = c("로만손", "KB금융", "동서")
# Plot 1. Monthly cc returns vs time
plot(Returns.z, plot.type="single", lty=1:3, col=1:3, lwd=2)
legend(x="bottomleft", legend=colnames(Returns.z), lty=1:3, col=1:3, lwd=2)
abline(h=0)
title("Monthly cc returns")
## Plot 2. Pairwise scatterplot
#pairs(coredata(Returns.z), col="blue", pch=16)
## Compute estimates of CER model parameters
muhat.vals <- apply(Returns.z, 2, mean)
sigma2hat.vals <- apply(Returns.z, 2, var)
sigmahat.vals <- apply(Returns.z, 2, sd)
cov.mat <- var(Returns.z)
cor.mat <- cor(Returns.z)
###############################################################################
## GMVP
gmin.port <- globalMin.portfolio(muhat.vals, cov.mat, shorts=FALSE)
summary(gmin.port)
gmin.SR = (12*gmin.port$er - r.free.a) / (sqrt(12)*gmin.port$sd)
print("GMVP Sharpe Ratio"); print(gmin.SR)
## Plot 3. GMVP weights
plot(gmin.port, col="blue")
## Tangency portfolio
tan.port <- tangency.portfolio(muhat.vals, cov.mat, risk.free=r.free, shorts=shorts)
summary(tan.port)
tp.SR.m = (tan.port$er - r.free) / (tan.port$sd)
tp.SR = (12*tan.port$er - r.free.a) / (sqrt(12)*tan.port$sd)
print("TP Sharpe Ratio"); print(tp.SR)
## Plot 4. Tangency portfolio weights
plot(tan.port, col="blue")
## Efficient Frontier
ef <- efficient.frontier(muhat.vals, cov.mat, nport=21, alpha.min=-10, alpha.max=10, shorts=shorts)
## Plot 6. Efficient Frontier allowing short sales
plot(ef, plot.assets=T, col="blue", pch=16, cex=1)
points(tan.port$sd, tan.port$er, col="green", pch=16, cex=1)
text(tan.port$sd, tan.port$er, labels="TP", pos=2)
abline(a=r.free, b=tp.SR.m, col="green", lwd=2) ## CAL
print("Done")

댓글 없음:
댓글 쓰기