B
    [_/                 @   sJ  d 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 ddlmZmZ ddlmZmZmZmZmZmZ ddlmZmZmZ ddlm Z m!Z!m"Z" dd	l#m$Z$ dd
l%m&Z& ddl'm(Z( ddl)m*Z* G dd deZ+G dd deeZ,G dd deZ-G dd de,eZ.dd Z/G dd deeZ0G dd deZ1dS )zq
Joint Random Variables Module

See Also
========
sympy.stats.rv
sympy.stats.frv
sympy.stats.crv
sympy.stats.drv
    )print_functiondivision)	BasicLambdasympifyIndexedSymbol
ProductSetSDummyMul)Sum	summation)Integral	integrate)ProductPSpaceNamedArgsMixinProductDomainRandomSymbolrandom_symbolsSingleDomain)ContinuousDistributionSingleContinuousDistributionSingleContinuousPSpace)DiscreteDistributionSingleDiscreteDistributionSingleDiscretePSpace)string_types)ImmutableMatrix)Tuple)
filldedentc               @   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edd Zdd Zd$ddZdd Zdd Z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   sZ   t |trt||S t |tr(t||S t |tr:t|}t |tsLtdt	| ||S )Nz#s should have been string or Symbol)

isinstancer   r   r   r   r   r   	TypeErrorr   __new__)clssymdist r(   3lib/python3.7/site-packages/sympy/stats/joint_rv.pyr$   %   s    





zJointPSpace.__new__c             C   s   | j jS )N)domainset)selfr(   r(   r)   r+   0   s    zJointPSpace.setc             C   s
   | j d S )Nr   )args)r,   r(   r(   r)   symbol4   s    zJointPSpace.symbolc             C   s
   | j d S )N   )r-   )r,   r(   r(   r)   distribution8   s    zJointPSpace.distributionc             C   s   t | j| S )N)JointRandomSymbolr.   )r,   r(   r(   r)   value<   s    zJointPSpace.valuec             C   s    | j j}t|trt|jS dS )Nr/   )r0   r+   r"   r	   lenr-   )r,   _setr(   r(   r)   component_count@   s    zJointPSpace.component_countc                s"    fddt  jD } j| S )Nc                s   g | ]}t  j|qS r(   )r   r.   ).0i)r,   r(   r)   
<listcomp>G   s    z#JointPSpace.pdf.<locals>.<listcomp>)ranger5   r0   )r,   r&   r(   )r,   r)   pdfE   s    zJointPSpace.pdfc             C   s6   t | j}t|dkr$t| j| jS tdd |D  S )Nr   c             S   s   g | ]}|j jqS r(   )pspacer*   )r6   rvr(   r(   r)   r8   O   s    z&JointPSpace.domain.<locals>.<listcomp>)r   r0   r3   r   r.   r+   r   )r,   rvsr(   r(   r)   r*   J   s    
zJointPSpace.domainc             C   s   | j j| S )N)r+   r-   )r,   indexr(   r(   r)   component_domainQ   s    zJointPSpace.component_domainc             C   s   | j jS )N)r*   symbols)r,   r(   r(   r)   r@   T   s    zJointPSpace.symbolsc       
         s    j } fddt|D }dd |D }tt||} fdd|D tfdd|D }d}xHt|D ]<}||krn||  jjj|  t	|| ||< |d7 }qnW t	|} jj
rtt j| |}	n jjrtt ||}	|	|S )	Nc                s   g | ]}t  j|qS r(   )r   r.   )r6   r7   )r,   r(   r)   r8   Z   s    z5JointPSpace.marginal_distribution.<locals>.<listcomp>c             S   s   g | ]}t t|qS r(   )r   str)r6   r7   r(   r(   r)   r8   [   s    c                s    g | ]}t tt j|qS r(   )r   rA   r   r.   )r6   r7   )r,   r(   r)   r8   ]   s    c             3   s   | ]}| kr|gV  qd S )Nr(   )r6   r7   )r&   r(   r)   	<genexpr>^   s    z4JointPSpace.marginal_distribution.<locals>.<genexpr>r   r/   )r5   r9   dictziplistappendr0   r+   r-   tupleis_Continuousr   r   is_Discreter   xreplace)
r,   indicescountZorigZall_symsZreplace_dictlimitsr>   r7   fr(   )r,   r&   r)   marginal_distributionX   s$    z!JointPSpace.marginal_distributionNFc                s   t fddtjD } p"| t fdd|D s>|S |j }xR D ]J}t|tr~||tt|j	|j
d i}qNt|trN|||ji}qNW jt|krttdt fdd|D }t|f| S )Nc             3   s   | ]} j | V  qd S )N)r2   )r6   r7   )r,   r(   r)   rB   m   s    z2JointPSpace.compute_expectation.<locals>.<genexpr>c                s   g | ]}| kqS r(   r(   )r6   r7   )r=   r(   r)   r8   o   s    z3JointPSpace.compute_expectation.<locals>.<listcomp>r/   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 )r/   N)r   rA   baser-   r0   r+   )r6   r<   )r,   r(   r)   rB   {   s   )rG   r9   r5   anyr:   r"   r   rJ   rA   rP   r-   r   r.   r2   r   NotImplementedErrorr    r   )r,   exprr=   Zevaluatekwargssymsr<   rM   r(   )r=   r,   r)   compute_expectationl   s     


