Advertisement
168888

hw1-2

Jul 9th, 2019
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 11.08 KB | None | 0 0
  1. require(data.table)  
  2. require(ggplot2)
  3. require(MASS)
  4. require(fBasics)
  5. require(moments)
  6. alpha = 0.05
  7.  
  8. #Question 1
  9. # 1. Consider the daily stock returns of American Express (AXP), Caterpillar (CAT), and Starbucks
  10. # (SBUX) from January 1999 to December 2008. The data are simple returns given in the file d-3stocks9908.txt (date, axp, cat, sbux).
  11. # (a) Compute the sample mean, standard deviation, skewness, excess kurtosis, minimum, and maximum of each simple return series. (Hint: use the R command basicStats of fBasics)
  12. # (b) Transform the simple returns to log returns. Compute the sample mean, standard deviation, skewness, excess kurtosis, minimum, and maximum of each log return series.
  13. # (c) Test the null hypothesis that the mean of the log returns of AXP is zero. (Hint: use the R command t.test)
  14. # (d) Obtain the histogram (with nclass=40) and sample density plot of the daily log returns of AXP stock.
  15. # (e) Test if log return of AXP follows normal distribution by using at least two methods. (Need to  give explanation)
  16.  
  17. # Import Data
  18. q1_data = fread("d-3stocks9908.txt",header=T)
  19.  
  20. # Part a
  21. q1_data_stats = basicStats(q1_data)
  22. sprintf("Getting Simple Returns")
  23.  
  24. for (stock in colnames(q1_data_stats)[-1]){
  25.   print(sprintf("%s (Simple Returns): Mean = %s , Stdev = %s, Skewness = %s , Kurtosis = %s , Minimum = %s , Maximum = %s",
  26.                 stock , q1_data_stats["Mean",stock] , q1_data_stats["Stdev",stock] , q1_data_stats["Skewness",stock] , q1_data_stats["Kurtosis",stock] ,  q1_data_stats["Minimum",stock] ,  q1_data_stats["Maximum",stock] ))
  27. }
  28.  
  29. # Part B
  30. q1_logR = log(q1_data+1)
  31. q1_logR_stats = basicStats(q1_logR)
  32.  
  33. for (stock in colnames(q1_logR_stats)[-1]){
  34.   print(sprintf("%s (Log Returns): Mean = %s , Stdev = %s, Skewness = %s , Kurtosis = %s , Minimum = %s , Maximum = %s",
  35.                 stock , q1_logR_stats["Mean",stock] , q1_logR_stats["Stdev",stock] , q1_logR_stats["Skewness",stock] , q1_logR_stats["Kurtosis",stock] ,  q1_logR_stats["Minimum",stock] ,  q1_logR_stats["Maximum",stock] ))
  36. }
  37.  
  38. # Part c
  39. axp_pvalue = t.test(q1_logR$axp)$p.value
  40. sprintf("AXP: The p-value is %s, hence we %s the null hypothesis that the mean is 0", axp_pvalue , if (axp_pvalue <= alpha) "Reject"  else "Do not reject")
  41.  
  42.  
  43.  
  44. # Part D
  45.  
  46. ggplot(data = q1_logR, aes(q1_logR$axp)) +
  47.   geom_histogram(bins = 40, col="grey",  fill="green",  alpha = .2) +
  48.   labs(title="Histogram of Daily Log Returns") +
  49.   labs(x="Daily Log Returns", y="Count")
  50.  
  51. plot(density(q1_logR$axp))
  52.  
  53. # Part E
  54. axp_jb_test = normalTest(q1_logR$axp,method="jb")
  55. axp_jb_pvalue = axp_jb_test@test$p.value
  56. sprintf("Jarque Bera Test returns the values for the 'Chi-squared' statistic with 2 degrees of freedom, and the asymptotic p-value.")
  57. sprintf("P Value is %s, hence %s null hypothesis of normality", axp_jb_pvalue ,if (axp_jb_pvalue <= alpha) "Reject"  else "Do not reject" )
  58.  
  59. axp_sw_test = normalTest(q1_logR$axp,method="sw")
  60. axp_sw_pvalue = axp_sw_test@test$p.value
  61. sprintf("Shapiro Test returns the values for the 'W' statistic and the p-value")
  62. sprintf("P Value is %s, hence %s null hypothesis of normality", axp_sw_pvalue ,if (axp_sw_pvalue <= alpha) "Reject"  else "Do not reject" )
  63.  
  64.  
  65. #Question 2
  66. # 2. Answer the same questions as Problem 1 but using monthly returns for General Motors (GM),
  67. # CRSP value-weighted index (VW), CRSP equal-weighted index (EW) and S&P composite index
  68. # from January 1975 to December 2008. The returns of the indexes include dividend distributions.
  69. # Data file is m-gm3dx7508.txt (date, gm, vw, ew, sp).
  70.  
  71. # Import Data
  72. q2_data <- fread("m-gm3dx7508.txt",header=T)
  73.  
  74. # Part a
  75. q2_data_stats = basicStats(q2_data)
  76.  
  77.  
  78. for (stock in colnames(q2_data_stats)[-1]){
  79.   print(sprintf("%s (Simple Returns): Mean = %s , Stdev = %s, Skewness = %s , Kurtosis = %s , Minimum = %s , Maximum = %s",
  80.                 stock , q2_data_stats["Mean",stock] , q2_data_stats["Stdev",stock] , q2_data_stats["Skewness",stock] , q2_data_stats["Kurtosis",stock] ,  q2_data_stats["Minimum",stock] ,  q2_data_stats["Maximum",stock] ))
  81. }
  82.  
  83. # Part B
  84. q2_logR = log(q2_data+1)
  85. q2_logR_stats = basicStats(q2_logR)
  86.  
  87. for (stock in colnames(q2_logR_stats)[-1]){
  88.   print(sprintf("%s (Log Returns): Mean = %s , Stdev = %s, Skewness = %s , Kurtosis = %s , Minimum = %s , Maximum = %s",
  89.                 stock , q2_logR_stats["Mean",stock] , q2_logR_stats["Stdev",stock] , q2_logR_stats["Skewness",stock] , q2_logR_stats["Kurtosis",stock] ,  q2_logR_stats["Minimum",stock] ,  q2_logR_stats["Maximum",stock] ))
  90. }
  91.  
  92.  
  93. for (stock in colnames(q2_logR)[-1]){
  94.   # Part c
  95.   pvalue = t.test(q2_logR_stats[,stock])$p.value
  96.   print(sprintf("%s: The p-value is %s, hence we %s the null hypothesis that the mean is 0", stock, pvalue , if (pvalue <= alpha) "Reject"  else "Do not reject"))
  97.  
  98.   # Part D
  99.   print( ggplot(data = q2_logR, aes(q2_logR[[stock]])) +
  100.            geom_histogram(bins = 40, col="grey",  fill="green",  alpha = .2) +
  101.            labs(title= cat("Histogram of Daily Log Returns")) +
  102.            labs(x=paste0("Daily Log Returns for ", stock), y="Count"))
  103.  
  104.   plot(density(q2_logR[[stock]]))
  105.  
  106.   # Part E
  107.   jb_test = normalTest(q2_logR[[stock]],method="jb")
  108.   jb_pvalue = jb_test@test$p.value
  109.   print(sprintf("Jarque Bera Test returns the values for the 'Chi-squared' statistic with 2 degrees of freedom, and the asymptotic p-value."))
  110.   print(sprintf("P Value is %s, hence %s null hypothesis of normality", jb_pvalue ,if (jb_pvalue <= alpha) "Reject"  else "Do not reject" ))
  111.  
  112.   sw_test = normalTest(q2_logR[[stock]],method="sw")
  113.   sw_pvalue = sw_test@test$p.value
  114.   print(sprintf("Shapiro Test returns the values for the 'W' statistic and the p-value"))
  115.   print(sprintf("P Value is %s, hence %s null hypothesis of normality", sw_pvalue ,if (sw_pvalue <= alpha) "Reject"  else "Do not reject" ))
  116.  
  117. }
  118.  
  119.  
  120. # Question 3
  121. # 3. Consider the monthly stock returns of value-weighted index (VW) from January 1975 to December 2008 in Problem 2.
  122. # Perform the tests and draw conclusions using the 5% significance level.
  123. # (a) Test H0: u = 0 versus Ha : μ != 0,where μ denotes the mean return.
  124. # (b) Test H0: m3 = 0 versus Ha : m3 != 0, where m3 denotes the skewness.
  125. # (c) Test H0: K = 3 versus Ha : K != 3, where K denotes the kurtosis.
  126.  
  127. # Part a
  128. stock = "vw"
  129. pvalue = t.test(q2_logR[[stock]]  ) $p.value
  130. sprintf("%s: The p-value is %s, hence we %s the null hypothesis that the mean is 0", stock,  pvalue , if (pvalue <= alpha) "Reject"  else "Do not reject")
  131.  
  132. # Part b
  133. pvalue = agostino.test(q2_logR[[stock]] )$p.value
  134. sprintf("%s:: The p-value is %s, hence we %s the null hypothesis that skewness is 0",  stock, pvalue , if (pvalue <= alpha) "Reject"  else "Do not reject")
  135.  
  136. # Part c
  137. pvalue = anscombe.test(q2_logR[[stock]] )$p.value
  138. sprintf("%s:: The p-value is %s, hence we %s the null hypothesis that the kurtosis is 3", stock,  pvalue , if (pvalue <= alpha) "Reject"  else "Do not reject")
  139.  
  140.  
  141.  
  142. # Question 4
  143. # 4. Consider the daily log returns of AXP stock from January 1999 to December 2008 as in
  144. # Problem 1. Perform the following tests:
  145. # (a) Test the null hypothesis that the skewness measure of the returns is zero;
  146. # (b) Test the null hypothesis that the excess kurtosis of the returns is zero.
  147.  
  148. stock = "axp"
  149. pvalue = agostino.test(q1_logR[[stock]] )$p.value
  150. sprintf("%s:: The p-value is %s, hence we %s the null hypothesis that skewness is 0",  stock, pvalue , if (pvalue <= alpha) "Reject"  else "Do not reject")
  151.  
  152. # Part c
  153. pvalue = anscombe.test(q1_logR[[stock]] )$p.value
  154. sprintf("%s:: The p-value is %s, hence we %s the null hypothesis that the kurtosis is 3", stock,  pvalue , if (pvalue <= alpha) "Reject"  else "Do not reject")
  155.  
  156.  
  157.  
  158. #Question 5
  159. # 5. Daily foreign exchange rates (spot rates) can be obtained from the Federal Reserve Bank in St
  160. # Louis (FRED). The data are the noon buying rates in New York City certified by the Federal
  161. # Reserve Bank of New York. Consider the exchange rates between the U.S. dollar and the Euro
  162. # from January 4, 1999 to March 8, 2013. See the file d-exuseu.txt.
  163. #
  164. # (a) Compute the daily log return of the exchange rate.
  165. # (b) Compute the sample mean, standard deviation, skewness, excess kurtosis, minimum, and
  166. # maximum of the log returns of the exchange rate.
  167. # (c) Obtain a density plot of the daily long returns of Dollar-Euro exchange rate.
  168. # (d) Test H0: u = 0 versus Ha : u != 0, where μ denotes the mean of the daily log return of Dollar-Euro exchange rate.
  169.  
  170. # Par A
  171. q5_data =  fread("d-exuseu.txt",header=T)
  172. q5_logR = diff(log(q5_data$VALUE))
  173.  
  174. #5b
  175. q5_stats = basicStats(q5_logR)
  176. sprintf("FRED (Log Returns): Mean = %s , Stdev = %s, Skewness = %s , Kurtosis = %s , Minimum = %s , Maximum = %s",
  177.         q5_stats["Mean",] , q5_stats["Stdev",] , q5_stats["Skewness",] , q5_stats["Kurtosis",] ,  q5_stats["Minimum",] ,  q5_stats["Maximum",] )
  178.  
  179. #5c
  180. plot(density(q5_logR))
  181. #5d
  182.  
  183. pvalue = t.test(q5_logR)$p.value
  184. sprintf("%s: The p-value is %s, hence we %s the null hypothesis that the mean is 0", "Dollar Euro Exchange Return",  pvalue , if (pvalue <= alpha) "Reject"  else "Do not reject")
  185.  
  186. #Question 6
  187. # 6. Consider SP500 and IBM data (in file ‘SP.csv’ and ‘IBM.csv’ respectively):
  188. # (a) Find the log return of SP and IBM by using ‘Adjusted Price’
  189. # (b) Merge the above two return series into a new dataset (hint: using ‘data.frame’) and plot them (y is IBM, x is SP)
  190. # (c) Find the best linear model by using AIC as criterion (need to show all your models)
  191.  
  192. sp_data =  fread("SP.csv",header=T)
  193. ibm_data =  fread("IBM.csv",header=T)
  194.  
  195. # Part A
  196. sp_logR = diff(log(sp_data[["SP.Adjusted"]]))
  197. ibm_logR = diff(log(ibm_data[["IBM.Adjusted"]]))
  198.  
  199. # Part B
  200. new_table = data.table(y = ibm_logR , x = sp_logR)
  201. qplot(new_table$y, new_table$x)
  202.  
  203.  
  204. # Part C
  205. aic_list = list()
  206.  
  207. # MODEL 1: Simple linear regression with intercept
  208. m1 <- lm(y~x, new_table)
  209. summary(m1)
  210. aic_list = c(aic_list ,AIC(m1))
  211.  
  212. # MODEL 2: Simple linear regression with no intercept
  213. m2 <- lm(y ~ -1 + x, new_table)
  214. summary(m2)
  215. aic_list = c(aic_list ,AIC(m2))
  216.  
  217.  
  218. # Get index of all elements whose returns are non positive
  219. idx = which(new_table$x <= 0)
  220.  
  221. # List with non positive returns, otherwise 0
  222. nsp <- rep(0,length(new_table$x))
  223. nsp[idx] = new_table$x[idx]
  224.  
  225. # List with 1 when non positive returns, otherwise 0
  226. c1 <- rep(0,length(new_table$x))
  227. c1[idx] = 1
  228.  
  229.  
  230. new_table2 <- data.frame(y = new_table$y, x = new_table$x, c1, nsp)   # Show the resulting variables
  231.  
  232. # Model 3: Different intercepts for positive, and non positive returns
  233. m3 <- lm(y ~ x + c1 , new_table2) #with different intercepts (alpha) for positive and negative SP log returns
  234. summary(m3)
  235. aic_list = c(aic_list ,AIC(m3))
  236.  
  237. # Model 4: Different coefficients for positive and non positive returns
  238. m4 <- lm(y ~ x + nsp, new_table2) #with different coefficients (beta) for positive and negative SP log returns
  239. summary(m4)
  240. aic_list = c(aic_list ,AIC(m4))
  241.  
  242.  
  243. # Model 5: Different coefficients and intercept for positive and non positive returns
  244. m5 <- lm(y ~ x + c1 + nsp, new_table2)
  245. summary(m5)
  246. aic_list = c(aic_list ,AIC(m5))
  247. idx = which.min(aic_list)
  248. sprintf("The best model is model %s, with an AIC of %s" , idx , aic_list[idx])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement