這幾天玩R語言,然後我做了以下的測試:
(1)2014年宏達電(t2498)與大立光(t3008)的股價關係
(2)2014年蘋果(taapl)與大立光(t3008)的股價關係
數據方面:
AAPL我是抓google提供的股價,所以已經有還原分割前的價格
大立光股價除以20以免價格與宏達電或蘋果相差太多
測試的結果如附圖:
結論:果然大立光與蘋果的股價高度正相關ㄚ~~
相關程式碼如下:
#!/usr/bin/Rscript
Sys.setenv("HADOOP_PREFIX"="/usr/local/hadoop")
Sys.setenv("HADOOP_CMD"="/usr/local/hadoop/bin/hadoop")
Sys.setenv("HADOOP_STREAMING"="/usr/local/hadoop/contrib/streaming/hadoop-streaming-1.1.2.jar")
library(quantmod)
library(rhdfs)
hdfs.init()
library(rmr2)
makeReport = function(input)
{
report=as.data.frame(input, stringsAsFactors=F)
colnames(report)=c('Month','Max Price')
return(report)
}
getMaxDirect = function(symbol,year,from="google")
{
stock = getSymbols(symbol,src=from,auto.assign=FALSE)
monthes=1:12
data = c()
for(m in monthes)
{
data = c(data,max(stock[paste(year,"-",m,sep=""),4]))
}
result=keyval(monthes,data)
return(result)
}
getMaxReduce = function(symbol,year,from="google")
{
stock = getSymbols(symbol,src=from,auto.assign=FALSE)
data=stock[year,4,1]
monthes=1:12
date=numeric(length(index(data)))
for(m in monthes)
{
date[index(data)>=as.Date(paste(year,"-",m,"-1",sep=""))]=m
}
indata=to.dfs(keyval(date,as.double(data)))
out=mapreduce(input=indata,reduce=function(k,v) keyval(k,max(v)))
result=from.dfs(out)
return(result)
}
getMaxMapReduce = function(symbol,year,from="google")
{
stock = getSymbols(symbol,src=from,auto.assign=FALSE)
data=stock[year,4,1]
indata=to.dfs(keyval(as.double(index(data)),as.double(data)))
mymap=function(k,v){
monthes=1:12
date=numeric(length(k))
for(m in monthes)
{
date[k>=as.Date(paste(year,"-",m,"-1",sep=""))]=m
}
keyval(date,v)
}
out=mapreduce(input=indata,map=mymap,reduce=function(k,v)keyval(k,max(v)))
result=from.dfs(out)
return(result)
}
result=getMaxDirect("AAPL","2014","yahoo")
aapl2014 = makeReport(result)
aapl2013 = makeReport(getMaxReduce("AAPL","2013","yahoo"))
aapl2012 = makeReport(getMaxMapReduce("AAPL","2012","yahoo"))
aapl2012
aapl2013
aapl2014
t3008=getMaxDirect("3008.tw","2014","yahoo")
t2498=getMaxDirect("2498.tw","2014","yahoo")
x=c(t3008$key,t2498$key)
y=c(t3008$val/10,t2498$val)
plot(x,y)
abline(lm(y~x))
cor(x,y)