Machine Learning for hackers读书笔记(八)PCA:构建股票市场指数
library('ggplot2')
prices <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\08-PCA\\data\\stock_prices.csv',stringsAsFactors = FALSE)
library('lubridate')
#把日期列转成日期对象
prices <- transform(prices, Date = ymd(Date))
#prices中的数据只有三列,日期,股票名,价格
library('reshape')
#转下格式,矩阵中,每一天是一行,一行中包含了所有股票当天的价格,Date~Stock,行方向是Date列,列方向是Stock列
date.stock.matrix <- cast(prices, Date ~ Stock, value = 'Close')
#以上矩阵有缺失数据,先去prices删除缺失数据,然后重新生成矩阵
prices <- subset(prices, Date != ymd('2002-02-01'))
prices <- subset(prices, Stock != 'DDR')
date.stock.matrix <- cast(prices, Date ~ Stock, value = 'Close')
#做出相关性矩阵
cor.matrix <- cor(date.stock.matrix[, 2:ncol(date.stock.matrix)])
#转成一个数值向量
correlations <- as.numeric(cor.matrix)
#画图
ggplot(data.frame(Correlation = correlations),aes(x = Correlation, fill = 1)) +geom_density() + theme(legend.position = 'none')
#从图上看出,大部分相关性是正数,因此适合使用PCA

pca <- princomp(date.stock.matrix[, 2:ncol(date.stock.matrix)])
#看下pca第一主成分的载荷,并画图
principal.component <- pca$loadings[, 1]
loadings <- as.numeric(principal.component)
ggplot(data.frame(Loading = loadings), aes(x = Loading, fill = 1)) + geom_density() + theme(legend.position = 'none')

#从图上看,几乎全是负数
#预测一下
market.index <- predict(pca)[, 1]
#加载道琼斯指数
dji.prices <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\08-PCA\\data\\DJI.csv', stringsAsFactors = FALSE)
#把日期列转化一下
dji.prices <- transform(dji.prices, Date = ymd(Date))
#只拿一个子集看一下
dji.prices <- subset(dji.prices, Date > ymd('2001-12-31'))
dji.prices <- subset(dji.prices, Date != ymd('2002-02-01'))
#道琼斯数据集中数据好多,只拿收盘价和日期来看一下
dji <- with(dji.prices, rev(Close))
dates <- with(dji.prices, rev(Date))
#弄个数据集,MarketIndex是预测的
comparison <- data.frame(Date = dates, MarketIndex = market.index,DJI = dji)
ggplot(comparison, aes(x = MarketIndex, y = DJI)) + geom_point() + geom_smooth(method = 'lm', se = FALSE)
#从图上看,预测值和DJI指数负相关

comparison <- transform(comparison, MarketIndex = -1 * MarketIndex)
ggplot(comparison, aes(x = MarketIndex, y = DJI)) + geom_point() + geom_smooth(method = 'lm', se = FALSE)

#comparison有三列,日期,预测值,道琼斯指数
alt.comparison <- melt(comparison, id.vars = 'Date')
#melt后alt.comparison有三列,日期,类别(预测/道琼斯),价格
names(alt.comparison) <- c('Date', 'Index', 'Price')
ggplot(alt.comparison,aes(x = Date, y = Price, group = Index, color = Index)) + geom_point() + geom_line()
#图上看,预测值太低了,没法比对

#用scale把两部分数值放在同一刻度下
comparison <- transform(comparison, MarketIndex = scale(MarketIndex))
comparison <- transform(comparison, DJI = scale(DJI))
#重新melt
alt.comparison <- melt(comparison, id.vars = 'Date')
names(alt.comparison) <- c('Date', 'Index', 'Price')
#重新画图
ggplot(alt.comparison, aes(x = Date, y = Price, group = Index, color = Index)) + geom_point() + geom_line()

