• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

R语言股价分析

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

首先判断股价的分布是不是正态分布:

#获取3m公司收盘价
mmmdata = read.csv("E:\\kuaipan\\A Introduction to Analysis of Financial Data with R\\chapter 1\\ch1data\\d-mmm-0111.txt",header = T)
mmmprice = as.numeric(sapply(mmmdata, function(l){substring(l,15)}))

#绘制频度直方图
hist(mmmprice, nclass = 35)

#绘制密度图,并和同方差同均值的正态分布做比较
mmmprice.density=density(mmmprice)
x=seq(-.1,.1,.001) # Create a sequence of x with increment 0.001.
y1=dnorm(x,mean(mmmprice),sd(mmmprice))
plot(mmmprice.density$x,mmmprice.density$y,xlab='rtn',ylab='density',type='l')
lines(x, y1, lty=2)
legend(0.06,32,c('price','Norm'),lty = c(1,2))

#使用流行的qq图来与正态分布做比较
qqnorm(mmmprice)
qqline(mmmprice)


#ohlc analysis
#ohlc means open highest lowest and close price
library(quantmod)
getSymbols("AAPL",from="2015-01-03",to="2015-09-30")
chartSeries(AAPL)

#the left protuberance means open price, the right one means close price
barChart(AAPL,theme='white.mono',bar.type='ohlc')

#最近n个price的均值的变化趋势——移动平均曲线
"ma" <- function(pri,n,plot=TRUE){
  # pri: price series of an asset (univariate)
  # n: window size
  #
  nob=length(pri)
  ma1=pri
  range=max(pri)-min(pri)
  if(nob > n){
    psum=sum(pri[1:(n-1)])
    ma1[1:n]=psum/(n-1)
    for (i in n:nob){
      psum=psum+pri[i]
      ma1[i]=psum/n
      psum=psum-pri[i-n+1]
    }
  }
  if(plot){
    par(mfcol=c(1,1))
    plot(pri,type='l',xlab="time index")
    lines(ma1,lty=2)
    loc=max(pri)-range/3
    legend(n/2,loc,c(paste("n = ",c(n))),lty=2)
    title(main='Moving average plot')
  }
  ma <- list(ma=ma1)
}
ma(as.numeric(AAPL$AAPL.Close))


下面这段代码可以用来对二元正态假设进行判断, 代码中对IBM 和 SP 的股价收益率进行了分析

分析手段1: 协方差矩阵

分析手段2: 用rmnorm函数生成了2元正态分布的变量, 对比了两个plot, 来得出ibm和sp的股价收益率不符合二元正态假设!

da = read.table("E:\\kuaipan\\A Introduction to Analysis of Financial Data with R\\chapter 1\\ch1data\\m-ibmsp-2611.txt", header = T)
ibm=log(da$ibm+1) # Transform to log returns
sp=log(da$sp+1)
tdx=c(1:nrow(da))/12+1926 # Create time index
par(mfcol=c(2,1))
plot(tdx,ibm,xlab='year',ylab='lrtn',type='l')
title(main='(a) IBM returns')
plot(tdx,sp,xlab='year',ylab='lrtn',type='l') # X-axis first.
title(main='(b) SP index')
cor(ibm,sp)  # Obtain sample correlation
m1=lm(ibm~sp)  # Fit the Market Model (linear model)
summary(m1)
plot(sp,ibm,cex=0.8)  # Obtain scatter plot
ablines(0.008,.807) # Add the linear regression line


da = read.table("E:\\kuaipan\\A Introduction to Analysis of Financial Data with R\\chapter 1\\ch1data\\m-ibmsp-2611.txt", header = T)
ibm = log(da$ibm + 1)
sp = log(da$sp + 1)
rt=cbind(ibm, sp)
m1=apply(rt,2,mean)
v1= cov(rt) #协方差, 判断两个维度的相关度
library(mnormt)
x=rmnorm(1029,mean=m1,varcov=v1)#随机二元正态分布生成
plot(x[,2],x[,1],xlab='sim-sp',ylab='sim-ibm',cex=.8)



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
R语言绘图如何把图例绘制在外边及调整图例位置发布时间:2022-07-22
下一篇:
R语言预测实战(第一章)发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap