o
    8VaM=                     @   sd  d 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 ddlmZ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 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*m+Z+ ddl,m-Z- G dd de"Z.G dd dZ/G dd dZ0G dd dZ1e/e1e0dZ2G dd de$e#Z3G dd de&Z4G dd de$Z5dS )zq
Joint Random Variables Module

See Also
========
sympy.stats.rv
sympy.stats.frv
sympy.stats.crv
sympy.stats.drv
    )	BasicLambdasympifyIndexedSymbol
ProductSetSDummyprod)Product)Sum	summation)iterable)Tuple)Integral	integrate)ImmutableMatrixmatrix2numpy
list2numpy)SingleContinuousDistributionSingleContinuousPSpace)SingleDiscreteDistributionSingleDiscretePSpace)ProductPSpaceNamedArgsMixinDistributionProductDomainRandomSymbolrandom_symbolsSingleDomain_symbol_converter)
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*   6/usr/lib/python3/dist-packages/sympy/stats/joint_rv.pyr&   $   s   



zJointPSpace.__new__c                 C   s   | j jS r$   )domainsetselfr*   r*   r+   r-   ,   s   zJointPSpace.setc                 C   
   | j d S )Nr   argsr.   r*   r*   r+   symbol0      
zJointPSpace.symbolc                 C   r0   N   r1   r.   r*   r*   r+   distribution4   r4   zJointPSpace.distributionc                 C   s   t | j| S r$   )JointRandomSymbolr3   r.   r*   r*   r+   value8      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   )
r7   r-   r%   r   r   lenr2   r   limitsZOne)r/   _setr*   r*   r+   component_count<   s   

zJointPSpace.component_countc                    s"    fddt  jD } j| S )Nc                       g | ]}t  j|qS r*   r   r3   .0ir.   r*   r+   
<listcomp>G       z#JointPSpace.pdf.<locals>.<listcomp>)ranger?   r7   r/   r(   r*   r.   r+   pdfE   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,   rC   rvr*   r*   r+   rE   O       z&JointPSpace.domain.<locals>.<listcomp>)r   r7   r   r3   r-   r   r/   rvsr*   r*   r+   r,   J   s   
zJointPSpace.domainc                 C   s   | j j| S r$   )r-   r2   )r/   indexr*   r*   r+   component_domainQ   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 tfdd|D }d}t|D ]}||vr_|| 	 j
jj|  t|| ||< |d	7 }qA j
jrstt j
| g|R  }	n j
jrtt j
| g|R  }	|	|S )
Nz_Marginal distributions cannot be computed for symbolic dimensions. It is a work under progress.c                    r@   r*   rA   rB   r.   r*   r+   rE   Y   rF   z5JointPSpace.marginal_distribution.<locals>.<listcomp>c                 S   s   g | ]}t t|qS r*   )r   strrB   r*   r*   r+   rE   Z   rF   c                 3   s$    | ]}t tt j|V  qd S r$   )r   rS   r   r3   rB   r.   r*   r+   	<genexpr>\   s   " z4JointPSpace.marginal_distribution.<locals>.<genexpr>c                 3   s    | ]
}| vr|gV  qd S r$   r*   rB   )r(   r*   r+   rT   ]   s    r   r6   )r?   Zatomsr   
ValueErrorrG   dictziptuplelistappendr7   r-   r2   is_Continuousr   r   is_Discreter   xreplace)
r/   indicescountZorigZall_symsZreplace_dictr=   rQ   rD   fr*   rH   r+   marginal_distributionT   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$   )r9   rB   r.   r*   r+   rT   k       z2JointPSpace.compute_expectation.<locals>.<genexpr>c                    s   g | ]}| v qS r*   r*   rB   rP   r*   r+   rE   m   rN   z3JointPSpace.compute_expectation.<locals>.<listcomp>r6   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 )r6   N)r   rS   baser2   r7   r-   rL   r.   r*   r+   rT   y   s    )rX   rG   r?   anyrI   r%   r   r]   rS   rd   r2   r   r3   r9   r   NotImplementedErrorr!   r   )r/   exprrP   ZevaluatekwargssymsrM   r=   r*   )rP   r/   r+   compute_expectationj   s"   

