Warm tip: This article is reproduced from serverfault.com, please click

What is the efficient way to compute transition matrix from a time series data?

发布于 2020-11-28 00:46:32

I am trying to compute the transition matrix from time-series data. I wrote a custom function like the following code that serves my purpose.

def compute_transition_matrix(data, n, step = 1):
    P = np.zeros((n, n))
    m = len(data)
    for i in range(m):
        initial, final = i, i + step
        if final < m:
            P[data[initial]][data[final]] += 1
    sums = np.sum(P, axis = 1)
    for i in range(n):
        for j in range(n):
            P[i][j] = P[i][j] / sums[i]
    return P

print(compute_transition_matrix([3, 0, 1, 3, 2, 6, 5, 4, 7, 5, 4], 8, 1))

In the above function, data is the input time series data, n is the total number of states in the Markov chain, step is the transition step.

As a sample example, I took,

data = [3, 0, 1, 3, 2, 6, 5, 4, 7, 5, 4]
n = 8 (this means there are 8 states in Markov chain from 0 - 7, both inclusive)
step = 1

However, I was just wondering if there is a way to achieve this using built-in functions in NumPy/pandas/scikit?

Questioner
Sai Nikhil
Viewed
0
swag2198 2020-11-28 10:42:35

I am not sure if there are built-in functions to achieve this, but I can think of doing this in numpy (using fancy indexing, broadcasting and stride tricks) like this:

def compute_transition_matrix2(data, n, step = 1):
    
    t = np.array(data)
    step = step
    total_inds = t.size - (step + 1) + 1
    t_strided = np.lib.stride_tricks.as_strided(
                                    t,
                                    shape = (total_inds, 2),
                                    strides = (t.strides[0], step * t.strides[0]))
    
    inds, counts = np.unique(t_strided, axis = 0, return_counts = True)

    P = np.zeros((n, n))
    P[inds[:, 0], inds[:, 1]] = counts
    
    sums = P.sum(axis = 1)
    # Avoid divide by zero error by normalizing only non-zero rows
    P[sums != 0] = P[sums != 0] / sums[sums != 0][:, None]
    
    # P = P / P.sum(axis = 1)[:, None]
    return P

print(compute_transition_matrix2([3, 0, 1, 3, 2, 6, 5, 4, 7, 5, 4], 8, 1))
[[0.  1.  0.  0.  0.  0.  0.  0. ]
 [0.  0.  0.  1.  0.  0.  0.  0. ]
 [0.  0.  0.  0.  0.  0.  1.  0. ]
 [0.5 0.  0.5 0.  0.  0.  0.  0. ]
 [0.  0.  0.  0.  0.  0.  0.  1. ]
 [0.  0.  0.  0.  1.  0.  0.  0. ]
 [0.  0.  0.  0.  0.  1.  0.  0. ]
 [0.  0.  0.  0.  0.  1.  0.  0. ]]

Your code's result:

def compute_transition_matrix(data, n, step = 1):
    P = np.zeros((n, n))
    m = len(data)
    for i in range(m):
        initial, final = i, i + step
        if final < m:
            P[data[initial]][data[final]] += 1
    sums = np.sum(P, axis = 1)
    for i in range(n):
        if sums[i] != 0: # Added this check
            for j in range(n):
                P[i][j] = P[i][j] / sums[i]
    return P

print(compute_transition_matrix([3, 0, 1, 3, 2, 6, 5, 4, 7, 5, 4], 8, 1))
[[0.  1.  0.  0.  0.  0.  0.  0. ]
 [0.  0.  0.  1.  0.  0.  0.  0. ]
 [0.  0.  0.  0.  0.  0.  1.  0. ]
 [0.5 0.  0.5 0.  0.  0.  0.  0. ]
 [0.  0.  0.  0.  0.  0.  0.  1. ]
 [0.  0.  0.  0.  1.  0.  0.  0. ]
 [0.  0.  0.  0.  0.  1.  0.  0. ]
 [0.  0.  0.  0.  0.  1.  0.  0. ]]

Intermediate values in my code: (for your reference)

t_strided =

array([[3, 0],
       [0, 1],
       [1, 3],
       [3, 2],
       [2, 6],
       [6, 5],
       [5, 4],
       [4, 7],
       [7, 5],
       [5, 4]])

inds, counts =

(array([[0, 1],
        [1, 3],
        [2, 6],
        [3, 0],
        [3, 2],
        [4, 7],
        [5, 4],
        [6, 5],
        [7, 5]]),
 array([1, 1, 1, 1, 1, 1, 2, 1, 1]))

Timing comparisons:

# Generate some random large data
n = 1000
t = np.random.choice(np.arange(n), size = n)
data = list(t)

%timeit compute_transition_matrix(data, n, 1)
# 433 ms ± 21.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit compute_transition_matrix2(data, n, 1)
# 5.5 ms ± 304 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)