This vignette describes various scenarios of creating sample datasets
that fit the preferences and needs of the users; and this for the
different models. The functions in the package PINstimation use two
types of datasets: (1) A sequence of daily buys and sells (2) A
high-frequency trading data. This is the reason why only two sample
datasets are preloaded with the package, namely
dailytrades, and hfdata. The users can also
generate simulated data using the function
generatedata_mpin() for PIN, and MPIN models; and the
function generatedata_adjpin() for the ADJPIN model. Below
we provide some scenarios of creating sample datasets both for the PIN,
MPIN, and ADJPIN models.
The PIN model is a multilayer PIN model with a single information
layer. We can, therefore, use the function
generatedata_mpin(), in order to generate sample data for
the PIN model. Generically, this is done as follows:
If the user would like to create a sample dataset for infrequently
traded stock, she can specify low values or ranges for the trade
intensity rates. For instance, let’s assume that the user suspects that
an infrequently-traded stock has an average of uninformed trading
intensity for buys and sells between 300 and
500. They generate a single sample dataset for this
scenario as follows:
pindata <- generatedata_mpin(layers=1, ranges = list(eps.b=c(300, 500), eps.s=c(300,500)), verbose = FALSE)The details of the generated sample dataset can be displayed with the following code
## ----------------------------------
## Data series successfully generated
## ----------------------------------
## Simulation model : MPIN model
## Number of layers : 1 layer(s)
## Number of trading days : 60 days
## ----------------------------------
## Type object@data to get the simulated data
##
## Data simulation
##
## =========== ============== ============ =============
## Variables Theoretical. Empirical. Aggregates.
## =========== ============== ============ =============
## alpha 0.551579 0.6 0.6
## delta 0.996239 1 1
## mu 181 181.89 181.89
## eps.b 403 400.8 400.8
## eps.s 444 442 442
## ----
## Likelihood - (575.64) (575.64)
## mpin - 0.114644 0.114644
## =========== ============== ============ =============
##
## -------
## Running time: 0.004 seconds
You access the sequences of buys, and sells through the slot
@data of the object pindata.
## b s
## 1 433 444
## 2 399 609
## 3 398 611
## 4 369 610
## 5 385 606
## 6 420 636
## 7 404 617
## 8 374 616
## 9 368 616
## 10 399 610
You can, now use the dataset object pindata to check the
accuracy of the different estimation functions. You can do that by
comparing the actual parameters of the sample datasets to the estimated
parameters of the estimation functions. Let us start with displaying the
actual parameters of the sample datasets. These can be accessed through
the slot @empiricals of the dataset object, which stores
the empirical parameters computed from the sequences of buys and sells
generated. Please refer to the documentation of
generatedata_mpin() for more information.
## alpha delta mu eps.b eps.s
## 0.6000 1.0000 181.8889 400.8000 442.0000
Estimate the PIN model using the function pin_ea(), and
display the estimated parameters
## alpha delta mu eps.b eps.s
## 0.6000001 1.0000000 181.8885897 400.8000064 442.0001301
Now calculate the absolute errors of the estimation method.
## alpha delta mu eps.b eps.s
## 1.212426e-07 7.182267e-10 2.991441e-04 6.354252e-06 1.300945e-04
In contrast to the PIN model, the number of information layers is
free. We can, therefore, use the function
generatedata_mpin() with the desired number of information
layers, in order to generate sample data for the MPIN model. We can also
skip specifying the number of layers, and the default setting will be
used: the number of layers will be randomly selected from the integer
set from 1 to 5. Generically, this is done as
follows:
If the user would like to create a sample dataset for frequently
traded stock with two information layers, she can set the argument
layers to 2, and specify high values or ranges for the trade intensity
rates. For instance, let’s assume that the user suspects that a
frequently-traded stock has an average of uninformed trading intensity
for buys and sells between 12000 and 15000.
They generate a single sample dataset for this scenario as follows:
mpindata <- generatedata_mpin(layers=2, ranges = list(eps.b=c(12000, 15000), eps.s=c(12000,15000)), verbose = FALSE)The details of the generated sample dataset can be displayed with the following code
## ----------------------------------
## Data series successfully generated
## ----------------------------------
## Simulation model : MPIN model
## Number of layers : 2 layer(s)
## Number of trading days : 60 days
## ----------------------------------
## Type object@data to get the simulated data
##
## Data simulation
##
## =========== ================== ================== =============
## Variables Theoretical. Empirical. Aggregates.
## =========== ================== ================== =============
## alpha 0.469414, 0.186332 0.516667, 0.183333 0.7
## delta 0.886274, 0.250064 0.870968, 0.181818 0.690476
## mu 1403, 2903 1414.53, 2863.52 1794.03
## eps.b 13328 13355.36 13355.36
## eps.s 14848 14818 14818
## ----
## Likelihood - (826.654) (826.654)
## mpin - 0.042673 0.042673
## =========== ================== ================== =============
##
## -------
## Running time: 0.004 seconds
You access the sequences of buys, and sells through the slot
@data of the object mpindata.
## b s
## 1 16320 14887
## 2 16152 14823
## 3 13104 14889
## 4 13269 16203
## 5 13252 16166
## 6 14725 14650
## 7 13529 16119
## 8 14616 15005
## 9 16172 14690
## 10 16127 14767
You can, now use the dataset object mpindata to check
the accuracy of the different estimation functions, namely
mpin_ml(), and mpin_ecm(). You can do that by
comparing the empirical PIN value derived from the sample dataset to the
estimated PIN value of the estimation functions. Let us start with
displaying the empirical PIN value obtained from the sample dataset.
This value can be accessed through the slot @emp.pin of the
dataset object, which stores the empirical PIN value computed from the
sequences of buys and sells generated. Please refer to the documentation
of generatedata_mpin() for more information.
## MPIN
## 0.04267266
Estimate the MPIN model using the functions mpin_ml(),
and mpin_ecm(), and display the estimated MPIN values.
model_ml <- mpin_ml(data=mpindata@data, verbose = FALSE)
model_ecm <- mpin_ecm(data=mpindata@data, verbose = FALSE)
mlmpin <- model_ml@mpin
ecmpin <- model_ecm@mpin
estimates <- setNames(c(mlmpin, ecmpin), c("ML", "ECM"))
show(estimates)## ML ECM
## 0.04244863 0.04244889
Now calculate the absolute errors of both estimation methods.
## ML ECM
## 0.0002240371 0.0002237758
The function generatedata_mpin() can generate a
data.series object that contains a collection of
dataset objects. For instance, the user can generate
layers, and use it to check the accuracy of the MPIN estimation.
size <- 3
collection <- generatedata_mpin(series = size, layers = 3, verbose = FALSE)
show(collection)## ----------------------------------
## Simulated data successfully generated
## ----------------------------------
## Simulation model : MPIN model
## Number of layers : 3 layer(s)
## Number of datasets : 3 datasets
## Number of trading days : 60 days
## ----------------------------------
## Type object@datasets to access the list of dataset objects
##
## Data simulation
##
## -------
## Running time: 0.014 seconds
accuracy <- devmpin <- 0
for (i in 1:size) {
sdata <- collection@datasets[[i]]
model <- mpin_ml(sdata@data, xtraclusters = 3, verbose=FALSE)
accuracy <- accuracy + (sdata@layers == model@layers)
devmpin <- devmpin + abs(sdata@emp.pin - model@mpin)
}
cat('The accuracy of layer detection: ', paste0(accuracy*(100/size),"%.\n"), sep="")## The accuracy of layer detection: 100%.
## The average error in MPIN estimates: 0.0007706718.
The AdjPIN model is an extension of the PIN model that includes the
possibility of liquidity shocks. To obtain a sample dataset distributed
according to the assumptions of the AdjPIN model, users can use the
function generatedata_adjpin(). Generically, this is done
as follows:
If the user desires to create 2 sample datasets for frequently traded
stock, they can specify high values or ranges for the trade intensity
rates. For instance, let’s assume that the user suspects that a
frequently-traded stock has an average of uninformed trading intensity
for buys and sells between 10000 and
15000.
adjpindatasets <- generatedata_adjpin(series = 2, ranges = list(eps.b=c(10000, 15000), eps.s=c(10000,15000)), verbose = FALSE)The details of the generated sample data series can be displayed with the following code:
## ----------------------------------
## Simulated data successfully generated
## ----------------------------------
## Simulation model : AdjPIN model
## Model Restrictions : Unrestricted model
## Number of datasets : 2 datasets
## Number of trading days : 60 days
## ----------------------------------
## Type object@datasets to access the list of dataset objects
##
## Data simulation
##
## -------
## Running time: 0.029 seconds
You access the first dataset from adjpindatasets using
this code:
## ----------------------------------
## Data series successfully generated
## ----------------------------------
## Simulation model : AdjPIN model
## Model Restrictions : Unrestricted model
## Number of trading days : 60 days
## ----------------------------------
## Type object@data to get the simulated data
##
## Data simulation
##
## =========== ============== ============
## Variables Theoretical. Empirical.
## =========== ============== ============
## alpha 0.890669 0.883333
## delta 0.682942 0.679245
## theta 0.825821 0.428571
## theta' 0.029343 0.018868
## ----
## eps.b 10730 10680.61
## eps.s 11341 11383.56
## mu.b 55981 56023.51
## mu.s 48348 48261.6
## d.b 25566 25644.39
## d.s 27048 26979.14
## ----
## Likelihood (845.605)
## adjPIN 0.616 0.637
## PSOS 0.083 0.05
## =========== ============== ============
##
## -------
## Running time: 0.013 seconds
You can, now use the dataset object adjpindata to check
the accuracy of the different estimation functions, namely MLE, and ECM
algorithms. You can do that by comparing the empirical adjpin, and psos
values derived from the sample dataset to the estimated adjpin, and psos
values obtained from the estimation functions. Let us start with
displaying the empirical adjpin, and psos values obtained from the
sample dataset. These values can be accessed through the slot
@emp.pin of the dataset object, which stores the empirical
adjpin/psos value computed from the sequences of buys and sells
generated. Please refer to the documentation of
generatedata_adjpin() for more information.
## adjpin psos
## 0.63676953 0.04983099
Estimate the AdjPIN model using adjpin(method="ML"), and
adjpin(method="ECM"), and display the estimated adjpin/psos
values.
model_ml <- adjpin(data=adjpindata@data, method = "ML", verbose = FALSE)
model_ecm <- adjpin(data=adjpindata@data, method = "ECM", verbose = FALSE)
mlpins <- c(model_ml@adjpin, model_ml@psos)
ecmpins <- c(model_ecm@adjpin, model_ecm@psos)
estimates <- rbind(mlpins, ecmpins)
colnames(estimates) <- c("adjpin", "psos")
rownames(estimates) <- c("ML", "ECM")
show(estimates)## adjpin psos
## ML 0.6367212 0.04988604
## ECM 0.6367248 0.04988135
Now calculate the absolute errors of both estimation methods.
## adjpin psos
## ML 4.830336e-05 5.505426e-05
## ECM 4.470273e-05 5.035911e-05
If you encounter a clear bug, please file an issue with a minimal reproducible example on GitHub.