Load Package and and Upload Data

library(ACDm)
data1 <- read.csv(url("https://www.dropbox.com/s/6qtvawchgkeapss/ALI%20dataset.csv?dl=1"), header = TRUE)

Data Cleaning and Data Preparation

# change time as factor
data2 <- data.frame(time = as.factor(data1$time), price = data1$price, volume = data1$volume) 

# Compute Durations
durData <- computeDurations(data2, open = "09:29:59", close="15:30:00")
## The 17116 transactions resulted in 6655 durations
# Apply Diurnal Adjustment using Friedman's SuperSmoother to remove deterministic part
adjDurData <- diurnalAdj(durData, method = "supsmu", aggregation = "all")

str(durData)
## 'data.frame':    6655 obs. of  5 variables:
##  $ time     : POSIXct, format: "2018-01-24 09:30:00" "2018-01-24 09:30:32" ...
##  $ price    : num  45.6 45.5 45.5 45.6 45.5 ...
##  $ volume   : int  18200 200 400 100 5500 200 300 200 2000 100 ...
##  $ Ntrans   : int  49 2 3 1 20 2 3 2 3 1 ...
##  $ durations: num  1 32 69 1 2 1 8 3 2 59 ...

ACD Model

ACD (1,1)

model01 <- acdFit(durations = adjDurData, model = "ACD", dist = "weibull", order = c(1,1), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "weibull",      order = c(1, 1), dailyRestart = 1) 
## 
## Model:
##   ACD(1, 1)
## 
## Distribution:
##   weibull
## 
## N: 6655
## 
## Parameter estimate:
##         Coef      SE PV
## omega  0.353 0.06739  0
## alpha1 0.124 0.01789  0
## beta1  0.539 0.07329  0
## gamma  0.761 0.00584  0
## 
## Note: The p-value for the distribution parameter gamma is from the 2-tailed test H0: gamma = 1.
## 
## The fixed/unfree mean distribution parameter: 
##  theta: 1.131797
## 
## Goodness of fit:
##                     value
## LogLikelihood -6495.11407
## AIC           12998.22814
## BIC           13025.44064
## MSE              47.71501
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 223 
## 
## Estimation time: 0.3428 secs 
## 
## Description: Estimated at 2022-05-19 19:43:49 by user Raffy Cee
model02 <- acdFit(durations = adjDurData, model = "ACD", dist = "gengamma", order = c(1,1), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "gengamma",      order = c(1, 1), dailyRestart = 1) 
## 
## Model:
##   ACD(1, 1)
## 
## Distribution:
##   gengamma
## 
## N: 6655
## 
## Parameter estimate:
##           Coef      SE PV
## omega   0.8262 0.02134  0
## alpha1  0.1797 0.01925  0
## beta1  -0.0158 0.00187  0
## kappa  24.4987 2.43931  0
## gamma   0.1657 0.00832  0
## 
## Note: For the distribution parameters the null hypothesis is such that the parameter = 1 (2-sided). If the null is true, the generelized gamma distribution reduces to the exponential distribution
## 
## The fixed/unfree mean distribution parameter: 
##  lambda: 2.324012e-09
## 
## Goodness of fit:
##                     value
## LogLikelihood -5748.32236
## AIC           11506.64473
## BIC           11540.66035
## MSE              48.21413
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 954 
## 
## Estimation time: 1.3812 secs 
## 
## Description: Estimated at 2022-05-19 19:43:50 by user Raffy Cee

ACD (0,1)

