/ Published in: R
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
library(e1071) # skewness, kurtosis library(NORMT3) library(VGAM) # Laplace library(ADGofTest) # ad.test library(fBasics) # dstable, stableFit -> levy data <- read.csv("wig20_d.csv") num_of_quotes <- nrow(data) daily_changes <- log(data$Close[2:num_of_quotes]/data$Close[1:(num_of_quotes-1)]) par(mfrow=c(2,1)) plot(data$Close,type="l") plot(daily_changes,type="l") par(mfrow=c(1,1)) summary(daily_changes) sd(daily_changes) quantile(daily_changes) quantile(daily_changes,c(0.001,0.01,0.05,0.95,0.99,0.999)) skewness(daily_changes) kurtosis(daily_changes) fitdistr(daily_changes,"normal") mec <- mean(daily_changes) sdc <- sd(daily_changes) ad.test(daily_changes,pnorm,mec,sdc) fitdistr(exp(mec+sdc*daily_changes),"lognormal") (cauchy_fit <- fitdistr(daily_changes,"cauchy")) cf_l <- cauchy_fit$estimate[1] cf_s <- cauchy_fit$estimate[2] ad.test(daily_changes,pcauchy,cf_l,cf_s) laplace_fit <- vglm(daily_changes~1,laplace,data.frame(daily_changes),trace=TRUE,crot="l") Coef(laplace_fit) lf_l <- Coef(laplace_fit)[1] lf_s <- Coef(laplace_fit)[2] ad.test(daily_changes,plaplace,lf_l,lf_s) (stable_fit <- stableFit(daily_changes)) sf_a <- stable_fit@fit$estimate[1] sf_b <- stable_fit@fit$estimate[2] sf_g <- stable_fit@fit$estimate[3] sf_d <- stable_fit@fit$estimate[4] ad.test(daily_changes,pstable,sf_a,sf_b,sf_g,sf_d) stabledist::pstable(-0.1,sf_a,sf_b,sf_g,sf_d) # probability of -10% or less 1-stabledist::pstable(0.1,sf_a,sf_b,sf_g,sf_d) # probability of +10% or more 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%) nd <- dnorm(x,mec,sdc) cd <- dcauchy(x,cauchy_fit$estimate[1],cauchy_fit$estimate[2]) ld <- dlaplace(x,lf_l,lf_s) sd <- stabledist::dstable(x,sf_a,sf_b,sf_g,sf_d) plot(density(daily_changes),col="Black",ylim=c(0,max(nd,cd,ld,sd)),lwd=2) curve(dnorm(x,mec,sdc),add=T,col="Green") curve(dcauchy(x,cf_l,cf_s),add=T,col="Blue") curve(dlaplace(x,lf_l,lf_s),add=T,col="Orange") curve(stabledist::dstable(x,sf_a,sf_b,sf_g,sf_d),add=T,col="Red") legend("left",c("black - Observed","green - Normal","blue - Cauchy","orange - Laplace","red - Stable"),fill=c("Black","Green","Blue","Orange","Red"))
URL: http://reakkt.com