# How to implement the herding measure proposed by Lakonishok et. al (1992) in python

I would like to test for herding behaviour using the herding measure developed by Lakonishok et. al (1992) on a dataset containing trader transactions during 2013, however, i am having some trouble implementing it in Python and I am not sure I quite understand how to compute each of the components of the equation (especially the Adjustment Factor).

The herding measure is given by H(i) = |B(i)/(B(i) + S(i)) - p(t)| - AF(i)

where AF(1) = |$\hat(B)$(1)/(B(1) + S(1)) - p(t)| ..... and ..... $\hat(B)$(i) $\sim$(p(t), B(i) + S(i))

A sample of my dataset looks as follows:

PositionID  TraderID    AssetID OrderID   Leverage  Equity  TotalAmount InitialRate PnL    Long=1   EndRate OpenDate            CloseDate
103764400   39          8       4565523   50        20      1000        0.8349       7.03   0       0.8306  24/11/2013 22:05    29/11/2013 21:31
103764489   39          3       4565521   50        20      1000        0.8175       3.9    0       0.8136  24/11/2013 22:06    29/11/2013 21:31
103764661   39          10      4565524   50        20      1000        137.3        19.73  1       139.32  24/11/2013 22:07    29/11/2013 00:53
103764698   39          1       4565518   50        20      1000        1.3553       2.8    1       1.3581  24/11/2013 22:07    29/11/2013 21:31
43611297    57          1       4565519   10        23.02   230.2       1.3         -9.74   0       1.3423  12/12/2012 00:15    08/02/2013 10:56
79572882    57          1       4565520   50        20      1000        1.3101      -0.2    1       1.3099  23/06/2013 21:13    23/06/2013 21:13
79572945    57          1       4565521   50        20      1000        1.3098      -1.5    0       1.3113  23/06/2013 21:13    24/06/2013 10:20
79683082    57          5       4565522   50        20      1000        97.96       -0.2    1       97.94   24/06/2013 10:20    24/06/2013 10:20
83630718    57          16      4565523   100       10      1000        106.41      -0.7    0       106.48  19/07/2013 08:49    19/07/2013 08:49
41039724    59          11      4565524   25        24.23   69.5        129.31      -19.89  0       157.54  20/11/2012 09:26    15/10/2013 15:42
41054904    59          11      4565525   25        24.01   69.5        129.63      -19.67  0       157.54  20/11/2012 11:47    15/10/2013 15:42
41244158    59          11      4565526   25        22.66   68          130.84      -18.41  0       157.54  21/11/2012 09:19    15/10/2013 15:42


Let's say I wanted to calculate the herding measure for AssetID=1, then:

H(1) = |2/4 - (4/12)| - AF(1) but i'm not sure how to calculate AF(1).

UPDATE: Frey et. al 2012 discuss the AF, but I still don't know how to calculate it.

Then, once I calculate the heading measure for all stocks, should I average them across all stocks and across all quarters?

Also, in the study, the authors apply this measure on subsets of the data (by size, past quarter performance, etc...). I am not sure how to do this exactly. Should I first filter the data (by size lets say) and take the largest quintile and calculate the average herding measure?

I am trying to implement this in python so I would really appreciate as much detail as possible and the best way to implement it in python.

Thank you!

• Ok, so i think I figured out how to calculate the AF(1). B(i) would be a bernoulli distribution with number of trials equal (B(i) + S(i)) and probability of success of (4/12) and successes equal to 2. So AF(1) = |2/4 - (4/12)|-|(0.2933)/4 - (4/12)|= -0.09334 Is my calculation correct? – finstats Oct 12 '14 at 23:01
• In all studies on herding, they say that the $\hat(B)$(i) can be easily calculated using a bernoulli distribution given p(t) and the number of investors, but they do not say anything about specifying the number of successes (or buys in this case). But i believe that to calculate the bernoulli distribution, one would require all three parameters? So in this link calculator i should specify all 3 parameters, am I right? – finstats Oct 12 '14 at 23:27
• In all the literature I've read, Bernoulli r.v.'s are just one outcome, 0 or 1, whereas the distribution you're talking about is called Binomial. In other words, Binomial is a sequence of bernoulli-distributed r.v.'s – Good Guy Mike Oct 13 '14 at 7:07
• @GoodGuyMike I believe i've calculated it wrongly. Can you please provide your calculation of AF(1) given the example above? – finstats Oct 13 '14 at 9:12
• Sorry @roland, I'm not familiar with this model. Also I don't have time to get acquainted with it for the time being, although I find it very interesting. I assume it's herding as in the behavioral finance term you're refering to? – Good Guy Mike Oct 13 '14 at 12:41

# Formula:

$$H(i) = \mid\frac{B(i)}{B(i)+S(i)}-p(t)\mid-AF(i)$$ (Lakonishok et al. 1992)

# How to calculate $$AF(i)$$?

• Given that

1. Institutions in a company are neither net-buying nor net-selling, $$p=0,5$$, and
2. there are two institutions active in the given quarter, $$n=2$$.
• Then the probabilities will be...

For 0 buys: $$\frac{2!\times0.5\times(1-0.5)}{(2-0)\times0!}=0.25$$

For 1 buys: $$\frac{2!\times0.5\times(1-0.5)}{(2-1)\times1!}=0.5$$

For 2 buys: $$\frac{2!\times0.5\times(1-0.5)}{(2-2)\times2!}=0.25$$

• And the absolute values will be...

For 0 buys: $$\mid \frac{0}{2}-0.5 \mid = 0.5$$

For 1 buys: $$\mid \frac{1}{2}-0.5 \mid = 0$$

For 2 buys: $$\mid \frac{2}{2}-0.5 \mid = 0.5$$

• Then the products will be...

For 0 buys: $$0.25\times0.5=0.125$$

For 1 buys: $$0.5\times0=0$$

For 2 buys: $$0.25\times0.5=0.125$$

• So the answer will be $$AF=0.125+0+0.125=0.25$$