"

zJointPSpace.compute_expectationc             C   s
   t  d S )N)rR   )r,   	conditionr(   r(   r)   where   s    zJointPSpace.wherec             C   s
   t  d S )N)rR   )r,   rS   r(   r(   r)   compute_density   s    zJointPSpace.compute_densityc             C   s
   t  d S )N)rR   )r,   r(   r(   r)   sample   s    zJointPSpace.samplec             C   s
   t  d S )N)rR   )r,   rW   r(   r(   r)   probability   s    zJointPSpace.probability)NF)__name__
__module____qualname____doc__r$   propertyr+   r.   r0   r2   r5   r:   r*   r?   r@   rO   rV   rX   rY   rZ   r[   r(   r(   r(   r)   r!       s"   
r!   c               @   sD   e Zd ZdZdZdd Zedd Zedd Z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.
    )r:   c             G   sR   t tt|}x2tt|D ]"}t|| t rt|| ||< qW tj| f| S )N)	rE   mapr   r9   r3   r"   r   r   r$   )r%   r-   r7   r(   r(   r)   r$      s
    zJointDistribution.__new__c             C   s
   t | jS )N)r   r@   )r,   r(   r(   r)   r*      s    zJointDistribution.domainc             G   s   | j jd S )Nr/   )densityr-   )r,   r-   r(   r(   r)   r:      s    zJointDistribution.pdfc             C   s   t |tst| }| jj}| tdd | jD }xjt	|D ]^}|| j
rtt||| || j|||  f}qB|| jrBt||| || j|||  f}qBW |S )Nc             s   s   | ]}|j d  V  qdS )r   N)r-   )r6   r7   r(   r(   r)   rB      s    z(JointDistribution.cdf.<locals>.<genexpr>)r"   rC   AssertionErrorkeysr*   r+   r:   rG   r@   r3   rH   r   infrI   r   )r,   otherr=   r4   rS   r7   rc   r(   r(   r)   cdf   s    

zJointDistribution.cdfc             G   s
   | j | S )N)r:   )r,   r-   r(   r(   r)   __call__   s    zJointDistribution.__call__N)r\   r]   r^   r_   	_argnamesr$   r`   r*   r:   rh   ri   r(   r(   r(   r)   ra      s   ra   c               @   s   e Zd ZdZdd ZdS )r1   zg
    Representation of random symbols with joint probability distributions
    to allow indexing."
    c             C   sL   ddl m} t| j|rH| jj|kr>td| j| jjd f t| |S d S )Nr   )r!   z$Index keys for %s can only up to %s.r/   )Zsympy.stats.joint_rvr!   r"   r;   r5   
ValueErrornamer   )r,   keyr!   r(   r(   r)   __getitem__   s    zJointRandomSymbol.__getitem__N)r\   r]   r^   r_   rn   r(   r(   r(   r)   r1      s   r1   c               @   s    e Zd ZdZdZedd ZdS )JointDistributionHandmade)r:   Tc             C   s
   | j d S )Nr/   )r-   )r,   r(   r(   r)   r+      s    zJointDistributionHandmade.setN)r\   r]   r^   rj   rH   r`   r+   r(   r(   r(   r)   ro      s   ro   c             G   sz   t |}x4tt|D ]$}t|| tr|| jd ||< qW | j}|dkrTtdt|j	drp|j	
|| jS |j
| S )aR  
    Marginal distribution function of a joint random variable.

    Parameters
    ==========

    rv: A random variable with a joint probability distribution.
    indices: component indices or the indexed random symbol
        for whom the joint distribution is to be calculated

    Returns
    =======
    A Lambda expression n `sym`.

    Examples
    ========
    >>> from sympy.stats.crv_types import Normal
    >>> from sympy.stats.joint_rv import marginal_distribution
    >>> m = Normal('X', [1, 2], [[2, 1], [1, 2]])
    >>> marginal_distribution(m, m[0])(1)
    1/(2*sqrt(pi))

    r/   r(   z6At least one component for marginal density is needed.rO   )rE   r9   r3   r"   r   r-   r;   rk   hasattrr0   rO   r.   )r<   rK   r7   Z
prob_spacer(   r(   r)   rO      s    rO   c               @   s<   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dS )CompoundDistributionz
    Represents a compound probability distribution.

    Constructed using a single probability distribution with a parameter
    distributed according to some given distribution.
    c             C   sB   t |ttfsttd|j}tdd |D s6|S t| |S )Nz{CompoundDistribution can only be
             initialized from ContinuousDistribution or DiscreteDistribution
             c             S   s   g | ]}t |tqS r(   )r"   r   )r6   r7   r(   r(   r)   r8      s    z0CompoundDistribution.__new__.<locals>.<listcomp>)	r"   r   r   rk   r    r-   rQ   r   r$   )r%   r'   Z_argsr(   r(   r)   r$      s    zCompoundDistribution.__new__c             C   s   t | jd S )Nr   )r   r-   )r,   r(   r(   r)   latent_distributions   s    z)CompoundDistribution.latent_distributionsc             G   sR   | j d }td}t|tr*t||j}nt|tr@t||j}t| |fj	| S )Nr   z)
