ó
9­\c           @  sÂ  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 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 d d	 l m Z m Z m Z d d
 l m Z m Z m  Z  d d l! m" Z" m# Z# m$ Z$ m% Z% m& Z& m' Z' d d l( m) Z) d e" f d „  ƒ  YZ* d e e# f d „  ƒ  YZ+ d e% f d „  ƒ  YZ, d e+ e# f d „  ƒ  YZ- d „  Z. d e e# f d „  ƒ  YZ/ d e f d „  ƒ  YZ0 d S(   sq   
Joint Random Variables Module

See Also
========
sympy.stats.rv
sympy.stats.frv
sympy.stats.crv
sympy.stats.drv
iÿÿÿÿ(   t   print_functiont   division(   t   Basict   Lambdat   sympifyt   Indexedt   Symbolt
   ProductSett   St   Dummy(   t   Sumt	   summation(   t   string_types(   t   Tuple(   t   Integralt	   integrate(   t   ImmutableMatrix(   t   ContinuousDistributiont   SingleContinuousDistributiont   SingleContinuousPSpace(   t   DiscreteDistributiont   SingleDiscreteDistributiont   SingleDiscretePSpace(   t   ProductPSpacet   NamedArgsMixint   ProductDomaint   RandomSymbolt   random_symbolst   SingleDomain(   t
   filldedentt   JointPSpacec           B  sÔ   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 e d „  ƒ Z d	 „  Z e d
 „  ƒ Z d „  Z d e d „ Z d „  Z d „  Z d „  Z d „  Z RS(   st   
    Represents a joint probability space. Represented using symbols for
    each component and a distribution.
    c         C  s‡   t  | t ƒ r t | | ƒ St  | t ƒ r8 t | | ƒ St  | t ƒ rV t | ƒ } n  t  | t ƒ st t d ƒ ‚ n  t j	 |  | | ƒ S(   Ns#   s should have been string or Symbol(
   t
   isinstanceR   R   R   R   R   R   t	   TypeErrorR   t   __new__(   t   clst   symt   dist(    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR!   %   s    c         C  s
   |  j  j S(   N(   t   domaint   set(   t   self(    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR&   0   s    c         C  s   |  j  d S(   Ni    (   t   args(   R'   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   symbol4   s    c         C  s   |  j  d S(   Ni   (   R(   (   R'   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   distribution8   s    c         C  s   t  |  j |  ƒ S(   N(   t   JointRandomSymbolR)   (   R'   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   value<   s    c         C  s,   |  j  j } t | t ƒ r( t | j ƒ Sd S(   Ni   (   R*   R&   R   R   t   lenR(   (   R'   t   _set(    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   component_count@   s    c         C  s;   g  t  |  j ƒ D] } t |  j | ƒ ^ q } |  j | Œ  S(   N(   t   rangeR/   R   R)   R*   (   R'   t   iR#   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   pdfE   s    .c         C  sW   t  |  j ƒ } t | ƒ d k r4 t |  j |  j ƒ St g  | D] } | j j ^ q> Œ  S(   Ni    (	   R   R*   R-   R   R)   R&   R   t   pspaceR%   (   R'   t   rvst   rv(    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR%   J   s    c         C  s   |  j  j | S(   N(   R&   R(   (   R'   t   index(    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   component_domainQ   s    c         C  s
   |  j  j S(   N(   R%   t   symbols(   R'   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR8   T   s    c   
        sœ  |  j  } g  t | ƒ D] } t |  j | ƒ ^ q } g  | D] } t t | ƒ ƒ ^ q; } t t | | ƒ ƒ } g  | D]$ } t t t |  j | ƒ ƒ ƒ ^ qu ‰  t ‡  f d †  | Dƒ ƒ } d } x_ t | ƒ D]Q } | | k rÎ | | j	 |  j
 j j | ƒ t | | ƒ | | <| d 7} qÎ qÎ Wt | ƒ } |  j
 j r_t ˆ  t |  j
 | Œ  | ƒ ƒ }	 n0 |  j
 j rt ˆ  t |  j
 | ƒ | ƒ ƒ }	 n  |	 j | ƒ S(   Nc         3  s$   |  ] } | ˆ  k r | g Vq d  S(   N(    (   t   .0R1   (   R#   (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pys	   <genexpr>^   s    i    i   (   R/   R0   R   R)   R   t   strt   dictt   zipt   listt   appendR*   R&   R(   t   tuplet   is_ContinuousR   R   t   is_DiscreteR   t   xreplace(
   R'   t   indicest   countR1   t   origt   all_symst   replace_dictt   limitsR6   t   f(    (   R#   s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   marginal_distributionX   s$    	+%1$$c   	        s7  t  ‡  f d †  t ˆ  j ƒ Dƒ ƒ } | p. | } t g  | D] } | | k ^ q; ƒ sZ | S| ˆ  j } xz | D]r } t | t ƒ rµ | j i t t | j	 ƒ | j
 d ƒ | 6ƒ } qn t | t ƒ rn | j i | j | 6ƒ } qn qn Wˆ  j t | ƒ k rt t d ƒ ƒ ‚ n  t  ‡  f d †  | Dƒ ƒ } t | | Œ S(   Nc         3  s   |  ] } ˆ  j  | Vq d  S(   N(   R,   (   R9   R1   (   R'   (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pys	   <genexpr>m   s    i   sq   
            Expectations of expression with unindexed joint random symbols
            cannot be calculated yet.c         3  sH   |  ]> } t  t | j ƒ | j d  ƒ ˆ  j j j | j d  f Vq d S(   i   N(   R   R:   t   baseR(   R*   R&   (   R9   R5   (   R'   (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pys	   <genexpr>{   s   (   R?   R0   R/   t   anyR2   R   R   RB   R:   RK   R(   R   R)   R,   R   t   NotImplementedErrorR   R   (	   R'   t   exprR4   t   evaluatet   kwargst   symsR1   R5   RH   (    (   R'   s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   compute_expectationl   s     %%2 c         C  s   t  ƒ  ‚ d  S(   N(   RM   (   R'   t	   condition(    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   where   s    c         C  s   t  ƒ  ‚ d  S(   N(   RM   (   R'   RN   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   compute_density‚   s    c         C  s   t  ƒ  ‚ d  S(   N(   RM   (   R'   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   sample…   s    c         C  s   t  ƒ  ‚ d  S(   N(   RM   (   R'   RS   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   probabilityˆ   s    N(   t   __name__t
   __module__t   __doc__R!   t   propertyR&   R)   R*   R,   R/   R2   R%   R7   R8   RJ   t   Nonet   FalseRR   RT   RU   RV   RW   (    (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR       s"   						t   JointDistributionc           B  sM   e  Z d  Z d Z d „  Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z	 RS(   s“   
    Represented by the random variables part of the joint distribution.
    Contains methods for PDF, CDF, sampling, marginal densities, etc.
    R2   c         G  sl   t  t t | ƒ ƒ } xD t t | ƒ ƒ D]0 } t | | t  ƒ r( t | | ƒ | | <q( q( Wt j |  | Œ S(   N(	   R=   t   mapR   R0   R-   R   R   R   R!   (   R"   R(   R1   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR!   “   s
    c         C  s   t  |  j ƒ S(   N(   R   R8   (   R'   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR%   š   s    c         G  s   |  j  j d S(   Ni   (   t   densityR(   (   R'   R(   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR2   ž   s    c         C  sæ   t  | t ƒ s t ‚ | j ƒ  } |  j j } |  j t d „  |  j Dƒ ƒ ƒ } x t	 t
 | ƒ ƒ D]| } | | j r£ t | | | | | j | | | f ƒ } qb | | j rb t | | | | | j | | | f ƒ } qb qb W| S(   Nc         s  s   |  ] } | j  d  Vq d S(   i    N(   R(   (   R9   R1   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pys	   <genexpr>¦   s    (   R   R;   t   AssertionErrort   keysR%   R&   R2   R?   R8   R0   R-   R@   R   t   infRA   R
   (   R'   t   otherR4   R.   RN   R1   R`   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   cdf¢   s    "c         G  s   |  j  | Œ  S(   N(   R2   (   R'   R(   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   __call__°   s    (   R2   (
   RX   RY   RZ   t	   _argnamesR!   R[   R%   R2   Re   Rf   (    (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR^   ‹   s   		R+   c           B  s   e  Z d  Z d „  Z RS(   sg   
    Representation of random symbols with joint probability distributions
    to allow indexing."
    c         C  s[   t  |  j t ƒ rW |  j j | k rJ t d |  j |  j j d f ƒ ‚ n  t |  | ƒ Sd  S(   Ns$   Index keys for %s can only up to %s.i   (   R   R3   R   R/   t
   ValueErrort   nameR   (   R'   t   key(    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   __getitem__¸   s
     (   RX   RY   RZ   Rk   (    (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR+   ³   s   t   JointDistributionHandmadec           B  s#   e  Z d Z e Z e d „  ƒ Z RS(   R2   c         C  s   |  j  d S(   Ni   (   R(   (   R'   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR&   Ä   s    (   R2   (   RX   RY   Rg   t   TrueR@   R[   R&   (    (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyRl   ¿   s   c         G  s­   t  | ƒ } xE t t | ƒ ƒ D]1 } t | | t ƒ r | | j d | | <q q W|  j } | d k rx t d ƒ ‚ n  t | j	 d ƒ r  | j	 j
 | |  j ƒ S| j
 | Œ  S(   sT  
    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))

    i   s6   At least one component for marginal density is needed.RJ   (    (   R=   R0   R-   R   R   R(   R3   Rh   t   hasattrR*   RJ   R)   (   R5   RC   R1   t
   prob_space(    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyRJ   È   s    	t   CompoundDistributionc           B  sA   e  Z d  Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z RS(   s¹   
    Represents a compound probability distribution.

    Constructed using a single probability distribution with a parameter
    distributed according to some given distribution.
    c         C  so   t  | t t f ƒ s* t t d ƒ ƒ ‚ n  | j } t g  | D] } t  | t ƒ ^ q= ƒ s_ | St j	 |  | ƒ S(   Ns{   CompoundDistribution can only be
             initialized from ContinuousDistribution or DiscreteDistribution
             (
   R   R   R   Rh   R   R(   RL   R   R   R!   (   R"   R$   t   _argsR1   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR!   ö   s    	(c         C  s   t  |  j d ƒ S(   Ni    (   R   R(   (   R'   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   latent_distributions   s    c         G  sz   |  j  d } t d ƒ } t | t ƒ r= t | | ƒ j } n$ t | t ƒ ra t | | ƒ j } n  t |  | f ƒ j	 | Œ  S(   Ni    t   z(
   R(   R	   R   R   R   R,   R   R   t   MarginalDistributionR2   (   R'   t   xR$   Rs   R5   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR2     s    c         C  s   |  j  d j S(   Ni    (   R(   R&   (   R'   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR&     s    c         G  s   |  j  | Œ  S(   N(   R2   (   R'   R(   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyRf     s    (	   RX   RY   RZ   R!   R[   Rr   R2   R&   Rf   (    (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyRp   ï   s   	
			Rt   c           B  sb   e  Z d  Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z	 d „  Z
 d „  Z RS(	   sî   
    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ƒ ƒ s+ t t d ƒ ƒ ‚ n  t j d „  | Dƒ ƒ } t | t ƒ rp t t | ƒ ƒ d k rp | St	 j
 |  | | ƒ S(   Nc         s  s'   |  ] } t  | t t f ƒ g Vq d  S(   N(   R   R   R   (   R9   R5   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pys	   <genexpr>  s    s   Marginal distribution can be
             intitialised only in terms of random variables or indexed random
             variablesc         s  s   |  ] } | Vq d  S(   N(    (   R9   R5   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pys	   <genexpr>"  s    i    (   t   allRh   R   R   t   fromiterR   R^   R-   R   R   R!   (   R"   R$   R4   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR!     s    (c         C  s   d  S(   N(    (   R'   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   check'  s    c         C  s    g  t  |  j d ƒ D] } | ^ q } g  t  |  j d ƒ D] } | |  j d k r: | ^ q: } x* | D]" } | | k rf | j | ƒ qf qf Wt d „  | Dƒ ƒ S(   Ni   c         s  s   |  ] } | j  j Vq d  S(   N(   R3   R&   (   R9   R1   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pys	   <genexpr>2  s    (   R   R(   t   removeR   (   R'   R1   R4   t   marginalise_out(    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR&   *  s    &c         C  s0   |  j  d } t g  | D] } | j j ^ q ƒ S(   Ni   (   R(   R&   R3   R)   (   R'   R4   R5   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR8   4  s    c         G  s  |  j  d |  j  d } } g  t | ƒ D] } | |  j  d k r( | ^ q( } g  |  j  d D] } | j j ^ q[ } xT | j t ƒ D]C } t | t ƒ rƒ t | j t ƒ rƒ | | k rƒ | j	 | ƒ qƒ qƒ Wt | t
 ƒ rt d d t ƒ} | j  d j | ƒ } nm t | t ƒ rqt | j j  ƒ } t d d t d t ƒ} g  | D] } t | | ƒ ^ qD} t j | ƒ } n  t | |  j | | ƒ ƒ | Œ  S(   Ni    i   Ru   t   realt   finite(   R(   R   R3   R)   t   atomsR   R   RK   R   R>   Rp   R	   Rm   R2   R^   R-   R%   t
   expressionR   t   compute_pdf(   R'   Ru   RN   R4   R1   Rz   RQ   RD   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR2   9  s     2&!"c         C  sO   xH | D]@ } d } t  | t ƒ r1 | j j } n  |  j | | | ƒ } q W| S(   Ni   (   R   R   R3   R2   Rz   (   R'   RN   R4   R5   t   lpdf(    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyR   K  s    c         C  s  d d l  m } t | t ƒ r. | j j } n7 t | t ƒ re | j j | j j | j	 d ƒ ƒ } n  | j
 i | j j | 6ƒ } | j j r« t | | j j | f ƒ } n] | j j r| t j t j t j f k rê | j | j f } n  | | | j j | f ƒ } n  | S(   Niÿÿÿÿ(   R
   i   (   t   sympy.concrete.summationsR
   R   R   R3   R&   R   RK   R7   R(   RB   R)   R@   R   RA   R   t   Integerst   Naturalst	   Naturals0Rc   t   sup(   R'   RN   R5   R
   t   dom(    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyRz   S  s    	c         G  s   |  j  | Œ  S(   N(   R2   (   R'   R(   (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyRf   f  s    (   RX   RY   RZ   R!   Rx   R[   R&   R8   R2   R   Rz   Rf   (    (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyRt     s   	
	
			N(1   RZ   t
   __future__R    R   t   sympyR   R   R   R   R   R   R   R	   R   R
   R   t   sympy.core.compatibilityR   t   sympy.core.containersR   t   sympy.integrals.integralsR   R   t   sympy.matricesR   t   sympy.stats.crvR   R   R   t   sympy.stats.drvR   R   R   t   sympy.stats.rvR   R   R   R   R   R   t   sympy.utilities.miscR   R   R^   R+   Rl   RJ   Rp   Rt   (    (    (    s3   lib/python2.7/site-packages/sympy/stats/joint_rv.pyt   <module>
   s$   :.k(		'%