model03 <- acdFit(durations = adjDurData, model = "ACD", dist = "weibull", order = c(0,1), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "weibull",      order = c(0, 1), dailyRestart = 1) 
## 
## Model:
##   ACD(0, 1)
## 
## Distribution:
##   weibull
## 
## N: 6655
## 
## Parameter estimate:
##         Coef     SE PV
## omega 0.0399 0.0105  0
## beta1 0.9604 0.0104  0
## gamma 0.7606 0.0059  0
## 
## Note: The p-value for the distribution parameter gamma is from the 2-tailed test H0: gamma = 1.
## 
## The fixed/unfree mean distribution parameter: 
##  theta: 1.132064
## 
## Goodness of fit:
##                     value
## LogLikelihood -6532.44376
## AIC           13070.88751
## BIC           13091.29689
## MSE              46.71144
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 104 
## 
## Estimation time: 0.1709 secs 
## 
## Description: Estimated at 2022-05-19 19:43:51 by user Raffy Cee
model04 <- acdFit(durations = adjDurData, model = "ACD", dist = "gengamma", order = c(0,1), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "gengamma",      order = c(0, 1), dailyRestart = 1) 
## 
## Model:
##   ACD(0, 1)
## 
## Distribution:
##   gengamma
## 
## N: 6655
## 
## Parameter estimate:
##           Coef       SE PV
## omega  0.00384 0.000122  0
## beta1  0.99580 0.000147  0
## kappa 29.87228 4.387297  0
## gamma  0.15053 0.011069  0
## 
## Note: For the distribution parameters the null hypothesis is such that the parameter = 1 (2-sided). If the null is true, the generelized gamma distribution reduces to the exponential distribution
## 
## The fixed/unfree mean distribution parameter: 
##  lambda: 8.789124e-11
## 
## Goodness of fit:
##                     value
## LogLikelihood -5781.53038
## AIC           11571.06076
## BIC           11598.27325
## MSE              46.73396
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 415 
## 
## Estimation time: 0.5587 secs 
## 
## Description: Estimated at 2022-05-19 19:43:51 by user Raffy Cee

ACD (1,0)

model05 <- acdFit(durations = adjDurData, model = "ACD", dist = "weibull", order = c(1,0), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "weibull",      order = c(1, 0), dailyRestart = 1) 
## 
## Model:
##   ACD(1, 0)
## 
## Distribution:
##   weibull
## 
## N: 6655
## 
## Parameter estimate:
##         Coef      SE PV
## omega  0.902 0.02098  0
## alpha1 0.125 0.01917  0
## gamma  0.760 0.00586  0
## 
## Note: The p-value for the distribution parameter gamma is from the 2-tailed test H0: gamma = 1.
## 
## The fixed/unfree mean distribution parameter: 
##  theta: 1.132183
## 
## Goodness of fit:
##                     value
## LogLikelihood -6504.98368
## AIC           13015.96737
## BIC           13036.37674
## MSE              47.43051
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 102 
## 
## Estimation time: 0.1629 secs 
## 
## Description: Estimated at 2022-05-19 19:43:52 by user Raffy Cee
model06 <- acdFit(durations = adjDurData, model = "ACD", dist = "gengamma", order = c(1,0), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "gengamma",      order = c(1, 0), dailyRestart = 1) 
## 
## Model:
##   ACD(1, 0)
## 
## Distribution:
##   gengamma
## 
## N: 6655
## 
## Parameter estimate:
##             Coef       SE PV
## omega     0.8374 0.021922  0
## alpha1    0.1693 0.019985  0
## kappa  2881.0073 5.109708  0
## gamma     0.0154 0.000133  0
## 
## Note: For the distribution parameters the null hypothesis is such that the parameter = 1 (2-sided). If the null is true, the generelized gamma distribution reduces to the exponential distribution
## 
## The fixed/unfree mean distribution parameter: 
##  lambda: 1.040237e-225
## 
## Goodness of fit:
##                     value
## LogLikelihood -5721.04124
## AIC           11450.08249
## BIC           11477.29498
## MSE              48.04151
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 2063 
## 
## Estimation time: 2.5801 secs 
## 
## Description: Estimated at 2022-05-19 19:43:52 by user Raffy Cee

ACD (0,0)

