ó
Z_Sc           @  sÙ   d  d l  m Z m Z d  d l Z d  d l m Z m Z m Z d  d l Z	 d  d l m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z d  d l Td e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d d d
 d d „ Z d S(   iÿÿÿÿ(   t   divisiont   print_functionN(   t   logt   sqrtt   exp(	   t   arrayt   asarrayt   float64t   onest   zerost   int32t   allt   anyt   shape(   t   *t	   Dirichletc           B  sw   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z	 d
 „  Z
 d „  Z d „  Z d „  Z d „  Z RS(   s^  The Dirichlet probability distribution. The Dirichlet is a continuous 
    multivariate probability distribution across non-negative unit length
    vectors. In other words, the Dirichlet is a probability distribution of 
    probability distributions. It is conjugate to the multinomial
    distribution and is widely used in Bayesian statistics.
    
    The Dirichlet probability distribution of order K-1 is 

     p(theta_1,...,theta_K) d theta_1 ... d theta_K = 
        (1/Z) prod_i=1,K theta_i^{alpha_i - 1} delta(1 -sum_i=1,K theta_i)

    The normalization factor Z can be expressed in terms of gamma functions:

      Z = {prod_i=1,K Gamma(alpha_i)} / {Gamma( sum_i=1,K alpha_i)}  

    The K constants, alpha_1,...,alpha_K, must be positive. The K parameters, 
    theta_1,...,theta_K are nonnegative and sum to 1.
    
    Status:
        Alpha
    t   alphat   _totalt   _meanc         C  s.   t  | t ƒ |  _ t | ƒ |  _ d |  _ d S(   s¤   
        Args:
            - alpha  -- The parameters of the Dirichlet prior distribution.
                        A vector of non-negative real numbers.  
        N(   R   R   R   t   sumR   t   NoneR   (   t   selfR   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   __init__N   s    c         C  sl   |  j  } t | ƒ } t | f t ƒ } x. t | ƒ D]  } t j | | d ƒ | | <q4 W| t | ƒ } | S(   s}  Return a randomly generated probability vector.
        
        Random samples are generated by sampling K values from gamma
        distributions with parameters a=lpha_i, b=1, and renormalizing. 
    
        Ref:
            A.M. Law, W.D. Kelton, Simulation Modeling and Analysis (1991).
        Authors:
            Gavin E. Crooks <gec@compbio.berkeley.edu> (2002)
        g      ð?(   R   t   lenR	   R   t   ranget   randomt   gammavariateR   (   R   R   t   Kt   thetat   k(    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   sample\   s    	c         C  s,   |  j  d  k r% |  j |  j |  _  n  |  j  S(   N(   R   R   R   R   (   R   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   meanq   s    c         C  sù   |  j  } t | ƒ } t | ƒ } t | | f t ƒ } xD t | ƒ D]6 } | | d | | | | | d | | | f <qC Wxu t | ƒ D]g } x^ t | d | ƒ D]I } | | | | | | | d } | | | | f <| | | | f <q¤ WqŠ W| S(   Ng      ð?i   (   R   R   R   R	   R   R   (   R   R   t   AR   t   cvt   it   jt   v(    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt
   covariancev   s    	4#c         C  sM   t  | t ƒ } t | ƒ t |  j ƒ k r9 t d ƒ ‚ n  t | |  j ƒ  ƒ S(   Ns,   Argument must be same dimension as Dirichlet(   R   R   R   R   t
   ValueErrorR   R   (   R   t   x(    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   mean_x‡   s    c         C  sp   t  | t ƒ } t | ƒ t |  j ƒ k r9 t d ƒ ‚ n  |  j ƒ  } t j t j t j | ƒ | ƒ | ƒ } | S(   Ns,   Argument must be same dimension as Dirichlet(	   R   R   R   R   R&   R%   t   nat   dott	   transpose(   R   R'   R!   t   var(    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt
   variance_x   s    'c         C  s   |  j  } t t | ƒ ƒ } d } x9 | D]1 } | d k r( | d | t d | ƒ 7} q( q( W| | } | t | d ƒ 7} | S(   s|  Calculate the average entropy of probabilities sampled
        from this Dirichlet distribution. 
        
        Returns:
            The average entropy.
            
        Ref:
            Wolpert & Wolf, PRE 53:6841-6854 (1996) Theorem 7
            (Warning: this paper contains typos.)
        Status:
            Alpha
        Authors:
            GEC 2005
    
        g        i    g      ð¿g      ð?(   R   t   floatR   t   digamma(   R   R   R    t   entt   a(    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   mean_entropy—   s    	 #
c         C  s²  |  j  } t t | ƒ ƒ } | | d } t | ƒ } t | t ƒ } t | t ƒ } t | t ƒ } x\ t | ƒ D]N } t | | d ƒ | | <t | | d ƒ | | <t | | d ƒ | | <qo Wt | d ƒ }	 t | d ƒ }
 |  j	 ƒ  } d } xª t | ƒ D]œ } x“ t | ƒ D]… } | | k r^| | | |	 | | |	 |
 | | | | | 7} q| | | |	 d | | |
 | | | | d | 7} qWq W| | d 8} | S(   s¸   Calculate the variance of the Dirichlet entropy. 

        Ref:
            Wolpert & Wolf, PRE 53:6841-6854 (1996) Theorem 8
            (Warning: this paper contains typos.)
        i   g      ð?g       @g        i   (
   R   R.   R   R   R	   R   R   R/   t   trigammaR2   (   R   R   R    t   A2t   Lt   dg1t   dg2t   tg2R"   t   dg_Ap2t   tg_Ap2R   R,   R#   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   variance_entropy³   s0    	6?c         C  s'   t  j | ƒ } |  j | ƒ |  j ƒ  S(   N(   R)   R   R(   R2   (   R   t   pvect   ln_p(    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   mean_relative_entropyÞ   s    c         C  s&   t  j | ƒ } |  j | ƒ |  j ƒ  S(   N(   R)   R   R-   R;   (   R   R<   R=   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   variance_relative_entropyã   s    c   	      C  s«   |  j  | ƒ } |  j | ƒ } t | ƒ } | | d k r] t d | | d ƒ | | d f St j | | ƒ } | j d | d ƒ } | j d d | d ƒ } | | f S(   Ng      @g        g\Âõ(\ÿ?g      ð?g       @(   R>   R?   R   t   maxt   Gammat   from_mean_variancet   inverse_cdf(	   R   R<   t   fracR   t   variancet   sdt   gt	   low_limitt
   high_limit(    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   interval_relative_entropyè   s    #(   s   alphas   _totals   _mean(   t   __name__t
   __module__t   __doc__t	   __slots__R   R   R   R%   R(   R-   R2   R;   R>   R?   RJ   (    (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyR   3   s   						
		+		RA   c           B  sq   e  Z d  Z d Z d „  Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z RS(   sd   The gamma probability distribution. (Not to be confused with the
    gamma function.)
    
    
    R   t   betac         C  sL   | d k r t  d ƒ ‚ n  | d k r6 t  d ƒ ‚ n  | |  _ | |  _ d  S(   Ng        s   alpha must be positives   beta must be positive(   R&   R   RO   (   R   R   RO   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyR     s    	c         C  s   |  | d | ƒ S(   Ng      ð?(    (   t   clsR   t   scale(    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   from_shape_scale	  s    c         C  s%   | d | } | | } |  | | ƒ S(   Ni   (    (   RP   R   RE   R   RO   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyRB     s    
c         C  s   |  j  |  j S(   N(   R   RO   (   R   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyR     s    c         C  s   |  j  |  j d S(   Ni   (   R   RO   (   R   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyRE     s    c         C  s   t  j |  j d |  j ƒ S(   Ng      ð?(   R   R   R   RO   (   R   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyR     s    c         C  sO   | d k r d S|  j  } |  j } | | d t | | ƒ | | t | ƒ S(   Ng        g      ð?(   R   RO   R   t   gamma(   R   R'   R1   t   b(    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   pdf  s
     		c         C  s   d t  |  j |  j | ƒ S(   Ng      ð?(   t   normalized_incomplete_gammaR   RO   (   R   R'   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   cdf"  s    c           s1   ‡  ‡ f d †  } t  t | t ˆ j ƒ  ƒ ƒ ƒ S(   Nc           s   ˆ j  t |  ƒ ƒ ˆ  S(   N(   RW   R   (   R'   (   t   pR   (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   rootof&  s    (   R   t	   find_rootR   R   (   R   RX   RY   (    (   RX   R   s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyRC   %  s    (   s   alphas   beta(   RK   RL   RM   RN   R   t   classmethodRR   RB   R   RE   R   RU   RW   RC   (    (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyRA   ù   s   						g`sáÓbÈO>i2   c   	        s|   d „  } ‡  f d †  } d „  } | d k	 rC | |  ˆ  | | | ƒ S| d k	 re | |  ˆ  | | | ƒ S| |  ˆ  | | ƒ Sd S(   sˆ  Return argument 'x' of the function f(x), such that f(x)=0 to
    within the given tolerance. 
    
    f : The function to optimize, f(x)
    x : The initial guess
    y : An optional second guess that shoudl bracket the root.
    fprime : The derivate of f'(x) (Optional)
    tolerance : The error bounds 
    max_iterations : Maximum number of iterations
    
    Raises:
        ArithmeticError :
            Failure to converge to the given tolerence

    Notes:
        Uses Newton-Raphson algorihtm if f'(x) is given, else uses bisect if
        y is given and brackets the root, else uses secant. 

    Status : Beta (Not fully tested)
    c   
      S  s±   | } | d } |  | ƒ } |  | ƒ } d } xf t  | ƒ D]X }	 | | | | | | } t | | ƒ | k  ru | S| } | } | } |  | ƒ } q; Wt d | | f ƒ ‚ d  S(   Ng-Cëâ6?i    s3   Failed to converge after %d iterations, value is %f(   R   t   abst   ArithmeticError(
   t   fR'   t	   tolerancet   max_iterationst   x0t   x1t   v0t   v1t   x2R"   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   secantD  s     
 c           sç   |  | ƒ } |  | ƒ } | d k r( | S| d k r8 | S| | d k rW t  d ƒ ‚ n  xs t | ƒ D]e } | | } | d | }	 |  |	 ƒ }
 | | k  rž |	 S|
 | d k r½ |	 } |
 } qd |	 } |
 } qd Wt  d | ˆ  f ƒ ‚ d  S(   Ni    s!   Start points do not bracket root.g      à?s3   Failed to converge after %d iterations, value is %f(   R]   R   (   R^   R1   RT   R_   R`   t   fat   fbR"   t   deltat   xmt   fm(   R'   (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   bisectY  s,      
 	
c         S  sq   | } xN t  | ƒ D]@ } | |  | ƒ | | ƒ } t | | ƒ | k  rM | S| } q Wt d | | f ƒ ‚ d  S(   Ns3   Failed to converge after %d iterations, value is %f(   R   R\   R]   (   R^   R'   t   fprimeR_   R`   Ra   R"   Rb   (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   newtonv  s     
N(   R   (	   R^   R'   t   yRm   R_   R`   Rf   Rl   Rn   (    (   R'   s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyRZ   -  s    		(   t
   __future__R    R   R   t   mathR   R   R   t   numpyR)   R   R   R   R   R	   R
   R   R   R   t   corebio.morematht   objectR   RA   R   RZ   (    (    (    s=   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/weblogolib/logomath.pyt   <module>(   s   @
Æ4