o
    §ƒðjÁ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ÚdistributionÚdim_nÚdim_m© r$   ú¢/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/stats/matrix_distributions.pyr      s
   zMatrixPSpace.__new__c                 C   ó
   | j d S )Né   ©Úargs©Úselfr$   r$   r%   Ú<lambda>    ó   
 zMatrixPSpace.<lambda>c                 C   r&   ©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   r0   r)   r*   r$   r$   r%   Úvalue'   s   zMatrixPSpace.valuec                 C   s   | j hS r/   )r5   r*   r$   r$   r%   Úvalues+   ó   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.)Úatomsr   ÚlenÚ
isinstanceÚNotImplementedErrorr!   Úpdf)r+   Úexprr)   Ú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)r5   r!   Úsample)r+   ÚsizerA   rB   r$   r$   r%   rC   7   s   zMatrixPSpace.sample©r$   r@   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr!   r0   r2   r5   r6   r?   rC   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   r5   )r0   r   r)   ÚdistÚdimÚpspacer$   r$   r%   Úrv@   s   
rR   c                   @   ó&   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 r/   )Ú_sample_scipy©r   rO   rD   rB   r$   r$   r%   r   K   ó   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|vr,dS |du s5t|tƒr=|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)ÚdfÚscalerD   )ÚwishartÚrvsÚintÚnr   Úscale_matrixÚfloat©rO   rD   Ú
rand_state©Ú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)ÚmeanÚrowcovÚcolcovrD   Úrandom_state)Úmatrix_normalr]   r   Úlocation_matrixra   Úscale_matrix_1Úscale_matrix_2rb   rd   r$   r%   r,   W   s
    

ý©ÚWishartDistributionÚMatrixNormalDistributionc                 S   ó   | j jS r/   ©r`   Úshape©rO   r$   r$   r%   r,   ^   ó    c                 S   rq   r/   ©rk   rs   rt   r$   r$   r%   r,   _   ru   ©rB   )r@   rY   ÚnumpyÚkeysÚ	__class__rF   r:   r^   ÚrandomÚdefault_rngr   Úreshape)
r   rO   rD   rB   rx   Úscipy_rv_mapÚsample_shapeÚ	dist_listrc   Úsampr$   rd   r%   rV   N   s    

ý
þzSampleMatrixScipy._sample_scipyr/   )rF   rG   rH   rI   r   ÚclassmethodrV   r$   r$   r$   r%   rT   I   s
    
rT   c                   @   rS   )ÚSampleMatrixNumpyz7Returns the sample from numpy of the given distributionNc                 C   rU   r/   )Ú_sample_numpyrW   r$   r$   r%   r   s   rX   zSampleMatrixNumpy.__new__c           
      C   s€   i }i }|  ¡ }|jj|vrdS ddl}|du st|tƒr%|jj|d�}n|}||jj |t|ƒ|ƒ}	|	 	|||jj |ƒ ¡S )zSample from NumPy.Nr   rw   )
ry   rz   rF   rx   r:   r^   r{   r|   r   r}   )
r   rO   rD   rB   Únumpy_rv_mapr   r€   rx   rc   r�   r$   r$   r%   r„   v   s   zSampleMatrixNumpy._sample_numpyr/   )rF   rG   rH   rI   r   r‚   r„   r$   r$   r$   r%   rƒ   o   s
    
rƒ   c                   @   rS   )ÚSampleMatrixPymcz6Returns the sample from pymc of the given distributionNc                 C   rU   r/   )Ú_sample_pymcrW   r$   r$   r%   r   ‘   rX   zSampleMatrixPymc.__new__c           	   	      sö   zddl ‰ W n ty   ddl‰ Y nw ‡ fdd„‡ fdd„dœ}dd„ dd„ d	œ}| ¡ }|jj|vr6dS ddl}| d
¡ |j	¡ ˆ  
¡ � ||jj |ƒ ˆ jt|ƒdd|ddd�d }W d  ƒ n1 siw   Y  | |||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)Úmurg   rh   rs   )ÚMatrixNormalr   rk   ra   rl   rm   rs   rt   ©Úpymcr$   r%   r,   �   s    


üz/SampleMatrixPymc._sample_pymc.<locals>.<lambda>c                    s   ˆ j dt| jƒt| jtƒd�S )Nrˆ   )Únur   )ÚWishartBartlettr^   r_   r   r`   ra   rt   r‹   r$   r%   r,   ¢   s    ÿ)rp   ro   c                 S   rq   r/   rr   rt   r$   r$   r%   r,   §   ru   c                 S   rq   r/   rv   rt   r$   r$   r%   r,   ¨   ru   rn   rŒ   r'   F)ÚdrawsÚchainsÚprogressbarÚrandom_seedÚreturn_inferencedataÚcompute_convergence_checksrˆ   )rŒ   ÚImportErrorÚpymc3ry   rz   rF   ÚloggingÚ	getLoggerÚsetLevelÚERRORÚModelrC   r   r}   )	r   rO   rD   rB   Úpymc_rv_mapr   r€   r—   Úsampsr$   r‹   r%   r‡   ”   s*   ÿ

