U
     ‹ÑhÁU  ã                   @   sz  d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZm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mZmZmZmZmZmZm Z  d d
l!m"Z" G dd„ deƒZ#dd„ Z$G dd„ dƒZ%G dd„ dƒZ&G dd„ dƒZ'e%e'e'e&dœZ(G dd„ de eƒZ)G dd„ de)ƒZ*dd„ Z+G dd„ de)ƒZ,dd„ Z-G d d!„ d!e)ƒZ.d"d#„ Z/G d$d%„ d%e)ƒZ0d&d'„ Z1d(S ))é    )Úprod)ÚBasic)Úpi)ÚS)Úexp)Ú
multigamma)ÚsympifyÚ_sympify)	ÚImmutableMatrixÚInverseÚTraceÚDeterminantÚMatrixSymbolÚ
MatrixBaseÚ	TransposeÚ	MatrixSetÚmatrix2numpy)Ú_value_checkÚRandomMatrixSymbolÚNamedArgsMixinÚPSpaceÚ_symbol_converterÚMatrixDomainÚDistribution)Úimport_modulec                   @   sf   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZed	d
„ ƒZ	edd„ ƒZ
dd„ Zddd„ZdS )ÚMatrixPSpacezD
    Represents probability space for
    Matrix Distributions.
    c                 C   s@   t |ƒ}t|ƒt|ƒ }}|jr&|js.tdƒ‚t | ||||¡S )NzDimensions should be integers)r   r	   Ú
is_integerÚ
ValueErrorr   Ú__new__)ÚclsÚsymÚdistributionZdim_nZdim_m© r"   úN/tmp/user/2202/pip-unpacked-wheel-qe3242ng/sympy/stats/matrix_distributions.pyr      s
    zMatrixPSpace.__new__c                 C   s
   | j d S )Né   ©Úargs©Úselfr"   r"   r#   Ú<lambda>    ó    zMatrixPSpace.<lambda>c                 C   s
   | j d S ©Nr   r%   r'   r"   r"   r#   r)   !   r*   c                 C   s   t | j| jjƒS ©N)r   Úsymbolr!   Úsetr'   r"   r"   r#   Údomain#   s    zMatrixPSpace.domainc                 C   s   t | j| jd | jd | ƒS )Né   é   )r   r-   r&   r'   r"   r"   r#   Úvalue'   s    zMatrixPSpace.valuec                 C   s   | j hS r,   )r2   r'   r"   r"   r#   Úvalues+   s    zMatrixPSpace.valuesc                 G   s4   |  t¡}t|ƒdks t|tƒs(tdƒ‚| j |¡S )Nr$   ztCurrently, no algorithm has been implemented to handle general expressions containing multiple matrix distributions.)Zatomsr   ÚlenÚ
isinstanceÚNotImplementedErrorr!   Úpdf)r(   Úexprr&   Zrmsr"   r"   r#   Úcompute_density/   s    
zMatrixPSpace.compute_densityr"   ÚscipyNc                 C   s   | j | jj|||d�iS )zu
        Internal sample method

        Returns dictionary mapping RandomMatrixSymbol to realization value.
        )ÚlibraryÚseed)r2   r!   Úsample)r(   Úsizer;   r<   r"   r"   r#   r=   7   s    zMatrixPSpace.sample)r"   r:   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr!   r-   r/   r2   r3   r9   r=   r"   r"   r"   r#   r      s   


r   c                 C   sB   t tt|ƒƒ}||Ž }|j|Ž  |j}t| ||d |d ƒ}|jS )Nr   r$   )ÚlistÚmapr   ÚcheckÚ	dimensionr   r2   )r-   r   r&   ÚdistZdimZpspacer"   r"   r#   Úrv@   s    
rI   c                   @   s&   e Zd ZdZddd„Zedd„ ƒZdS )ÚSampleMatrixScipyz7Returns the sample from scipy of the given distributionNc                 C   s   |   |||¡S r,   )Ú_sample_scipy©r   rH   r>   r<   r"   r"   r#   r   K   s    zSampleMatrixScipy.__new__c           
         s°   ddl m‰  ddl}‡ fdd„‡ fdd„dœ}dd„ d	d„ dœ}| ¡ }|jj|krXdS |dksjt|tƒrz|jj	|d