r-   r   r"   r   r   r2   r   r   MarginalDistributionr:   )r,   xr'   rs   r<   r(   r(   r)   r:     s    


zCompoundDistribution.pdfc             C   s   | j d jS )Nr   )r-   r+   )r,   r(   r(   r)   r+     s    zCompoundDistribution.setc             G   s
   | j | S )N)r:   )r,   r-   r(   r(   r)   ri     s    zCompoundDistribution.__call__N)
r\   r]   r^   r_   r$   r`   rr   r:   r+   ri   r(   r(   r(   r)   rq      s   
	rq   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 )rt   z
    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             C   s^   t dd |D sttdtdd |D }t|tsPtt|dkrP|S t	
| ||S )Nc             s   s   | ]}t |ttfgV  qd S )N)r"   r   r   )r6   r<   r(   r(   r)   rB     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 )Nr(   )r6   r<   r(   r(   r)   rB   !  s    r   )allrk   r    r   Zfromiterr"   ra   r3   r   r   r$   )r%   r'   r=   r(   r(   r)   r$     s    zMarginalDistribution.__new__c             C   s   d S )Nr(   )r,   r(   r(   r)   check&  s    zMarginalDistribution.checkc                sf   dd t  jd D } fddt  jd D }x|D ]}||kr:|| q:W tdd |D S )Nc             S   s   g | ]}|qS r(   r(   )r6   r7   r(   r(   r)   r8   +  s    z,MarginalDistribution.set.<locals>.<listcomp>r/   c                s   g | ]}| j d  kr|qS )r/   )r-   )r6   r7   )r,   r(   r)   r8   ,  s    c             s   s   | ]}|j jV  qd S )N)r;   r+   )r6   r7   r(   r(   r)   rB   1  s    z+MarginalDistribution.set.<locals>.<genexpr>)r   r-   remover	   )r,   r=   marginalise_outr7   r(   )r,   r)   r+   )  s    
zMarginalDistribution.setc             C   s   | j d }tdd |D S )Nr/   c             S   s   g | ]}|j jqS r(   )r;   r.   )r6   r<   r(   r(   r)   r8   6  s    z0MarginalDistribution.symbols.<locals>.<listcomp>)r-   r+   )r,   r=   r(   r(   r)   r@   3  s    
zMarginalDistribution.symbolsc                s    j d  j d  }} fddt|D }dd  j d D }x:|tD ],}t|trLt|jtrL||krL|| qLW t|trt	ddd}|j d 
|}n@t|trt|jj }t	dddd	fd
d|D }t
|}t| || S )Nr   r/   c                s   g | ]}| j d  kr|qS )r/   )r-   )r6   r7   )r,   r(   r)   r8   :  s    z,MarginalDistribution.pdf.<locals>.<listcomp>c             S   s   g | ]}|j jqS r(   )r;   r.   )r6   r7   r(   r(   r)   r8   ;  s    ru   T)real)rz   Zfinitec                s   g | ]}t  |qS r(   )r   )r6   r7   )ru   r(   r)   r8   F  s    )r-   r   Zatomsr   r"   rP   r   rF   rq   r   r:   ra   r3   r*   Z
expressionr   compute_pdf)r,   ru   rS   r=   ry   rU   r7   rL   r(   )r,   ru   r)   r:   8  s     


zMarginalDistribution.pdfc             C   s8   x2|D ]*}d}t |tr |jj}| || |}qW |S )Nr/   )r"   r   r;   r:   ry   )r,   rS   r=   r<   Zlpdfr(   r(   r)   r{   J  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   r/   )sympy.concrete.summationsr   r"   r   r;   r+   r   rP   r?   r-   rJ   r.   rH   r   rI   r
   ZIntegersZNaturalsZ	Naturals0rf   Zsup)r,   rS   r<   r   Zdomr(   r(   r)   ry   R  s    


z$MarginalDistribution.marginalise_outc             G   s
   | j | S )N)r:   )r,   r-   r(   r(   r)   ri   e  s    zMarginalDistribution.__call__N)r\   r]   r^   r_   r$   rw   r`   r+   r@   r:   r{   ry   ri   r(   r(   r(   r)   rt     s   

rt   N)2r_   Z
__future__r   r   Zsympyr   r   r   r   r   r	   r
   r   r   r|   r   r   Zsympy.integrals.integralsr   r   Zsympy.stats.rvr   r   r   r   r   r   Zsympy.stats.crvr   r   r   Zsympy.stats.drvr   r   r   Zsympy.core.compatibilityr   Zsympy.matricesr   Zsympy.core.containersr   Zsympy.utilities.miscr    r!   ra   r1   ro   rO   rq   rt   r(   r(   r(   r)   <module>
   s$   , k(	%%