B
    h}\;&                 @   s   d dl mZ d dlZd dlZd dlmZmZmZ d dlm	Z	m
Z
mZmZ d dlmZmZmZmZ d dlZG dd deZG dd	 d	eZdS )
    )TupleN)logsqrtexp)asarrayfloat64zerosshape)gammadigamma	polygamma	gammainccc               @   s   e Zd ZdZdZejddddZejddd	Zejdd
dZ	e
dddZeje
dddZeje
dddZe
dddZe
dddZeje
dddZeje
dddZeje
ee
e
f dddZdS )	DirichletaO  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
    )alpha_total_meanN)r   returnc             C   s(   t |t| _t|| _| j| j | _dS )z
        Args:
            - alpha  -- The parameters of the Dirichlet prior distribution.
                        A vector of non-negative real numbers.
        N)r   r   r   sumr   r   )selfr    r   Z/home/fristb/BIRCH/lib-linux-x86_64/python/lib/python3.7/site-packages/weblogo/logomath.py__init__I   s    
zDirichlet.__init__)r   c             C   sP   | j }t|}t|ft}x$t|D ]}t|| d||< q$W |t| }|S )ap  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   lenr   r   rangerandomgammavariater   )r   r   Kthetakr   r   r   sampleV   s    zDirichlet.samplec             C   s   | j S )N)r   )r   r   r   r   meank   s    zDirichlet.meanc             C   s   | j }t|}t|}t||ft}x<t|D ]0}|| d|| |   ||d   |||f< q.W xbt|D ]V}xPt|d |D ]>}||  ||  || |d   }||||f< ||||f< qW qlW |S )Ng      ?   )r   r   r   r   r   r   )r   r   Ar   cvijvr   r   r   
covariancen   s    0"zDirichlet.covariance)xr   c             C   s4   t |t}t|t| jkr$tdt||   S )Nz,Argument must be same dimension as Dirichlet)r   r   r	   r   
ValueErrorr   r    )r   r(   r   r   r   mean_x   s    
zDirichlet.mean_xc             C   sJ   t |t}t|t| jkr$td|  }ttt|||}|S )Nz,Argument must be same dimension as Dirichlet)	r   r   r	   r   r)   r'   npdotZ	transpose)r   r(   r#   varr   r   r   
variance_x   s    
zDirichlet.variance_xc             C   s`   | j }tt|}d}x,|D ]$}|dkr|d| td|  7 }qW || }|t|d 7 }|S )ac  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        r   g      g      ?)r   floatr   r   )r   r   r"   entar   r   r   mean_entropy   s    
zDirichlet.mean_entropyc             C   sf  | j }tt|}||d  }t|}t|t}t|t}t|t}xNt|D ]B}t|| d ||< t|| d ||< td|| d ||< qNW t|d }	td|d }
| 	 }d}xt|D ]}xt|D ]|}||kr||| |	 || |	  |
 || ||   | 7 }q||| |	 d || |
  || || d   | 7 }qW qW ||d 8 }|S )zCalculate the variance of the Dirichlet entropy.

        Ref:
            Wolpert & Wolf, PRE 53:6841-6854 (1996) Theorem 8
            (Warning: this paper contains typos.)
        r!   g      ?g       @g           )
r   r/   r   r   r   r   r   r   r   r2   )r   r   r"   A2LZdg1Zdg2Ztg2r$   Zdg_Ap2Ztg_Ap2r    r-   r%   r   r   r   variance_entropy   s0    



4>zDirichlet.variance_entropy)pvecr   c             C   s   t |}| | |   S )N)r+   r   r*   r2   )r   r7   ln_pr   r   r   mean_relative_entropy   s    
zDirichlet.mean_relative_entropyc             C   s   t |}| ||   S )N)r+   r   r.   r6   )r   r7   r8   r   r   r   variance_relative_entropy   s    
z#Dirichlet.variance_relative_entropy)r7   fracr   c       	      C   s   |  |}| |}t|}|| dkrFtd||d  ||d  fS t||}|d| d }|dd| d  }||fS )Ng      @g        g\(\?g      ?g       @)r9   r:   r   maxGammafrom_mean_varianceinverse_cdf)	r   r7   r;   r    variancesdgZ	low_limitZ
high_limitr   r   r   interval_relative_entropy   s    

z#Dirichlet.interval_relative_entropy)__name__
__module____qualname____doc__	__slots__r+   Zndarrayr   r   r    r/   r'   r*   r.   r2   r6   r9   r:   r   rC   r   r   r   r   r   1   s   	)r   c               @   s   e Zd ZdZdZeeddddZeeed ddd	Zeeed d
ddZ	edddZ
edddZedddZeedddZeedddZeedddZdS )r=   z\The gamma probability distribution. (Not to be confused with the
    gamma function.)


    )r   betaN)r   rI   r   c             C   s0   |dkrt d|dkr t d|| _|| _d S )Ng        zalpha must be positivezbeta must be positive)r)   r   rI   )r   r   rI   r   r   r   r      s    zGamma.__init__)r	   scaler   c             C   s   | |d| S )Ng      ?r   )clsr	   rJ   r   r   r   from_shape_scale   s    zGamma.from_shape_scale)r    r@   r   c             C   s   |d | }|| }| ||S )Nr3   r   )rK   r    r@   r   rI   r   r   r   r>      s    zGamma.from_mean_variance)r   c             C   s   | j | j S )N)r   rI   )r   r   r   r   r      s    z
Gamma.meanc             C   s   | j | jd  S )Nr3   )r   rI   )r   r   r   r   r@     s    zGamma.variancec             C   s   t | jd| j S )Ng      ?)r   r   r   rI   )r   r   r   r   r     s    zGamma.sample)r(   r   c             C   sB   |dkrdS | j }| j}||d  t| |  ||  t| S )Ng        g      ?)r   rI   r   r
   )r   r(   r1   br   r   r   pdf  s
    z	Gamma.pdfc             C   s   dt | j| j|  S )Ng      ?)r   r   rI   )r   r(   r   r   r   cdf  s    z	Gamma.cdf)pr   c                s4   t t d fdd}tj|t }t|S )N)r(   r   c                s    t|   S )N)rO   r   )r(   )rP   r   r   r   rootof  s    z!Gamma.inverse_cdf.<locals>.rootof)r/   scipyoptimizeZnewtonr   r    r   )r   rP   rQ   rootr   )rP   r   r   r?     s    zGamma.inverse_cdf)rD   rE   rF   rG   rH   r/   r   classmethodrL   r>   r    r@   r   rN   rO   r?   r   r   r   r   r=      s   r=   )typingr   numpyr+   r   mathr   r   r   r   r   r   r	   Zscipy.specialr
   r   r   r   Zscipy.optimizerR   objectr   r=   r   r   r   r   <module>'   s    ;