�}n|}||jj |t
|ƒ|ƒ}	|	 |||jj |ƒ ¡S )zSample from SciPy.r   )ÚstatsNc                    s    ˆ j jt| jƒt| jtƒ|d�S )N)ZdfZscaler>   )ZwishartÚrvsÚintÚnr   Úscale_matrixÚfloat©rH   r>   Ú
rand_state©Zscipy_statsr"   r#   r)   U   s    
 ÿz1SampleMatrixScipy._sample_scipy.<locals>.<lambda>c                    s.   ˆ j jt| jtƒt| jtƒt| jtƒ||d�S )N)ZmeanÚrowcovÚcolcovr>   Zrandom_state)Zmatrix_normalrN   r   Úlocation_matrixrR   Úscale_matrix_1Úscale_matrix_2rS   rU   r"   r#   r)   W   s   


  ý©ÚWishartDistributionÚMatrixNormalDistributionc                 S   s   | j jS r,   ©rQ   Úshape©rH   r"   r"   r#   r)   ^   r*   c                 S   s   | j jS r,   ©rX   r_   r`   r"   r"   r#   r)   _   r*   ©r<   )r:   rM   ÚnumpyÚkeysÚ	__class__r?   r5   rO   ÚrandomÚdefault_rngr   Úreshape)
r   rH   r>   r<   rc   Zscipy_rv_mapÚsample_shapeÚ	dist_listrT   Úsampr"   rU   r#   rK   N   s     

ý
þzSampleMatrixScipy._sample_scipy)N)r?   r@   rA   rB   r   ÚclassmethodrK   r"   r"   r"   r#   rJ   I   s   
rJ   c                   @   s&   e Zd ZdZddd„Zedd„ ƒZdS )ÚSampleMatrixNumpyz7Returns the sample from numpy of the given distributionNc                 C   s   |   |||¡S r,   )Ú_sample_numpyrL   r"   r"   r#   r   s   s    zSampleMatrixNumpy.__new__c           
      C   s€   i }i }|  ¡ }|jj|kr dS ddl}|dks:t|tƒrJ|jj|d�}n|}||jj |t|ƒ|ƒ}	|	 	|||jj |ƒ ¡S )zSample from NumPy.Nr   rb   )
rd   re   r?   rc   r5   rO   rf   rg   r   rh   )
r   rH   r>   r<   Znumpy_rv_mapri   rj   rc   rT   rk   r"   r"   r#   rn   v   s    zSampleMatrixNumpy._sample_numpy)N)r?   r@   rA   rB   r   rl   rn   r"   r"   r"   r#   rm   o   s   
rm   c                   @   s&   e Zd ZdZddd„Zedd„ ƒZdS )ÚSampleMatrixPymcz6Returns the sample from pymc of the given distributionNc                 C   s   |   |||¡S r,   )Ú_sample_pymcrL   r"   r"   r#   r   ‘   s    zSampleMatrixPymc.__new__c           	   	      sä   zddl ‰ W n tk
r(   ddl‰ Y nX ‡ fdd„‡ fdd„dœ}dd„ dd„ d	œ}| ¡ }|jj|krndS ddl}| d
¡ |j	¡ ˆ  
¡ �4 ||jj |ƒ ˆ jt|ƒdd|ddd�d }W 5 Q R X | |||jj |ƒ ¡S )zSample from PyMC.r   Nc                    s0   ˆ j dt| jtƒt| jtƒt| jtƒ| jjd�S )NÚX)ÚmurV   rW   r_   )ÚMatrixNormalr   rX   rR   rY   rZ   r_   r`   ©Úpymcr"   r#   r)   �   s
   


üz/SampleMatrixPymc._sample_pymc.<locals>.<lambda>c                    s   ˆ j dt| jƒt| jtƒd�S )Nrq   )Únur   )ZWishartBartlettrO   rP   r   rQ   rR   r`   rt   r"   r#   r)   ¢   s    
ÿ)r]   r\   c                 S   s   | j jS r,   r^   r`   r"   r"   r#   r)   §   r*   c                 S   s   | j jS r,   ra   r`   r"   r"   r#   r)   ¨   r*   r[   ru   r$   F)ZdrawsZchainsZprogressbarZrandom_seedZreturn_inferencedataZcompute_convergence_checksrq   )ru   ÚImportErrorÚpymc3rd   re   r?   ÚloggingÚ	getLoggerÚsetLevelÚERRORZModelr=   r   rh   )	r   rH   r>   r<   Zpymc_rv_mapri   rj   ry   Úsampsr"   rt   r#   rp   ”   s&    

