Title: | Tools for Autoregressive Conditional Duration Models |
---|---|
Description: | Package for Autoregressive Conditional Duration (ACD, Engle and Russell, 1998) models. Creates trade, price or volume durations from transactions (tic) data, performs diurnal adjustments, fits various ACD models and tests them. |
Authors: | Markus Belfrage |
Maintainer: | Markus Belfrage <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.0.4.3 |
Built: | 2024-10-18 06:21:21 UTC |
Source: | CRAN |
Package for Autoregressive Conditional Duration (ACD, Engle and Russell, 1998) models. Creates trade, price or volume durations from transactions (tic) data, performs diurnal adjustments, fits various ACD models and tests them.
The author would like to thank the department of statistics at Hanken School of Economics, as the bulk of this work was done there while working as a research assistant.
Markus Belfrage
Maintainer: Markus Belfrage <[email protected]>
Engle R.F, Russell J.R. (1998) Autoregressive Conditional Duration: A New Model for Irregularly Spaced Transaction Data, Econometrica, 66(5): 1127-1162.
This function estimates various ACD models with various assumed error term distributions, using Maximum Likelihood Estimation.
The currently available models (conditional mean specifications) are:
Standard ACD, Log-ACD (two alternative specifications), AMACD, ABACD, SNIACD and LSNIACD.
The currently available distributions are:
Exponential (also used for QML), Weibull, Burr, generalized Gamma, and generalized F.
acdFit(durations = NULL, model = "ACD", dist = "exponential", order = NULL, startPara = NULL, dailyRestart = 0, optimFnc = "optim", method = "Nelder-Mead", output = TRUE, bootstrapErrors = FALSE, forceErrExpec = TRUE, fixedParamPos = NULL, bp = NULL, exogenousVariables = NULL, control = list())
acdFit(durations = NULL, model = "ACD", dist = "exponential", order = NULL, startPara = NULL, dailyRestart = 0, optimFnc = "optim", method = "Nelder-Mead", output = TRUE, bootstrapErrors = FALSE, forceErrExpec = TRUE, fixedParamPos = NULL, bp = NULL, exogenousVariables = NULL, control = list())
durations |
either (1) a data frame including, at least, a column named 'durations' or 'adjDur' (for adjusted durations), or (2) a vector of durations |
model |
the conditional mean model specification. Must be one of |
dist |
the assumed error term distribution. Must be one of |
order |
a vector detailing the order of the particular ACD model. For example an ACD(p, q) specification should have |
startPara |
a vector with parameter values to start the maximization algorithm from. Must be in the correct order according to the model specification (see Details). |
dailyRestart |
if |
optimFnc |
Specifies which optimization function to use for the estimation. |
method |
Argument passed to the optimization function if |
output |
if |
bootstrapErrors |
if |
forceErrExpec |
if |
fixedParamPos |
a logical vector of |
bp |
used only for the SNIACD or LSNIACD model. A vector of break points. |
exogenousVariables |
specifies the columns in the |
control |
a list of control values,
|
The startPara
argument is a vector of the parameter values to start from. The length of the vector naturally depends on the model
and distribution
. The first elements represent the model parameters, and the last elements the distribution parameters. For example for an ACD(1,1) with Weibull errors the first 3 elements are for the model, and the last is
for the Weibull distribution.
The family of ACD models are
where different specifications of the conditional mean duration and the error term
give rise to different models as shown below.
When exogenous variables are used, they are added in the form of
to the right hand side of the equations, where are the exogenous variables.
Conditional mean duration specifications according to the
model
argument:
ACD(p, q) specification: (Engle and Russell, 1998)
The element order of the startPara
vector is .
LACD1(p, q): (Bauwens and Giot, 2000)
The element order of the startPara
vector is .
LACD2(p, q): (Lunde, 1999)
The element order of the startPara
vector is .
AMACD(p, r, q) (Additive and Multiplicative ACD): (Hautsch , 2012)
The element order of the startPara
vector is .
ABACD(p, q) (Augmented Box-Cox ACD): (Hautsch, 2012)
The element order of the startPara
vector is .
BACD(p, q) (Box-Cox ACD): (Hautsch, 2003)
The element order of the startPara
vector is .
SNIACD(p, q, M) (Spline News Impact ACD): (Hautsch, 2012, with a slight difference)
where is an indicator function and
.
The element order of the startPara
vector is (The number of
-parameters are
).
The distribution of the error term specifications according to the
dist
argument:
Exponential distribution, dist = "exponential"
:
Weibull distribution, dist = "weibull"
:
where if
forceErrExpec = TRUE
.
Burr distribution, dist = "burr"
:
where,
if forceErrExpec = TRUE
.
The element order of the startPara
vector is .
Generalized Gamma distribution, dist = "gengamma"
:
where if
forceErrExpec = TRUE
.
The element order of the startPara
vector is .
Generalized F distribution, dist = "genf"
:
where , and if
forceErrExpec = TRUE
,
The element order of the startPara
vector is .
q-Weibull distribution, dist = "qweibull"
:
where if forceErrExpec = TRUE
,
The element order of the startPara
vector is .
a list of class "acdFit"
with the following slots:
durations |
the durations object used to fit the model. |
muHats |
a vector of the estimated conditional mean durations |
residuals |
the residuals from the fitted model, calculated as durations/mu |
model |
the model for the conditional mean durations |
order |
the order of the model |
distribution |
the assumed error term distribution |
distCode |
the internal code used to represent the distribution |
mPara |
a vector of the estimated conditional mean duration parameters |
dPara |
a vector of the estimated error distribution parameters |
Npar |
total number of parameters |
goodnessOfFit |
a data.frame with the log likelihood, AIC, BIC, and MSE calculated as the mean squared deviation of the durations and the estimated conditional durations. |
parameterInference |
a data.frame with the estimated coefficients and their standard errors and p-values |
forcedDistPara |
the value of the unfree distribution parameter. If |
comments |
|
hessian |
the numerical hessian of the log likelihood evaluated at the estimate |
N |
number of observations |
evals |
number of log-likelihood evaluations needed for the maximization algorithm |
convergence |
if the maximization algorithm converged, this value is zero. (see the help file optim, nlminb or solnp) |
estimationTime |
time required for estimation |
description |
who fitted the model and when |
robustCorr |
only available for QML estimation (choosing the exponential distribution) for the standard ACD(p, q) model. The robust correlation matrix of the parameter estimates. |
Markus Belfrage
Bauwens, L., and P. Giot (2000) The logarithmic ACD model: an application to the bid-ask quote process of three NYSE stocks. Annales d'Economie et de Statistique, 60, 117-149.
Engle R.F, Russell J.R. (1998) Autoregressive Conditional Duration: A New Model for Irregularly Spaced Transaction Data, Econometrica, 66(5): 1127-1162.
Grammig, J., and Maurer, K.-O. (2000) Non-monotonic hazard functions and the autoregressive conditional duration model. Econometrics Journal 3: 16-38.
Hautsch, N. (2003) Assessing the Risk of Liquidity Suppliers on the Basis of Excess Demand Intensities. Journal of Financial Econometrics (2003) 1 (2): 189-215
Hautsch, N. (2012) Econometrics of Financial High-Frequency Data. Berlin, Heidelberg: Springer.
Lunde, A. (1999): A generalized gamma autoregressive conditional duration model, Working paper, Aalborg University.
fitModel <- acdFit(durations = adjDurData, model = "ACD", dist = "exponential", order = c(1,1), dailyRestart = 1)
fitModel <- acdFit(durations = adjDurData, model = "ACD", dist = "exponential", order = c(1,1), dailyRestart = 1)
residuals.acdFit()
returns the residuals and coef.acdFit()
returns the coefficients of a fitted ACD model of class 'acdFit', while print.acdFit()
prints the essential information. predict.acdFit()
predicts the next N
durations by thier expected value.
## S3 method for class 'acdFit' residuals(object, ...) ## S3 method for class 'acdFit' coef(object, returnCoef = "all", ...) ## S3 method for class 'acdFit' print(x, ...) ## S3 method for class 'acdFit' predict(object, N = 10, ...)
## S3 method for class 'acdFit' residuals(object, ...) ## S3 method for class 'acdFit' coef(object, returnCoef = "all", ...) ## S3 method for class 'acdFit' print(x, ...) ## S3 method for class 'acdFit' predict(object, N = 10, ...)
object |
the fitted ACD model of class 'acdFit' (as returned by the function |
x |
same as |
returnCoef |
on of "all", "distribution", or "model". Specifies whether all estimated parameters should be returned or only the distribution parameters or the model (for the conditional mean duration) parameters. |
N |
the number of the predictions in |
... |
additional arguments to |
plots the ACF (Auto Correlation Function) for the durations, diurnally adjusted durations, and residuals.
acf_acd(fitModel = NULL, conf_level = 0.95, max = 50, min = 1)
acf_acd(fitModel = NULL, conf_level = 0.95, max = 50, min = 1)
fitModel |
a fitted model of class "acdFit", or a data.frame containing at least one the columns "durations", "adjDur", or "residuals". Can also be a vector of durations or residuals. |
conf_level |
the confidence level of the confidence bands |
max |
the largest lag to plot |
min |
the smallest lag to plot |
returns a data.frame with the values of the sample autocorrelations for each lag and variable.
Markus Belfrage
fitModel <- acdFit(adjDurData) acf_acd(fitModel, conf_level = 0.95, max = 50, min = 1) f <- acf_acd(durData) f
fitModel <- acdFit(adjDurData) acf_acd(fitModel, conf_level = 0.95, max = 50, min = 1) f <- acf_acd(durData) f
Density, distribution function, quantile function, random generation and calculation of the expected value for the Burr distribution with parameters theta, kappa and sig2.
dburr(x, theta = 1, kappa = 1.2, sig2 = 0.3, forceExpectation = F) pburr(x, theta = 1, kappa = 1.2, sig2 = .3, forceExpectation = F) qburr(p, theta = 1, kappa = 1.2, sig2 = .3, forceExpectation = F) rburr(n = 1, theta = 1, kappa = 1.2, sig2 = .3, forceExpectation = F) burrExpectation(theta = 1, kappa = 1.2, sig2 = .3)
dburr(x, theta = 1, kappa = 1.2, sig2 = 0.3, forceExpectation = F) pburr(x, theta = 1, kappa = 1.2, sig2 = .3, forceExpectation = F) qburr(p, theta = 1, kappa = 1.2, sig2 = .3, forceExpectation = F) rburr(n = 1, theta = 1, kappa = 1.2, sig2 = .3, forceExpectation = F) burrExpectation(theta = 1, kappa = 1.2, sig2 = .3)
x |
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations.. |
theta , kappa , sig2
|
parameters, see 'Details'. |
forceExpectation |
logical; if |
The PDF for the Burr distribution is (as in e.g. Grammig and Maurer, 2000):
dburr
gives the density (PDF), qburr
the quantile function (inverted CDF), rburr
generates random deviates, and burrExpectation
returns the expected value of the distribution, given the parameters.
Markus Belfrage
Grammig, J., and Maurer, K.-O. (2000) Non-monotonic hazard functions and the autoregressive conditional duration model. Econometrics Journal 3: 16-38.
Computes durations from a data.frame containing the time stamps of transactions. Trade durations, price durations and volume durations can be computed (if the appropriate data columns are given).
computeDurations(transactions, open = "10:00:00", close = "18:25:00", rm0dur = TRUE, type = "trade", priceDiff = .1, cumVol = 10000)
computeDurations(transactions, open = "10:00:00", close = "18:25:00", rm0dur = TRUE, type = "trade", priceDiff = .1, cumVol = 10000)
transactions |
a data.frame with, at least, transaction time in a column named 'time' (see Details) |
open |
the opening time of the exchange. Transactions done outside the trading hours will be ignored. |
close |
the closing time of the exchange. |
rm0dur |
if |
type |
the type of durations to be computed. Either "trade", "price", or "volume". |
priceDiff |
only if |
cumVol |
only if |
The data.frame must include a column named 'time' with the time of each transaction, in a time format recognizable by POSIXlt or strings in format "yyyy-mm-dd hh:mm:ss". If the column 'price' or 'volume' is included its also possible to compute price- and volume durations (see arguments priceDiff
and cumVol
)
a data.frame with columns:
time |
the calander time of the start of each duration spell. |
price |
the volume weighted avrage price of the shares traded during the spell of the duration. |
volume |
the volume (total shares traded) during the duration spell. |
Ntrans |
number of transactions done during the spell. |
durations |
the computed duration. |
Markus Belfrage
## Not run: #only the first 3 days of data: durDataShort <- computeDurations(transData[1:56700, ]) str(durDataShort) head(durDataShort) ## End(Not run)
## Not run: #only the first 3 days of data: durDataShort <- computeDurations(transData[1:56700, ]) str(durDataShort) head(durDataShort) ## End(Not run)
The data file transData
is the base data used in all of the examples. It is a data.frame with rows representing a single transaction and has the columns 'time', 'price', giving the trade price, and 'volume', giving the number of shares traded for the transaction. The data set is based on real transactions but has been obfuscated by transforming the dates, price and volume, for proprietary reasons. It covers two weeks of nearly 100 000 transactions, recorded with 1 second precision.
The durData
data.frame is simply the trade durations formed from transData
using the function durData <- computeDurations(transData)
The adjDurData
data object is in turn created by adjDurData <- diurnalAdj(durData, aggregation = "all")
to add diurnally adjusted durations.
defaultSplineObj
is an estimated cubic spline of the diurnal component using the sample data. It is used when simulating from sim_ACD()
with the argument diurnalFactor
set to TRUE
, when no user splineObj
is provided.
Density and distribution function for the generalized F distribution. Warning: the distribution function pgenf
and genfHazard
are computed numerically, and may not be precise!
dgenf(x, kappa = 5, eta = 1.5, gamma = .8, lambda = 1, forceExpectation = F) pgenf(q, kappa = 5, eta = 1.5, gamma = .8, lambda = 1, forceExpectation = F) genfHazard(x, kappa = 5, eta = 1.5, gamma = .8, lambda = 1, forceExpectation = F)
dgenf(x, kappa = 5, eta = 1.5, gamma = .8, lambda = 1, forceExpectation = F) pgenf(q, kappa = 5, eta = 1.5, gamma = .8, lambda = 1, forceExpectation = F) genfHazard(x, kappa = 5, eta = 1.5, gamma = .8, lambda = 1, forceExpectation = F)
x , q
|
vector of quantiles. |
kappa , eta , gamma , lambda
|
parameters, see 'Details'. |
forceExpectation |
logical; if |
The PDF for the generelized F distribution is:
where is the beta function.
Density (PDF), distribution function (CDF), and hazard function for a discreetly mixed distribution of the q-Weibull and the exponential distributions.
dmixqwe(x, pdist = .5, a = .8, qdist = 1.5, lambda = .8, b = 1, forceExpectation = F) pmixqwe(q, pdist = .5, a = .8, qdist = 1.5, lambda = .8, b = 1, forceExpectation = F) mixqweHazard(x, pdist = .5, a = .8, qdist = 1.5, lambda = .8, b = 1, forceExpectation = F)
dmixqwe(x, pdist = .5, a = .8, qdist = 1.5, lambda = .8, b = 1, forceExpectation = F) pmixqwe(q, pdist = .5, a = .8, qdist = 1.5, lambda = .8, b = 1, forceExpectation = F) mixqweHazard(x, pdist = .5, a = .8, qdist = 1.5, lambda = .8, b = 1, forceExpectation = F)
x , q
|
vector of quantiles. |
pdist , a , qdist , lambda , b
|
parameters, see 'Details'. |
forceExpectation |
logical; if |
The PDF for the mixed distribution is:
if forceExpectation = TRUE
the b
parameter is a function of the other parameters to force the expectation to be 1.
qWeibullDist
for the Q-Weibull distribution and pmixqww
for Q-Weibull mixed with the ordinary Weibull.
Density (PDF), distribution function (CDF), and hazard function for a discreetly mixed distribution of the q-Weibull and the ordinary Weibull distributions.
dmixqww(x, pdist = .5, a = 1.2, qdist = 1.5, theta = .8, gamma = 1, b = 1, forceExpectation = F) pmixqww(q, pdist = .5, a = 1.2, qdist = 1.5, theta = .8, gamma = 1, b = 1, forceExpectation = F) mixqwwHazard(x, pdist = .5, a = 1.2, qdist = 1.5, theta = .8, gamma = 1, b = 1, forceExpectation = F)
dmixqww(x, pdist = .5, a = 1.2, qdist = 1.5, theta = .8, gamma = 1, b = 1, forceExpectation = F) pmixqww(q, pdist = .5, a = 1.2, qdist = 1.5, theta = .8, gamma = 1, b = 1, forceExpectation = F) mixqwwHazard(x, pdist = .5, a = 1.2, qdist = 1.5, theta = .8, gamma = 1, b = 1, forceExpectation = F)
x , q
|
vector of quantiles. |
pdist , a , qdist , theta , gamma , b
|
parameters, see 'Details'. |
forceExpectation |
logical; if |
The PDF for the mixed distribution is:
if forceExpectation = TRUE
the b
parameter is a function of the other parameters to force the expectation to be 1.
qWeibullDist
for the Q-Weibull distribution and pmixqwe
for Q-Weibull mixed with the exponential distribution.
Performs a diurnal adjustment of the durations, i.e. removes a daily seasonal component. Four different methods of diurnal adjustment are available, namely "cubicSpline", "supsmu" (Friedman's SuperSmoother), "smoothSpline" (smoothed version of the cubic spline), or "FFF" (Flexible Fourier Form).
diurnalAdj(dur, method = "cubicSpline", nodes = c(seq(600, 1105, 60), 1105), aggregation = "all", span = "cv", spar = 0, Q = 4, returnSplineFnc = FALSE)
diurnalAdj(dur, method = "cubicSpline", nodes = c(seq(600, 1105, 60), 1105), aggregation = "all", span = "cv", spar = 0, Q = 4, returnSplineFnc = FALSE)
dur |
a data.frame containing the columns |
method |
the method used. One of "cubicSpline", "supsmu", "smoothSpline", or "FFF". |
nodes |
only for |
aggregation |
what type of aggregation to use. Either "weekdays", "all", or "none". If for example "weekdays" is chosen, all Mondays will have the same daily seasonal component, and so on. |
span |
argument passed to supsmu if |
spar |
argument passed to smooth.spline if |
Q |
number of trigonometric function pairs for |
returnSplineFnc |
if |
If returnSplineFnc
is FALSE
(default): the input
data.frame dur
with an added column of the diurnally adjusted
durations called 'adjDur'.
Otherwise, a list of spline objects containing the coefficents of the spline function.
Markus Belfrage
diurnalAdj(durData, aggregation = "none", method = "supsmu") ## Not run: head(durData) f <- diurnalAdj(durData, aggregation = "weekdays", method = "FFF", Q = 3) head(f) f <- diurnalAdj(durData, aggregation = "all", returnSplineFnc = TRUE) f ## End(Not run)
diurnalAdj(durData, aggregation = "none", method = "supsmu") ## Not run: head(durData) f <- diurnalAdj(durData, aggregation = "weekdays", method = "FFF", Q = 3) head(f) f <- diurnalAdj(durData, aggregation = "all", returnSplineFnc = TRUE) f ## End(Not run)
Density (PDF), distribution function (CDF), and hazard function for Finite mixture of inverse Gaussian Distributions.
dmixinvgauss(x, theta = .2, lambda = .1, gamma = .05, forceExpectation = F) pmixinvgauss(q, theta = .2, lambda = .1, gamma = .05, forceExpectation = F) mixinvgaussHazard(x, theta = .2, lambda = .1, gamma = .05, forceExpectation = F)
dmixinvgauss(x, theta = .2, lambda = .1, gamma = .05, forceExpectation = F) pmixinvgauss(q, theta = .2, lambda = .1, gamma = .05, forceExpectation = F) mixinvgaussHazard(x, theta = .2, lambda = .1, gamma = .05, forceExpectation = F)
x , q
|
vector of quantiles. |
theta , lambda , gamma
|
parameters, see 'Details'. |
forceExpectation |
logical; if |
The finite mixture of inverse Gaussian distributions was used by Gomes-Deniz and Perez-Rodrigues (201X) for ACD-models. Its PDF is:
If forceExpectation = TRUE
the distribution is transformed by dividing the random variable with its expectation and using the change of variable function.
Gomez-Deniz Perez-Rodriguez (201X) Non-exponential mixtures, non-monotonic financial hazard functions and the autoregressive conditional duration model. Working paper. Retrieved June 16, 2015, from http://dea.uib.es/digitalAssets/254/254084_perez.pdf.
Density (PDF), distribution function (CDF), quantile function (inverted CDF), random generation and hazard function for the generelized Gamma distribution with parameters gamma, kappa and lambda.
dgengamma(x, gamma = 0.3, kappa = 1.2, lambda = 0.3, forceExpectation = F) pgengamma(x, gamma = .3, kappa = 3, lambda = .3, forceExpectation = F) qgengamma(p, gamma = .3, kappa = 3, lambda = .3, forceExpectation = F) rgengamma(n = 1, gamma = .3, kappa = 3, lambda = .3, forceExpectation = F) gengammaHazard(x, gamma = .3, kappa = 3, lambda = .3, forceExpectation = F)
dgengamma(x, gamma = 0.3, kappa = 1.2, lambda = 0.3, forceExpectation = F) pgengamma(x, gamma = .3, kappa = 3, lambda = .3, forceExpectation = F) qgengamma(p, gamma = .3, kappa = 3, lambda = .3, forceExpectation = F) rgengamma(n = 1, gamma = .3, kappa = 3, lambda = .3, forceExpectation = F) gengammaHazard(x, gamma = .3, kappa = 3, lambda = .3, forceExpectation = F)
x |
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations.. |
gamma , kappa , lambda
|
parameters, see 'Details'. |
forceExpectation |
logical; if |
The PDF for the generelized Gamma distribution is:
dgengamma
gives the density (PDF), pgengamma
gives the distribution function (CDF), qgengamma
gives the quantile function (inverted CDF), rgenGamma
generates random deviates, and genGammaHazard
gives the hazard function.
Markus Belfrage
Plots (1) the price over time, (2) volume traded over time for a given interval, and (3) number of transactions over time for a given interval.
plotDescTrans(trans, windowunit = "hours", window = 1)
plotDescTrans(trans, windowunit = "hours", window = 1)
trans |
a data.frame with the column 'time', 'price', and 'volume'. Currently only works if all of those are available. |
windowunit |
the unit of the time interval. One of "secs", "mins", "hours", or "days". |
window |
a positive integer giving the length of the interval. |
## Not run: plotDescTrans(transData, windowunit = "hours", window = 1) ## End(Not run)
## Not run: plotDescTrans(transData, windowunit = "hours", window = 1) ## End(Not run)
Estimates and plots the hazard function from an estimatated ACD model.
plotHazard(fitModel, breaks = 20, implied = TRUE, xstop)
plotHazard(fitModel, breaks = 20, implied = TRUE, xstop)
fitModel |
an estimated model of class acdFit. Can also be a numerical vector. |
breaks |
the number of quantiles used to estimate the hazard. |
implied |
a logical flag. If TRUE then the implied hazard function using the distribution parameter estimates will be plotted together with the nonparametric estimate of the error term hazard function. |
xstop |
where to stop plotting the implied hazard. |
This estimator of the hazard function is based on the one used by Engle and Russell (1998). It is modified sligthly to decrease its bias and inconsistency. However, the estimator is still not fully consistent when using a fixed number of breaks (quantiles).
Markus Belfrage
Engle, R.F and Russell, J.R. (1998) Autoregressive Conditional Duration: A New Model for Irregularly Spaced Transaction Data. Econometrica, 66(5): 1127-1162.
## Not run: fitModelWei <- acdFit(adjDurData, dist = "wei") plotHazard(fitModelWei) ## End(Not run)
## Not run: fitModelWei <- acdFit(adjDurData, dist = "wei") plotHazard(fitModelWei) ## End(Not run)
Plots the mean duration over time at chosen interval length
plotHistAcd(durations, windowunit = "mins", window = 1)
plotHistAcd(durations, windowunit = "mins", window = 1)
durations |
a data.frame containing the durations and their time stamps. |
windowunit |
the unit of the time interval. One of "secs", "mins", "hours", or "days". |
window |
a positive integer giving the length of the interval. |
Markus Belfrage
plotHistAcd(durData, windowunit = "days", window = 1) ## Not run: plotHistAcd(durData, windowunit = "mins", window = 30) ## End(Not run)
plotHistAcd(durData, windowunit = "days", window = 1) ## Not run: plotHistAcd(durData, windowunit = "mins", window = 30) ## End(Not run)
Plots the log likelihood for a fitted model against either one or two of the parameters at a time. This can help to find issues with for example poor identification of a model.
plotLL(fitModel, parameter1 = 1, parameter2 = NULL, param1sequence, param2sequence, startpoint = NULL, returnOutput = FALSE)
plotLL(fitModel, parameter1 = 1, parameter2 = NULL, param1sequence, param2sequence, startpoint = NULL, returnOutput = FALSE)
fitModel |
a fitted model of class acdFit. |
parameter1 |
the first parameter for the log likelihood to be plotted against. Either the index of the parameter as an integer, or the name of the parameter. |
parameter2 |
the second parameter for the log likelihood to be plotted against. Either the index of the parameter as an integer, or the name of the parameter. If left empty, a plot with only the |
param1sequence , param2sequence
|
the sequence of points from with the log likelihood is computed. If left empty, the log likelihood will be computed at 21 points spanning between MLE-3*SD and MLE+3*SD in the one dimensional case, and the 11x11 points for the same range in the two dimensional case. |
startpoint |
a vector of size equal to the number of parameters in the model. If this is supplied, the log likelihood will be evaluated at this point instead of the point of the MLE (for the parameters not in |
returnOutput |
a logical flag. If set to |
Only if returnOutput = TRUE
1. For the one dimensional case: a data.frame with the columns 'logLikelihood', and 'param1sequence' for all the values of the parameter1
witch the log likelihood was evaluated at
2. For the two dimensional case: a list with the following items:
para1 |
a vector with the sequence of the |
para2 |
a vector with the sequence of the |
z |
a matrix with the log likelihood values. The element at the ith row and jth column is evaluated at the ith |
Markus Belfrage
## Not run: #Indicates identification issues with the generelized gamma distibution: #(Try a diffrent 'startPara' in acdFit() to get slightly a better fit) fitModel2 <- acdFit(durations = adjDurData[1:3000, ], dist = "gengamma") seq1 <- seq(500, 1000, 50) seq2 <- seq(.02, 0.045, 0.001) plotLL(fitModel = fitModel2, parameter1 = "kappa", parameter2 = "gamma", param1sequence = seq1, param2sequence = seq2) ## End(Not run)
## Not run: #Indicates identification issues with the generelized gamma distibution: #(Try a diffrent 'startPara' in acdFit() to get slightly a better fit) fitModel2 <- acdFit(durations = adjDurData[1:3000, ], dist = "gengamma") seq1 <- seq(500, 1000, 50) seq2 <- seq(.02, 0.045, 0.001) plotLL(fitModel = fitModel2, parameter1 = "kappa", parameter2 = "gamma", param1sequence = seq1, param2sequence = seq2) ## End(Not run)
Plots rolling means of durations
plotRollMeanAcd(durations, window = 500)
plotRollMeanAcd(durations, window = 500)
durations |
a data.frame containing the column 'time' and 'durations'. |
window |
the length of the rolling window. |
plotRollMeanAcd(durData, window = 500)
plotRollMeanAcd(durData, window = 500)
Function to help scatter plot different variables of a fitted ACD model and superimposes a smoothed conditional mean using ggplot2. Can be used to investigate the possible need for non-linear models and issues with the diurnal adjustment.
plotScatterAcd(fitModel, x = "muHats", y = "residuals", xlag = 0, ylag = 0, colour = NULL, xlim = NULL, ylim = NULL, alpha = 1/10, smoothMethod = "auto")
plotScatterAcd(fitModel, x = "muHats", y = "residuals", xlag = 0, ylag = 0, colour = NULL, xlim = NULL, ylim = NULL, alpha = 1/10, smoothMethod = "auto")
fitModel |
a fitted model of class "acdFit" |
x |
the variable used on the x-axis. One of "muHats", "residuals", "durations", "adjDur", "dayTime", "time", or "index". |
y |
the variable used on the y-axis. One of "muHats", "residuals", "durations", "adjDur", "dayTime", "time", or "index". |
xlag |
number of lags used for the variable shown on the x-axis. |
ylag |
number of lags used for the variable shown on the y-axis. |
colour |
a possible third variable to be represented with a colour scale. One of "muHats", "residuals", "durations", "adjDur", "dayTime", or "time". |
xlim |
a vector of the limits of the x-axis to possibly zoom in on a certain region. |
ylim |
a vector of the limits of the y-axis to possibly zoom in on a certain region. |
alpha |
|
smoothMethod |
value passed as |
Markus Belfrage
## Not run: # The mean residuals are too small for small values of the estimated conditional # mean, suggesting a need for a different conditional mean model specification: fitModel <- acdFit(adjDurData) plotScatterAcd(fitModel, x = "muHats", y = "residuals") ## End(Not run)
## Not run: # The mean residuals are too small for small values of the estimated conditional # mean, suggesting a need for a different conditional mean model specification: fitModel <- acdFit(adjDurData) plotScatterAcd(fitModel, x = "muHats", y = "residuals") ## End(Not run)
Plots a QQ-plot of the residuals and the theoretical quantiles implied by the model estimates.
qqplotAcd(fitModel, xlim = NULL, ylim = NULL)
qqplotAcd(fitModel, xlim = NULL, ylim = NULL)
fitModel |
a fitted ACD model, i.e. an object of class "acdFit" |
xlim |
an optional vector of limits for the x-axis |
ylim |
an optional vector of limits for the y-axis |
fitModelExp <- acdFit(adjDurData, dist = "exp") qqplotAcd(fitModelExp)
fitModelExp <- acdFit(adjDurData, dist = "exp") qqplotAcd(fitModelExp)
Density (PDF), distribution function (CDF), quantile function (inverted CDF), random generation, exepcted value, and hazard function for the q-Weibull distribution.
dqweibull(x, a = .8, qdist = 1.2, b = 1, forceExpectation = F) pqweibull(q, a = .8, qdist = 1.2, b = 1, forceExpectation = F) qqweibull(p, a = .8, qdist = 1.2, b = 1, forceExpectation = F) rqweibull(n = 1, a = .8, qdist = 1.2, b = 1, forceExpectation = F) qweibullExpectation(a = .8, qdist = 1.2, b = 1) qweibullHazard(x, a = .8, qdist = 1.2, b = 1, forceExpectation = F)
dqweibull(x, a = .8, qdist = 1.2, b = 1, forceExpectation = F) pqweibull(q, a = .8, qdist = 1.2, b = 1, forceExpectation = F) qqweibull(p, a = .8, qdist = 1.2, b = 1, forceExpectation = F) rqweibull(n = 1, a = .8, qdist = 1.2, b = 1, forceExpectation = F) qweibullExpectation(a = .8, qdist = 1.2, b = 1) qweibullHazard(x, a = .8, qdist = 1.2, b = 1, forceExpectation = F)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. |
a , qdist , b
|
parameters, see 'Details'. |
forceExpectation |
logical; if |
The PDF for the q-Weibull distribution is:
The distribution was used for ACD models by Vuorenmaa (2009).
Vuorenmaa, T. (2009) A q-Weibull Autoregressive Conditional Duration Model with an Application to NYSE and HSE data. Available at SSRN: http://ssrn.com/abstract=1952550.
Plots a density histogram of the residuals and superimposes the density implied by the model estimates.
resiDensityAcd(fitModel, xlim = NULL, binwidth = .1, density = FALSE)
resiDensityAcd(fitModel, xlim = NULL, binwidth = .1, density = FALSE)
fitModel |
a fitted ACD model, i.e. an object of class "acdFit" |
xlim |
an optional vector of limits for the x-axis |
binwidth |
the width of the bins of the density histogram. |
density |
if |
Markus Belfrage
## Not run: fitModelBurr <- acdFit(adjDurData, dist = "burr") resiDensityAcd(fitModelBurr) ## End(Not run)
## Not run: fitModelBurr <- acdFit(adjDurData, dist = "burr") resiDensityAcd(fitModelBurr) ## End(Not run)
Simulates a sample from a specified ACD model
and error term distribution dist
. The error terms can also be sampled from residuals. The possibility of including a diurnal seasonal component in the simulated sample is included.
sim_ACD(N = 1000, model = "ACD", dist = "exponential", param = NULL, order = NULL, Nburn = 50, startX = c(1), startMu = c(1), errors = NULL, sampleErrors = TRUE, roundToSec = FALSE, rm0 = FALSE, diurnalFactor = FALSE, splineObj = NULL, open = NULL, close = NULL)
sim_ACD(N = 1000, model = "ACD", dist = "exponential", param = NULL, order = NULL, Nburn = 50, startX = c(1), startMu = c(1), errors = NULL, sampleErrors = TRUE, roundToSec = FALSE, rm0 = FALSE, diurnalFactor = FALSE, splineObj = NULL, open = NULL, close = NULL)
N |
sample size |
model |
the class of conditional mean duration specification. One of |
dist |
the distribution of the error terms (only if |
param |
a vector of the parameters of the DGP (data generating process). |
order |
a vector describing the order of the conditional mean duration specification, e.g. |
Nburn |
the number of burned observations. Used to lower the effect of the start values of the simulated series. |
startX |
a vector of values to start the simulation from. |
startMu |
a vector of conditional mean values to start the simulation from. |
errors |
a vector of error terms. If provided and |
sampleErrors |
logical flag, see |
roundToSec |
if |
rm0 |
if |
diurnalFactor |
if |
splineObj |
a cubic spline return by |
open |
only used if |
close |
only used if |
a numerical vector of simulated ACD durations
Markus Belfrage
x <- sim_ACD() #simulates 1000 observations from an ACD(1,1) with exp. errors as default acdFit(x)
x <- sim_ACD() #simulates 1000 observations from an ACD(1,1) with exp. errors as default acdFit(x)
Standardizes residuals from a fitted ACD model of class 'acdFit' by a probability integral transformation (taking the CDF, using the estimated distribution parameters, of the residuals) or by returning the Cox-Snell residuals.
standardizeResi(fitModel, transformation = "probIntegral")
standardizeResi(fitModel, transformation = "probIntegral")
fitModel |
a fitted ACD model of class 'acdFit'. |
transformation |
type of transformation done, either "probIntegral", or "cox-snell". |
The probability integral transformation is done by taking the CDF of the residuals from the model estimation, using the estimated distribution parameters. Under correct specification the probability integral transformed residuals should be iid. uniform(0, 1).
The Cox-Snell residuals is the computed by taking the integrated hazard of the residuals from the model estimation, using the estimated distribution parameters. Under correct specification the probability integral transformed residuals should be iid. unit exponentially distributed.
Tests if there is any remaining ACD structure in the residuals
testRmACD(fitModel, pStar = 2, robust = TRUE)
testRmACD(fitModel, pStar = 2, robust = TRUE)
fitModel |
a fitted ACD model, i.e. an object of class "acdFit". |
pStar |
the number of alpha parameters in the alternative hypothesis. See |
robust |
if |
For the model
the function tests the null hypothesis
a list containing:
chi2 |
the value of the LM statistic. |
pv |
the pvalue of the test statistic. |
Markus Belfrage
Meitz, M. and Terasvirta, T. (2006). Evaluating models of autoregressive conditional duration. Journal of Business and Economic Statistics 24: 104-124.
fitModel3000obs <- acdFit(adjDurData[1:3000,]) testRmACD(fitModel3000obs, pStar = 2, robust = TRUE)
fitModel3000obs <- acdFit(adjDurData[1:3000,]) testRmACD(fitModel3000obs, pStar = 2, robust = TRUE)
Tests if the alpha parameters and the constant should be varying with the value of the lagged durations, according to a logistic transition function.
testSTACD(fitModel, K = 2, robust = TRUE)
testSTACD(fitModel, K = 2, robust = TRUE)
fitModel |
a fitted ACD model, i.e. an object of class "acdFit". |
K |
the order of the logistic transition function used for the alternative hypothesis. |
robust |
if |
a list of:
chi2 |
the value of the LM statistic. |
pv |
the pvalue of the test statistic. |
fitModel3000obs <- acdFit(adjDurData[1:3000,]) testSTACD(fitModel3000obs, K = 2, robust = TRUE)
fitModel3000obs <- acdFit(adjDurData[1:3000,]) testSTACD(fitModel3000obs, K = 2, robust = TRUE)
Tests if the parameters are time-varying.
testTVACD(fitModel, K = 2, type = "total", robust = TRUE)
testTVACD(fitModel, K = 2, type = "total", robust = TRUE)
fitModel |
a fitted ACD model, i.e. an object of class "acdFit". |
K |
the order of the logistic transition function used for the alternative hypothesis. |
type |
either "total" or "intraday". If "total", the possible time varying parameters under the alternative varies over the total time of the sample, whereas for "intraday", the time variable is time of the day. See 'Details' |
robust |
if |
This function tests the fitted standard ACD model against the TVACD model of Meitz and Terasvirta (2006). The TVACD model lets the ACD parameters vary over time by a logistic transition function.
In one specification, the time variable is total time, and a test rejecting the null in favor of this alternative specification would indicate that the ACD parameters are changing over time over the total sample.
The other specification lets the parameters be intraday varying, by letting the transition variable be the time of the day. Failing this test could indicate that the diurnal adjustment was inadequate at removing any diurnal component.
a list of:
chi2 |
the value of the LM statistic. |
pv |
the pvalue of the test statistic. |
Markus Belfrage
Meitz, M. and Terasvirta, T. (2006). Evaluating models of autoregressive conditional duration. Journal of Business and Economic Statistics 24: 104-124.
fitModel5000obs <- acdFit(adjDurData[1:5000,]) testTVACD(fitModel5000obs, K = 2, type = "total", robust = TRUE) testTVACD(fitModel5000obs, K = 2, type = "intraday", robust = TRUE)
fitModel5000obs <- acdFit(adjDurData[1:5000,]) testTVACD(fitModel5000obs, K = 2, type = "total", robust = TRUE) testTVACD(fitModel5000obs, K = 2, type = "intraday", robust = TRUE)