model07 <- acdFit(durations = adjDurData, model = "ACD", dist = "weibull", order = c(0,0), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "weibull",      order = c(0, 0), dailyRestart = 1) 
## 
## Model:
##   ACD(0, 0)
## 
## Distribution:
##   weibull
## 
## N: 6655
## 
## Parameter estimate:
##       Coef      SE PV
## omega 1.02 0.01638  0
## gamma 0.76 0.00591  0
## 
## Note: The p-value for the distribution parameter gamma is from the 2-tailed test H0: gamma = 1.
## 
## The fixed/unfree mean distribution parameter: 
##  theta: 1.132188
## 
## Goodness of fit:
##                     value
## LogLikelihood -6534.19540
## AIC           13072.39079
## BIC           13085.99704
## MSE              46.71116
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 61 
## 
## Estimation time: 0.091 secs 
## 
## Description: Estimated at 2022-05-19 19:43:55 by user Raffy Cee
model08 <- acdFit(durations = adjDurData, model = "ACD", dist = "gengamma", order = c(0,0), dailyRestart = 1)
## 
## 
## Error: Oops, seems like the the optimization function failed. Changing the 'optimFnc' or/and its settings, or starting from a diffrent 'startPara' might work. You can also trace the MLE search path by adding the argument 'control = list(trace = 1)'.

ACD (2,2)

model09 <- acdFit(durations = adjDurData, model = "ACD", dist = "weibull", order = c(2,2), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "weibull",      order = c(2, 2), dailyRestart = 1) 
## 
## Model:
##   ACD(2, 2)
## 
## Distribution:
##   weibull
## 
## N: 6655
## 
## Parameter estimate:
##           Coef      SE    PV
## omega   0.6700 0.10201 0.000
## alpha1  0.1176 0.02002 0.000
## alpha2  0.1020 0.04192 0.015
## beta1   0.1962 0.17196 0.254
## beta2  -0.0485 0.04662 0.298
## gamma   0.7654 0.00593 0.000
## 
## Note: The p-value for the distribution parameter gamma is from the 2-tailed test H0: gamma = 1.
## 
## The fixed/unfree mean distribution parameter: 
##  theta: 1.128618
## 
## Goodness of fit:
##                     value
## LogLikelihood -6485.10184
## AIC           12982.20368
## BIC           13023.02242
## MSE              48.08956
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 751 
## 
## Estimation time: 1.0469 secs 
## 
## Description: Estimated at 2022-05-19 19:43:56 by user Raffy Cee
model10 <- acdFit(durations = adjDurData, model = "ACD", dist = "gengamma", order = c(2,2), dailyRestart = 1)
## 
## 
## Error: Oops, seems like the the optimization function failed. Changing the 'optimFnc' or/and its settings, or starting from a diffrent 'startPara' might work. You can also trace the MLE search path by adding the argument 'control = list(trace = 1)'.

ACD (2,1)

model11 <- acdFit(durations = adjDurData, model = "ACD", dist = "weibull", order = c(2,1), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "weibull",      order = c(2, 1), dailyRestart = 1) 
## 
## Model:
##   ACD(2, 1)
## 
## Distribution:
##   weibull
## 
## N: 6655
## 
## Parameter estimate:
##           Coef      SE PV
## omega   0.7514 0.02348  0
## alpha1  0.1418 0.02154  0
## alpha2  0.1915 0.02097  0
## beta1  -0.0139 0.00127  0
## gamma   0.7614 0.00593  0
## 
## Note: The p-value for the distribution parameter gamma is from the 2-tailed test H0: gamma = 1.
## 
## The fixed/unfree mean distribution parameter: 
##  theta: 1.131471
## 
## Goodness of fit:
##                     value
## LogLikelihood -6481.43185
## AIC           12972.86371
## BIC           13006.87933
## MSE              49.29598
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 712 
## 
## Estimation time: 0.9505 secs 
## 
## Description: Estimated at 2022-05-19 19:43:59 by user Raffy Cee
model12 <- acdFit(durations = adjDurData, model = "ACD", dist = "gengamma", order = c(2,1), dailyRestart = 1)
## 
## 
## Error: Oops, seems like the the optimization function failed. Changing the 'optimFnc' or/and its settings, or starting from a diffrent 'startPara' might work. You can also trace the MLE search path by adding the argument 'control = list(trace = 1)'.

