def p_topic_given_document(document_topic_counts,
                           document_lengths,
                           topic,
                           d,
                           k,
                           alpha=0.1):
    """the fraction of words in document d
    that are assigned to topic (plus some smoothing)"""
    return ((document_topic_counts[d][topic] + alpha) /
            (document_lengths[d] + k * alpha))