Cython function to compute transition statistics