changed 3 years ago
Linked with GitHub

sketch for renormalize transition matrix (ignore repetition)

This is the core function to convert Fabian's transition matrix to the one without repetition

def renormalize(matrix: np.ndarray) -> np.ndarray:
    """
    :param matrix: n by n matrix
    :return: n by n matrix
    """
    identity = np.identity(matrix.shape[0])
    masked_matrix = np.ma.array(matrix, mask=identity)
    renormalized_matrix = masked_matrix / masked_matrix.sum(axis=0)
    return renormalized_matrix

Here is a simple demonstration

import numpy as np
import matplotlib.pyplot as plt



def renormalize(matrix):
    """
    :param matrix: n by n matrix
    :return: n by n matrix
    """
    identity = np.identity(matrix.shape[0])
    masked_matrix = np.ma.array(matrix, mask=identity)
    renormalized_matrix = masked_matrix / masked_matrix.sum(axis=0)
    return renormalized_matrix

def prepare_test_matrix():
    total_matrix = np.random.random(size=(100, 100))
    total_matrix = total_matrix / total_matrix.sum(axis=0)
    matrix = np.array(total_matrix[:25, :25])
    return matrix


def test():
    test_matrix = prepare_test_matrix()
    new_matrix = renormalize(test_matrix)
    plt.imshow(new_matrix)
    plt.show()


if __name__ == '__main__':
    test()

Select a repo