úþ
 þzSampleMatrixPymc._sample_pymcr/   )rF   rG   rH   rI   r   r‚   r‡   r$   r$   r$   r%   r†   Ž   s
    
r†   )r@   r–   rŒ   rx   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| g|¢R Ž S )Nc                 S   s&   g | ]}t |tƒrt|ƒnt|ƒ‘qS r$   )r:   rK   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%   rM   Ê   s   zMatrixDistribution.checkc                 C   s   t |tƒr	t|ƒ}|  |¡S r/   )r:   rK   r
   r<   )r+   r=   r$   r$   r%   Ú__call__Î   s   

zMatrixDistribution.__call__r$   r@   Nc                 C   sd   g d¢}||vrt dt|ƒ ƒ‚t|ƒstd| ƒ‚t| | ||ƒ}|dur(|S t d| jj|f ƒ‚)zo
        Internal sample method

        Returns dictionary mapping RandomSymbol to realization value.
        )r@   rx   r–   rŒ   z&Sampling from %s is not supported yet.zFailed to import %sNz4Sampling for %s is not currently implemented from %s)r;   Ústrr   r   Ú_get_sample_class_matrixrvrz   rF   )r+   rD   rA   rB   Ú	librariesr�   r$   r$   r%   rC   Ó   s   ÿ
ÿÿzMatrixDistribution.samplerE   )	rF   rG   rH   rI   r   ÚstaticmethodrM   r¢   rC   r$   r$   r$   r%   rž   Á   s    
rž   c                   @   ó<   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Úbetar`   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.©r:   r   r   Úis_positive_definiteÚ	is_squareÚis_positiver©   r$   r$   r%   rM   õ   s
   
zMatrixGammaDistribution.checkc                 C   ó   | j jd }t||tjƒS r.   ©r`   rs   r   r   ÚReals©r+   Úkr$   r$   r%   r1   ÿ   ó   zMatrixGammaDistribution.setc                 C   rq   r/   rr   r*   r$   r$   r%   rN     r7   z!MatrixGammaDistribution.dimensionc           
      C   sº   | j | j| j}}}|jd }t|tƒrt|ƒ}t|ttfƒs(t	dt
|ƒ ƒ‚t|ƒ | | }tt|ƒƒ|||  t||ƒ  }t|ƒ|  }t|ƒ|t|d ƒd   }	|| |	 S )Nr   ú4%s should be an isinstance of Matrix or MatrixSymbolr'   r3   )rª   r«   r`   rs   r:   rK   r
   r   r   r   r£   r   r   r   r   r   r   )
r+   Úxrª   r«   r`   ÚpÚsigma_inv_xÚterm1Úterm2Úterm3r$   r$   r%   r<     s   

ÿ"zMatrixGammaDistribution.pdfN©
rF   rG   rH   Ú	_argnamesr¦   rM   rJ   r1   rN   r<   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

    )r:   rK   r
   rR   r¨   )r0   rª   r«   r`   r$   r$   r%   ÚMatrixGamma  s   