"
zJointPSpace.compute_expectationc                 C      t  r$   rf   r/   Z	conditionr*   r*   r+   where}      zJointPSpace.wherec                 C   rk   r$   rl   )r/   rg   r*   r*   r+   compute_density   ro   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   r7   sample)r/   sizerr   rs   r*   r*   r+   rt      s   zJointPSpace.samplec                 C   rk   r$   rl   rm   r*   r*   r+   probability   ro   zJointPSpace.probability)NFr*   rq   N)__name__
__module____qualname____doc__r&   propertyr-   r3   r7   r9   r?   rI   r,   rR   ra   rj   rn   rp   rt   rv   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)   ru   rs   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rs   )statsc                    s$   j jt| j t| j| dS )N)meancovru   random_state)multivariate_normalrP   r   muflattensigmar)   ru   
rand_stateZscipy_statsr*   r+   <lambda>   s    z0SampleJointScipy._sample_scipy.<locals>.<lambda>c                    s   j jt| jt | dS )N)alpharu   r   )	dirichletrP   r   r   floatr   r   r   r*   r+   r      s    c                    s&   j jt| jt| jt | dS )N)npru   r   )multinomialrP   intr   r   r   r   r   r   r   r*   r+   r      s    ZMultivariateNormalDistributionZMultivariateBetaDistributionZ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_rngrq   r   keys	__class__rx   reshape)	r'   r)   ru   rs   r   Zscipy_rv_mapsample_shape	dist_listsamplesr*   r   r+   r      s$   zSampleJointScipy._sample_scipyr$   )rx   ry   rz   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   ru   )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   ru   )r   r   r   r   r   r   r   r*   r+   r      s    c                    s"    j t| jt| jt |dS )N)r   Zpvalsru   )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   rx   r
   r   )	r'   r)   ru   rs   r   Znumpy_rv_mapr   r   r   r*   r   r+   r      s"   


zSampleJointNumpy._sample_numpyr$   )rx   ry   rz   r{   r&   r   r   r*   r*   r*   r+   r      
    
r   c                   @   r}   )SampleJointPymcz7Returns the sample from pymc3 of the given distributionNc                 C   r   r$   )_sample_pymc3r   r*   r*   r+   r&      r   zSampleJointPymc.__new__c           	   	      s   ddl   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 sew   Y  ||||jj | S )zSample from PyMC3.r   Nc                    s2    j dt| jt t| jtd| jjd fdS )NXr6   r   )r   r   r   )ZMvNormalr   r   r   r   r   r   r   pymc3r*   r+   r      s   z/SampleJointPymc._sample_pymc3.<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   r6   )r   r   r   )Z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   r   r6   F)ZdrawsZchainsZprogressbarZrandom_seedZreturn_inferencedataZcompute_convergence_checksr   )r   r   r   rx   loggingZ	getLoggerZsetLevelZERRORZModelrt   r
   r   )	r'   r)   ru   rs   Zpymc3_rv_mapr   r   r   r   r*   r   r+   r      s&   



(zSampleJointPymc._sample_pymc3r$   )rx   ry   rz   r{   r&   r   r   r*   r*   r*   r+   r      r   r   )rq   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.
    rI   c                 G   sP   t tt|}tt|D ]}t|| t rt|| ||< qtj| g|R  S r$   )	rY   mapr   rG   r<   r%   r   r   r&   )r'   r2   rD   r*   r*   r+   r&     s   zJointDistribution.__new__c                 C   s
   t | jS r$   )r   symbolsr.   r*   r*   r+   r,     r4   zJointDistribution.domainc                 C   s   | j jd S r5   )densityr2   r.   r*   r*   r+   rI      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   Nr1   rB   r*   r*   r+   rT   )  rb   z(JointDistribution.cdf.<locals>.<genexpr>)r%   rV   rU   typer   r,   r-   ZsetsrI   rX   r   rG   r<   r[   r   infr\   r   )r/   otherrP   r>   rg   rD   r   r*   r*   r+   cdf$  s    