úþ
(zSampleMatrixPymc._sample_pymc)N)r?   r@   rA   rB   r   rl   rp   r"   r"   r"   r#   ro   Ž   s   
ro   )r:   rx   ru   rc   c                   @   s6   e Zd ZdZdd„ Zedd„ ƒZdd„ Zddd„Zd
S )ÚMatrixDistributionz1
    Abstract class for Matrix Distribution.
    c                 G   s   dd„ |D ƒ}t j| f|žŽ S )Nc                 S   s&   g | ]}t |tƒrt|ƒnt|ƒ‘qS r"   )r5   rD   r
   r	   )Ú.0Úargr"   r"   r#   Ú
<listcomp>Æ   s   ÿz.MatrixDistribution.__new__.<locals>.<listcomp>)r   r   )r   r&   r"   r"   r#   r   Å   s    ÿzMatrixDistribution.__new__c                  G   s   d S r,   r"   r%   r"   r"   r#   rF   Ê   s    zMatrixDistribution.checkc                 C   s   t |tƒrt|ƒ}|  |¡S r,   )r5   rD   r
   r7   )r(   r8   r"   r"   r#   Ú__call__Î   s    
zMatrixDistribution.__call__r"   r:   Nc                 C   sl   ddddg}||kr$t dt|ƒ ƒ‚t|ƒs8td| ƒ‚t| | ||ƒ}|dk	rT|S t d| jj|f ƒ‚dS )	zo
        Internal sample method

        Returns dictionary mapping RandomSymbol to realization value.
        r:   rc   rx   ru   z&Sampling from %s is not supported yet.zFailed to import %sNz4Sampling for %s is not currently implemented from %s)r6   Ústrr   r   Ú_get_sample_class_matrixrvre   r?   )r(   r>   r;   r<   Ú	librariesr}   r"   r"   r#   r=   Ó   s    ÿ
ÿÿzMatrixDistribution.sample)r"   r:   N)	r?   r@   rA   rB   r   ÚstaticmethodrF   r‚   r=   r"   r"   r"   r#   r~   Á   s   
r~   c                   @   s<   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZdd	„ Z	d
S )ÚMatrixGammaDistribution©ÚalphaÚbetarQ   c                 C   s>   t |tƒst|jdƒ t|jdƒ t| jdƒ t|jdƒ d S )Nú+The shape matrix must be positive definite.úShould be square matrixú#Shape parameter should be positive.z#Scale parameter should be positive.©r5   r   r   Úis_positive_definiteÚ	is_squareÚis_positiverˆ   r"   r"   r#   rF   õ   s
    
zMatrixGammaDistribution.checkc                 C   s   | j jd }t||tjƒS r+   ©rQ   r_   r   r   ÚReals©r(   Úkr"   r"   r#   r.   ÿ   s    zMatrixGammaDistribution.setc                 C   s   | j jS r,   r^   r'   r"   r"   r#   rG     s    z!MatrixGammaDistribution.dimensionc           
      C   sº   | j | j| j  }}}|jd }t|tƒr2t|ƒ}t|ttfƒsPt	dt
