U
    ZÊ¹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+   úS/var/www/html/bwave/python/venv/lib/python3.8/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   s
   | j d S )Nr   ©Úargsr/   r+   r+   r,   Úsymbol5   s    zJointPSpace.symbolc                 C   s
   | j d S ©Né   r1   r/   r+   r+   r,   Údistribution9   s    zJointPSpace.distributionc                 C   s   t | j| ƒS r%   )ÚJointRandomSymbolr3   r/   r+   r+   r,   Úvalue=   s    zJointPSpace.valuec                 C   s>   | j j}t|tƒr tt|jƒƒS t|tƒr8|jd d S tj	S )Nr   éÿÿÿÿ)
r6   r.   r&   r	   r   Úlenr2   r   ÚlimitsZOne)r0   Ú_setr+   r+   r,   Úcomponent_countA   s    

zJointPSpace.component_countc                    s"   ‡ fdd„t ˆ jƒD ƒ}ˆ j|Ž S )Nc                    s   g | ]}t ˆ j|ƒ‘qS r+   ©r   r3   ©Ú.0Úir/   r+   r,   Ú
<listcomp>L   s     z#JointPSpace.pdf.<locals>.<listcomp>)Úranger=   r6   ©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   s   g | ]}|j j‘qS r+   )Úpspacer-   ©r@   Úrvr+   r+   r,   rB   T   s     z&JointPSpace.domain.<locals>.<listcomp>)r   r6   r   r3   r.   r   ©r0   Úrvsr+   r+   r,   r-   O   s    
zJointPSpace.domainc                 C   s   | j j| S r%   )r.   r2   )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 ]<}||kr~||  ˆ j	j
j| ¡ t|| ƒ||< |d	7 }q~ˆ j	jràtˆtˆ j	|Ž f|žŽ ƒ}	n$ˆ j	j�rtˆtˆ j	|Ž f|žŽ ƒ}	|	 |¡S )
Nz_Marginal distributions cannot be computed for symbolic dimensions. It is a work under progress.c                    s   g | ]}t ˆ j|ƒ‘qS r+   r>   r?   r/   r+   r,   rB   ^   s     z5JointPSpace.marginal_distribution.<locals>.<listcomp>c                 S   s   g | ]}t t|ƒƒ‘qS r+   )r   Ústrr?   r+   r+   r,   rB   _   s     c                 3   s"   | ]}t ttˆ j|ƒƒƒV  qd S r%   )r   rM   r   r3   r?   r/   r+   r,   Ú	<genexpr>a   s     z4JointPSpace.marginal_distribution.<locals>.<genexpr>c                    s   g | ]}|ˆ kr|g‘qS r+   r+   r?   )r)   r+   r,   rB   b   s      r   r5   )r=   Zatomsr   Ú
ValueErrorrC   ÚdictÚzipÚtupleÚappendr6   r.   r2   Úis_Continuousr   r   Úis_Discreter   Úxreplace)
r0   ÚindicesÚcountÚorigZall_symsZreplace_dictr;   rK   rA   Úfr+   rD   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 ]J}t|tƒr|| |tt|j	ƒ|j
d ƒi¡}qLt|tƒrL| ||ji¡}qLˆjt|ƒkr²ttdƒƒ‚t ‡fdd„|D ƒƒ}t|f|žŽ S )Nc                 3   s   | ]}ˆ j | V  qd S r%   )r8   r?   r/   r+   r,   rN   p   s     z2JointPSpace.compute_expectation.<locals>.<genexpr>c                 3   s   | ]}|ˆ kV  qd S r%   r+   r?   ©rJ   r+   r,   rN   r   s     r5   zq
            Expectations of expression with unindexed joint random symbols
            cannot be calculated yet.c                 3   s8   | ]0}t t|jƒ|jd  ƒˆ jjj|jd   fV  qdS )r5   N)r   rM   Úbaser2   r6   r.   rG   r/   r+   r,   rN   ~   s   ÿÿ)rR   rC   r=   ÚanyrE   r&   r   rV   rM   r]   r2   r   r3   r8   r   ÚNotImplementedErrorr"   r   )r0   ÚexprrJ   ÚevaluateÚkwargsÚsymsrH   r;   r+   )rJ   r0   r,   Úcompute_expectationo   s     