ACD (2,0)

model13 <- acdFit(durations = adjDurData, model = "ACD", dist = "weibull", order = c(2,0), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "weibull",      order = c(2, 0), dailyRestart = 1) 
## 
## Model:
##   ACD(2, 0)
## 
## Distribution:
##   weibull
## 
## N: 6655
## 
## Parameter estimate:
##         Coef     SE PV
## omega  0.787 0.0245  0
## alpha1 0.108 0.0190  0
## alpha2 0.145 0.0217  0
## gamma  0.764 0.0059  0
## 
## Note: The p-value for the distribution parameter gamma is from the 2-tailed test H0: gamma = 1.
## 
## The fixed/unfree mean distribution parameter: 
##  theta: 1.129433
## 
## Goodness of fit:
##                     value
## LogLikelihood -6480.49782
## AIC           12968.99564
## BIC           12996.20814
## MSE              48.20903
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 217 
## 
## Estimation time: 0.3318 secs 
## 
## Description: Estimated at 2022-05-19 19:44:00 by user Raffy Cee
model14 <- acdFit(durations = adjDurData, model = "ACD", dist = "gengamma", order = c(2,0), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "gengamma",      order = c(2, 0), dailyRestart = 1) 
## 
## Model:
##   ACD(2, 0)
## 
## Distribution:
##   gengamma
## 
## N: 6655
## 
## Parameter estimate:
##         Coef     SE PV
## omega  0.686 0.0219  0
## alpha1 0.125 0.0183  0
## alpha2 0.202 0.0233  0
## kappa  9.277 0.7786  0
## gamma  0.269 0.0115  0
## 
## Note: For the distribution parameters the null hypothesis is such that the parameter = 1 (2-sided). If the null is true, the generelized gamma distribution reduces to the exponential distribution
## 
## The fixed/unfree mean distribution parameter: 
##  lambda: 0.0001557101
## 
## Goodness of fit:
##                     value
## LogLikelihood -5802.12287
## AIC           11614.24575
## BIC           11648.26137
## MSE              49.32068
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 1042 
## 
## Estimation time: 1.329 secs 
## 
## Description: Estimated at 2022-05-19 19:44:01 by user Raffy Cee

ACD (1,2)

model13 <- acdFit(durations = adjDurData, model = "ACD", dist = "weibull", order = c(1,2), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "weibull",      order = c(1, 2), dailyRestart = 1) 
## 
## Model:
##   ACD(1, 2)
## 
## Distribution:
##   weibull
## 
## N: 6655
## 
## Parameter estimate:
##           Coef      SE PV
## omega   0.2319 0.01623  0
## alpha1  0.0916 0.00870  0
## beta1   1.0233 0.01751  0
## beta2  -0.3347 0.00651  0
## gamma   0.7620 0.00584  0
## 
## Note: The p-value for the distribution parameter gamma is from the 2-tailed test H0: gamma = 1.
## 
## The fixed/unfree mean distribution parameter: 
##  theta: 1.131095
## 
## Goodness of fit:
##                     value
## LogLikelihood -6485.69760
## AIC           12981.39520
## BIC           13015.41082
## MSE              47.78744
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 870 
## 
## Estimation time: 1.1409 secs 
## 
## Description: Estimated at 2022-05-19 19:44:02 by user Raffy Cee
model14 <- acdFit(durations = adjDurData, model = "ACD", dist = "gengamma", order = c(1,2), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "gengamma",      order = c(1, 2), dailyRestart = 1) 
## 
## Model:
##   ACD(1, 2)
## 
## Distribution:
##   gengamma
## 
## N: 6655
## 
## Parameter estimate:
##           Coef      SE PV
## omega   0.2657 0.03356  0
## alpha1  0.1267 0.01640  0
## beta1  -0.0141 0.00268  0
## beta2   0.6153 0.04146  0
## kappa   2.5441 0.09523  0
## gamma   0.5032 0.00967  0
## 
## Note: For the distribution parameters the null hypothesis is such that the parameter = 1 (2-sided). If the null is true, the generelized gamma distribution reduces to the exponential distribution
## 
## The fixed/unfree mean distribution parameter: 
##  lambda: 0.1128987
## 
## Goodness of fit:
##                     value
## LogLikelihood -6016.32385
## AIC           12044.64770
## BIC           12085.46644
## MSE              47.96015
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 599 
## 
## Estimation time: 0.8483 secs 
## 
## Description: Estimated at 2022-05-19 19:44:03 by user Raffy Cee

