2013년 12월 30일 월요일

neural network kospi

#install.packages('neuralnet')
library("neuralnet")
#Going to create a neural network to perform sqare rooting
#Type ?neuralnet for more information on the neuralnet library
#Generate 50 random numbers uniformly distributed between 0 and 100
#And store them as a dataframe

library(quantmod)
library(fGarch)

URL <- "http://ichart.finance.yahoo.com/table.csv?s=^KS11"
dat <- read.csv(URL)
dat$Date <- as.Date(dat$Date, "%Y-%m-%d")

odat<-dat[c(1:100),]

attach(odat)

x<-odat[order(Date),]

t <- c(1:100)
traininginput <-  cbind(t, Lag(x$Close, 1))
trainingoutput <- x$Close


#Column bind the data into one variable
trainingdata <- data.frame(traininginput,trainingoutput)
colnames(trainingdata) <- c("Input","Output")


#Train the neural network
#Going to have 10 hidden layers
#Threshold is a numeric value specifying the threshold for the partial
#derivatives of the error function as stopping criteria.
net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=5, threshold=0.01)

#Plot the neural network
plot(net.sqrt)

#Test the neural network on some training data
testdata <- c(101:110) #Generate some squared numbers
net.results <- compute(net.sqrt, testdata) #Run them through the neural network

#Lets display a better version of the results
cleanoutput <- cbind(testdata,sqrt(testdata),
                         as.data.frame(net.results$net.result))
colnames(cleanoutput) <- c("Input","Expected Output","Neural Net Output")


cleanoutput

2013년 12월 28일 토요일

neural network

R script
--------------

library(datasets)

names(infert)

#  *------------------------------------------------------------------*
#  | train the network
#  *------------------------------------------------------------------*

library(neuralnet)

#input is age, parity, induced ...

nn <- neuralnet(
 case~age+parity+induced+spontaneous,
 data=infert, hidden=5, err.fct="ce",
 linear.output=FALSE)

#  *------------------------------------------------------------------*
#  | output training results
#  *------------------------------------------------------------------* 

# basic
nn

# reults options
names(nn)


# result matrix contains weights

nn$result.matrix

# The given data is saved in nn$covariate and
# nn$response as well as in nn$data for the whole data
# set inclusive non-used variables. The output of the
# neural network, i.e. the fitted values o(x), is provided
# by nn$net.result:

out <- cbind(nn$covariate,nn$net.result[[1]])

dimnames(out) <- list(NULL, c("age", "parity","induced","spontaneous","nn-output"))

head(out)


# visualization
plot(nn)


ar(1) model with prediction

R script
----------------------
library(quantmod)
library(fGarch)

URL <- "http://ichart.finance.yahoo.com/table.csv?s=^KS11"
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,0))

#fore <- predict(fit, n.ahead=10, doplot=F)

err <- rnorm(20, 0, .002)

sim <- diff(log(abs(x$Close)))[99]

for (t in 2:20) {
    sim[t] <- fit$coef[2]+ fit$coef[1]*sim[t-1] + err[t]
}

dev.new()
plot(x$Close, type='l', xlim=c(0, 120),
    panel.first=grid(col = "gray", lty = "dotted"), pch=20)
t <- c(101:120)
points(x$Close, pch = 20, col = "red")
for (j in 2:20) {
ye[j] <- exp(sim[j-1])*ye[j-1]
}

points(t, ye, col='red', pch=20, type='b')

지수가중이동평균법

코스피 분석

R 스크립트
-----------------
require(TTR)

URL <- "http://ichart.finance.yahoo.com/table.csv?s=^KS11"
dat <- read.csv(URL)
dat$Date <- as.Date(dat$Date, "%Y-%m-%d")

odat<-dat[c(1:100),]

#N<-99

attach(odat)

x<-odat[order(Date),]

par(mfrow=c(2,1), mar=c(2,3,3,2))
plot(x$Close,type="l", xaxt='n', xlab=NA,
panel.first=grid(col = "gray", lty = "dotted"))
  points(x$Close, pch = 20, col = "red")

price<-x$Close

row<-diff(log(price),1,1)*100

x<-c(1:99)

plot(x, row, type="l", ylim=c(-0.8,0.8), col='lightgray', xaxt='n', xlab=NA)

ema<-EMA(row,n=10)

points(x=x, y=ema, col='green', type='l', xlab=NA)

wma<-WMA(row, n=5, wts=1:5)

points(x=x, y=wma, col='red', type='l')

abline(h=0, col="lightgray")

2013년 12월 1일 일요일

vmware tools download

wget -r -np http://packages.vmware.com/tools/esx/latest/rhel6/i386/index.html