
p7]c        3   @   s  d  Z  d d l m Z m Z m Z d d l m Z d d l Z d d l	 m
 Z d d l m Z d d  Z d   Z d	 d d
  Z d   Z d   Z d   Z d d  Z d d  Z d d d  Z d d  Z d d  Z d d  Z d d d d  Z d d d d  Z e d k rd GHd GHd d d d d g Z d d d  d! d" g Z x e D] Z  e e   GHqSWx e D] Z  e e   GHqoWe e  GHe e  GHd# d$ d% d& d' d( d d) d* d+ d, d- d. g Z! e j" d/  e j# d0  e j$ d1  e j% d2 d d3  Z& e j' e& e e&   e j" d/  e j# d4  e j$ d1  e j% d2 d d5  Z& e j' e& e e e e& d e&    e j( d2 d2 d6 d7 g d d8 d d8 d d8 g d d9 d d8 d d: g g  Z) e) j* d2  Z+ e) j* d  Z, e e+  Z- e e,  Z. e e)  Z/ e e+ e, e)  Z0 e e, e+ e)  Z1 e d e j2  e j3 e+ e,  Z4 e d e j2  e j3 e, e+  Z5 e e+ e, e)  Z6 d; GHe- e. e/ e0 e1 e4 e5 e6 f GHd< GHe j( d= d> d? d@ dA dB dC dD dE dF dG dH dI dJ dK d9 dL dM dN dO dL dP dQ dR dS dT dU dV dW dX dY dZ d[ d\ d] d^ d_ dQ d` da db dc dd de df dg dh di dj dk g2  Z e e  Z7 Hdl GHdm GHdn GHdo GHdp GHdq e+ GHdr GHd e+ d Z8 ds e8 GHe e8 d e8 g  Z9 dt e9 GHdu e9 e8 e j: d  e0 f GHdv GHdw e8 e- d e j: d7  f GHdx GHdy GHe j( dz dz d6 g d d{ d d{ d d{ g d d: d d{ d d| g g  Z; e; GHd} GHd~ e j< d2 e e; d  e e; d  g  d e j: d7  GHd GHe j( d d d g d d d g d d d g g  Z= n  d S(   s:  
Information Theoretic and Entropy Measures

References
----------
Golan, As. 2008. "Information and Entropy Econometrics -- A Review and
    Synthesis." Foundations And Trends in Econometrics 2(1-2), 1-145.

Golan, A., Judge, G., and Miller, D.  1996.  Maximum Entropy Econometrics.
    Wiley & Sons, Chichester.