"
ÿzJointPSpace.compute_expectationc                 C   s
   t ƒ ‚d S r%   ©r_   ©r0   Ú	conditionr+   r+   r,   Úwhere‚   s    zJointPSpace.wherec                 C   s
   t ƒ ‚d S r%   re   )r0   r`   r+   r+   r,   Úcompute_density…   s    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   r3   r6   Úsample)r0   Úsizerk   rl   r+   r+   r,   rm   ˆ   s     ÿzJointPSpace.samplec                 C   s
   t ƒ ‚d S r%   re   rf   r+   r+   r,   Úprobability‘   s    zJointPSpace.probability)NF)r+   rj   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   Úpropertyr.   r3   r6   r8   r=   rE   r-   rL   r[   rd   rh   ri   rm   ro   r+   r+   r+   r,   r$   $   s.   








	r$   c                   @   s&   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   |   |||¡S r%   )Ú_sample_scipy©r(   r*   rn   rl   r+   r+   r,   r'   —   s    zSampleJointScipy.__new__c           	         sÀ   ddl }|dk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	|kr”dS ||jj	 ||ƒ}| 
|||jj	 |ƒ ¡S )zSample from SciPy.r   N©rl   )Ústatsc                    s$   ˆj jt| jƒ ¡ t| jƒ|ˆ d�S )N)ÚmeanÚcovrn   Úrandom_state)Úmultivariate_normalrJ   r   ÚmuÚflattenÚsigma©r*   rn   ©Ú
rand_stateZscipy_statsr+   r,   Ú<lambda>¥   s
     þz0SampleJointScipy._sample_scipy.<locals>.<lambda>c                    s   ˆj jt| jtƒ ¡ |ˆ d�S )N)Úalpharn   r|   )Ú	dirichletrJ   r   r…   Úfloatr   r�   r‚   r+   r,   r„   ¨   s     ÿc                    s&   ˆj jt| jƒt| jtƒ ¡ |ˆ d�S )N)ÚnÚprn   r|   )ÚmultinomialrJ   Úintrˆ   r   r‰   r‡   r   r�   r‚   r+   r,   r„   ª   s
      ÿ©ZMultivariateNormalDistributionZMultivariateBetaDistributionZMultinomialDistributionc                 S   s   t | jƒ ¡ jS r%   ©r   r~   r   Úshape©r*   r+   r+   r,   r„   ¯   ó    c                 S   s   t | jƒ ¡ jS r%   ©r   r…   r   rŽ   r�   r+   r+   r,   r„   °   r�   c                 S   s   t | jƒ ¡ jS r%   ©r   r‰   r   rŽ   r�   r+   r+   r,   r„   ±   r�   )Únumpyr&   r‹   ÚrandomÚdefault_rngrj   ry   ÚkeysÚ	__class__rp   Úreshape)	r(   r*   rn   rl   r“   Zscipy_rv_mapÚsample_shapeÚ	dist_listÚsamplesr+   r‚   r,   rv   š   s$    úýzSampleJointScipy._sample_scipy)N)rp   rq   rr   rs   r'   Úclassmethodrv   r+   r+   r+   r,   ru   •   s   
ru   c                   @   s&   e Zd ZdZddd„Zedd„ ƒZdS )ÚSampleJointNumpyz7Returns the sample from numpy of the given distributionNc                 C   s   |   |||¡S r%   )Ú_sample_numpyrw   r+   r+   r,   r'   ¿   s    zSampleJointNumpy.__new__c           	         s²   ddl }|dk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|kr‚dS ||jj |t|ƒƒ}| 	|||jj |ƒ ¡S )zSample from NumPy.r   Nrx   c                    s$   ˆ j t| jtƒ ¡ t| jtƒ|d�S )N)rz   r{   rn   )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…   rn   )r†   r   r…   r‡   r   r�   rŸ   r+   r,   r„   Ï   s    ÿc                    s"   ˆ j t| jƒt| jtƒ ¡ |d�S )N)rˆ   Zpvalsrn   )rŠ   r‹   rˆ   r   r‰   r‡   r   r�   rŸ   r+   r,   r„   Ñ   s     ÿrŒ   c                 S   s   t | jƒ ¡ jS r%   r�   r�   r+   r+   r,   r„   Ö   r�   c                 S   s   t | jƒ ¡ jS r%   r‘   r�   r+   r+   r,   r„   ×   r�   c                 S   s   t | jƒ ¡ jS r%   r’   r�   r+   r+   r,   r„   Ø   r�   )
r“   r&   r‹   r”   r•   r–   r—   rp   r   r˜   )	r(   r*   rn   rl   r“   Znumpy_rv_mapr™   rš   r›   r+   rŸ   r,   rž   Â   s"    


úýzSampleJointNumpy._sample_numpy)N)rp   rq   rr   rs   r'   rœ   rž   r+   r+   r+   r,   r�   ¼   s   
r�   c                   @   s&   e Zd ZdZddd„Zedd„ ƒZdS )ÚSampleJointPymcz6Returns the sample from pymc of the given distributionNc                 C   s   |   |||¡S r%   )Ú_sample_pymcrw   r+   r+   r,   r'   æ   s    zSampleJointPymc.__new__c           	   	      sü   zddl ‰ W n tk
r(   ddl‰ Y nX ‡ fdd„‡ fdd„‡ fdd„dœ}dd„ d	d„ d
d„ dœ}| ¡ }|jj|kr~dS ddl}| d¡ |j	¡ ˆ  
¡ �< ||jj |ƒ ˆ jt|ƒdd|ddd�dd… d }W 5 Q R X | |||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ÚXr5   r   )r~   r{   rŽ   )Z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)Z	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¢   r5   )rˆ   r‰   rŽ   )ZMultinomialr‹   rˆ   r   r‰   r‡   r   r:   r�   r£   r+   r,   r„   ÷   s     ÿrŒ   c                 S   s   t | jƒ ¡ jS r%   r�   r�   r+   r+   r,   r„   ý   r�   c                 S   s   t | jƒ ¡ jS r%   r‘   r�   r+   r+   r,   r„   þ   r�   c                 S   s   t | jƒ ¡ jS r%   r’   r�   r+   r+   r,   r„   ÿ   r�   Úpymc3r5   F)ZdrawsÚchainsZprogressbarZrandom_seedZreturn_inferencedataZcompute_convergence_checksr¢   )r¤   ÚImportErrorr¦   r–   r—   rp   ÚloggingÚ	getLoggerÚsetLevelÚERRORZModelrm   r   r˜   )	r(   r*   rn   rl   Zpymc_rv_mapr™   rš   r©   r›   r+   r£   r,   r¡   é   s*    


úý
0zSampleJointPymc._sample_pymc)N)rp   rq   rr   rs   r'   rœ   r¡   r+   r+   r+   r,   r    ã   s   
r    )rj   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.
    ©rE   c                 G   sN   t tt|ƒƒ}tt|ƒƒD ]"}t|| t ƒrt|| ƒ||< qtj| f|žŽ S r%   )	ÚlistÚmapr   rC   r:   r&   r   r   r'   )r(   r2   rA   r+   r+   r,   r'     s
    zJointDistribution.__new__c                 C   s
   t | jƒS r%   )r   Úsymbolsr/   r+   r+   r,   r-   %  s    zJointDistribution.domainc                 C   s   | j jd S r4   )Údensityr2   r/   r+   r+   r,   rE   )  s    zJointDistribution.pdfc                 C   sº   t |tƒstd|t|ƒf ƒ‚| ¡ }| jjj}|  t	dd„ | j
D ƒƒ¡}tt|ƒƒD ]^}|| jrˆt||| || j|||  fƒ}qV|| jrVt||| || j|||  fƒ}qV|S )Nz!%s should be of type dict, got %sc                 s   s   | ]}|j d  V  qdS ©r   Nr1   r?   r+   r+   r,   rN   2  s     z(JointDistribution.cdf.<locals>.<genexpr>)r&   rP   rO   Útyper–   r-   r.   ZsetsrE   rR   r±   rC   r:   rT   r   ÚinfrU   r   )r0   ÚotherrJ   r<   r`   rA   r²   r+   r+   r,   Úcdf-  s    



