df=read.csv(file="sample_data.csv",header=TRUE, sep=",")
head(df)
## stock1 stock2 stock3 stock4 stock5
## 1 100.00000 100.00000 100.00000 100.00000 100.0000
## 2 100.02068 99.99415 100.04909 99.92849 100.1941
## 3 100.02660 100.01964 99.94416 99.92583 100.4359
## 4 100.00023 100.07849 99.94365 99.96918 100.4597
## 5 100.00754 100.13089 99.87517 99.93167 100.4614
## 6 99.96268 100.15621 99.91428 99.96727 100.3518
stock1=ts(df[1])
stock2=ts(df[2])
stock3=ts(df[3])
stock4=ts(df[4])
stock5=ts(df[5])
Diff SDEs
library(Sim.DiffProc)
library(Ecdat)
fx <-{}
gx <-{}
#model 1 drift and diffusion
fx[1] <- expression( theta[1]*x )
gx[1]<- expression( theta[2]*x^theta[3] )
#model 2 drift and diffusion
fx[2] <- expression( theta[1]+theta[2]*x )
gx[2]<- expression( theta[3]*x^theta[4] )
#model 3 drift and diffusion
fx[3] <- expression( theta[1]+theta[2]*x )
gx[3]<- expression( theta[3]*sqrt(x) )
#model 4 drift and diffusion
fx[4]<- expression( theta[1] )
gx[4] <- expression( theta[2]*x^theta[3] )
#model 5 drift and diffusion
fx[5] <- expression( theta[1]*x )
gx[5] <- expression(theta[2] + (theta[3]*x^theta[4]) )
pmle=eval(formals(fitsde.default)$pmle)
print("We'll use euler method for our Maximum Likelyhood")
## [1] "We'll use euler method for our Maximum Likelyhood"
Best.fit<-function(data,pmle)
{
#model1
mod1 <- fitsde(data=data,drift=fx[1],diffusion=gx[1],start =
list(theta1=1, theta2=1,theta3=1),pmle=pmle)
#model 2
mod2 <- fitsde(data=data,drift=fx[2],diffusion=gx[2],start =
list(theta1=1, theta2=1,theta3=1,theta4=1),pmle=pmle)
#model 3
mod3 <- fitsde(data=data,drift=fx[3],diffusion=gx[3],start =
list(theta1=1, theta2=1,theta3=1),pmle=pmle)
#model 4
mod4 <- fitsde(data=data,drift=fx[4],diffusion=gx[4],start =
list(theta1=1, theta2=1,theta3=1),pmle=pmle)
#model 5
mod5 <- fitsde(data=data,drift=fx[5],diffusion=gx[5],start =
list(theta1=1, theta2=1,theta3=1, theta4=1),pmle=pmle)
#Computes AIC
AIC <- c(AIC(mod1),AIC(mod2),AIC(mod3),AIC(mod4),AIC(mod5))
Test <- data.frame(AIC,row.names = c("Model 1","Model 2","Model 3", "Model 4","Model 5"))
Test
# Bestmod <- rownames(Test)[which.min(Test[,1])]
Bestmod <- which.min(Test[,1])
list('best.model'=Bestmod,'AIC.results'=Test)
}
Diff.mle <-function(fx,gx,data)
{
pmle <- eval(formals(fitsde.default)$pmle)
fitres <- lapply(1:4, function(i) fitsde(data=data,drift=fx,diffusion=gx,pmle=pmle[i],
start = list(theta1=1,theta2=1,theta3=1,theta4=1)))
Coef <- data.frame(do.call("cbind",lapply(1:4,function(i) coef(fitres[[i]]))))
Info <- data.frame(do.call("rbind",lapply(1:4,function(i) AIC(fitres[[i]]))),
row.names=pmle)
names(Coef) <- c(pmle)
names(Info) <- c("AIC")
list("Info"=Info,"Coef"=Coef)
}
fit1=Best.fit(data =stock1,pmle = pmle[1])
print(paste("Best model = model ",fit1$best.model))
## [1] "Best model = model 2"
print("The parameter estimates are:")
## [1] "The parameter estimates are:"
ls1=Diff.mle(fx=fx[fit1$best.model],gx=gx[fit1$best.model],data = stock1)
print(ls1$Coef)
## euler kessler ozaki shoji
## theta1 3.504814e-05 1.0000448 0.0547352626 -5.415661e-04
## theta2 2.214008e-05 0.6862942 -0.0001325927 2.478136e-05
## theta3 7.536530e-03 0.8824043 0.0040184172 9.165665e-03
## theta4 3.903047e-01 0.4091350 0.5276808366 3.500519e-01
print(ls1$Info)
## AIC
## euler -252820.0
## kessler 8.0
## ozaki -231969.0
## shoji -251262.5
print(paste(rownames(ls1$Info)[which.min(ls1$Info[,1])]," method gives the best estimate"))
## [1] "euler method gives the best estimate"
fit2=Best.fit(data =stock2,pmle = pmle[1])
print(paste("Best model = model ",fit2$best.model))
## [1] "Best model = model 4"
print("The parameter estimates are:")
## [1] "The parameter estimates are:"
ls2=Diff.mle(fx=fx[fit2$best.model],gx=gx[fit2$best.model],data = stock2)
print(ls2$Coef)
## euler kessler ozaki shoji
## theta1 0.007636416 0.004397859 1 1
## theta2 0.006855019 0.007841422 1 1
## theta3 0.537076888 0.513984058 1 1
## theta4 1.000000000 1.000000000 1 1
print(ls2$Info)
## AIC
## euler -125662.2
## kessler -125065.5
## ozaki 8.0
## shoji 8.0
print(paste(rownames(ls2$Info)[which.min(ls2$Info[,1])]," method gives the best estimate"))
## [1] "euler method gives the best estimate"
fit3=Best.fit(data =stock3,pmle = pmle[1])
print(paste("Best model = model ",fit3$best.model))
## [1] "Best model = model 1"
print("The parameter estimates are:")
## [1] "The parameter estimates are:"
ls3=Diff.mle(fx=fx[fit3$best.model],gx=gx[fit3$best.model],data = stock3)
print(ls3$Coef)
## euler kessler ozaki shoji
## theta1 0.003498813 0.5498652 7.747055e-06 1.803336e-05
## theta2 -3.583787343 0.9359193 -2.858406e-03 5.676480e-03
## theta3 3.734064202 0.6267437 8.356426e-01 7.071774e-01
## theta4 1.000000000 1.0000000 1.000000e+00 1.000000e+00
print(ls3$Info)
## AIC
## euler 8.00
## kessler 8.00
## ozaki 39763.07
## shoji 44836.87
print(paste(rownames(ls3$Info)[which.min(ls3$Info[,1])]," method gives the best estimate"))
## [1] "euler method gives the best estimate"
fit4=Best.fit(data =stock4,pmle = pmle[1])
print(paste("Best model = model ",fit4$best.model))
## [1] "Best model = model 2"
print("The parameter estimates are:")
## [1] "The parameter estimates are:"
ls4=Diff.mle(fx=fx[fit4$best.model],gx=gx[fit4$best.model],data = stock4)
print(ls4$Coef)
## euler kessler ozaki shoji
## theta1 -9.421015e-05 1.0072354 6.279268e-03 3.042835e-05
## theta2 9.618334e-06 0.4606634 -5.520604e-05 9.567708e-06
## theta3 1.471477e-02 0.6495840 2.751369e-02 1.455193e-02
## theta4 4.269734e-01 0.1907163 3.015314e-01 4.293345e-01
print(ls4$Info)
## AIC
## euler -128994.3
## kessler 8.0
## ozaki -127672.7
## shoji -129012.0
print(paste(rownames(ls4$Info)[which.min(ls4$Info[,1])]," method gives the best estimate"))
## [1] "shoji method gives the best estimate"
fit5=Best.fit(data =stock5,pmle = pmle[1])
print(paste("Best model = model ",fit5$best.model))
## [1] "Best model = model 4"
print("The parameter estimates are:")
## [1] "The parameter estimates are:"
ls5=Diff.mle(fx=fx[fit5$best.model],gx=gx[fit5$best.model],data = stock5)
print(ls5$Coef)
## euler kessler ozaki shoji
## theta1 0.003342042 0.01128962 1 1
## theta2 0.009936535 0.01075011 1 1
## theta3 0.530964394 0.51575952 1 1
## theta4 1.000000000 1.00000000 1 1
print(ls5$Info)
## AIC
## euler -51791.13
## kessler -51313.00
## ozaki 8.00
## shoji 8.00
print(paste(rownames(ls5$Info)[which.min(ls5$Info[,1])]," method gives the best estimate"))
## [1] "euler method gives the best estimate"