1、r语言股票下载 ar模型 描述统计 附代码数据christyxyu.RAdministratorWed Feb 08 23:39:24 2017# a. Download the manual for Time Series Analysis with R, Part I by Walter Zucchini, Oleg Nenadi for reference# as you may need it to complete the assignment.# http:/www.statoek.wiso.uni-goettingen.de/veranstaltungen/zeitreihen/
2、sommer03/ts_r_intro.pdf# b. Download Quantmod manual# c. Search for Microsoft symbol in Google financelibrary(quantmod)# Loading required package: xts# Loading required package: zoo# # Attaching package: zoo# The following objects are masked from package:base:# # as.Date, as.Date.numeric# Loading re
3、quired package: TTR# Version 0.4-0 included new data defaults. See ?getSymbols.#Microsoft symbol in Google finance is MSFT# d. Download Microsoft stock price data from Jan 3, 2010 to Jan 20, 2016 in R using getSymbols command in# Quantmod, check if data is read properly using head and tail command 5
4、getSymbols(MSFT, from = 2010-01-03, to = 2016-01-20) # As of 0.4-0, getSymbols uses env=parent.frame() and# auto.assign=TRUE by default.# # This behavior will be phased out in 0.5-0 when the call will# default to use auto.assign=FALSE. getOption(getSymbols.env) and # getOptions(getSymbols.auto.assig
5、n) are now checked for alternate defaults# # This message is shown once per session and may be disabled by setting # options(getSymbols.warning4.0=FALSE). See ?getSymbols for more details.# 1 MSFThead(MSFT)# MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume# 2010-01-04 30.62 31.10 30.59 30.95 3840
6、9100# 2010-01-05 30.85 31.10 30.64 30.96 49749600# 2010-01-06 30.88 31.08 30.52 30.77 58182400# 2010-01-07 30.63 30.70 30.19 30.45 50559700# 2010-01-08 30.28 30.88 30.24 30.66 51197400# 2010-01-11 30.71 30.76 30.12 30.27 68754700# MSFT.Adjusted# 2010-01-04 25.71042# 2010-01-05 25.71872# 2010-01-06 2
7、5.56089# 2010-01-07 25.29506# 2010-01-08 25.46951# 2010-01-11 25.14553tail(MSFT)# MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume# 2016-01-12 52.76 53.10 52.06 52.78 36095500# 2016-01-13 53.80 54.07 51.30 51.64 66883600# 2016-01-14 52.00 53.42 51.57 53.11 52381900# 2016-01-15 51.31 51.97 50.34 5
8、0.99 70739100# 2016-01-19 51.48 51.68 50.06 50.56 43564500# 2016-01-20 49.98 51.38 49.10 50.79 63273000# MSFT.Adjusted# 2016-01-12 51.37038# 2016-01-13 50.26083# 2016-01-14 51.69157# 2016-01-15 49.62819# 2016-01-19 49.20968# 2016-01-20 49.43353# e. Plot graph for stock price 5plot(MSFT)# Warning in
9、plot.xts(MSFT): only the univariate series will be plotted# f. Calculate log returns for Adjusted Series and Plot simple time series graph for returns 5 n - length(MSFT,4);lrest - log(as.numeric(MSFT,4)-1/as.numeric(MSFT,4)-n)# g. Plot returns distribution graph 5hist(lrest,breaks = 50,col = green,f
10、req = F)lines(density(lrest),col=red)# h. What did you learn about the data in step (g) 5#直方图来看,数据为左偏分布# i. Calculate Basic Statistics for Return Series and report 5summary(lrest)# Min. 1st Qu. Median Mean 3rd Qu. Max. # -0.1210000 -0.0076650 0.0000000 0.0003257 0.0081750 0.0994100# j. Look at the b
11、asic stats and comment on the values for skewness and kurtosis 5#library(fBasics)# Loading required package: timeDate# Loading required package: timeSeries# # Attaching package: timeSeries# The following object is masked _by_ .GlobalEnv:# # MSFT# The following object is masked from package:zoo:# # t
12、ime-# # Rmetrics Package fBasics# Analysing Markets and calculating Basic Statistics# Copyright (C) 2005-2014 Rmetrics Association Zurich# Educational Software for Financial Engineering and Computational Science# Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.# https:/www.rmetrics.o
13、rg - Mail to: informetrics.org# # Attaching package: fBasics# The following object is masked from package:TTR:# # volatilitydatadesc = function(X) result = list(0);#result list to return mean = mean(X);#mean var = var(X)#variance, pearsonskew = 3*(mean(X)-median(X)/sd(X)#Pearson coefficient of skewn
14、ess kurt = kurtosis(X) #kurtosis, quantile1 = quantile(X,probs = 0.25) # first quartile, med = median(X)# median, quantile3 = quantile(X,probs = 0.25)# third quartile, max = max(X)# minimum and min = min(X)# maximum. result = list( mean = mean, variance = var, skewness = pearsonskew, kurtosis = kurt
15、, first quartile = quantile1, median = med, third quartile = quantile3, maximum = max, minimum = min ) return(result)datadesc(lrest)# $mean# 1 0.0003256584# # $variance# 1 0.0002164387# # $skewness# 1 0.06640734# # $kurtosis# 1 7.519028# attr(,method)# 1 excess# # $first quartile# 25% # -0.007664925
16、 # # $median# 1 0# # $third quartile# 25% # -0.007664925 # # $maximum# 1 0.09941299# # $minimum# 1 -0.1210332#从峰度和偏度的值来看,由于偏度大于零,因此数据成右偏分布。峰度大于0,说明它是比正态分布要陡峭# k. Setup Hypothesis for testing mean, skewness and kurtosis 5 #H0: If values of mean, skewness and kurtosis is close to zero, then data set i
17、s normally distributed.# l. Test the Hypothesis using tests learned in class (i-individual tests; and ii-combined test JB;# report your test results with p-values and whether you reject or accept H0) 5#i-individual tests;# Shapiro-Wilkshapiro.test(lrest)# # Shapiro-Wilk normality test# # data: lrest
18、# W = 0.93798, p-value 2.2e-16#ii-combined test JBtseries:jarque.bera.test(lrest)# # Jarque Bera Test# # data: lrest# X-squared = 3599.1, df = 2, p-value 2.2e-16#从结果来看,由于p小于0.05,因此可以拒绝原假设H0 # m. Comment about data properties based on these tests: #从正态分布的Shapiro-Wilk检验和jarque-Bera正态性检验的结果来看,有p小于0.05,
19、因此可以拒绝对数收益率符合正态分布的假设,可以认为收益率,不是正态分布# i. The difference between step (g) vs step (j) 5#从密度直方图的结果只能从直觉上判断数据是否符合正态分布,而通过峰度和偏度来判断,更加精确,可以从数字的角度来判断数据是否呈现偏正态分布。 # ii. why joint tests is better than individual# A joint hypothesis tests more than one condition simultaneously# 7. Download the sp500data using
20、 Symbol GSPC from Jan 3, 2011 to Jan 20, 2016 (50 pts)# a. Carry out initial steps, such as calculate log returns, draw time plot, distribution graph,# calculate returns, basic stat and employ related tests 25getSymbols(GSPC, from = 2011-01-03, to = 2016-01-20) # 1 GSPChead(GSPC)# GSPC.Open GSPC.Hig
21、h GSPC.Low GSPC.Close GSPC.Volume# 2011-01-03 22.60 22.600 22.4708 22.4900 6600# 2011-01-04 22.59 22.590 22.4500 22.4752 5100# 2011-01-05 22.61 22.760 22.6100 22.7100 15000# 2011-01-06 22.76 22.939 22.7100 22.7600 15900# 2011-01-07 22.93 22.930 22.7900 22.9200 7100# 2011-01-10 22.81 22.970 22.8000 2
22、2.9700 21500# GSPC.Adjusted# 2011-01-03 16.9375# 2011-01-04 16.9264# 2011-01-05 17.1032# 2011-01-06 17.1409# 2011-01-07 17.2614# 2011-01-10 17.2990tail(GSPC)# GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume# 2016-01-12 21.40 21.41 21.3400 21.4000 6900# 2016-01-13 21.40 21.42 21.3000 21.4000 1190
23、0# 2016-01-14 21.44 21.44 21.2201 21.3500 11800# 2016-01-15 21.30 21.30 21.0500 21.2245 11100# 2016-01-19 21.38 21.40 21.0500 21.2659 8100# 2016-01-20 21.25 21.27 21.0746 21.2700 23000# GSPC.Adjusted# 2016-01-12 20.4356# 2016-01-13 20.4356# 2016-01-14 20.3878# 2016-01-15 20.2680# 2016-01-19 20.3075#
24、 2016-01-20 20.3114plot(GSPC)# Warning in plot.xts(GSPC): only the univariate series will be plottedn - length(GSPC,4);lrest - log(as.numeric(GSPC,4)-1/as.numeric(GSPC,4)-n) hist(lrest,breaks = 50,col = green,freq = F)lines(density(lrest),col=red)datadesc(lrest)# $mean# 1 -4.395046e-05# # $variance#
25、 1 6.726461e-05# # $skewness# 1 -0.0160765# # $kurtosis# 1 5.903059# attr(,method)# 1 excess# # $first quartile# 25% # -0.003570522 # # $median# 1 0# # $third quartile# 25% # -0.003570522 # # $maximum# 1 0.04301739# # $minimum# 1 -0.05164179# Shapiro-Wilkshapiro.test(lrest)# # Shapiro-Wilk normality
26、 test# # data: lrest# W = 0.92202, p-value 2.2e-16#ii-combined test JBtseries:jarque.bera.test(lrest)# # Jarque Bera Test# # data: lrest# X-squared = 1866.5, df = 2, p-value 2.2e-16# b. What did you learn about the data properties in step (a) 5# 从描述统计量的结果来看,数据为左偏分布,从假设检验的结果来,由于p小于0.05,因此拒绝原假设,可以认为数据
27、不符合正态分布# c. Plot pacf, acf, also check the order of the model using ar command in R# Which AR model is suggested 5acf(lrest)pacf(lrest)ar(lrest)# # Call:# ar(x = lrest)# # Coefficients:# 1 2 3 4 5 6 7 8 # -0.0060 0.0549 0.0132 0.0118 -0.0237 0.0496 0.0342 0.0983 # 9 10 # -0.0411 0.0416 # # Order sel
28、ected 10 sigma2 estimated as 6.625e-05# d. Estimate both AR(1) and AR(2) model for returns to SP500 index# and comment on significance of parameters 5fit =ar(lrest, FALSE, 1) # fit ar(1)fit # # Call:# ar(x = lrest, aic = FALSE, order.max = 1)# # Coefficients:# 1 # -0.0084 # # Order selected 1 sigma2
29、 estimated as 6.731e-05 (1-pnorm(abs(fit$ar)/sqrt(diag(fit$asy.var.coef)*2# 1 0.7640941fit=ar(lrest, FALSE, 2) # fit ar(2)fit # # Call:# ar(x = lrest, aic = FALSE, order.max = 2)# # Coefficients:# 1 2 # -0.0079 0.0649 # # Order selected 2 sigma2 estimated as 6.708e-05(1-pnorm(abs(fit$ar)/sqrt(diag(fit$asy.var.coef)*2# 1 0.77862276 0.02073683#从结果来,ar2模型的系数显著性相对ar1更强 # e. Check and report if the residuals for models estimated in part (d) are white noise or not 5fit =ar(lrest, FALSE, 1) # fit ar(1)fit # # Call:# ar(x = lrest, aic = FAL
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1