ÿ

ÿzJointDistribution.cdfr+   rj   Nc                 C   sf   d}||krt dt|ƒ ƒ‚t|ƒs0td| ƒ‚t| | ||d�}|dk	rN|S t d| jj|f ƒ‚dS )z, A random realization from the distribution )rj   r“   r¦   r¤   z&Sampling from %s is not supported yet.zFailed to import %srx   Nz4Sampling for %s is not currently implemented from %s)r_   rM   r#   rO   Ú_get_sample_class_jrvr—   rp   )r0   rn   rk   rl   Ú	librariesZsampsr+   r+   r,   rm   <  s    ÿ
ÿÿzJointDistribution.samplec                 G   s
   | j |Ž S r%   r®   ©r0   r2   r+   r+   r,   Ú__call__O  s    zJointDistribution.__call__)r+   rj   N)rp   rq   rr   rs   Z	_argnamesr'   rt   r-   rE   r·   rm   r»   r+   r+   r+   r,   r­     s   


r­   c                   @   s   e Zd ZdZdd„ ZdS )r7   zg
    Representation of random symbols with joint probability distributions
    to allow indexing."
    c                 C   sD   t | jtƒr@| jj|kdkr6td| j| jjd f ƒ‚t| |ƒS d S )NTz$Index keys for %s can only up to %s.r5   )r&   rF   r$   r=   rO   Únamer   )r0   Úkeyr+   r+   r,   Ú__getitem__W  s    ÿzJointRandomSymbol.__getitem__N)rp   rq   rr   rs   r¾   r+   r+   r+   r,   r7   R  s   r7   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 ƒƒsBttdƒƒ‚t dd„ |D ƒ¡}t|t	ƒstt t