|ƒ ƒ‚t|ƒ | | }tt|ƒƒ|||  t||ƒ  }t|ƒ|  }t|ƒ|t|d ƒd   }	|| |	 S )Nr   ú4%s should be an isinstance of Matrix or MatrixSymbolr$   r0   )r‰   rŠ   rQ   r_   r5   rD   r
   r   r   r   rƒ   r   r   r   r   r   r   )
r(   Úxr‰   rŠ   rQ   ÚpÚsigma_inv_xÚterm1Úterm2Úterm3r"   r"   r#   r7     s    

ÿ"zMatrixGammaDistribution.pdfN©
r?   r@   rA   Z	_argnamesr†   rF   rC   r.   rG   r7   r"   r"   r"   r#   r‡   ñ   s   
	

r‡   c                 C   s$   t |tƒrt|ƒ}t| t|||fƒS )a  
    Creates a random variable with Matrix Gamma Distribution.

    The density of the said distribution can be found at [1].

    Parameters
    ==========

    alpha: Positive Real number
        Shape Parameter
    beta: Positive Real number
        Scale Parameter
    scale_matrix: Positive definite real square matrix
        Scale Matrix

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import density, MatrixGamma
    >>> from sympy import MatrixSymbol, symbols
    >>> a, b = symbols('a b', positive=True)
    >>> M = MatrixGamma('M', a, b, [[2, 1], [1, 2]])
    >>> X = MatrixSymbol('X', 2, 2)
    >>> density(M)(X).doit()
    exp(Trace(Matrix([
    [-2/3,  1/3],
    [ 1/3, -2/3]])*X)/b)*Determinant(X)**(a - 3/2)/(3**a*sqrt(pi)*b**(2*a)*gamma(a)*gamma(a - 1/2))
    >>> density(M)([[1, 0], [0, 1]]).doit()
    exp(-4/(3*b))/(3**a*sqrt(pi)*b**(2*a)*gamma(a)*gamma(a - 1/2))


    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Matrix_gamma_distribution

    )r5   rD   r
   rI   r‡   )r-   r‰   rŠ   rQ   r"   r"   r#   ÚMatrixGamma  s    +
rž   c                   @   s<   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZdd	„ Z	d
S )r\   ©rP   rQ   c                 C   s2   t |tƒst|jdƒ t|jdƒ t| jdƒ d S )Nr‹   rŒ   r�   rŽ   rŸ   r"   r"   r#   rF   L  s    
zWishartDistribution.checkc                 C   s   | j jd }t||tjƒS r+   r’   r”   r"   r"   r#   r.   U  s    zWishartDistribution.setc                 C   s   | j jS r,   r^   r'   r"   r"   r#   rG   Z  s    zWishartDistribution.dimensionc           	      C   sÎ   | j | j }}|jd }t|tƒr*t|ƒ}t|ttfƒsHtdt	|ƒ ƒ‚t
|ƒ | tdƒ }tt|ƒƒd|| tdƒ  t|tdƒ |ƒ  }t|ƒ| tdƒ  }t|ƒt|| d ƒd  }|| | S )Nr   r–   r0   r$   )rP   rQ   r_   r5   rD   r
   r   r   r   rƒ   r   r   r   r   r   r   )	r(   r—   rP   rQ   r˜   r™   rš   r›   rœ   r"   r"   r#   r7   ^  s    

ÿ2zWishartDistribution.pdfNr�   r"   r"   r"   r#   r\   H  s   


r\   c                 C   s"   t |tƒrt|ƒ}t| t||fƒS )aÉ  
    Creates a random variable with Wishart Distribution.

    The density of the said distribution can be found at [1].

    Parameters
    ==========

    n: Positive Real number
        Represents degrees of freedom
    scale_matrix: Positive definite real square matrix
        Scale Matrix

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import density, Wishart
    >>> from sympy import MatrixSymbol, symbols
    >>> n = symbols('n', positive=True)
    >>> W = Wishart('W', n, [[2, 1], [1, 2]])
    >>> X = MatrixSymbol('X', 2, 2)
    >>> density(W)(X).doit()
    exp(Trace(Matrix([
    [-1/3,  1/6],
    [ 1/6, -1/3]])*X))*Determinant(X)**(n/2 - 3/2)/(2**n*3**(n/2)*sqrt(pi)*gamma(n/2)*gamma(n/2 - 1/2))
    >>> density(W)([[1, 0], [0, 1]]).doit()
    exp(-2/3)/(2**n*3**(n/2)*sqrt(pi)*gamma(n/2)*gamma(n/2 - 1/2))

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Wishart_distribution

    )r5   rD   r
   rI   r\   )r-   rP   rQ   r"   r"   r#   ÚWishartl  s    (
