def entropy(pk, qk=None, base=None):
pk = asarray(pk)<EOL>pk = <NUM_LIT:1.0>*pk / sum(pk, axis=<NUM_LIT:0>)<EOL>if qk is None:<EOL><INDENT>vec = entr(pk)<EOL><DEDENT>else:<EOL><INDENT>qk = asarray(qk)<EOL>if len(qk) != len(pk):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>qk = <NUM_LIT:1.0>*qk / sum(qk, axis=<NUM_LIT:0>)<EOL>vec = kl_div(pk, qk)<EOL><DEDENT>S = sum(vec, axis=<NUM_LIT:0>)<EOL>if base is not None:<EOL><INDENT>S /= log(base)<EOL><DEDENT>return S<EOL>
Calculate the entropy of a distribution for given probability values.

    If only probabilities `pk` are given, the entropy is calculated as
    ``S = -sum(pk * log(pk), axis=0)``.

    If `qk` is not None, then compute the Kullback-Leibler divergence
    ``S = sum(pk * log(pk / qk), axis=0)``.

    This routine will normalize `pk` and `qk` if they don't sum to 1.

    Parameters
    ----------
    pk : sequence
        Defines the (discrete) distribution. ``pk[i]`` is the (possibly
        unnormalized) probability of event ``i``.
    qk : sequence, optional
        Sequence against which the relative entropy is computed. Should be in
        the same format as `pk`.
    base : float, optional
        The logarithmic base to use, defaults to ``e`` (natural logarithm).

    Returns
    -------
    S : float
        The calculated entropy.