zJointDistribution.cdfr*   rq   Nc                 C   sf   g 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 )rq   r   r   z&Sampling from %s is not supported yet.zFailed to import %sr   Nz4Sampling for %s is not currently implemented from %s)rf   rS   r"   rU   _get_sample_class_jrvr   rx   )r/   ru   rr   rs   Z	librariesZsampsr*   r*   r+   rt   3  s   
zJointDistribution.samplec                 G   
   | j | S r$   r   r/   r2   r*   r*   r+   __call__F     
zJointDistribution.__call__rw   )rx   ry   rz   r{   Z	_argnamesr&   r|   r,   rI   r   rt   r   r*   r*   r*   r+   r     s    


r   c                   @   s   e Zd ZdZdd ZdS )r8   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.r6   )r%   rK   r#   r?   rU   namer   )r/   keyr*   r*   r+   __getitem__N  s   
zJointRandomSymbol.__getitem__N)rx   ry   rz   r{   r   r*   r*   r*   r+   r8   I  s    r8   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 )Nr6   r   c                 s   s     | ]}t |ttfgV  qd S r$   )r%   r   r   rL   r*   r*   r+   rT   c  s    z/MarginalDistribution.__new__.<locals>.<genexpr>zMarginal distribution can be
             intitialised only in terms of random variables or indexed random
             variablesc                 s   s    | ]}|V  qd S r$   r*   rL   r*   r*   r+   rT   g  s    )r<   r   rX   allrU   r!   r   Zfromiterr%   r   r   r   r&   )r'   r)   rP   r*   r*   r+   r&   `  s   zMarginalDistribution.__new__c                 C   s   d S r$   r*   r.   r*   r*   r+   checkl  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   rB   r*   r*   r+   rE   q  s    z,MarginalDistribution.set.<locals>.<listcomp>r6   c                 S   rJ   r*   )rK   r-   rL   r*   r*   r+   rE   r  rN   )r2   r   rO   r*   r*   r+   r-   o  s   zMarginalDistribution.setc                 C   s   | j d }dd |D S )Nr6   c                 S   s   h | ]}|j jqS r*   )rK   r3   rL   r*   r*   r+   	<setcomp>w  rN   z/MarginalDistribution.symbols.<locals>.<setcomp>r1   rO   r*   r*   r+   r   t  s   
zMarginalDistribution.symbolsc                    s   | j d | j d }  fddt|D }t|tr9t|jj }tddddtfdd	|D }||}n	td
d	  D }t	|| 
|| S )Nr   r6   c                    s   g | ]}| vr|qS r*   r*   rB   rc   r*   r+   rE   {  rF   z,MarginalDistribution.pdf.<locals>.<listcomp>xT)realZfinitec                 3   s    | ]}t  |V  qd S r$   )r   rB   )r   r*   r+   rT     rb   z+MarginalDistribution.pdf.<locals>.<genexpr>c                 s   s,    | ]}t |tr|jjn|jd  V  qdS r   )r%   r   rK   r3   r2   rL   r*   r*   r+   rT     s   * )r2   r   r%   r   r<   r,   r	   rX   rI   r   compute_pdf)r/   r   rg   marginalise_outr_   ri   r*   )rP   r   r+   rI   y  s   
zMarginalDistribution.pdfc                 C   s4   |D ]}d}t |tr|jj}| || |}q|S r5   )r%   r   rK   rI   r   )r/   rg   rP   rM   Z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   r6   )sympy.concrete.summationsr   r%   r   rK   r-   r   rd   rR   r2   r]   r3   r[   r   r\   r   ZIntegersZNaturalsZ	Naturals0r   sup)r/   rg   rM   r   Zdomr*   r*   r+   r     s    


z$MarginalDistribution.marginalise_outc                 G   r   r$   r   r   r*   r*   r+   r     r   zMarginalDistribution.__call__N)rx   ry   rz   r{   r&   r   r|   r-   r   rI   r   r   r   r*   r*   r*   r+   r   W  s    

r   N)6r{   Zsympyr   r   r   r   r   r   r   r	   r
   Zsympy.concrete.productsr   r   r   r   Zsympy.core.compatibilityr   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.miscr!   Zsympy.externalr"   r#   r~   r   r   r   r   r8   r   r*   r*   r*   r+   <module>   s0    ,(q''*<