1、#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 5getSymbols(MSFT, from = 2010-01-03, to = 2016-01-20) # As of 0.4-0, getSymbols
2、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.assign) are now checked for alternate defaults# This message is shown once per session
3、 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 38409100# 2010-01-05 30.85 31.10 30.64 30.96 49749600# 2010-01-06 30.88 31.08 30.52 30.
4、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 25.56089# 2010-01-07 25.29506# 2010-01-08 25.46951# 2010-01-11 25.14553tail(MSFT)# M
5、SFT.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 50.99 70739100# 2016-01-19 51.48 51.68 50.06 50.56 43564500# 2016-01-20 49.98 51.38
6、49.10 50.79 63273000# 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 plot.xts(MSFT): only the univariate series will be plotted# f. Calculate log returns for Adjusted
7、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,freq = F)lines(density(lrest),col=red)# h. What did you learn about the data in step (g) 5#直方图来看,数据
8、为左偏分布# 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 basic stats and comment on the values for skewness and kurtosis 5#library(fBasics) timeDate timeSer
9、iestimeSeries# The following object is masked _by_ .GlobalEnv# MSFT# The following object is masked from # time-# Rmetrics Package fBasics# Analysing Markets and calculating Basic Statistics# Copyright (C) 2005-2014 Rmetrics Association Zurich# Educational Software for Financial Engineering and Comp
10、utational Science# Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.# https:/www.rmetrics.org - Mail to: informetrics.orgfBasicsTTR# volatilitydatadesc = function(X) result = list(0);#result list to return mean = mean(X);#mean var = var(X)#variance, pearsonskew = 3*(mean(X)-median(X)/
11、sd(X)#Pearson coefficient of skewness 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, skewn
12、ess = pearsonskew, kurtosis = kurt, 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(,methodexcess# $first quartile#
13、 25% # -0.007664925 # $median# 1 0# $third quartile# $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 is normally
14、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# W = 0.93798,
15、p-value 2.2e-16#ii-combined test JBtseries:jarque.bera.test(lrest)# Jarque Bera Test# X-squared = 3599.1, df = 2, p-value #从结果来看,由于p小于0.05,因此可以拒绝原假设H0 # m. Comment about data properties based on these tests: #从正态分布的Shapiro-Wilk检验和jarque-Bera正态性检验的结果来看,有p小于0.05,因此可以拒绝对数收益率符合正态分布的假设,可以认为收益率,不是正态分布# i.
16、 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 Symbol GSPC from Jan 3, 2011 to Jan 20,
17、 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 25GSPC2011-01-03GSPChead(GSPC)# GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume# 2011-01-03 22.60 22.600 22.4708 22.4900 6600# 20
18、11-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 22.9700 21500# GSPC.Adjusted# 2011-01-03 16.9375# 2011-01-04 16.9264# 2011-01-05 17.
19、1032# 2011-01-06 17.1409# 2011-01-07 17.2614# 2011-01-10 17.2990tail(GSPC)# 2016-01-12 21.40 21.41 21.3400 21.4000 6900# 2016-01-13 21.40 21.42 21.3000 21.4000 11900# 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 81
20、00# 2016-01-20 21.25 21.27 21.0746 21.2700 23000# 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# 2016-01-20 20.3114plot(GSPC)# Warning in plot.xts(GSPC):- length(GSPC,4);- log(as.numeric(GSPC,4)-1/as.numeric(GSPC,4)-n)# 1 -4.395046e-05# 1 6.726461
21、e-05# 1 -0.0160765# 1 5.903059# -0.003570522 # 1 0.04301739# 1 -0.05164179# W = 0.92202, p-value # X-squared = 1866.5, df = 2, p-value # b. What did you learn about the data properties in step (a) 5# 从描述统计量的结果来看,数据为左偏分布,从假设检验的结果来,由于p小于0.05,因此拒绝原假设,可以认为数据不符合正态分布# c. Plot pacf, acf, also check the ord
22、er 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 selected 10 sigma2 estimated as 6.625e-05# d. Estimate
23、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 # ar(x = lrest, aic = FALSE, order.max = 1)# 1 # -0.0084 # Order selected 1 sigma2 estimated as 6.731e-05 (1-pnorm(abs(fit$ar)/sqrt(diag(fit$asy.var.coef)*2# 1 0.
24、7640941fit=ar(lrest, FALSE, 2) # fit ar(2)# ar(x = lrest, aic = FALSE, order.max = 2)# 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 5# ar(x = lrest, aic = FAL
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1