# Posted By

mjaniec on 09/13/11

# Statistics

Viewed 882 times
Favorited by 0 user(s)

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

/ Published in: R

Copy this code and paste it in your HTML
1. library(e1071) # skewness, kurtosis
2. library(NORMT3)
3. library(VGAM) # Laplace
5. library(fBasics) # dstable, stableFit -> levy
6.
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.
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.
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.
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.
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)