|ƒƒdkrt|S t | ||¡S )Nr5   r   c                 s   s   | ]}t |ttfƒV  qd S r%   )r&   r   r   rG   r+   r+   r,   rN   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+   rG   r+   r+   r,   rN   p  s     )r:   r!   rR   ÚallrO   r"   r   Zfromiterr&   r­   r   r   r'   )r(   r*   rJ   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   r?   r+   r+   r,   rB   z  s     
 z,MarginalDistribution.set.<locals>.<listcomp>r5   c                 S   s   g | ]}|j j‘qS r+   )rF   r.   rG   r+   r+   r,   rB   {  s     )r2   r	   rI   r+   r+   r,   r.   x  s    zMarginalDistribution.setc                 C   s   | j d }dd„ |D ƒS )Nr5   c                 S   s   h | ]}|j j’qS r+   )rF   r3   rG   r+   r+   r,   Ú	<setcomp>€  s     z/MarginalDistribution.symbols.<locals>.<setcomp>r1   rI   r+   r+   r,   r±   }  s    
zMarginalDistribution.symbolsc                    s˜   | j d | j d  }‰ ‡ fdd„t|ƒD ƒ}t|tƒrpt|jj ƒ}tddd�‰t‡fdd	„|D ƒƒ}| |¡}ntd
d	„ ˆ D ƒƒ}t	||  
||¡ƒˆŽ S )Nr   r5   c                    s   g | ]}|ˆ kr|‘qS r+   r+   r?   r\   r+   r,   rB   „  s      z,MarginalDistribution.pdf.<locals>.<listcomp>ÚxT)Úrealc                 3   s   | ]}t ˆ |ƒV  qd S r%   r
   r?   )rÃ   r+   r,   rN   ˆ  s     z+MarginalDistribution.pdf.<locals>.<genexpr>c                 s   s*   | ]"}t |tƒr|jjn|jd  V  qdS r³   )r&   r   rF   r3   r2   rG   r+   r+   r,   rN   ‹  s     )r2   r   r&   r­   r:   r-   r   rR   rE   r   Úcompute_pdf)r0   rÃ   r`   Úmarginalise_outrX   rc   r+   )rJ   rÃ   r,   rE   ‚  s    
zMarginalDistribution.pdfc                 C   s4   |D ]*}d}t |tƒr|jj}|  || |¡}q|S r4   )r&   r   rF   rE   rÆ   )r0   r`   rJ   rH   Zlpdfr+   r+   r,   rÅ   Ž  s    
z MarginalDistribution.compute_pdfc                 C   s°   ddl m} t|tƒr |jj}n$t|tƒrD|j |j |j	d ¡¡}| 
||jji¡}|jjrrt||jj|fƒ}n:|jjr¬|tjtjtjfkrš|j|jf}|||jj|fƒ}|S )Nr   )r   r5   )Úsympy.concrete.summationsr   r&   r   rF   r.   r   r]   rL   r2   rV   r3   rT   r   rU   r   ZIntegersZNaturalsZ	Naturals0rµ   Úsup)r0   r`   rH   r   Údomr+   r+   r,   rÆ   –  s    


ÿz$MarginalDistribution.marginalise_outc                 G   s
   | j |Ž S r%   r®   rº   r+   r+   r,   r»   ©  s    zMarginalDistribution.__call__N)rp   rq   rr   rs   r'   rÁ   rt   r.   r±   rE   rÅ   rÆ   r»   r+   r+   r+   r,   r¿   `  s   

r¿   N)=rs   Úmathr   Zsympy.core.basicr   Zsympy.core.functionr   Zsympy.core.singletonr   Zsympy.core.symbolr   r   Zsympy.core.sympifyr   Zsympy.sets.setsr	   Zsympy.tensor.indexedr   Zsympy.concrete.productsr   rÇ   r   r   Zsympy.core.containersr   Zsympy.integrals.integralsr   r   Zsympy.matricesr   r   r   Zsympy.stats.crvr   r   Zsympy.stats.drvr   r   Zsympy.stats.rvr   r   r   r   r   r   r   r    Zsympy.utilities.iterablesr!   Zsympy.utilities.miscr"   Zsympy.externalr#   r$   ru   r�   r    r¸   r­   r7   r¿   r+   r+   r+   r,   Ú<module>   s>   
(q''-ü<