from functools import reduce


def dinucleotides_proba(sequence, tmatrix, pi_k):
    """
    Returns the probability of occurrence of a word at a given position with the dinucleotide model.

    Parameters
    ----------
    sequence : list of int
        The integer-converted word to be considered.
    tmatrix: numpy.ndarray
        The transition matrix.
    pi_k: numpy.array
        The probability distribution at position `k`.

    Returns
    -------
        the probability of occurrence of the word.

    """
    def tmp(p, n):
        return p[0] * tmatrix[p[1]][n], n
    first_nb = sequence[0]
    initial_proba = pi_k[first_nb]
    return reduce(tmp, sequence[1:], (initial_proba, first_nb))[0]