o
    §ƒðj[>  ã                   @   sž  d 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 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 ddlmZmZ ddlmZmZmZ ddl m!Z!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. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 G dd„ de'ƒZ5G dd„ dƒZ6G dd„ dƒZ7G dd„ dƒZ8e6e8e8e7dœZ9G dd„ de)e(ƒZ:G d d!„ d!e+ƒZ;G d"d#„ d#e)ƒZ<d$S )%zq
Joint Random Variables Module

See Also
========
sympy.stats.rv
sympy.stats.frv
sympy.stats.crv
sympy.stats.drv
é    )Úprod)ÚBasic)ÚLambda)ÚS)ÚDummyÚSymbol)Úsympify)Ú
ProductSet©ÚIndexed)ÚProduct)ÚSumÚ	summation)ÚTuple)ÚIntegralÚ	integrate)ÚImmutableMatrixÚmatrix2numpyÚ
list2numpy)ÚSingleContinuousDistributionÚSingleContinuousPSpace)ÚSingleDiscreteDistributionÚSingleDiscretePSpace)ÚProductPSpaceÚNamedArgsMixinÚDistributionÚProductDomainÚRandomSymbolÚrandom_symbolsÚSingleDomainÚ_symbol_converter)Úiterable)Ú
filldedent)Úimport_modulec                   @   s¨   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
edd„ ƒZedd„ ƒZdd„ Zdd„ Zd$dd„Zdd„ Zdd„ Zd%d d!„Zd"d#„ ZdS )&ÚJointPSpacezt
    Represents a joint probability space. Represented using symbols for
    each component and a distribution.
    c                 C   s>   t |tƒr
t||ƒS t |tƒrt||ƒS t|ƒ}t | ||¡S ©N)Ú
isinstancer   r   r   r   r    r   Ú__new__)ÚclsÚsymÚdist© r+   ú–/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/stats/joint_rv.pyr'   )   s   



zJointPSpace.__new__c                 C   s   | j jS r%   )ÚdomainÚset©Úselfr+   r+   r,   r.   1   s   zJointPSpace.setc                 C   ó
   | j d S )Nr   ©Úargsr/   r+   r+   r,   Úsymbol5   ó   
zJointPSpace.symbolc                 C   r1   ©Né   r2   r/   r+   r+   r,   Údistribution9   r5   zJointPSpace.distributionc                 C   s   t | j| ƒS r%   )ÚJointRandomSymbolr4   r/   r+   r+   r,   Úvalue=   ó   zJointPSpace.valuec                 C   s>   | j j}t|tƒrtt|jƒƒS t|tƒr|jd d S tj	S )Nr   éÿÿÿÿ)
r8   r.   r&   r	   r   Úlenr3   r   ÚlimitsÚOne)r0   Ú_setr+   r+   r,   Úcomponent_countA   s   

zJointPSpace.component_countc                    s"   ‡ fdd„t ˆ jƒD ƒ}ˆ j|Ž S )Nc                    ó   g | ]}t ˆ j|ƒ‘qS r+   ©r   r4   ©Ú.0Úir/   r+   r,   Ú
<listcomp>L   ó    z#JointPSpace.pdf.<locals>.<listcomp>)ÚrangerA   r8   ©r0   r)   r+   r/   r,   ÚpdfJ   s   
zJointPSpace.pdfc                 C   s0   t | jƒ}|st| j| jjƒS tdd„ |D ƒŽ S )Nc                 S   ó   g | ]}|j j‘qS r+   )Úpspacer-   ©rE   Úrvr+   r+   r,   rG   T   ó    z&JointPSpace.domain.<locals>.<listcomp>)r   r8   r   r4   r.   r   ©r0   Úrvsr+   r+   r,   r-   O   s   
zJointPSpace.domainc                 C   s   | j j| S r%   )r.   r3   )r0   Úindexr+   r+   r,   Úcomponent_domainV   s   zJointPSpace.component_domainc           
         s  ˆ j }| t¡rtdƒ‚‡ fdd„t|ƒD ƒ}dd„ |D ƒ}tt||ƒƒ}t‡ fdd„|D ƒƒ‰‡fdd„|D ƒ}d}t|ƒD ]}||vr]||  ˆ j	j
