Showing posts with label Estimation methods based on L-moments. Show all posts
Showing posts with label Estimation methods based on L-moments. Show all posts

## Estimation methods based on L-moments for various distributions

SieDates <- as.character(format(as.POSIXct(attr(siemens,"times")),"%Y-%m-%d"))
SieRet <- timeSeries(siemens * 100,charvec = SieDates)
colnames(SieRet) <- "SieRet"
## Stylised Facts 1
par(mfrow = c(2,2))
seriesPlot(SieRet,title = FALSE,main = "Daily Returns of Siemens",col = "blue")
boxPlot(SieRet, title = FALSE, main = "Box plot of Returns",col = "red",cex =0.5,pch = 19)
acf(SieRet,main ="ACF of Returns",lag.max = 20, ylab = "", xlab ="",col = "blue",ci.col = "red")
pacf(SieRet,main ="PACF of Returns",lag.max = 20,ylab ="",xlab= "",col="blue",ci.col="red")
## Stylised Facts II
SieRetAbs <- abs(SieRet)
SieRet100 <- tail(sort(abs(series(SieRet))), 100)[1]
idx <- which(series(SieRetAbs) > SieRet100,arr.ind =TRUE)
SieRetAbs100 <- timeSeries(rep(0,length(SieRet)),charvec = time(SieRet))
SieRetAbs100[idx , 1] <- SieRetAbs[idx]
acf(SieRetAbs,main="ACF of Absolute Returns",lag.max = 20,ylab="",xlab="",col ="blue",ci.col="red")
pacf(SieRetAbs,main="PACF of Absolute Returns",lag.max = 20, ylab="", xlab="",col="blue",ci.col="red")
qqnormPlot(SieRet,main="QQ-Plot of Returns",title = FALSE,col = "blue",cex = 0.5,pch= 19)
plot(SieRetAbs100,type="h",main="Volatility Clustering",ylab="",xlab="",col="blue")
li b ra r y ( zoo ) 1
data ( EuStockMarkets ) 2
## Time Series plot of Levels
EuStockLevel <- as.zoo(EuStockMarkets)[,c("DAX", "CAC","FTSE")]
plot(EuStockLevel,xlab = "",main= "")
##Percentage returns
EuStockRet <- diff(log(EuStockLevel))*100
plot(EuStockRet,xlab = "",main= "")

## Cross correlation
layout(matrix(1:6,nrow = 3,ncol = 2,byrow = TRUE ))
ccf(EuStockRet[,1],EuStockRet[,2], ylab="",xlab="",lag.max = 20,main="Returns DAX vs CAC")
ccf(abs(EuStockRet)[, 1],abs(EuStockRet)[,2], ylab="",xlab="",lag.max = 20,main="Absolute returns DAX vs CAC")
ccf(EuStockRet[,1],EuStockRet[,3], ylab="", xlab="",lag.max = 20,main="Returns DAX vs FTSE")
ccf(abs(EuStockRet)[, 1],abs(EuStockRet)[,3], ylab="",xlab="",lag.max = 20,main="Absolute returns DAX vs FTSE")
ccf(EuStockRet[,2],EuStockRet[,3], ylab="",xlab="",lag.max = 20,main="Returns CAC vs FTSE")
ccf(abs(EuStockRet)[, 2],abs(EuStockRet)[,3], ylab="",xlab="",lag.max = 20,main="Absolute returns CAC vs FTSE")
## Rolling correlations
rollc <- function(x){dim <- ncol(x)
rcor <- cor(x)[lower.tri(diag(dim),diag = FALSE)]
return(rcor)
}
rcor<- rollapply(EuStockRet, width = 250, rollc,align = "right",by.column = FALSE)
colnames(rcor)<- c("DAX & CAC","DAX & FTSE","CAC & FTSE")
plot(rcor,main ="",xlab="")
li b r a r y ( ghyp ) 1
library (timeSeries ) 2
library ( fBasics ) 3
## Return calculation 4
data ( DowJones30 )
y <-timeSeries(DowJones30[,"HWP"],charvec = as.character(DowJones30[,1]))
yret<- na.omit(diff(log(y))*100)
##Fitting
ef<-density(yret)
ghdfit<-fit.ghypuv(yret,symmetric = FALSE,control = list(maxit =1000))
hypfit <-fit.hypuv(yret,symmetric = FALSE,control = list(maxit =1000))
nigfit<-fit.NIGuv(yret,symmetric = FALSE,control = list(maxit =1000))
##Densities
ghddens <- dghyp(ef\$x,ghdfit)
hypdens <- dghyp(ef\$x,hypfit)
nigdens <- dghyp(ef\$x,nigfit)
nordens <- dnorm(ef\$x,mean = mean(yret),sd=sd(c(yret[,1])))
col.def <-c("black","red","blue","green","orange")
plot(ef,xlab="",ylab=expression(f(x)),ylim =c(0,0.25))
lines(ef\$x,ghddens,col="red")
lines(ef\$x,hypdens,col="blue")
lines(ef\$x,nigdens,col="green")
lines(ef\$x,nordens,col="orange")

legend("topleft",legend = c("empirical","GHD","HYP","NIG","NORM"),col = col.def, Ity = 1)

qqghyp(ghdfit,line = TRUE,ghyp.col = "red",plot.legend = FALSE,gaussian = FALSE,main = "",cex=0.8)

### Black-Scholes formula-R

Black-Scholes formula-R > BlackScholes <- function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma) { TypeFla...