def marginal_topic_distrib(doc_topic_distrib, doc_lengths):
    """
    Return marginal topic distribution ``p(T)`` (topic proportions) given the document-topic distribution (theta)
    `doc_topic_distrib` and the document lengths `doc_lengths`. The latter can be calculated with
    :func:`~tmtoolkit.bow.bow_stats.doc_lengths`.

    :param doc_topic_distrib: document-topic distribution; shape NxK, where N is the number of documents, K is the
                              number of topics
    :param doc_lengths: array of size N (number of docs) with integers indicating the number of terms per document
    :return: array of size K (number of topics) with marginal topic distribution
    """
    unnorm = (doc_topic_distrib.T * doc_lengths).sum(axis=1)
    return unnorm / unnorm.sum()