r    c                   @   s<   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZdd	„ Z	d
S )r]   )rX   rY   rZ   c                 C   s¤   t |tƒst|jdƒ t |tƒs,t|jdƒ t|jdƒ t|jdƒ | jd }| jd }t|jd |kdt|ƒt|ƒf ƒ t|jd |kdt|ƒt|ƒf ƒ d S )Nr‹   ú)Scale matrix 1 should be be square matrixú)Scale matrix 2 should be be square matrixr   r$   ú)Scale matrix 1 should be of shape %s x %sú)Scale matrix 2 should be of shape %s x %s)r5   r   r   r�   r�   r_   rƒ   )rX   rY   rZ   rP   r˜   r"   r"   r#   rF   Ÿ  s    



ÿÿzMatrixNormalDistribution.checkc                 C   s   | j j\}}t||tjƒS r,   ©rX   r_   r   r   r“   ©r(   rP   r˜   r"   r"   r#   r.   ²  s    zMatrixNormalDistribution.setc                 C   s   | j jS r,   ra   r'   r"   r"   r#   rG   ·  s    z"MatrixNormalDistribution.dimensionc           
      C   sÒ   | j | j| j  }}}|j\}}t|tƒr2t|ƒ}t|ttfƒsPt	dt
|ƒ ƒ‚t|ƒt|| ƒ t|ƒ ||  }tt|ƒ tdƒ ƒ}dt t|| ƒd  t|ƒt|ƒd   t|ƒt|ƒd   }	||	 S )Nr–   r0   )rX   rY   rZ   r_   r5   rD   r
   r   r   r   rƒ   r   r   r   r   r   r   r   )
r(   r—   ÚMÚUÚVrP   r˜   rš   ÚnumZdenr"   r"   r#   r7   »  s    

ÿ$@zMatrixNormalDistribution.pdfNr�   r"   r"   r"   r#   r]   ›  s   


r]   c                 C   sL   t |tƒrt|ƒ}t |tƒr$t|ƒ}t |tƒr6t|ƒ}|||f}t| t|ƒS )a·  
    Creates a random variable with Matrix Normal Distribution.

    The density of the said distribution can be found at [1].

    Parameters
    ==========

    location_matrix: Real ``n x p`` matrix
        Represents degrees of freedom
    scale_matrix_1: Positive definite matrix
        Scale Matrix of shape ``n x n``
    scale_matrix_2: Positive definite matrix
        Scale Matrix of shape ``p x p``

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy import MatrixSymbol
    >>> from sympy.stats import density, MatrixNormal
    >>> M = MatrixNormal('M', [[1, 2]], [1], [[1, 0], [0, 1]])
    >>> X = MatrixSymbol('X', 1, 2)
    >>> density(M)(X).doit()
    exp(-Trace((Matrix([
    [-1],
    [-2]]) + X.T)*(Matrix([[-1, -2]]) + X))/2)/(2*pi)
    >>> density(M)([[3, 4]]).doit()
    exp(-4)/(2*pi)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Matrix_normal_distribution

    )r5   rD   r
   rI   r]   )r-   rX   rY   rZ   r&   r"   r"   r#   rs   È  s    )



