library(quantmod)
library(fGarch)
URL <- "http://ichart.finance.yahoo.com/table.csv?s=^KQ11"
dat <- read.csv(URL)
dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:100),]
attach(odat)
x<-odat[order(Date),]
#kospiGarch <- garchFit(~arma(1, 2) + garch(1, 1), data=as.ts(x$Close))
fit <- arima(diff(log(x$Close)), order=c(1,0,1))
#fore <- predict(fit, n.ahead=10, doplot=F)
err <- rnorm(30, 0, sd(diff(log(x$Close))))
sim <- diff(log(abs(x$Close)))[99]
for (t in 2:30) {
sim[t] <- fit$coef[1]*sim[t-1] + fit$coef[2]*err[t-1]
}
dev.new()
par(mfrow=c(3,1))
par(mar=c(3,3,1,2))
x$newday <- as.Date(x$Date, "%m/%d/%Y")
plot(x$newday, x$Close, type='l', col='red',
panel.first=grid(col = "gray", lty = "dotted"), pch=20)
points(x$newday, x$Close, pch = 20, col = "red")
ye <- x$Close[100]
td<-x$newday[length(x$newday)]+1
for (j in 2:30) {
td[j] <- x$newday[length(x$newday)] + j
ye[j] <- exp(sim[j-1])*ye[j-1]
}
td<-as.Date(td)
ye
points(td, ye, col='red', pch=20, type='b')
##########################################################
library(forecast)
fit <- arima(x$Close, order=c(1,1,10))
fore <- forecast(fit, h=15)
plot(fore)
fore
2014년 2월 24일 월요일
변동성 garch(1)
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)),]
apply(na.omit(odat),2,max)
attach(odat)
x<-odat[order(일자),]
apply(na.omit(x),2,max)
price <- as.numeric(x$안랩)
ret <- 0
for (i in 1:length(price)) {
ret[i] <- (price[i]-price[1])/price[1]*100
}
chg <-0
for (i in 2:length(price)) {
chg[i] <- (price[i]-price[i-1])/price[i-1]*100
}
#garch
mod.fit<-garch(x = ret - mean(ret), order = c(0,1), trace = TRUE) #Fit ARCH(1)
summary(mod.fit) #Summarizes fit
names(mod.fit)
mod.fit$coef
# plot(mod.fit)
coef(mod.fit) # see above and compare with coefficient alpha0 and alpha1 caculated by arch model
par(mfrow=c(3,1))
par(mar=c(3,3,1,2))
plot(mod.fit$fitted.values[,1], type='l', xaxt='n',
panel.first=grid(col = "gray", lty = "dotted"), pch=20)
title(main = "변동성", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
plot(chg, type='l', xaxt='n'
)
points(chg, pch = 20, col = "red")
title(main = "일등락율%", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
plot(ret, type='l', xaxt='n'
)
points(ret, pch = 20, col = "red")
title(main = "수익률%", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
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)),]
apply(na.omit(odat),2,max)
attach(odat)
x<-odat[order(일자),]
apply(na.omit(x),2,max)
price <- as.numeric(x$안랩)
ret <- 0
for (i in 1:length(price)) {
ret[i] <- (price[i]-price[1])/price[1]*100
}
chg <-0
for (i in 2:length(price)) {
chg[i] <- (price[i]-price[i-1])/price[i-1]*100
}
#garch
mod.fit<-garch(x = ret - mean(ret), order = c(0,1), trace = TRUE) #Fit ARCH(1)
summary(mod.fit) #Summarizes fit
names(mod.fit)
mod.fit$coef
# plot(mod.fit)
coef(mod.fit) # see above and compare with coefficient alpha0 and alpha1 caculated by arch model
par(mfrow=c(3,1))
par(mar=c(3,3,1,2))
plot(mod.fit$fitted.values[,1], type='l', xaxt='n',
panel.first=grid(col = "gray", lty = "dotted"), pch=20)
title(main = "변동성", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
plot(chg, type='l', xaxt='n'
)
points(chg, pch = 20, col = "red")
title(main = "일등락율%", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
plot(ret, type='l', xaxt='n'
)
points(ret, pch = 20, col = "red")
title(main = "수익률%", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
2014년 2월 23일 일요일
최소분산포트폴리오
# 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")
# 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")
2014년 2월 7일 금요일
git 사용법
git bash를 연다
사용자 이름 및 이메일주소 등록
git --global username
git --global email
// git 초기화
git init
// 저장소에 파일 추가
git add *.c
git add README
사용자 이름 및 이메일주소 등록
git --global username
git --global email
// git 초기화
git init
// 저장소에 파일 추가
git add *.c
git add README
2014년 2월 2일 일요일
자바스크립트 div 노드 추가
$(document).ready(function(){
var container = document.getElementById('container');
var outerdiv = document.createElement('div');
container.appendChild(outerdiv);
for (var i = 0; i < 10; i++) {
var innerdiv = document.createElement('div');
innerdiv.innerHTML = "this is";
innerdiv.appendChild(document.createTextNode(i));
outerdiv.appendChild(innerdiv);
}
}
);
------------------
<body>
<div id="container">
</div>
</body>
var container = document.getElementById('container');
var outerdiv = document.createElement('div');
container.appendChild(outerdiv);
for (var i = 0; i < 10; i++) {
var innerdiv = document.createElement('div');
innerdiv.innerHTML = "this is";
innerdiv.appendChild(document.createTextNode(i));
outerdiv.appendChild(innerdiv);
}
}
);
------------------
<body>
<div id="container">
</div>
</body>
피드 구독하기:
글 (Atom)