Machine Learning for hackers读书笔记(八)PCA:构建股票市场指数的更多相关文章
- Machine Learning for hackers读书笔记(七)优化:密码破译
#凯撒密码:将每一个字母替换为字母表中下一位字母,比如a变成b. english.letters <- c('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ...
- Machine Learning for hackers读书笔记(六)正则化:文本回归
data<-'F:\\learning\\ML_for_Hackers\\ML_for_Hackers-master\\06-Regularization\\data\\' ranks < ...
- Machine Learning for hackers读书笔记(三)分类:垃圾邮件过滤
#定义函数,打开每一个文件,找到空行,将空行后的文本返回为一个字符串向量,该向量只有一个元素,就是空行之后的所有文本拼接之后的字符串 #很多邮件都包含了非ASCII字符,因此设为latin1就可以读取 ...
- Machine Learning for hackers读书笔记_一句很重要的话
为了培养一个机器学习领域专家那样的直觉,最好的办法就是,对你遇到的每一个机器学习问题,把所有的算法试个遍,直到有一天,你凭直觉就知道某些算法行不通.
- Machine Learning for hackers读书笔记(十二)模型比较
library('ggplot2')df <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\12-Model_C ...
- Machine Learning for hackers读书笔记(十)KNN:推荐系统
#一,自己写KNN df<-read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\10-Recommendations\\ ...
- Machine Learning for hackers读书笔记(九)MDS:可视化地研究参议员相似性
library('foreign') library('ggplot2') data.dir <- file.path('G:\\dataguru\\ML_for_Hackers\\ML_for ...
- Machine Learning for hackers读书笔记(五)回归模型:预测网页访问量
线性回归函数 model<-lm(Weight~Height,data=?) coef(model):得到回归直线的截距 predict(model):预测 residuals(model):残 ...
- Machine Learning for hackers读书笔记(四)排序:智能收件箱
#数据集来源http://spamassassin.apache.org/publiccorpus/ #加载数据 library(tm)library(ggplot2)data.path<-'F ...
随机推荐
- Python中的正则表达式regular expression
1 match = re.search(pat,str) If the search is successful, search() returns a match object or None o ...
- UIView的frame和bounds的含义
1.frame是该view相对于父view的坐标系中的位置和大小.(参照点是父view的坐标系) 2.bounds是该view相对于自己的坐标.(参照点是本身坐标系统) 3.uiresponder&l ...
- WinInet:HTTPS 请求出现无效的证书颁发机构的处理
首先,微软提供的WinInet库封装了对网页访问的方法. 最近工作需要从https服务器获取数据,都知道https和http网页的访问方式不同,多了一道证书认证程序,这样就使得https在请求起来比h ...
- C# 对动态编辑的一些学习笔记
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Comp ...
- unity3d中dllimport方法的使用,以接入腾讯平台为例!!!
说到有关dllimport方法可能还有很多人比较陌生,其实我自己也说不太清楚,大概说说什么时候要用它. 事实上功能类似于调用android的第三包,我们想要使用苹果上特定的api或者第三方平台的一些东 ...
- HDU3487 Play With Chains(Splay)
很裸的Splay,抄一下CLJ的模板当作复习,debug了一个下午,收获是终于搞懂了以前看这个模板里不懂的内容.以前用这个模板的时候没有看懂为什么get函数返回的前缀要加个引用,经过一下午的debug ...
- UVA 11609 Teams 组合数学+快速幂
In a galaxy far far away there is an ancient game played among the planets. The specialty of the gam ...
- 在Unity中使用贝塞尔曲线(转)
鼎鼎大名的贝塞尔曲线相信大家都耳熟能详.这两天因为工作的原因需要将贝塞尔曲线加在工程中,那么MOMO迅速的研究了一下成果就分享给大家了哦.贝塞尔曲线的原理是由两个点构成的任意角度的曲线,这两个点一个是 ...
- lintcode: 左填充
题目 实现一个leftpad库,如果不知道什么是leftpad可以看样例 样例 leftpad("foo", 5) >> " foo" leftpa ...
- ADB not responding. You can wait more,or kill"abd.exe" process manually and click 'Restart'
在使用Android Studio进行开发的过程中,有时候编译运行时,会出现如下提示: ADB not responding. You can wait more,or kill"abd.e ...