j| ¡ t|| ƒ||< |d	7 }q?ˆ j	jrqtˆtˆ j	|Ž g|¢R Ž ƒ}	nˆ j	jrƒtˆtˆ j	|Ž g|¢R Ž ƒ}	|	 |¡S )
Nz_Marginal distributions cannot be computed for symbolic dimensions. It is a work under progress.c                    rB   r+   rC   rD   r/   r+   r,   rG   ^   rH   z5JointPSpace.marginal_distribution.<locals>.<listcomp>c                 S   s   g | ]}t t|ƒƒ‘qS r+   )r   ÚstrrD   r+   r+   r,   rG   _   rH   c                 3   s$   � | ]}t ttˆ j|ƒƒƒV  qd S r%   )r   rU   r   r4   rD   r/   r+   r,   Ú	<genexpr>a   s   €" z4JointPSpace.marginal_distribution.<locals>.<genexpr>c                    s   g | ]	}|ˆ vr|g‘qS r+   r+   rD   )r)   r+   r,   rG   b   ó    r   r7   )rA   Úatomsr   Ú
ValueErrorrI   ÚdictÚzipÚtupleÚappendr8   r.   r3   Úis_Continuousr   r   Úis_Discreter   Úxreplace)
r0   ÚindicesÚcountÚorigÚall_symsÚreplace_dictr>   rS   rF   Úfr+   rJ   r,   Úmarginal_distributionY   s(   
€
z!JointPSpace.marginal_distributionNFc                    sØ   t ‡fdd„tˆjƒD ƒƒ}ˆ p|‰ t‡ fdd„|D ƒƒs|S |ˆj }ˆ D ]%}t|tƒr>| |tt|j	ƒ|j
d ƒi¡}q&t|tƒrK| ||ji¡}q&ˆjt|ƒv rYttdƒƒ‚t ‡fdd„|D ƒƒ}t|g|¢R Ž S )Nc                 3   s   � | ]}ˆ j | V  qd S r%   )r:   rD   r/   r+   r,   rV   p   ó   € z2JointPSpace.compute_expectation.<locals>.<genexpr>c                 3   s   � | ]}|ˆ v V  qd S r%   r+   rD   ©rR   r+   r,   rV   r   s   € r7   zq
            Expectations of expression with unindexed joint random symbols
            cannot be calculated yet.c                 3   s:   � | ]}t t|jƒ|jd  ƒˆ jjj|jd   fV  qdS )r7   N)r   rU   Úbaser3   r8   r.   rN   r/   r+   r,   rV   ~   s   € ÿÿ)r\   rI   rA   ÚanyrK   r&   r   r`   rU   rj   r3   r   r4   r:   r   ÚNotImplementedErrorr"   r   )r0   ÚexprrR   ÚevaluateÚkwargsÚsymsrO   r>   r+   )rR   r0   r,   Úcompute_expectationo   s"   

"
€ÿzJointPSpace.compute_expectationc                 C   ó   t ƒ ‚r%   ©rl   ©r0   Ú	conditionr+   r+   r,   Úwhere‚   ó   zJointPSpace.wherec                 C   rr   r%   rs   )r0   rm   r+   r+   r,   Úcompute_density…   rw   zJointPSpace.compute_densityr+   Úscipyc                 C   s   t | j| ƒ| jj|||d�iS )zo
        Internal sample method

        Returns dictionary mapping RandomSymbol to realization value.
        )ÚlibraryÚseed)r   r4   r8   Úsample)r0   Úsizerz   r{   r+   r+   r,   r|   ˆ   s   ÿzJointPSpace.samplec                 C   rr   r%   rs   rt   r+   r+   r,   Úprobability‘   rw   zJointPSpace.probability)NF©r+   ry   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   Úpropertyr.   r4   r8   r:   rA   rK   r-   rT   rg   rq   rv   rx   r|   r~   r+   r+   r+   r,   r$   $   s0    








	r$   c                   @   ó&   e Zd ZdZddd„Zedd„ ƒZdS )ÚSampleJointScipyz7Returns the sample from scipy of the given distributionNc                 C   ó   |   |||¡S r%   )Ú_sample_scipy©r(   r*   r}   r{   r+   r+   r,   r'   —   ó   zSampleJointScipy.__new__c           	         sÀ   ddl }|du st|tƒr|jj|d�‰ n|‰ ddlm‰ ‡ ‡fdd„‡ ‡fdd„‡ ‡fdd„d	œ}d
d„ dd„ dd„ d	œ}| ¡ }|jj	|vrJdS ||jj	 ||ƒ}| 
|||jj	 |ƒ ¡S )zSample from SciPy.r   N©r{   )Ústatsc                    s$   ˆj jt| jƒ ¡ t| jƒ|ˆ d�S )N)ÚmeanÚcovr}   Úrandom_state)Úmultivariate_normalrR   r   ÚmuÚflattenÚsigma©r*   r}   ©Ú
rand_stateÚscipy_statsr+   r,   Ú<lambda>¥   s    þz0SampleJointScipy._sample_scipy.<locals>.<lambda>c                    s   ˆj jt| jtƒ ¡ |ˆ d�S )N)Úalphar}   r�   )Ú	dirichletrR   r   r™   Úfloatr’   r”   r•   r+   r,   r˜   ¨   s    ÿc                    s&   ˆj jt| jƒt| jtƒ ¡ |ˆ d�S )N)ÚnÚpr}   r�   )ÚmultinomialrR   Úintrœ   r   r�   r›   r’   r”   r•   r+   r,   r˜   ª   s    ÿ©ÚMultivariateNormalDistributionÚMultivariateBetaDistributionÚMultinomialDistributionc                 S   ó   t | jƒ ¡ jS r%   ©r   r‘   r’   Úshape©r*   r+   r+   r,   r˜   ¯   ó    c                 S   r¤   r%   ©r   r™   r’   r¦   r§   r+   r+   r,   r˜   °   r¨   c                 S   r¤   r%   ©r   r�   r’   r¦   r§   r+   r+   r,   r˜   ±   r¨   )Únumpyr&   rŸ   ÚrandomÚdefault_rngry   rŒ   ÚkeysÚ	__class__r€   Úreshape)	r(   r*   r}   r{   r«   Úscipy_rv_mapÚsample_shapeÚ	dist_listÚsamplesr+   r•   r,   rˆ   š   s$   úýzSampleJointScipy._sample_scipyr%   )r€   r�   r‚   rƒ   r'   Úclassmethodrˆ   r+   r+   r+   r,   r†   •   s
    
r†   c                   @   r…   )ÚSampleJointNumpyz7Returns the sample from numpy of the given distributionNc                 C   r‡   r%   )Ú_sample_numpyr‰   r+   r+   r,   r'   ¿   rŠ   zSampleJointNumpy.__new__c           	         s²   ddl }|du st|tƒr|jj|d�‰ n|‰ ‡ fdd„‡ fdd„‡ fdd„dœ}d	d„ d
d„ dd„ dœ}| ¡ }|jj|vrAdS ||jj |t|ƒƒ}| 	|||jj |ƒ ¡S )zSample from NumPy.r   Nr‹   c                    s$   ˆ j t| jtƒ ¡ t| jtƒ|d�S )N)r�   rŽ   r}   )r�   r   r‘   r›   r’   r“   r”   ©r–   r+   r,   r˜   Ì   s    þz0SampleJointNumpy._sample_numpy.<locals>.<lambda>c                    s   ˆ j t| jtƒ ¡ |d�S )N)r™   r}   )rš   r   r™   r›   r’   r”   r¸   r+   r,   r˜   Ï   s    ÿc                    s"   ˆ j t| jƒt| jtƒ ¡ |d�S )N)rœ   Úpvalsr}   )rž   rŸ   rœ   r   r�   r›   r’   r”   r¸   r+   r,   r˜   Ñ   s    ÿr    c                 S   r¤   r%   r¥   r§   r+   r+   r,   r˜   Ö   r¨   c                 S   r¤   r%   r©   r§   r+   r+   r,   r˜   ×   r¨   c                 S   r¤   r%   rª   r§   r+   r+   r,   r˜   Ø   r¨   )
r«   r&   rŸ   r¬   r­   r®   r¯   r€   r   r°   )	r(   r*   r}   r{   r«   Únumpy_rv_mapr²   r³   r´   r+   r¸   r,   r·   Â   s"   


úýzSampleJointNumpy._sample_numpyr%   )r€   r�   r‚   rƒ   r'   rµ   r·   r+   r+   r+   r,   r¶   ¼   ó
    
r¶   c                   @   r…   )ÚSampleJointPymcz6Returns the sample from pymc of the given distributionNc                 C   r‡   r%   )Ú_sample_pymcr‰   r+   r+   r,   r'   æ   rŠ   zSampleJointPymc.__new__c           	   	      s  zddl ‰ W n ty   ddl‰ Y nw ‡ fdd„‡ fdd„‡ fdd„dœ}dd„ d	d„ d
d„ dœ}| ¡ }|jj|vr>dS ddl}| d¡ |j	¡ ˆ  
¡ �# ||jj |ƒ ˆ jt|ƒdd|ddd�dd… d }W d  ƒ n1 suw   Y  | |||jj |ƒ ¡S )zSample from PyMC.r   Nc                    s2   ˆ j dt| jtƒ ¡ t| jtƒd| jjd fd�S )NÚXr7   r   )r‘   rŽ   r¦   )ÚMvNormalr   r‘   r›   r’   r“   r¦   r§   ©Úpymcr+   r,   r˜   ò   s   ÿz.SampleJointPymc._sample_pymc.<locals>.<lambda>c                    s   ˆ j dt| jtƒ ¡ d�S )Nr¾   )Úa)Ú	Dirichletr   r™   r›   r’   r§   rÀ   r+   r,   r˜   õ   s   c                    s.   ˆ j dt| jƒt| jtƒ ¡ dt| jƒfd�S )Nr¾   r7   )rœ   r�   r¦   )ÚMultinomialrŸ   rœ   r   r�   r›   r’   r=   r§   rÀ   r+   r,   r˜   ÷   s   ÿr    c                 S   r¤   r%   r¥   r§   r+   r+   r,   r˜   ý   r¨   c                 S   r¤   r%   r©   r§   r+   r+   r,   r˜   þ   r¨   c                 S   r¤   r%   rª   r§   r+   r+   r,   r˜   ÿ   r¨   Úpymc3r7   F)ÚdrawsÚchainsÚprogressbarÚrandom_seedÚreturn_inferencedataÚcompute_convergence_checksr¾   )rÁ   ÚImportErrorrÅ   r®   r¯   r€   ÚloggingÚ	getLoggerÚsetLevelÚERRORÚModelr|   r   r°   )	r(   r*   r}   r{   Úpymc_rv_mapr²   r³   rÍ   r´   r+   rÀ   r,   r½   é   s.   ÿ


úý
(þzSampleJointPymc._sample_pymcr%   )r€   r�   r‚   rƒ   r'   rµ   r½   r+   r+   r+   r,   r¼   ã   r»   r¼   )ry   rÅ   rÁ   r«   c                   @   sN   e Zd ZdZdZdd„ Zedd„ ƒZedd„ ƒZd	d
„ Z	ddd„Z
dd„ ZdS )ÚJointDistributionz“
    Represented by the random variables part of the joint distribution.
    Contains methods for PDF, CDF, sampling, marginal densities, etc.
    ©rK   c                 G   sP   t tt|ƒƒ}tt|ƒƒD ]}t|| t ƒrt|| ƒ||< qtj| g|¢R Ž S r%   )	ÚlistÚmapr   rI   r=   r&   r   r   r'   )r(   r3   rF   r+   r+   r,   r'     s   €zJointDistribution.__new__c                 C   s
   t | jƒS r%   )r   Úsymbolsr/   r+   r+   r,   r-   %  r5   zJointDistribution.domainc                 C   s   | j jd S r6   )Údensityr3   r/   r+   r+   r,   rK   )  r;   zJointDistribution.pdfc                 C   sº   t |tƒstd|t|ƒf ƒ‚| ¡ }| jjj}|  t	dd„ | j
D ƒƒ¡}tt|ƒƒD ]/}|| jrDt||| || j|||  fƒ}q+|| jrZt||| || j|||  fƒ}q+|S )Nz!%s should be of type dict, got %sc                 s   s   � | ]}|j d  V  qdS ©r   Nr2   rD   r+   r+   r,   rV   2  rh   z(JointDistribution.cdf.<locals>.<genexpr>)r&   rZ   rY   Útyper®   r-   r.   ÚsetsrK   r\   r×   rI   r=   r^   r   Úinfr_   r   )r0   ÚotherrR   r@   rm   rF   rØ   r+   r+   r,   Úcdf-  s    



ÿ

ÿ€zJointDistribution.cdfr+   ry   Nc                 C   sb   d}||vrt dt|ƒ ƒ‚t|ƒstd| ƒ‚t| | ||d�}|dur'|S t d| jj|f ƒ‚)z, A random realization from the distribution )ry   r«   rÅ   rÁ   z&Sampling from %s is not supported yet.zFailed to import %sr‹   Nz4Sampling for %s is not currently implemented from %s)rl   rU   r#   rY   Ú_get_sample_class_jrvr¯   r€   )r0   r}   rz   r{   Ú	librariesÚsampsr+   r+   r,   r|   <  s   ÿ
ÿÿzJointDistribution.samplec                 G   ó
   | j |Ž S r%   rÔ   ©r0   r3   r+   r+   r,   Ú__call__O  ó   
zJointDistribution.__call__r   )r€   r�   r‚   rƒ   Ú	_argnamesr'   r„   r-   rK   rÞ   r|   rä   r+   r+   r+   r,   rÓ     s    


rÓ   c                   @   s   e Zd ZdZdd„ ZdS )r9   zg
    Representation of random symbols with joint probability distributions
    to allow indexing."
    c                 C   sD   t | jtƒr | jj|kdkrtd| j| jjd f ƒ‚t| |ƒS d S )NTz$Index keys for %s can only up to %s.r7   )r&   rM   r$   rA   rY   Únamer   )r0   Úkeyr+   r+   r,   Ú__getitem__W  s   ÿ
üzJointRandomSymbol.__getitem__N)r€   r�   r‚   rƒ   ré   r+   r+   r+   r,   r9   R  s    r9   c                   @   sX   e Zd ZdZdd„ Zdd„ Zedd„ ƒZedd	„ ƒZd
d„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚMarginalDistributionzî
    Represents the marginal distribution of a joint probability space.

    Initialised using a probability distribution and random variables(or
    their indexed components) which should be a part of the resultant
    distribution.
    c                 G   s‚   t |ƒdkrt|d ƒrt|d ƒ}tdd„ |D ƒƒs!ttdƒƒ‚t dd„ |D ƒ¡}t|t	ƒs:t t
|ƒƒdkr:|S t | ||¡S )Nr7   r   c                 s   s   � | ]
}t |ttfƒV  qd S r%   )r&   r   r   rN   r+   r+   r,   rV   l  s   € z/MarginalDistribution.__new__.<locals>.<genexpr>z�Marginal distribution can be
             intitialised only in terms of random variables or indexed random
             variablesc                 s   s   � | ]}|V  qd S r%   r+   rN   r+   r+   r,   rV   p  s   € )r=   r!   r\   ÚallrY   r"   r   Úfromiterr&   rÓ   r   r   r'   )r(   r*   rR   r+   r+   r,   r'   i  s   zMarginalDistribution.__new__c                 C   s   d S r%   r+   r/   r+   r+   r,   Úchecku  s   zMarginalDistribution.checkc                 C   s&   dd„ | j d D ƒ}tdd„ |D ƒŽ S )Nc                 S   s   g | ]	}t |tƒr|‘qS r+   )r&   r   rD   r+   r+   r,   rG   z  rW   z,MarginalDistribution.set.<locals>.<listcomp>r7   c                 S   rL   r+   )rM   r.   rN   r+   r+   r,   rG   {  rP   )r3   r	   rQ   r+   r+   r,   r.   x  s   zMarginalDistribution.setc                 C   s   | j d }dd„ |D ƒS )Nr7   c                 S   s   h | ]}|j j’qS r+   )rM   r4   rN   r+   r+   r,   Ú	<setcomp>€  rP   z/MarginalDistribution.symbols.<locals>.<setcomp>r2   rQ   r+   r+   r,   r×   }  s   
zMarginalDistribution.symbolsc                    s˜   | j d | j d }‰ ‡ fdd„t|ƒD ƒ}t|tƒr8t|jj ƒ}tddd�‰t‡fdd	„|D ƒƒ}| |¡}n	td
d	„ ˆ D ƒƒ}t	||  
||¡ƒˆŽ S )Nr   r7   c                    s   g | ]}|ˆ vr|‘qS r+   r+   rD   ri   r+   r,   rG   „  rH   z,MarginalDistribution.pdf.<locals>.<listcomp>ÚxT)Úrealc                 3   s   � | ]}t ˆ |ƒV  qd S r%   r
   rD   )rï   r+   r,   rV   ˆ  rh   z+MarginalDistribution.pdf.<locals>.<genexpr>c                 s   s,   � | ]}t |tƒr|jjn|jd  V  qdS rÙ   )r&   r   rM   r4   r3   rN   r+   r+   r,   rV   ‹  s   €* )r3   r   r&   rÓ   r=   r-   r   r\   rK   r   Úcompute_pdf)r0   rï   rm   Úmarginalise_outrb   rp   r+   )rR   rï   r,   rK   ‚  s   
zMarginalDistribution.pdfc                 C   s4   |D ]}d}t |tƒr|jj}|  || |¡}q|S r6   )r&   r   rM   rK   rò   )r0   rm   rR   rO   Úlpdfr+   r+   r,   rñ   Ž  s   
z MarginalDistribution.compute_pdfc                 C   s²   ddl m} t|tƒr|jj}nt|tƒr"|j |j |j	d ¡¡}| 
||jji¡}|jjr:t||jj|fƒ}|S |jjrW|tjtjtjfv rN|j|jf}|||jj|fƒ}|S )Nr   )r   r7   )Úsympy.concrete.summationsr   r&   r   rM   r.   r   rj   rT   r3   r`   r4   r^   r   r_   r   ÚIntegersÚNaturalsÚ	Naturals0rÜ   Úsup)r0   rm   rO   r   Údomr+   r+   r,   rò   –  s    


ÿûz$MarginalDistribution.marginalise_outc                 G   râ   r%   rÔ   rã   r+   r+   r,   rä   ©  rå   zMarginalDistribution.__call__N)r€   r�   r‚   rƒ   r'   rí   r„   r.   r×   rK   rñ   rò   rä   r+   r+   r+   r,   rê   `  s    

rê   N)=rƒ   Úmathr   Úsympy.core.basicr   Úsympy.core.functionr   Úsympy.core.singletonr   Úsympy.core.symbolr   r   Úsympy.core.sympifyr   Úsympy.sets.setsr	   Úsympy.tensor.indexedr   Úsympy.concrete.productsr   rô   r   r   Úsympy.core.containersr   Úsympy.integrals.integralsr   r   Úsympy.matricesr   r   r   Úsympy.stats.crvr   r   Úsympy.stats.drvr   r   Úsympy.stats.rvr   r   r   r   r   r   r   r    Úsympy.utilities.iterablesr!   Úsympy.utilities.miscr"   Úsympy.externalr#   r$   r†   r¶   r¼   rß   rÓ   r9   rê   r+   r+   r+   r,   Ú<module>   s@    
(q''-ü<