i(   t   ranget   lzipt   lmap(   t   statsN(   t   pyplot(   t	   logsumexpc         C   s   | d k r t |   St j |   }  t |  j  } d | | <|  j d |  } t j t j |  | j	 |   j
 d |   } | | } | S(   s-  
    Compute the log of the sum of exponentials log(e^{a_1}+...e^{a_n}) of a

    Avoids numerical overflow.

    Parameters
    ----------
    a : array-like
        The vector to exponentiate and sum
    axis : int, optional
        The axis along which to apply the operation.  Defaults is None.

    Returns
    -------
    sum(log(exp(a)))

    Notes
    -----
    This function was taken from the mailing list
    http://mail.scipy.org/pipermail/scipy-user/2009-October/022931.html

    This should be superceded by the ufunc when it is finished.
    i   t   axisN(   t   Nonet   sp_logsumexpt   npt   asarrayt   listt   shapet   maxt   logt   expt   reshapet   sum(   t   aR   t   shpt   a_maxt   st   lse(    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyR   -   s    

1
c         C   sc   t  j |   }  t  j t  j |   d  sW t  j |  d k  sW t  j |  d k  r[ t St Sd S(   sC   
    Checks to see if `X` is a proper probability distribution
    i   i    N(   R	   R
   t   allcloseR   t   allt   Falset   True(   t   X(    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt   _isproperdistQ   s    Ht   efc         C   sG  t  |   } | d k r3 t j t j |   } n  | d k rb t j | t j |   |  } n  | d k rCt j |   t j	 |   } t j | |  } t j
 |   \ } } t j |  } d } | d }	 | | | d <xa t d |  D]M }
 | |
 |	 | k  r| | | |
 <q | |
 }	 | d 7} | | | |
 <q Wn  | S(   s   
    Discretize `X`

    Parameters
    ----------
    bins : int, optional
        Number of bins.  Default is floor(sqrt(N))
    method : string
        "ef" is equal-frequency binning
        "ew" is equal-width binning

    Examples
    --------
    R   t   ewi   i    N(   t   lenR   R	   t   floort   sqrtt   ceilR   t   rankdataR   t   mint   fastsortt   zerosR    (   R   t   methodt   nbinst   nobst   discretet   widtht   svect   ivect   binnumt   baset   i(    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt
   discretize[   s(    #


c         C   s   t  j |  t  j |   S(   s   
    There is a one-to-one transformation of the entropy value from
    a log base b to a log base a :

    H_{b}(X)=log_{b}(a)[H_{a}(X)]

    Returns
    -------
    log_{b}(a)
    (   R	   R   (   R   t   b(    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt   logbasechange   s    c         C   s   t  t j d  |  S(   s$   
    Converts from nats to bits
    i   (   R3   R	   t   e(   R   (    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt
   natstobits   s    c         C   s   t  d t j  |  S(   s$   
    Converts from bits to nats
    i   (   R3   R	   R4   (   R   (    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt
   bitstonats   s    i   c         C   s   t  j |   }  t  j |  d k  s; t  j |  d k  rJ t d   n  t  j t  j |  t  j |     } | d k r t d |  | S| Sd S(   sC  
    This is Shannon's entropy

    Parameters
    ----------
    logbase, int or np.e
        The base of the log
    px : 1d or 2d array_like
        Can be a discrete probability distribution, a 2d joint distribution,
        or a sequence of probabilities.

    Returns
    -----
    For log base 2 (bits) given a discrete distribution
        H(p) = sum(px * log2(1/px) = -sum(pk*log2(px)) = E[log2(1/p(X))]

    For log base 2 (bits) given a joint distribution
        H(px,py) = -sum_{k,j}*w_{kj}log2(w_{kj})

    Notes
    -----
    shannonentropy(0) is defined as 0
    i   i    s&   px does not define proper distributioni   N(   R	   R
   R   t
   ValueErrorR   t
   nan_to_numt   log2R3   (   t   pxt   logbaset   entropy(    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt   shannonentropy   s    ,&c         C   s   t  j |   }  t  j |  d k  s; t  j |  d k  rJ t d   n  | d k rq t d |  t  j |   St  j |   Sd S(   s   
    Shannon's information

    Parameters
    ----------
    px : float or array-like
        `px` is a discrete probability distribution

    Returns
    -------
    For logbase = 2
    np.log2(px)
    i   i    s&   px does not define proper distributioni   N(   R	   R
   R   R7   R3   R9   (   R:   R;   (    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt   shannoninfo   s    ,c         C   s   t  |   s t  |  r) t d   n  | d k	 rQ t  |  rQ t d   n  | d k rr t j | |   } n  t j | t j t j | |    } | d k r | St d |  | Sd S(   s  
    Return the conditional entropy of X given Y.

    Parameters
    ----------
    px : array-like
    py : array-like
    pxpy : array-like, optional
        If pxpy is None, the distributions are assumed to be independent
        and conendtropy(px,py) = shannonentropy(px)
    logbase : int or np.e

    Returns
    -------
    sum_{kj}log(q_{j}/w_{kj}

    where q_{j} = Y[j]
    and w_kj = X[k,j]
    s1   px or py is not a proper probability distributions&   pxpy is not a proper joint distribtioni   N(	   R   R7   R   R	   t   outerR   R8   R9   R3   (   R:   t   pyt   pxpyR;   t   condent(    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt   condentropy   s    )c         C   s   t  |   s t  |  r) t d   n  | d k	 rQ t  |  rQ t d   n  | d k rr t j | |   } n  t |  d | t |  | | d | S(   sC  
    Returns the mutual information between X and Y.

    Parameters
    ----------
    px : array-like
        Discrete probability distribution of random variable X
    py : array-like
        Discrete probability distribution of random variable Y
    pxpy : 2d array-like
        The joint probability distribution of random variables X and Y.
        Note that if X and Y are independent then the mutual information
        is zero.
    logbase : int or np.e, optional
        Default is 2 (bits)

    Returns
    -------
    shannonentropy(px) - condentropy(px,py,pxpy)
    s1   px or py is not a proper probability distributions&   pxpy is not a proper joint distribtionR;   N(   R   R7   R   R	   R?   R=   RC   (   R:   R@   RA   R;   (    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt
   mutualinfo   s    c         C   s   t  |   s t  |  r) t d   n  | d k	 rQ t  |  rQ t d   n  | d k rr t j | |   } n  t |  | | d | t | d | S(   sa  
    An information theoretic correlation measure.

    Reflects linear and nonlinear correlation between two random variables
    X and Y, characterized by the discrete probability distributions px and py
    respectively.

    Parameters
    ----------
    px : array-like
        Discrete probability distribution of random variable X
    py : array-like
        Discrete probability distribution of random variable Y
    pxpy : 2d array-like, optional
        Joint probability distribution of X and Y.  If pxpy is None, X and Y
        are assumed to be independent.
    logbase : int or np.e, optional
        Default is 2 (bits)

    Returns
    -------
    mutualinfo(px,py,pxpy,logbase=logbase)/shannonentropy(py,logbase=logbase)

    Notes
    -----
    This is also equivalent to

    corrent(px,py,pxpy) = 1 - condent(px,py,pxpy)/shannonentropy(py)
    s1   px or py is not a proper probability distributions&   pxpy is not a proper joint distribtionR;   N(   R   R7   R   R	   R?   RD   R=   (   R:   R@   RA   R;   (    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt   corrent  s    c         C   s   t  |   s t  |  r) t d   n  | d k	 rQ t  |  rQ t d   n  | d k rr t j | |   } n  t |  | | d | t | |  | d | S(   sk  
    An information theoretic covariance measure.

    Reflects linear and nonlinear correlation between two random variables
    X and Y, characterized by the discrete probability distributions px and py
    respectively.

    Parameters
    ----------
    px : array-like
        Discrete probability distribution of random variable X
    py : array-like
        Discrete probability distribution of random variable Y
    pxpy : 2d array-like, optional
        Joint probability distribution of X and Y.  If pxpy is None, X and Y
        are assumed to be independent.
    logbase : int or np.e, optional
        Default is 2 (bits)

    Returns
    -------
    condent(px,py,pxpy,logbase=logbase) + condent(py,px,pxpy,
            logbase=logbase)

    Notes
    -----
    This is also equivalent to

    covent(px,py,pxpy) = condent(px,py,pxpy) + condent(py,px,pxpy)
    s1   px or py is not a proper probability distributions&   pxpy is not a proper joint distribtionR;   N(   R   R7   R   R	   R?   RB   (   R:   R@   RA   R;   (    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt   covent=  s    i   t   Rc         C   s   t  |   s t d   n  t |  } | d k r` t |   } | d k r\ t d |  | S| Sd t |  j   k s | t j k r t j	 t j
 |    S|  | }  t j	 |  j    } | d k r d d | | Sd d | t d |  | Sd S(   ss  
    Renyi's generalized entropy

    Parameters
    ----------
    px : array-like
        Discrete probability distribution of random variable X.  Note that
        px is assumed to be a proper probability distribution.
    logbase : int or np.e, optional
        Default is 2 (bits)
    alpha : float or inf
        The order of the entropy.  The default is 1, which in the limit
        is just Shannon's entropy.  2 is Renyi (Collision) entropy.  If
        the string "inf" or numpy.inf is specified the min-entropy is returned.
    measure : str, optional
        The type of entropy measure desired.  'R' returns Renyi entropy
        measure.  'T' returns the Tsallis entropy measure.

    Returns
    -------
    1/(1-alpha)*log(sum(px**alpha))

    In the limit as alpha -> 1, Shannon's entropy is returned.

    In the limit as alpha -> inf, min-entropy is returned.
    s+   px is not a proper probability distributioni   i   t   infN(   R   R7   t   floatR=   R3   t   strt   lowerR	   RH   R   R   R   (   R:   t   alphaR;   t   measuret   genent(    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt   renyientropyk  s    '
t   Tc         C   s   d S(   s  
    Generalized cross-entropy measures.

    Parameters
    ----------
    px : array-like
        Discrete probability distribution of random variable X
    py : array-like
        Discrete probability distribution of random variable Y
    pxpy : 2d array-like, optional
        Joint probability distribution of X and Y.  If pxpy is None, X and Y
        are assumed to be independent.
    logbase : int or np.e, optional
        Default is 2 (bits)
    measure : str, optional
        The measure is the type of generalized cross-entropy desired. 'T' is
        the cross-entropy version of the Tsallis measure.  'CR' is Cressie-Read
        measure.

    N(    (   R:   R@   RA   RL   R;   RM   (    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt   gencrossentropy  t    t   __main__sQ   From Golan (2008) "Information and Entropy Econometrics -- A Review and Synthesiss	   Table 3.1g?gS㥛?g;On?g'1Z?gK?gMbp?gh㈵>g-C6?gMbP?g{Gz?g?g333333?g      ?g333333?gffffff?g?g?g      ?io   t   Informationt   Probabilityi    i t   Entropyie   g      ?i   g      "@g      2@g      @s	   Table 3.3s   discretize functionsg3333335@g     @F@g      ?@g     3@gLD@gYC@g333333&@g/@gfffff?@g9@g3333334@gffffff,@g      8@g      5@g&@gL0@g3333336@g333333@g;@ǧA@g-@g1@g333333<@gffffff0@g     0@g      G@g      #@g2@g@@g:@g0@g333333@gffffff5@g      4@gL=@g @g     6@g)@gfffff:@g     9@gfffff6@gffffff&@g333334@g333333:@g"@g%@g333333/@s0   Example in section 3.6 of Golan, using table 3.3s'   Bounding errors using Fano's inequalitys"   H(P_{e}) + P_{e}log(K-1) >= H(X|Y)s   or, a weaker inequalitys   P_{e} >= [H(X|Y) - 1]/log(K)s	   P(x) = %ss?   X = 3 has the highest probability, so this is the estimate Xhats1   The probability of error Pe is 1 - p(X=3) = %0.4gs   H(Pe) = %0.4g and K=3s-   H(Pe) + Pe*log(K-1) = %0.4g >= H(X|Y) = %0.4gs   or using the weaker inequalitys'   Pe = %0.4g >= [H(X) - 1]/log(K) = %0.4gs>   Consider now, table 3.5, where there is additional informations.   The conditional probabilities of P(X|Y=y) are g        g      @g       @s2   The probability of error given this information iss   Pe = [H(X|Y) -1]/log(K) = %0.4gs+   such that more information lowers the errorgV-?gV-?gw/?g(\?g+?g%C?gzG?gPn?(>   t   __doc__t   statsmodels.compat.pythonR    R   R   t   scipyR   t   numpyR	   t
   matplotlibR   t   pltt
   scipy.miscR   R   R   R   R1   R3   R5   R6   R=   R>   RC   RD   RE   RF   RO   RQ   t   __name__R   t   YR0   t   pt   subplott   ylabelt   xlabelt   linspacet   xt   plott   arrayt   wR   R:   R@   t   H_Xt   H_Yt   H_XYt	   H_XgivenYt	   H_YgivenXR4   R<   t   D_YXt   D_XYt   I_XYt   discXt   pet   H_peR9   t   w2t   meant   markovchain(    (    (    s;   lib/python2.7/site-packages/statsmodels/sandbox/infotheo.pyt   <module>   s   $	
(			# (.3-&O""***'			 K@