library(stringr)
setwd("C:/rdata")
source("DTMC.R")
###here kospi data
dat <- read.csv('port3.csv', header=TRUE, na="")
odat<-dat[c(1:nrow(dat)),]
attach(odat)
x<-odat[order(일자),]
price <- as.numeric(str_replace_all(x$리홈쿠첸, ",", ""))
d <- diff(price)
up2up <-0
up2down <-0
down2up <-0
down2down <-0
for (i in 2:length(d)){
if (d[i-1]> 0 && d[i]>0){
up2up <- up2up+1
}
if (d[i-1]> 0 && d[i]<0){
up2down <- up2down +1
}
if (d[i-1]< 0 && d[i]>0){
down2up <- down2up+1
}
if (d[i-1]< 0 && d[i]<0){
down2down <- down2down+1
}
}
u2u <- up2up/(up2up+up2down)
u2d <- up2down/(up2up+up2down)
d2u <- down2up/(down2up+down2down)
d2d <- down2down/(down2up+down2down)
mattrans <- matrix(c(u2u, d2u, u2d, d2d),nrow=2,ncol=2)
colnames(mattrans) <- c("U","D")
row.names(mattrans) <- c("U","D")
rm(temp)
rm(sim)
temp<-0
sim <-data.frame(vector())
#now set states
state<- c(1,2,3,4,5)
nstate<-c(8000,9000,10000,11000,12000)
#current state
temp[1]<- 5
for (i in 1:100){
ins<- simula(mattrans,10)
for (j in 2:20){
if (ins[j] == "U"){
temp[j] <- temp[j-1]+1
}
else { temp[j] <- temp[j-1]-1}
}
sim <-rbind(sim, temp)
}
p1<-sum(rowSums(sim <=1))/(10*100)
p2<-sum(rowSums(sim == 2))/(10*100)
p3<-sum(rowSums(sim == 3))/(10*100)
p4<-sum(rowSums(sim == 4))/(10*100)
p5<-sum(rowSums(sim >= 5))/(10*100)
p <- c(p1,p2,p3,p4,p5)
cat("tansition matex is:\n")
matrix(mattrans, nrow = 2,ncol = 2)
f <- data.frame(nstate, p)
colnames(f)<-c("주가", "확률")
f[order(-f[,1]),]
cat("current state is:",temp[1], sep="\n")
댓글 없음:
댓글 쓰기