Count by groups (pandas)

Viewed 157

I have 5 years of stock date. I need to do this: take years 1, 2 and 3 What is the probability that after seeing k consecutive ”down days”, the next day is an ”up day”? For example, if k = 3, what is the probability of seeing ”−, −, −, +” as opposed to seeing ”−, −, −, −”. Compute this for k = 1, 2, 3. I have played with groupby and cumsum, but can't seem to get it right.

For example:

group1 = df[df['True Label'] == "-"].groupby((df['True Label'] != "-").cumsum()) 
Date True Label
2019-01-02 +
2019-01-03 -
2019-01-04 +
2019-01-07 +
2019-01-08 +
1 Answers

Try this bit of logic:

import pandas as pd
import numpy as np

np.random.seed(123)
s = pd.Series(np.random.choice(['+','-'], 1000))
sm = s.groupby((s == '+').cumsum()).cumcount()

prob = (sm.diff() == -3).sum() / (sm == 3).sum()
prob

Output:

0.43661971830985913

Details:

Use (s == '+').cumsum() to create groups of '-' records, groupby and cumcount the elements in this group, the first element is the '+' and cumcount starts with zero. There fore '+--' will become 0, 1, 2. Now, take the difference to find out where '-' turns to '+'.

If this is equal to -3 then we know this group has three minus and is followed by a '+'.

Check sm==3 to get to all number of times you hand '---', sum then divide.

Related