Estimates the Probability of Informed Trading (`PIN`

) using
Bayesian Gibbs sampling as in
Griffin et al. (2021)
and the initial sets
from the algorithm in Ersan and Alici (2016)
.

## Usage

```
pin_bayes(data, xtraclusters = 4, sweeps = 1000, burnin = 500,
prior.a = 1, prior.b = 2, verbose = TRUE)
```

## Arguments

- data
A dataframe with 2 variables: the first corresponds to buyer-initiated trades (buys), and the second corresponds to seller-initiated trades (sells).

- xtraclusters
An integer used to divide trading days into

`#(2 + xtraclusters)`

clusters, thereby resulting in`#comb(1 + xtraclusters, 1)`

initial parameter sets in line with Ersan and Alici (2016) . The default value is`4`

.- sweeps
An integer referring to the number of iterations for the Gibbs Sampler. This has to be large enough to ensure convergence of the Markov chain. The default value is

`1000`

.- burnin
An integer referring to the number of initial iterations for which the parameter draws should be discarded. This is to ensure that we keep the draws at the point where the MCMC has converged to the parameter space in which the parameter estimate is likely to fall. This figure must always be less than the sweeps. The default value is

`500`

.- prior.a
An integer controlling the mean number of informed trades, such as the prior of informed buys and sells is the Gamma density function with \(\mu\) ~

`Ga(prior.a,`

\(\eta\)`)`

. The default value is`1`

. For more details, please refer to Griffin et al. (2021) .- prior.b
An integer controlling the mean number of uninformed trades, such as the prior of uninformed buys and sells is the Gamma density function with \(\epsilon\)

_{b}~`Ga(prior.b,`

\(\eta\)`)`

, and \(\epsilon\)_{s}~`Ga(prior.b,`

\(\eta\)`)`

. The default value is`2`

. For more details, please refer to Griffin et al. (2021) .- verbose
A binary variable that determines whether detailed information about the steps of the estimation of the PIN model is displayed. No output is produced when

`verbose`

is set to`FALSE`

. The default value is`TRUE`

.

## Details

The argument 'data' should be a numeric dataframe, and contain
at least two variables. Only the first two variables will be considered:
The first variable is assumed to correspond to the total number of
buyer-initiated trades, while the second variable is assumed to
correspond to the total number of seller-initiated trades. Each row or
observation correspond to a trading day. `NA`

values will be ignored.

The function `pin_bayes()`

implements the algorithm detailed in
Ersan and Alici (2016)
.
The higher the number of the additional clusters (`xtraclusters`

), the
better is the estimation. Ersan and Alici (2016)
,
however, have shown the benefit of increasing this number beyond 5 is
marginal, and statistically insignificant.

The function `initials_pin_ea()`

provides the initial parameter sets
obtained through the implementation of the
Ersan and Alici (2016)
algorithm.
For further information on the initial parameter set determination, see
`initials_pin_ea()`

.

## References

Ersan O, Alici A (2016).
“An unbiased computation methodology for estimating the probability of informed trading (PIN).”
*Journal of International Financial Markets, Institutions and Money*, **43**, 74--94.
ISSN 10424431.

Griffin J, Oberoi J, Oduro SD (2021).
“Estimating the probability of informed trading: A Bayesian approach.”
*Journal of Banking & Finance*, **125**, 106045.

## Examples

```
# Use the function generatedata_mpin() to generate a dataset of
# 60 days according to the assumptions of the original PIN model.
sdata <- generatedata_mpin(layers = 1)
xdata <- sdata@data
# Estimate the PIN model using the Bayesian approach developed in
# Griffin et al. (2021), and initial parameter sets generated using the
# algorithm of Ersan and Alici (2016). The argument xtraclusters is
# set to 1. We also leave the arguments 'sweeps' and 'burnin' at their
# default values.
estimate <- pin_bayes(xdata, xtraclusters = 1, verbose = FALSE)
# Display the empirical PIN value at the data, and the PIN value
# estimated using the bayesian approach
setNames(c(sdata@emp.pin, estimate@pin), c("data", "estimate"))
#> data estimate
#> 0.05524984 0.05437086
# Display the empirial and the estimated parameters
show(unlist(sdata@empiricals))
#> alpha delta mu eps.b eps.s
#> 0.6500000 0.1282051 532.0970056 3024.5384615 2889.5818182
show(estimate@parameters)
#> alpha delta mu eps.b eps.s
#> 0.6430273 0.1443983 528.9448546 3027.6810417 2887.8599454
# Find the initial set that leads to the optimal estimate
optimal <- which.max(estimate@details$likelihood)
# Store the matrix of Monte Carlo simulation for the optimal
# estimate, and display its last five rows
mcmatrix <- estimate@details$markovmatrix[[optimal]]
show(tail(mcmatrix, 5))
#> alpha delta mu eps.b eps.s PIN
#> sweep.996 0.5203700 0.23884793 540.7341 3018.874 2891.656 0.04544345
#> sweep.997 0.5765544 0.11954560 539.2473 3024.824 2894.763 0.04990062
#> sweep.998 0.5624396 0.16407537 540.4137 3012.990 2888.946 0.04897770
#> sweep.999 0.6493391 0.05380942 537.9778 3032.159 2884.569 0.05574957
#> sweep.1000 0.6442249 0.13095960 532.3521 3035.799 2886.004 0.05474346
# Display the summary of Geweke test for the Monte Carlo matrix above.
show(estimate@details$summary[[optimal]])
#> mean std.dev geweke.z-score geweke.p-value
#> alpha 6.430273e-01 0.056566876 0.3865038 0.349561813
#> delta 1.443983e-01 0.053738173 -0.2289417 0.409457111
#> mu 5.289449e+02 10.516647568 -1.4281747 0.076620788
#> eps.b 3.027681e+03 9.274043789 2.7162555 0.003301247
#> eps.s 2.887860e+03 7.532196410 0.8198734 0.206144128
#> PIN 5.435453e-02 0.004726313 -2.1946570 0.014094107
```