rs   c                   @   s<   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZdd	„ Z	d
S )ÚMatrixStudentTDistribution)rv   rX   rY   rZ   c                 C   sÄ   t |tƒst|jdkdƒ t |tƒs4t|jdkdƒ t|jdkdƒ t|jdkdƒ |jd }|jd }t|jd |kdt|ƒt|ƒf ƒ t|jd |kdt|ƒt|ƒf ƒ t| jdkd	ƒ d S )
NFr‹   r¡   r¢   r   r$   r£   r¤   z#Degrees of freedom must be positive)r5   r   r   r�   r�   r_   rƒ   r‘   )rv   rX   rY   rZ   rP   r˜   r"   r"   r#   rF     s    



ÿÿz MatrixStudentTDistribution.checkc                 C   s   | j j\}}t||tjƒS r,   r¥   r¦   r"   r"   r#   r.     s    zMatrixStudentTDistribution.setc                 C   s   | j jS r,   ra   r'   r"   r"   r#   rG     s    z$MatrixStudentTDistribution.dimensionc           
      C   s  ddl m} t|tƒrt|ƒ}t|ttfƒs<tdt|ƒ ƒ‚| j	| j
| j| jf\}}}}|j\}}t|| | d d |ƒt|ƒ| d   t|ƒ| d   t|| d  t|| d d |ƒ  }	|	t||ƒt|ƒ||  t|ƒ t|| ƒ  ƒ|| | d  d   S )Nr   )Úeyer–   r$   r0   )Zsympy.matrices.denser¬   r5   rD   r
   r   r   r   rƒ   rv   rX   rY   rZ   r_   r   r   r   r   r   )
r(   r—   r¬   rv   r§   ÚOmegaÚSigmarP   r˜   ÚKr"   r"   r#   r7     s    
ÿ
<$ÿ0ÿzMatrixStudentTDistribution.pdfNr�   r"   r"   r"   r#   r«   ý  s   


r«   c                 C   sN   t |tƒrt|ƒ}t |tƒr$t|ƒ}t |tƒr6t|ƒ}||||f}t| t|ƒS )a˜  
    Creates a random variable with Matrix Gamma Distribution.

    The density of the said distribution can be found at [1].

    Parameters
    ==========

    nu: Positive Real number
        degrees of freedom
    location_matrix: Positive definite real square matrix
        Location Matrix of shape ``n x p``
    scale_matrix_1: Positive definite real square matrix
        Scale Matrix of shape ``p x p``
    scale_matrix_2: Positive definite real square matrix
        Scale Matrix of shape ``n x n``

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy import MatrixSymbol,symbols
    >>> from sympy.stats import density, MatrixStudentT
    >>> v = symbols('v',positive=True)
    >>> M = MatrixStudentT('M', v, [[1, 2]], [[1, 0], [0, 1]], [1])
    >>> X = MatrixSymbol('X', 1, 2)
    >>> density(M)(X)
    gamma(v/2 + 1)*Determinant((Matrix([[-1, -2]]) + X)*(Matrix([
    [-1],
    [-2]]) + X.T) + Matrix([[1]]))**(-v/2 - 1)/(pi**1.0*gamma(v/2)*Determinant(Matrix([[1]]))**1.0*Determinant(Matrix([
    [1, 0],
    [0, 1]]))**0.5)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Matrix_t-distribution

    )r5   rD   r
   rI   r«   )r-   rv   rX   rY   rZ   r&   r"   r"   r#   ÚMatrixStudentT/  s    ,


r°   N)2Úmathr   Zsympy.core.basicr   Zsympy.core.numbersr   Zsympy.core.singletonr   Z&sympy.functions.elementary.exponentialr   Z'sympy.functions.special.gamma_functionsr   Zsympy.core.sympifyr   r	   Zsympy.matricesr
   r   r   r   r   r   r   r   r   Zsympy.stats.rvr   r   r   r   r   r   r   Zsympy.externalr   r   rI   rJ   rm   ro   r„   r~   r‡   rž   r\   r    r]   rs   r«   r°   r"   r"   r"   r#   Ú<module>   s8   ,$,	&)ü0%2$/-52