+rÂ   c                   @   r§   )ro   ©r_   r`   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%   rM   L  s   
zWishartDistribution.checkc                 C   r³   r.   r´   r¶   r$   r$   r%   r1   U  r¸   zWishartDistribution.setc                 C   rq   r/   rr   r*   r$   r$   r%   rN   Z  r7   zWishartDistribution.dimensionc           	      C   sÎ   | j | j}}|jd }t|tƒrt|ƒ}t|ttfƒs$tdt	|ƒ ƒ‚t
|ƒ | tdƒ }tt|ƒƒd|| tdƒ  t|tdƒ |ƒ  }t|ƒ| tdƒ  }t|ƒt|| d ƒd  }|| | S )Nr   r¹   r3   r'   )r_   r`   rs   r:   rK   r
   r   r   r   r£   r   r   r   r   r   r   )	r+   rº   r_   r`   r»   r¼   r½   r¾   r¿   r$   r$   r%   r<   ^  s   

ÿ2zWishartDistribution.pdfNrÀ   r$   r$   r$   r%   ro   H  s    


ro   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

    )r:   rK   r
   rR   ro   )r0   r_   r`   r$   r$   r%   ÚWishartl  s   
(rÄ   c                   @   r§   )rp   )rk   rl   rm   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)r:   r   r   r°   r±   rs   r£   )rk   rl   rm   r_   r»   r$   r$   r%   rM   Ÿ  s   



ÿ
ÿzMatrixNormalDistribution.checkc                 C   ó   | j j\}}t||tjƒS r/   ©rk   rs   r   r   rµ   ©r+   r_   r»   r$   r$   r%   r1   ²  r¸   zMatrixNormalDistribution.setc                 C   rq   r/   rv   r*   r$   r$   r%   rN   ·  r7   z"MatrixNormalDistribution.dimensionc           
      C   sÒ   | j | j| j}}}|j\}}t|tƒrt|ƒ}t|ttfƒs(t	dt
|ƒ ƒ‚t|ƒt|| ƒ t|ƒ ||  }tt|ƒ tdƒ ƒ}dt t|| ƒd  t|ƒt|ƒd   t|ƒt|ƒd   }	||	 S )Nr¹   r3   )rk   rl   rm   rs   r:   rK   r
   r   r   r   r£   r   r   r   r   r   r   r   )
r+   rº   ÚMÚUÚVr_   r»   r½   ÚnumÚdenr$   r$   r%   r<   »  s   

ÿ$@zMatrixNormalDistribution.pdfNrÀ   r$   r$   r$   r%   rp   ›  s    


rp   c                 C   sL   t |tƒr	t|ƒ}t |tƒrt|ƒ}t |tƒrt|ƒ}|||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

    )r:   rK   r
   rR   rp   )r0   rk   rl   rm   r)   r$   r$   r%   rŠ   È  s   
)


rŠ   c                   @   r§   )ÚMatrixStudentTDistribution)r�   rk   rl   rm   c                 C   sÄ   t |tƒst|jdkdƒ t |tƒst|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)r:   r   r   r°   r±   rs   r£   r²   )r�   rk   rl   rm   r_   r»   r$   r$   r%   rM     s   



ÿÿz MatrixStudentTDistribution.checkc                 C   rÉ   r/   rÊ   rË   r$   r$   r%   r1     r¸   zMatrixStudentTDistribution.setc                 C   rq   r/   rv   r*   r$   r$   r%   rN     r7   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'   r3   )Úsympy.matrices.denserÒ   r:   rK   r
   r   r   r   r£   r�   rk   rl   rm   rs   r   r   r   r   r   )
r+   rº   rÒ   r�   rÌ   ÚOmegaÚSigmar_   r»   ÚKr$   r$   r%   r<     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ƒrt|ƒ}||||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

    )r:   rK   r
   rR   rÑ   )r0   r�   rk   rl   rm   r)   r$   r$   r%   ÚMatrixStudentT/  s   
,

r×   N)2Úmathr   Úsympy.core.basicr   Úsympy.core.numbersr   Úsympy.core.singletonr   Ú&sympy.functions.elementary.exponentialr   Ú'sympy.functions.special.gamma_functionsr   Úsympy.core.sympifyr   r	   Úsympy.matricesr
   r   r   r   r   r   r   r   r   Úsympy.stats.rvr   r   r   r   r   r   r   Úsympy.externalr   r   rR   rT   rƒ   r†   r¤   rž   r¨   rÂ   ro   rÄ   rp   rŠ   rÑ   r×   r$   r$   r$   r%   Ú<module>   s:    ,$,	&)ü0%2$/-52