yasa.Hypnogram.transition_matrix#

Hypnogram.transition_matrix()[source]#

Create a state-transition matrix from a hypnogram.

Returns:
countspandas.DataFrame

Counts transition matrix (number of transitions from stage A to stage B). The pre-transition states are the rows and the post-transition states are the columns.

probspandas.DataFrame

Conditional probability transition matrix, i.e. given that current state is A, what is the probability that the next state is B. probs is a right stochastic matrix, i.e. each row sums to 1.

Examples

>>> from yasa import Hypnogram, simulate_hypnogram
>>> # Generate a 8 hr (= 480 minutes) 5-stage hypnogram with a 30-seconds resolution
>>> hyp = simulate_hypnogram(tib=300, seed=42)
>>> counts, probs = hyp.transition_matrix()
>>> counts
To Stage    WAKE  N1   N2  N3  REM
From Stage
WAKE         153  11    0   0    0
N1             6  99   29   0    0
N2             3  16  447  10    5
N3             1   3    4  96    1
REM            0   5    1   0   69
>>> probs.round(3)
To Stage     WAKE     N1     N2     N3   REM
From Stage
WAKE        0.933  0.067  0.000  0.000  0.00
N1          0.045  0.739  0.216  0.000  0.00
N2          0.006  0.033  0.929  0.021  0.01
N3          0.010  0.029  0.038  0.914  0.01
REM         0.000  0.067  0.013  0.000  0.92