## Overview

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 simulation 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.

## Sample datasets for the PIN model

The PIN model is an 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:

generatedata_mpin(..., layers=1)

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

show(pindata)
## ----------------------------------
## 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.614181        0.633333      0.633333
## delta        0.167842        0.263158      0.263158
## mu           214             216.91        216.91
## eps.b        354             354.88        354.88
## eps.s        306             305.78        305.78
## ----
## Likelihood   -               (591.548)     (591.548)
## mpin         -               0.172146      0.172146
## ===========  ==============  ============  =============
##
## -------
## Running time: 0.014 seconds

You access the sequences of buys, and sells through the slot @data of the object pindata.

show(pindata@data[1:10, ])
##      b   s
## 1  597 283
## 2  342 509
## 3  352 315
## 4  557 299
## 5  381 278
## 6  574 344
## 7  357 312
## 8  604 310
## 9  359 506
## 10 382 326

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.

actual <- unlist(pindata@empiricals)
show(actual)
##       alpha       delta          mu       eps.b       eps.s
##   0.6333333   0.2631579 216.9131579 354.8750000 305.7800000

Estimate the PIN model using the function pin_ea(), and display the estimated parameters

model <- pin_ea(data=pindata@data, verbose = FALSE)
estimates <- model@parameters
show(estimates)
##       alpha       delta          mu       eps.b       eps.s
##   0.6333331   0.2631576 217.1151638 354.5894703 305.9374596

Now calculate the absolute errors of the estimation method.

errors <- abs(actual - estimates)
show(errors)
##        alpha        delta           mu        eps.b        eps.s
## 2.270963e-07 2.975662e-07 2.020059e-01 2.855297e-01 1.574596e-01

## Sample datasets for the MPIN model

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:

generatedata_mpin(...)

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

show(mpindata)
## ----------------------------------
## 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.554863, 0.095560  0.516667, 0.100000  0.616667
## delta        0.738233, 0.319985  0.806452, 0.500000  0.756757
## mu           1350, 3277          1380.73, 3388.52    1706.32
## eps.b        12481               12467.75            12467.75
## eps.s        13125               13077.88            13077.88
## ----
## Likelihood   -                   (807.447)           (807.447)
## mpin         -                   0.039561            0.039561
## ===========  ==================  ==================  =============
##
## -------
## Running time: 0.006 seconds

You access the sequences of buys, and sells through the slot @data of the object pindata.

show(mpindata@data[1:10, ])
##        b     s
## 1  12508 13069
## 2  12510 13162
## 3  12130 13058
## 4  12519 14606
## 5  13891 13244
## 6  15788 13014
## 7  15883 13107
## 8  12366 12830
## 9  13787 13055
## 10 13954 13216

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.

actualmpin <- unlist(mpindata@emp.pin)
show(actualmpin)
##       MPIN
## 0.03956071

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.03946732 0.03946723

Now calculate the absolute errors of both estimation methods.

errors <- abs(actualmpin - estimates)
show(errors)
##           ML          ECM
## 9.339031e-05 9.348154e-05

The function generatedata_mpin() can generate a data.series object that contains a collection of dataset objects. For instance, the user can generate a collection of 10 datasets, whose data sequences span 60 days, and contain 3 layers, and use it to check the accuracy of the MPIN estimation.

size <- 10
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   : 10 datasets
## Number of trading days   : 60 days
## ----------------------------------
## Type object@datasets to access the list of dataset objects
##
##  Data simulation
##
## -------
## Running time: 0.053 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: 90%.
cat('The average error in MPIN estimates: ', devmpin/size, ".\n", sep="")
## The average error in MPIN estimates: 0.001280141.

## Sample datasets for the ADJPIN model

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:

generatedata_adjpin(...)

If the user desires to create 10 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 = 10, 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:

show(adjpindatasets)
## ----------------------------------
## Simulated data successfully generated
## ----------------------------------
## Simulation model     : AdjPIN model
## Model Restrictions   : Unrestricted model
## Number of datasets   : 10 datasets
## Number of trading days   : 60 days
## ----------------------------------
## Type object@datasets to access the list of dataset objects
##
##  Data simulation
##
## -------
## Running time: 0.18 seconds

You access the first dataset from adjpindata using this code:

adjpindata <- adjpindatasets@datasets[[1]]
show(adjpindata)
## ----------------------------------
## 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.218383        0.2
## delta        0.98425         0.916667
## theta        0.907528        0.916667
## theta'       0.868224        0.833333
## ----
## eps.b        11420           11452.75
## eps.s        11249           11242.5
## mu.b         12275           12006.67
## mu.s         11284           11392.73
## d.b          15861           15857.58
## d.s          18222           18279.55
## ----
## Likelihood                   (826.288)
## PSOS         0.549           0.552
## ===========  ==============  ============
##
## -------
## Running time: 0.016 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.

actualpins <- unlist(adjpindata@emp.pin)
show(actualpins)
##     adjpin       psos
## 0.04108569 0.55151361

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)
estimates <- rbind(mlpins, ecmpins)
rownames(estimates) <- c("ML", "ECM")
show(estimates)
##         adjpin      psos
## ML  0.04124971 0.5505261
## ECM 0.04127596 0.5504656

Now calculate the absolute errors of both estimation methods.

errors <- abs(estimates - rbind(actualpins, actualpins))
show(errors)
##           adjpin        psos
## ML  0.0001640242 0.000987459
## ECM 0.0001902755 0.001047967

## Getting help

If you encounter a clear bug, please file an issue with a minimal reproducible example on GitHub.