ACD (0,2)

model13 <- acdFit(durations = adjDurData, model = "ACD", dist = "weibull", order = c(0,2), dailyRestart = 1)
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "weibull",      order = c(0, 2), dailyRestart = 1) 
## 
## Model:
##   ACD(0, 2)
## 
## Distribution:
##   weibull
## 
## N: 6655
## 
## Parameter estimate:
##         Coef     SE    PV
## omega 0.0644 0.0278 0.020
## beta1 0.5050 2.1353 0.813
## beta2 0.4311 2.1263 0.839
## gamma 0.7606 0.0059 0.000
## 
## Note: The p-value for the distribution parameter gamma is from the 2-tailed test H0: gamma = 1.
## 
## The fixed/unfree mean distribution parameter: 
##  theta: 1.132091
## 
## Goodness of fit:
##                     value
## LogLikelihood -6532.38602
## AIC           13072.77203
## BIC           13099.98453
## MSE              46.71135
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 145 
## 
## Estimation time: 0.2618 secs 
## 
## Description: Estimated at 2022-05-19 19:44:04 by user Raffy Cee
model14 <- acdFit(durations = adjDurData, model = "ACD", dist = "gengamma", order = c(0,2), dailyRestart = 1)
## Warning in sqrt(diag(solve(hessian))): NaNs produced
## 
## ACD model estimation by Maximum Likelihood 
## 
## Call:
##   acdFit(durations = adjDurData, model = "ACD", dist = "gengamma",      order = c(0, 2), dailyRestart = 1) 
## 
## Model:
##   ACD(0, 2)
## 
## Distribution:
##   gengamma
## 
## N: 6655
## 
## Parameter estimate:
##         Coef     SE  PV
## omega  0.362    NaN NaN
## beta1 -0.190    NaN NaN
## beta2  0.811    NaN NaN
## kappa 31.897 4.9307   0
## gamma  0.145 0.0113   0
## 
## Note: For the distribution parameters the null hypothesis is such that the parameter = 1 (2-sided). If the null is true, the generelized gamma distribution reduces to the exponential distribution
## 
## The fixed/unfree mean distribution parameter: 
##  lambda: 2.529077e-11
## 
## Goodness of fit:
##                     value
## LogLikelihood -5784.40206
## AIC           11578.80411
## BIC           11612.81973
## MSE              46.72628
## 
## Convergence: 0 
## 
## Number of log-likelihood function evaluations: 1564 
## 
## Estimation time: 1.9572 secs 
## 
## Description: Estimated at 2022-05-19 19:44:04 by user Raffy Cee

Plots

Time Index and Duration

plotData <- data.frame(time = c(1:length(adjDurData$durations)),dur = adjDurData$durations)
plot(plotData$time, plotData$dur, type="l", xlab="time index", ylab="duration", main="Time Index vs Duration")

Time Index and Standardized Residuals

plot(plotData$time, scale(model06$residuals) , type="l", xlab="time index", ylab="Standardized Residual", main="Time Index vs Standardized Residuals")

ACF Plots

acf_acd(model06)

Hazard Function Plot

plotHazard(model06)