Analysis of the stock market daily returns distribution based on WIG20 index


/ Published in: R
Save to your folder(s)



Copy this code and paste it in your HTML
  1. library(e1071) # skewness, kurtosis
  2. library(NORMT3)
  3. library(VGAM) # Laplace
  4. library(ADGofTest) # ad.test
  5. library(fBasics) # dstable, stableFit -> levy
  6.  
  7. data <- read.csv("wig20_d.csv")
  8.  
  9. num_of_quotes <- nrow(data)
  10.  
  11. daily_changes <- log(data$Close[2:num_of_quotes]/data$Close[1:(num_of_quotes-1)])
  12.  
  13. par(mfrow=c(2,1))
  14.  
  15. plot(data$Close,type="l")
  16. plot(daily_changes,type="l")
  17.  
  18. par(mfrow=c(1,1))
  19.  
  20. summary(daily_changes)
  21. sd(daily_changes)
  22.  
  23. quantile(daily_changes)
  24.  
  25. quantile(daily_changes,c(0.001,0.01,0.05,0.95,0.99,0.999))
  26.  
  27. skewness(daily_changes)
  28.  
  29. kurtosis(daily_changes)
  30.  
  31. fitdistr(daily_changes,"normal")
  32.  
  33. mec <- mean(daily_changes)
  34. sdc <- sd(daily_changes)
  35.  
  36. ad.test(daily_changes,pnorm,mec,sdc)
  37.  
  38. fitdistr(exp(mec+sdc*daily_changes),"lognormal")
  39.  
  40. (cauchy_fit <- fitdistr(daily_changes,"cauchy"))
  41.  
  42. cf_l <- cauchy_fit$estimate[1]
  43. cf_s <- cauchy_fit$estimate[2]
  44.  
  45. ad.test(daily_changes,pcauchy,cf_l,cf_s)
  46.  
  47. laplace_fit <- vglm(daily_changes~1,laplace,data.frame(daily_changes),trace=TRUE,crot="l")
  48. Coef(laplace_fit)
  49.  
  50. lf_l <- Coef(laplace_fit)[1]
  51. lf_s <- Coef(laplace_fit)[2]
  52.  
  53. ad.test(daily_changes,plaplace,lf_l,lf_s)
  54.  
  55. (stable_fit <- stableFit(daily_changes))
  56.  
  57. sf_a <- stable_fit@fit$estimate[1]
  58. sf_b <- stable_fit@fit$estimate[2]
  59. sf_g <- stable_fit@fit$estimate[3]
  60. sf_d <- stable_fit@fit$estimate[4]
  61.  
  62. ad.test(daily_changes,pstable,sf_a,sf_b,sf_g,sf_d)
  63.  
  64. stabledist::pstable(-0.1,sf_a,sf_b,sf_g,sf_d) # probability of -10% or less
  65. 1-stabledist::pstable(0.1,sf_a,sf_b,sf_g,sf_d) # probability of +10% or more
  66. stabledist::pstable(0.055,sf_a,sf_b,sf_g,sf_d)-stabledist::pstable(-0.055,sf_a,sf_b,sf_g,sf_d) # prob. of (-5.5%, +5.5%)
  67.  
  68. nd <- dnorm(x,mec,sdc)
  69. cd <- dcauchy(x,cauchy_fit$estimate[1],cauchy_fit$estimate[2])
  70. ld <- dlaplace(x,lf_l,lf_s)
  71. sd <- stabledist::dstable(x,sf_a,sf_b,sf_g,sf_d)
  72.  
  73. plot(density(daily_changes),col="Black",ylim=c(0,max(nd,cd,ld,sd)),lwd=2)
  74. curve(dnorm(x,mec,sdc),add=T,col="Green")
  75. curve(dcauchy(x,cf_l,cf_s),add=T,col="Blue")
  76. curve(dlaplace(x,lf_l,lf_s),add=T,col="Orange")
  77. curve(stabledist::dstable(x,sf_a,sf_b,sf_g,sf_d),add=T,col="Red")
  78.  
  79. legend("left",c("black - Observed","green - Normal","blue - Cauchy","orange - Laplace","red - Stable"),fill=c("Black","Green","Blue","Orange","Red"))

URL: http://reakkt.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.